From 72bae332d1be25fc60ba3317eeead8706e3c5226 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Fri, 31 Jul 2020 19:45:16 +0530 Subject: [PATCH] Refactor methods to break down into smaller units for re-usability. This commit breaks down `read_meta_data` so that individual methods for cache key and setting raw meta data can be reused. Also adds set_meta_data_from_raw_data to initialize metadata from manual cache. --- includes/abstracts/abstract-wc-data.php | 60 ++++++++++++++++++++----- src/Traits/Hydrable.php | 7 +++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/Traits/Hydrable.php diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index 383d6d19547..b07963ff8b5 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -392,6 +392,19 @@ abstract class WC_Data { } } + /** + * Helper function to create and set WC_Meta_data object from std objects. Automatically ignores internal keys. + * + * @param array $raw_data Array of objects of raw data to set. + */ + public function set_meta_data_from_raw_data( $raw_data ) { + if ( null === $this->meta_data ) { + $this->meta_data = array(); + } + $meta_data = $this->data_store->filter_raw_data( $this, $raw_data ); + $this->set_from_raw_meta_data( $meta_data ); + } + /** * Add meta data. * @@ -519,6 +532,20 @@ abstract class WC_Data { } } + /** + * Helper method to compute meta cache key. Different from WP Meta cache key. + * + * @since 4.4.0 + * + * @return string + */ + public function get_meta_cache_key() { + if ( ! $this->get_id() ) { + wc_doing_it_wrong( 'get_meta_cache_key', 'ID needs to be set before fetching a cache key.', '4.4.0' ); + return false; + } + return WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); + } /** * Read Meta Data from the database. Ignore any internal properties. * Uses it's own caches because get_metadata does not provide meta_ids. @@ -540,7 +567,7 @@ abstract class WC_Data { if ( ! empty( $this->cache_group ) ) { // Prefix by group allows invalidation by group until https://core.trac.wordpress.org/ticket/4476 is implemented. - $cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); + $cache_key = $this->get_meta_cache_key(); } if ( ! $force_read ) { @@ -551,16 +578,9 @@ abstract class WC_Data { } $raw_meta_data = $cache_loaded ? $cached_meta : $this->data_store->read_meta( $this ); + if ( $raw_meta_data ) { - foreach ( $raw_meta_data as $meta ) { - $this->meta_data[] = new WC_Meta_Data( - array( - 'id' => (int) $meta->meta_id, - 'key' => $meta->meta_key, - 'value' => maybe_unserialize( $meta->meta_value ), - ) - ); - } + $this->set_from_raw_meta_data( $raw_meta_data ); if ( ! $cache_loaded && ! empty( $this->cache_group ) ) { wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); @@ -568,6 +588,26 @@ abstract class WC_Data { } } + /** + * Helper function to create WC_Meta_data object from std objects. + * + * @param array $raw_meta_data Array of object of raw meta data. + */ + private function set_from_raw_meta_data( $raw_meta_data ) { + if ( ! is_array( $raw_meta_data ) ) { + return; + } + foreach ( $raw_meta_data as $meta ) { + $this->meta_data[] = new WC_Meta_Data( + array( + 'id' => (int) $meta->meta_id, + 'key' => $meta->meta_key, + 'value' => maybe_unserialize( $meta->meta_value ), + ) + ); + } + } + /** * Update Meta Data in the database. * diff --git a/src/Traits/Hydrable.php b/src/Traits/Hydrable.php new file mode 100644 index 00000000000..a2d1042b920 --- /dev/null +++ b/src/Traits/Hydrable.php @@ -0,0 +1,7 @@ +