Lazy load product meta data

This commit is contained in:
Mike Jolley 2016-12-09 12:18:21 +00:00
parent 563dda505a
commit 5562cfc1f6
3 changed files with 25 additions and 12 deletions

View File

@ -70,7 +70,7 @@ abstract class WC_Data {
* Stores additonal meta data. * Stores additonal meta data.
* @var array * @var array
*/ */
protected $meta_data = array(); protected $meta_data = null;
/** /**
* Default constructor. * Default constructor.
@ -179,6 +179,9 @@ abstract class WC_Data {
* @return array * @return array
*/ */
public function get_meta_data() { public function get_meta_data() {
if ( is_null( $this->meta_data ) ) {
$this->read_meta_data();
}
return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ); return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) );
} }
@ -191,6 +194,9 @@ abstract class WC_Data {
* @return mixed * @return mixed
*/ */
public function get_meta( $key = '', $single = true, $context = 'view' ) { public function get_meta( $key = '', $single = true, $context = 'view' ) {
if ( is_null( $this->meta_data ) ) {
$this->read_meta_data();
}
$array_keys = array_keys( wp_list_pluck( $this->get_meta_data(), 'key' ), $key ); $array_keys = array_keys( wp_list_pluck( $this->get_meta_data(), 'key' ), $key );
$value = ''; $value = '';
@ -216,6 +222,9 @@ abstract class WC_Data {
*/ */
public function set_meta_data( $data ) { public function set_meta_data( $data ) {
if ( ! empty( $data ) && is_array( $data ) ) { if ( ! empty( $data ) && is_array( $data ) ) {
if ( is_null( $this->meta_data ) ) {
$this->read_meta_data();
}
foreach ( $data as $meta ) { foreach ( $data as $meta ) {
$meta = (array) $meta; $meta = (array) $meta;
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
@ -237,6 +246,9 @@ abstract class WC_Data {
* @param bool $unique Should this be a unique key? * @param bool $unique Should this be a unique key?
*/ */
public function add_meta_data( $key, $value, $unique = false ) { public function add_meta_data( $key, $value, $unique = false ) {
if ( is_null( $this->meta_data ) ) {
$this->read_meta_data();
}
if ( $unique ) { if ( $unique ) {
$this->delete_meta_data( $key ); $this->delete_meta_data( $key );
} }
@ -254,11 +266,10 @@ abstract class WC_Data {
* @param int $meta_id * @param int $meta_id
*/ */
public function update_meta_data( $key, $value, $meta_id = '' ) { public function update_meta_data( $key, $value, $meta_id = '' ) {
$array_key = ''; if ( is_null( $this->meta_data ) ) {
if ( $meta_id ) { $this->read_meta_data();
$array_key = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id );
} }
if ( $array_key ) { if ( $array_key = $meta_id ? array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id ) : '' ) {
$this->meta_data[ current( $array_key ) ] = (object) array( $this->meta_data[ current( $array_key ) ] = (object) array(
'id' => $meta_id, 'id' => $meta_id,
'key' => $key, 'key' => $key,
@ -275,8 +286,10 @@ abstract class WC_Data {
* @param array $key Meta key * @param array $key Meta key
*/ */
public function delete_meta_data( $key ) { public function delete_meta_data( $key ) {
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key ); if ( is_null( $this->meta_data ) ) {
if ( $array_keys ) { $this->read_meta_data();
}
if ( $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key ) ) {
foreach ( $array_keys as $array_key ) { foreach ( $array_keys as $array_key ) {
$this->meta_data[ $array_key ]->value = null; $this->meta_data[ $array_key ]->value = null;
} }
@ -289,8 +302,10 @@ abstract class WC_Data {
* @param int $mid Meta ID * @param int $mid Meta ID
*/ */
public function delete_meta_data_by_mid( $mid ) { public function delete_meta_data_by_mid( $mid ) {
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $mid ); if ( is_null( $this->meta_data ) ) {
if ( $array_keys ) { $this->read_meta_data();
}
if ( $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $mid ) ) {
foreach ( $array_keys as $array_key ) { foreach ( $array_keys as $array_key ) {
$this->meta_data[ $array_key ]->value = null; $this->meta_data[ $array_key ]->value = null;
} }
@ -353,7 +368,7 @@ abstract class WC_Data {
* @since 2.6.0 * @since 2.6.0
*/ */
public function save_meta_data() { public function save_meta_data() {
if ( ! $this->data_store ) { if ( ! $this->data_store || is_null( $this->meta_data ) ) {
return; return;
} }
foreach ( $this->meta_data as $array_key => $meta ) { foreach ( $this->meta_data as $array_key => $meta ) {

View File

@ -130,7 +130,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'reviews_allowed' => 'open' === $post_object->comment_status, 'reviews_allowed' => 'open' === $post_object->comment_status,
) ); ) );
$product->read_meta_data();
$this->read_attributes( $product ); $this->read_attributes( $product );
$this->read_downloads( $product ); $this->read_downloads( $product );
$this->read_visibility( $product ); $this->read_visibility( $product );

View File

@ -66,7 +66,6 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
) ); ) );
$this->read_product_data( $product ); $this->read_product_data( $product );
$product->read_meta_data();
$product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) );
// Set object_read true once all data is read. // Set object_read true once all data is read.