2016-03-11 19:50:44 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-02 02:40:36 +00:00
|
|
|
* Abstract WC Data Class
|
|
|
|
*
|
|
|
|
* Implemented by classes using the same CRUD(s) pattern.
|
|
|
|
*
|
|
|
|
* @version 2.6.0
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-03-11 19:50:44 +00:00
|
|
|
abstract class WC_Data {
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
/**
|
|
|
|
* ID for this object.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $id = 0;
|
|
|
|
|
2016-03-17 15:22:29 +00:00
|
|
|
/**
|
2016-08-25 12:31:03 +00:00
|
|
|
* Core data for this object. Name value pairs (name + default value).
|
2016-08-24 11:34:19 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $data = array();
|
2016-08-24 11:34:19 +00:00
|
|
|
|
2016-11-08 10:16:16 +00:00
|
|
|
/**
|
|
|
|
* Core data changes for this object.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $changes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is false until the object is read from the DB.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $object_read = false;
|
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Extra data for this object. Name value pairs (name + default value).
|
|
|
|
* Used as a standard way for sub classes (like product types) to add
|
|
|
|
* additional information to an inherited class.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $extra_data = array();
|
|
|
|
|
2016-08-24 11:34:19 +00:00
|
|
|
/**
|
2016-08-25 12:31:03 +00:00
|
|
|
* Set to _data on construct so we can track and reset data if needed.
|
2016-03-17 15:22:29 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $default_data = array();
|
2016-03-17 15:22:29 +00:00
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Contains a reference to the data store for this class.
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $data_store;
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
* Stores meta in cache for future reads.
|
|
|
|
* A group must be set to to enable caching.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $cache_group = '';
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta type. This should match up with
|
|
|
|
* the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata.
|
|
|
|
* WP defines 'post', 'user', 'comment', and 'term'.
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $meta_type = 'post';
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This only needs set if you are using a custom metadata type (for example payment tokens.
|
|
|
|
* This should be the name of the field your table uses for associating meta with objects.
|
|
|
|
* For example, in payment_tokenmeta, this would be payment_token_id.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $object_id_field_for_meta = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores additonal meta data.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $meta_data = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed for the object.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $internal_meta_keys = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
|
2016-08-25 12:31:03 +00:00
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
* @param int|object|array $read ID to load from the DB (optional) or already queried data.
|
|
|
|
*/
|
|
|
|
public function __construct( $read = 0 ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->default_data = $this->data;
|
2016-08-25 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the unique ID for this object.
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
public function get_id() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates object data in the database.
|
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function delete( $force_delete = false ) {
|
|
|
|
if ( $this->data_store ) {
|
|
|
|
$this->data_store->delete( $this, $force_delete );
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save should create or update based on object existance.
|
2016-11-08 10:16:16 +00:00
|
|
|
*
|
|
|
|
* @return int
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function save() {
|
|
|
|
if ( $this->data_store ) {
|
|
|
|
if ( $this->get_id() ) {
|
|
|
|
$this->data_store->update( $this );
|
|
|
|
} else {
|
|
|
|
$this->data_store->create( $this );
|
|
|
|
}
|
|
|
|
return $this->get_id();
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
2016-03-17 15:22:29 +00:00
|
|
|
/**
|
|
|
|
* Change data to JSON format.
|
|
|
|
* @return string Data in JSON format.
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return json_encode( $this->get_data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all data for this object.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
|
2016-03-17 15:22:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Returns array of expected data keys for this object.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data_keys() {
|
|
|
|
return array_keys( $this->data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all "extra" data keys for an object (for sub objects like product types).
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_extra_data_keys() {
|
|
|
|
return array_keys( $this->extra_data );
|
|
|
|
}
|
|
|
|
|
2016-08-25 09:32:42 +00:00
|
|
|
/**
|
|
|
|
* Filter null meta values from array.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-25 09:45:50 +00:00
|
|
|
protected function filter_null_meta( $meta ) {
|
|
|
|
return ! is_null( $meta->value );
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
2016-03-17 16:38:56 +00:00
|
|
|
* Get All Meta Data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_meta_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. This is in
|
|
|
|
* addition to all data props with _ prefix.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-06 18:52:51 +00:00
|
|
|
* @return array
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
protected function prefix_key( $key ) {
|
|
|
|
return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. This is in
|
|
|
|
* addition to all data props with _ prefix.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-06 18:52:51 +00:00
|
|
|
* @return array
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
protected function get_internal_meta_keys() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_merge( array_map( array( $this, 'prefix_key' ), array_keys( $this->data ) ), $this->internal_meta_keys );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Meta Data by Key.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param bool $single return first found meta with key, or all with $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get_meta( $key = '', $single = true ) {
|
2016-08-25 09:32:42 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->get_meta_data(), 'key' ), $key );
|
2016-03-11 19:50:44 +00:00
|
|
|
$value = '';
|
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $array_keys ) ) {
|
2016-03-11 19:50:44 +00:00
|
|
|
if ( $single ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$value = $this->meta_data[ current( $array_keys ) ]->value;
|
2016-03-11 19:50:44 +00:00
|
|
|
} else {
|
2016-09-09 12:34:49 +00:00
|
|
|
$value = array_intersect_key( $this->meta_data, array_flip( $array_keys ) );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set all meta data from array.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param array $data Key/Value pairs
|
|
|
|
*/
|
|
|
|
public function set_meta_data( $data ) {
|
|
|
|
if ( ! empty( $data ) && is_array( $data ) ) {
|
2016-03-18 19:18:41 +00:00
|
|
|
foreach ( $data as $meta ) {
|
2016-03-11 19:50:44 +00:00
|
|
|
$meta = (array) $meta;
|
2016-08-22 18:37:34 +00:00
|
|
|
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-08-22 19:58:34 +00:00
|
|
|
'id' => $meta['id'],
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $meta['key'],
|
|
|
|
'value' => $meta['value'],
|
2016-03-11 19:50:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add meta data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-07 10:06:41 +00:00
|
|
|
* @param string $key Meta key
|
|
|
|
* @param string $value Meta value
|
|
|
|
* @param bool $unique Should this be a unique key?
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
public function add_meta_data( $key, $value, $unique = false ) {
|
|
|
|
if ( $unique ) {
|
2016-08-25 09:45:50 +00:00
|
|
|
$this->delete_meta_data( $key );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-03-11 19:50:44 +00:00
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update meta data by key or ID, if provided.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @param int $meta_id
|
|
|
|
*/
|
|
|
|
public function update_meta_data( $key, $value, $meta_id = '' ) {
|
2016-03-18 19:18:41 +00:00
|
|
|
$array_key = '';
|
|
|
|
if ( $meta_id ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$array_key = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id );
|
2016-03-18 19:18:41 +00:00
|
|
|
}
|
|
|
|
if ( $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ current( $array_key ) ] = (object) array(
|
2016-08-22 19:58:34 +00:00
|
|
|
'id' => $meta_id,
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
2016-03-11 19:50:44 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->add_meta_data( $key, $value, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete meta data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param array $key Meta key
|
|
|
|
*/
|
|
|
|
public function delete_meta_data( $key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key );
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( $array_keys ) {
|
|
|
|
foreach ( $array_keys as $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ $array_key ]->value = null;
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 12:43:33 +00:00
|
|
|
* Delete meta data.
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param int $mid Meta ID
|
|
|
|
*/
|
|
|
|
public function delete_meta_data_by_mid( $mid ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $mid );
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( $array_keys ) {
|
|
|
|
foreach ( $array_keys as $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ $array_key ]->value = null;
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 12:43:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 14:43:34 +00:00
|
|
|
/**
|
|
|
|
* Callback to remove unwanted meta data.
|
|
|
|
*
|
|
|
|
* @param object $meta
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function exclude_internal_meta_keys( $meta ) {
|
|
|
|
return ! in_array( $meta->meta_key, $this->get_internal_meta_keys() );
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:43:33 +00:00
|
|
|
/**
|
2016-03-11 19:50:44 +00:00
|
|
|
* Read Meta Data from the database. Ignore any internal properties.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-09 12:21:18 +00:00
|
|
|
public function read_meta_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
$cache_loaded = false;
|
|
|
|
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . $this->get_id();
|
|
|
|
$cached_meta = wp_cache_get( $cache_key, $this->cache_group );
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
if ( false !== $cached_meta ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data = $cached_meta;
|
2016-03-11 19:50:44 +00:00
|
|
|
$cache_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $cache_loaded ) {
|
|
|
|
global $wpdb;
|
2016-09-09 12:34:49 +00:00
|
|
|
$db_info = $this->get_db_info();
|
2016-03-11 19:50:44 +00:00
|
|
|
$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "
|
|
|
|
SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value
|
|
|
|
FROM " . $db_info['table'] . "
|
2016-08-15 16:37:18 +00:00
|
|
|
WHERE " . $db_info['object_id_field'] . "=%d AND meta_key NOT LIKE 'wp\_%%' ORDER BY " . $db_info['meta_id_field'] . "
|
2016-03-11 19:50:44 +00:00
|
|
|
", $this->get_id() ) );
|
|
|
|
|
2016-08-15 12:17:43 +00:00
|
|
|
if ( $raw_meta_data ) {
|
2016-08-31 14:43:34 +00:00
|
|
|
$raw_meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
|
2016-08-15 12:17:43 +00:00
|
|
|
foreach ( $raw_meta_data as $meta ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-08-22 19:59:03 +00:00
|
|
|
'id' => (int) $meta->{ $db_info['meta_id_field'] },
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $meta->meta_key,
|
|
|
|
'value' => maybe_unserialize( $meta->meta_value ),
|
2016-08-15 12:17:43 +00:00
|
|
|
);
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
wp_cache_set( $cache_key, $this->meta_data, $this->cache_group );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update Meta Data in the database.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-09 12:21:18 +00:00
|
|
|
public function save_meta_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
foreach ( $this->meta_data as $array_key => $meta ) {
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( is_null( $meta->value ) ) {
|
|
|
|
if ( ! empty( $meta->id ) ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
delete_metadata_by_mid( $this->meta_type, $meta->id );
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
} elseif ( empty( $meta->id ) ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$new_meta_id = add_metadata( $this->meta_type, $this->get_id(), $meta->key, $meta->value, false );
|
|
|
|
$this->meta_data[ $array_key ]->id = $new_meta_id;
|
2016-03-11 19:50:44 +00:00
|
|
|
} else {
|
2016-09-09 12:34:49 +00:00
|
|
|
update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
$this->read_meta_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table structure is slightly different between meta types, this function will return what we need to know.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @return array Array elements: table, object_id_field, meta_id_field
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected function get_db_info() {
|
2016-03-11 19:50:44 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$meta_id_field = 'meta_id'; // for some reason users calls this umeta_id so we need to track this as well.
|
|
|
|
$table = $wpdb->prefix;
|
2016-03-15 22:11:07 +00:00
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
// If we are dealing with a type of metadata that is not a core type, the table should be prefixed.
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) {
|
2016-03-11 21:48:16 +00:00
|
|
|
$table .= 'woocommerce_';
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
$table .= $this->meta_type . 'meta';
|
|
|
|
$object_id_field = $this->meta_type . '_id';
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
// Figure out our field names.
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( 'user' === $this->meta_type ) {
|
2016-03-11 19:50:44 +00:00
|
|
|
$meta_id_field = 'umeta_id';
|
2016-03-15 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $this->object_id_field_for_meta ) ) {
|
|
|
|
$object_id_field = $this->object_id_field_for_meta;
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'table' => $table,
|
|
|
|
'object_id_field' => $object_id_field,
|
|
|
|
'meta_id_field' => $meta_id_field,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
/**
|
|
|
|
* Set ID.
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
public function set_id( $id ) {
|
|
|
|
$this->id = absint( $id );
|
|
|
|
}
|
|
|
|
|
2016-08-24 11:34:19 +00:00
|
|
|
/**
|
|
|
|
* Set all props to default values.
|
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function set_defaults() {
|
2016-11-08 10:16:16 +00:00
|
|
|
$this->data = $this->default_data;
|
|
|
|
$this->changes = array();
|
|
|
|
$this->set_object_read( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set object read property.
|
|
|
|
* @param boolean $read
|
|
|
|
*/
|
|
|
|
public function set_object_read( $read = true ) {
|
|
|
|
$this->object_read = (bool) $read;
|
2016-08-24 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 12:05:27 +00:00
|
|
|
/**
|
|
|
|
* Set a collection of props in one go, collect any errors, and return the result.
|
2016-11-08 10:16:16 +00:00
|
|
|
* Only sets using public methods.
|
2016-08-25 12:05:27 +00:00
|
|
|
* @param array $props Key value pairs to set. Key is the prop and should map to a setter function name.
|
|
|
|
* @return WP_Error|bool
|
|
|
|
*/
|
2016-11-08 10:28:00 +00:00
|
|
|
public function set_props( $props ) {
|
2016-08-25 12:05:27 +00:00
|
|
|
$errors = new WP_Error();
|
|
|
|
|
|
|
|
foreach ( $props as $prop => $value ) {
|
|
|
|
try {
|
2016-08-31 14:43:34 +00:00
|
|
|
if ( 'meta_data' === $prop ) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-25 12:05:27 +00:00
|
|
|
$setter = "set_$prop";
|
|
|
|
if ( ! is_null( $value ) && is_callable( array( $this, $setter ) ) ) {
|
2016-11-08 10:16:16 +00:00
|
|
|
$reflection = new ReflectionMethod( $this, $setter );
|
|
|
|
|
|
|
|
if ( $reflection->isPublic() ) {
|
|
|
|
$this->{$setter}( $value );
|
|
|
|
}
|
2016-08-25 12:05:27 +00:00
|
|
|
}
|
|
|
|
} catch ( WC_Data_Exception $e ) {
|
|
|
|
$errors->add( $e->getErrorCode(), $e->getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof( $errors->get_error_codes() ) ? $errors : true;
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:16:16 +00:00
|
|
|
/**
|
|
|
|
* Sets a prop for a setter method.
|
|
|
|
*
|
|
|
|
* This stores changes in a special array so we can track what needs saving
|
|
|
|
* the the DB later.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $prop Name of prop to set.
|
|
|
|
* @param mixed $value Value of the prop.
|
|
|
|
*/
|
|
|
|
protected function set_prop( $prop, $value ) {
|
|
|
|
if ( array_key_exists( $prop, $this->data ) ) {
|
|
|
|
if ( true === $this->object_read ) {
|
|
|
|
$this->changes[ $prop ] = $value;
|
|
|
|
} else {
|
|
|
|
$this->data[ $prop ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return data changes only.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-09 12:21:18 +00:00
|
|
|
public function get_changes() {
|
2016-11-08 10:16:16 +00:00
|
|
|
return $this->changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge changes with data and clear.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
protected function apply_changes() {
|
|
|
|
$this->data = array_merge( $this->data, $this->changes );
|
|
|
|
$this->changes = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for action and filter hooks on data.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_hook_prefix() {
|
|
|
|
return 'woocommerce_get_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a prop for a getter method.
|
|
|
|
*
|
|
|
|
* Gets the value from either current pending changes, or the data itself.
|
|
|
|
* Context controls what happens to the value before it's returned.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $prop Name of prop to get.
|
|
|
|
* @param string $context What the value is for. Valid values are view and edit.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get_prop( $prop, $context = 'view' ) {
|
|
|
|
$value = null;
|
|
|
|
|
|
|
|
if ( array_key_exists( $prop, $this->data ) ) {
|
|
|
|
$value = isset( $this->changes[ $prop ] ) ? $this->changes[ $prop ] : $this->data[ $prop ];
|
|
|
|
|
|
|
|
if ( 'view' === $context ) {
|
|
|
|
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2016-08-17 10:44:56 +00:00
|
|
|
/**
|
2016-08-24 13:37:19 +00:00
|
|
|
* When invalid data is found, throw an exception unless reading from the DB.
|
2016-08-24 14:26:35 +00:00
|
|
|
* @param string $error_code Error code.
|
2016-08-25 12:05:27 +00:00
|
|
|
* @param string $error_message Error message.
|
2016-08-24 09:46:29 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-17 10:44:56 +00:00
|
|
|
*/
|
2016-08-25 12:05:27 +00:00
|
|
|
protected function error( $error_code, $error_message ) {
|
|
|
|
throw new WC_Data_Exception( $error_code, $error_message );
|
2016-08-17 10:44:56 +00:00
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|