From efd42e6bc43ae321e8b883801faa419a3ad26ab7 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Tue, 6 Jun 2017 19:10:24 +0200 Subject: [PATCH] Proof of concept to error when internal meta props are accessed directly --- includes/abstracts/abstract-wc-data.php | 17 +++++++++++++++++ includes/data-stores/class-wc-data-store-wp.php | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index 976028f1333..4321c15f391 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -268,6 +268,18 @@ abstract class WC_Data { return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ); } + /** + * Validate meta key. + * + * @since 3.x + * @param string $key + */ + private function validate_meta_key( $key ) { + if ( $this->data_store && ! empty( $key ) && in_array( $key, $this->data_store->get_internal_meta_keys() ) ) { + throw new Exception( __( 'Meta properties should not be accessed directly. Use getters and setters.', 'woocommerce' ) ); + } + } + /** * Get Meta Data by Key. * @@ -278,6 +290,7 @@ abstract class WC_Data { * @return mixed */ public function get_meta( $key = '', $single = true, $context = 'view' ) { + $this->validate_meta_key( $key ); $this->maybe_read_meta_data(); $meta_data = $this->get_meta_data(); $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key ); @@ -307,6 +320,7 @@ abstract class WC_Data { * @return boolean */ public function meta_exists( $key = '' ) { + $this->validate_meta_key( $key ); $this->maybe_read_meta_data(); $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); return in_array( $key, $array_keys ); @@ -343,6 +357,7 @@ abstract class WC_Data { * @param bool $unique Should this be a unique key? */ public function add_meta_data( $key, $value, $unique = false ) { + $this->validate_meta_key( $key ); $this->maybe_read_meta_data(); if ( $unique ) { $this->delete_meta_data( $key ); @@ -362,6 +377,7 @@ abstract class WC_Data { * @param int $meta_id */ public function update_meta_data( $key, $value, $meta_id = '' ) { + $this->validate_meta_key( $key ); $this->maybe_read_meta_data(); if ( $array_key = $meta_id ? array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id ) : '' ) { $this->meta_data[ current( $array_key ) ] = (object) array( @@ -381,6 +397,7 @@ abstract class WC_Data { * @param string $key Meta key */ public function delete_meta_data( $key ) { + $this->validate_meta_key( $key ); $this->maybe_read_meta_data(); if ( $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key ) ) { foreach ( $array_keys as $array_key ) { diff --git a/includes/data-stores/class-wc-data-store-wp.php b/includes/data-stores/class-wc-data-store-wp.php index cc0d53aa832..466df136dc3 100644 --- a/includes/data-stores/class-wc-data-store-wp.php +++ b/includes/data-stores/class-wc-data-store-wp.php @@ -417,4 +417,14 @@ class WC_Data_Store_WP { return $wp_query_args; } + + /** + * Return list of internal meta keys. + * + * @since 3.x + * @return array + */ + public function get_internal_meta_keys() { + return $this->internal_meta_keys; + } }