2017-04-26 14:18:41 +00:00
|
|
|
<?php
|
|
|
|
|
2017-05-23 23:15:13 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @version 3.0.x
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Class
|
|
|
|
* @author crodas
|
|
|
|
*/
|
2017-04-26 14:18:41 +00:00
|
|
|
class WC_Meta_Data {
|
2017-05-23 23:15:13 +00:00
|
|
|
/**
|
|
|
|
* Current data for metadata
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @var arrray
|
|
|
|
*/
|
|
|
|
protected $current_data;
|
2017-04-26 14:18:41 +00:00
|
|
|
|
2017-05-23 23:15:13 +00:00
|
|
|
/**
|
|
|
|
* Metadata data
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @var arrray
|
|
|
|
*/
|
|
|
|
protected $data;
|
2017-04-26 14:18:41 +00:00
|
|
|
|
2017-04-26 23:39:19 +00:00
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*
|
|
|
|
* @param Array meta data to wrap behind this function
|
|
|
|
*/
|
2017-04-26 14:18:41 +00:00
|
|
|
public function __construct( Array $meta ) {
|
2017-05-23 23:15:13 +00:00
|
|
|
$this->current_data = $meta;
|
2017-04-26 14:18:41 +00:00
|
|
|
$this->apply_changes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
*/
|
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`.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|