2017-04-26 14:18:41 +00:00
|
|
|
<?php
|
2017-04-26 23:39:19 +00:00
|
|
|
/**
|
|
|
|
* Wraps an array (meta data for now) and tells if there was any changes.
|
|
|
|
*
|
|
|
|
* The main idea behind this class is to avoid doing unneeded
|
|
|
|
* SQL updates if nothing changed.
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @version 3.2.0
|
2017-04-26 23:39:19 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
*/
|
2017-08-08 14:25:32 +00:00
|
|
|
|
2018-03-21 03:11:16 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2017-08-08 14:25:32 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-21 03:11:16 +00:00
|
|
|
* Meta data class.
|
2017-08-08 14:25:32 +00:00
|
|
|
*/
|
2017-10-04 13:12:53 +00:00
|
|
|
class WC_Meta_Data implements JsonSerializable {
|
2017-08-08 14:25:32 +00:00
|
|
|
|
2017-05-23 23:15:13 +00:00
|
|
|
/**
|
|
|
|
* Current data for metadata
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
2017-05-23 23:15:13 +00:00
|
|
|
*/
|
|
|
|
protected $current_data;
|
2017-04-26 14:18:41 +00:00
|
|
|
|
2017-05-23 23:15:13 +00:00
|
|
|
/**
|
|
|
|
* Metadata data
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
2017-05-23 23:15:13 +00:00
|
|
|
*/
|
|
|
|
protected $data;
|
2017-04-26 14:18:41 +00:00
|
|
|
|
2017-04-26 23:39:19 +00:00
|
|
|
/**
|
2017-08-08 14:25:32 +00:00
|
|
|
* Constructor.
|
2017-04-26 23:39:19 +00:00
|
|
|
*
|
2018-03-21 03:11:16 +00:00
|
|
|
* @param array $meta Data to wrap behind this function.
|
2017-04-26 23:39:19 +00:00
|
|
|
*/
|
2017-08-08 14:25:32 +00:00
|
|
|
public function __construct( $meta = array() ) {
|
2017-05-23 23:15:13 +00:00
|
|
|
$this->current_data = $meta;
|
2017-04-26 14:18:41 +00:00
|
|
|
$this->apply_changes();
|
|
|
|
}
|
|
|
|
|
2017-10-04 13:12:53 +00:00
|
|
|
/**
|
|
|
|
* When converted to JSON.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function jsonSerialize() {
|
|
|
|
return $this->get_data();
|
|
|
|
}
|
|
|
|
|
2017-04-26 14:18:41 +00:00
|
|
|
/**
|
|
|
|
* Merge changes with data and clear.
|
|
|
|
*/
|
|
|
|
public function apply_changes() {
|
2017-05-23 23:15:13 +00:00
|
|
|
$this->data = $this->current_data;
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:39:13 +00:00
|
|
|
/**
|
|
|
|
* Creates or updates a property in the metadata object.
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @param string $key Key to set.
|
|
|
|
* @param mixed $value Value to set.
|
2017-05-31 13:39:13 +00:00
|
|
|
*/
|
2017-05-23 23:15:13 +00:00
|
|
|
public function __set( $key, $value ) {
|
|
|
|
$this->current_data[ $key ] = $value;
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:39:13 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given key exists in our data. This is called internally
|
|
|
|
* by `empty` and `isset`.
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @param string $key Key to check if set.
|
2017-05-31 13:39:13 +00:00
|
|
|
*/
|
2017-05-23 23:15:13 +00:00
|
|
|
public function __isset( $key ) {
|
|
|
|
return array_key_exists( $key, $this->current_data );
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:39:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the value of any property.
|
|
|
|
*
|
2017-08-08 14:25:32 +00:00
|
|
|
* @param string $key Key to get.
|
2017-05-31 13:39:13 +00:00
|
|
|
* @return mixed Property value or NULL if it does not exists
|
|
|
|
*/
|
2017-05-23 23:15:13 +00:00
|
|
|
public function __get( $key ) {
|
2017-06-01 15:46:05 +00:00
|
|
|
if ( array_key_exists( $key, $this->current_data ) ) {
|
2017-05-23 23:15:13 +00:00
|
|
|
return $this->current_data[ $key ];
|
2017-04-26 14:18:41 +00:00
|
|
|
}
|
2017-05-23 23:15:13 +00:00
|
|
|
return null;
|
2017-04-26 14:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return data changes only.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_changes() {
|
|
|
|
$changes = array();
|
2017-06-01 15:47:36 +00:00
|
|
|
foreach ( $this->current_data as $id => $value ) {
|
2017-05-23 23:15:13 +00:00
|
|
|
if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) {
|
|
|
|
$changes[ $id ] = $value;
|
2017-04-26 14:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $changes;
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:12:33 +00:00
|
|
|
/**
|
|
|
|
* Return all data as an array.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
2017-04-26 14:18:41 +00:00
|
|
|
}
|