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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set( $key, $value ) {
|
|
|
|
$this->current_data[ $key ] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __isset( $key ) {
|
|
|
|
return array_key_exists( $key, $this->current_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get( $key ) {
|
|
|
|
if ( array_key_exists( $key, $this->current_data )) {
|
|
|
|
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() {
|
2017-05-23 23:15:13 +00:00
|
|
|
|
2017-04-26 14:18:41 +00:00
|
|
|
$changes = array();
|
2017-05-23 23:15:13 +00:00
|
|
|
foreach ( $this->current_data as $id => $value) {
|
|
|
|
if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) {
|
|
|
|
$changes[ $id ] = $value;
|
2017-04-26 14:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|