Proof of concept to error when internal meta props are accessed directly

This commit is contained in:
Boro Sitnikovski 2017-06-06 19:10:24 +02:00
parent ed783e1bbf
commit efd42e6bc4
2 changed files with 27 additions and 0 deletions

View File

@ -268,6 +268,18 @@ abstract class WC_Data {
return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ); 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. * Get Meta Data by Key.
* *
@ -278,6 +290,7 @@ 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' ) {
$this->validate_meta_key( $key );
$this->maybe_read_meta_data(); $this->maybe_read_meta_data();
$meta_data = $this->get_meta_data(); $meta_data = $this->get_meta_data();
$array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key ); $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key );
@ -307,6 +320,7 @@ abstract class WC_Data {
* @return boolean * @return boolean
*/ */
public function meta_exists( $key = '' ) { public function meta_exists( $key = '' ) {
$this->validate_meta_key( $key );
$this->maybe_read_meta_data(); $this->maybe_read_meta_data();
$array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' );
return in_array( $key, $array_keys ); return in_array( $key, $array_keys );
@ -343,6 +357,7 @@ 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 ) {
$this->validate_meta_key( $key );
$this->maybe_read_meta_data(); $this->maybe_read_meta_data();
if ( $unique ) { if ( $unique ) {
$this->delete_meta_data( $key ); $this->delete_meta_data( $key );
@ -362,6 +377,7 @@ 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 = '' ) {
$this->validate_meta_key( $key );
$this->maybe_read_meta_data(); $this->maybe_read_meta_data();
if ( $array_key = $meta_id ? array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id ) : '' ) { 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(
@ -381,6 +397,7 @@ abstract class WC_Data {
* @param string $key Meta key * @param string $key Meta key
*/ */
public function delete_meta_data( $key ) { public function delete_meta_data( $key ) {
$this->validate_meta_key( $key );
$this->maybe_read_meta_data(); $this->maybe_read_meta_data();
if ( $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key ) ) { 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 ) {

View File

@ -417,4 +417,14 @@ class WC_Data_Store_WP {
return $wp_query_args; 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;
}
} }