From d2fde8a33267770ff656aaae972b1106ed1daf69 Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Fri, 8 Sep 2023 13:36:06 +0200 Subject: [PATCH] Add added|updated|deleted_order_meta actions. --- .../DataStores/CustomMetaDataStore.php | 47 +++++++++++++++++-- .../Orders/OrdersTableDataStoreMeta.php | 35 ++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/src/Internal/DataStores/CustomMetaDataStore.php b/plugins/woocommerce/src/Internal/DataStores/CustomMetaDataStore.php index 6ece6e9acd8..18ca13b32d1 100644 --- a/plugins/woocommerce/src/Internal/DataStores/CustomMetaDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/CustomMetaDataStore.php @@ -78,10 +78,11 @@ abstract class CustomMetaDataStore { * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing at least ->id). + * @param string $meta_type Meta type. * * @return bool */ - public function delete_meta( &$object, $meta ) { + protected function delete_meta_with_type( &$object, $meta, $meta_type ) : bool { global $wpdb; if ( ! isset( $meta->id ) ) { @@ -91,7 +92,19 @@ abstract class CustomMetaDataStore { $db_info = $this->get_db_info(); $meta_id = absint( $meta->id ); - return (bool) $wpdb->delete( $db_info['table'], array( $db_info['meta_id_field'] => $meta_id ) ); + $result = (bool) $wpdb->delete( $db_info['table'], array( $db_info['meta_id_field'] => $meta_id ) ); + + /** + * Fires immediately after deleting metadata. + * + * @param int $meta_id ID of updated metadata entry. + * @param int $object_id Post ID. + * @param string $meta_key Metadata key. + * @param mixed $meta_value Metadata value. + */ + do_action( "deleted_{$meta_type}_meta", $meta_id, $object->get_id(), $meta->key, $meta->value ); + + return $result; } /** @@ -99,9 +112,11 @@ abstract class CustomMetaDataStore { * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing ->key and ->value). + * @param string $meta_type Meta type. + * * @return int meta ID */ - public function add_meta( &$object, $meta ) { + protected function add_meta_with_type( &$object, $meta, $meta_type ) { global $wpdb; $db_info = $this->get_db_info(); @@ -120,8 +135,19 @@ abstract class CustomMetaDataStore { ) ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_value,WordPress.DB.SlowDBQuery.slow_db_query_meta_key + $meta_id = $result ? (int) $wpdb->insert_id : false; - return $result ? (int) $wpdb->insert_id : false; + /** + * Fires immediately after adding metadata. + * + * @param int $meta_id ID of updated metadata entry. + * @param int $object_id Post ID. + * @param string $meta_key Metadata key. + * @param mixed $meta_value Metadata value. + */ + do_action( "added_{$meta_type}_meta", $meta_id, $object->get_id(), $meta_key, $meta_value ); + + return $meta_id; } /** @@ -129,10 +155,11 @@ abstract class CustomMetaDataStore { * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing ->id, ->key and ->value). + * @param string $meta_type Meta type. * * @return bool */ - public function update_meta( &$object, $meta ) { + protected function update_meta_with_type( &$object, $meta, $meta_type ) { global $wpdb; if ( ! isset( $meta->id ) || empty( $meta->key ) ) { @@ -156,6 +183,16 @@ abstract class CustomMetaDataStore { '%d' ); + /** + * Fires immediately after updating metadata. + * + * @param int $meta_id ID of updated metadata entry. + * @param int $object_id Post ID. + * @param string $meta_key Metadata key. + * @param mixed $meta_value Metadata value. + */ + do_action( "updated_{$meta_type}_meta", $meta->id, $object->get_id(), $meta->key, $meta->value ); + return 1 === $result; } diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStoreMeta.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStoreMeta.php index 9be17663bd7..73c2af14880 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStoreMeta.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStoreMeta.php @@ -30,4 +30,39 @@ class OrdersTableDataStoreMeta extends CustomMetaDataStore { return 'order_id'; } + /** + * Deletes meta based on meta ID. + * + * @param WC_Data $object WC_Data object. + * @param stdClass $meta (containing at least ->id). + * + * @return bool + */ + public function delete_meta( &$object, $meta ) { + return $this->delete_meta_with_type( $object, $meta, 'order' ); + } + + /** + * Add new piece of meta. + * + * @param WC_Data $object WC_Data object. + * @param stdClass $meta (containing ->key and ->value). + * + * @return int meta ID + */ + public function add_meta( &$object, $meta ) { + return $this->add_meta_with_type( $object, $meta, 'order' ); + } + + /** + * Update meta. + * + * @param WC_Data $object WC_Data object. + * @param stdClass $meta (containing ->id, ->key and ->value). + * + * @return bool + */ + public function update_meta( &$object, $meta ) { + return $this->update_meta_with_type( $object, $meta, 'order' ); + } }