Proof of concept to error when internal meta props are accessed directly
This commit is contained in:
parent
ed783e1bbf
commit
efd42e6bc4
|
@ -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 ) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue