woocommerce/includes/abstracts/abstract-wc-data.php

411 lines
11 KiB
PHP
Raw Normal View History

2016-03-11 19:50:44 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Abstract WC Data Class
*
* Implemented by classes using the same CRUD(s) pattern.
*
2016-03-15 22:44:04 +00:00
* @version 2.6.0
2016-03-11 19:50:44 +00:00
* @package WooCommerce/Abstracts
* @category Abstract Class
* @author WooThemes
*/
abstract class WC_Data {
/**
* Core data for this object, name value pairs (name + default value).
* @var array
*/
protected $_data = array();
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
*/
protected $_cache_group = '';
/**
* 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'.
*/
protected $_meta_type = 'post';
/**
* 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
*/
protected $_meta_data = array();
/**
* Internal meta keys we don't want exposed for the object.
* @var array
*/
protected $_internal_meta_keys = array();
/**
* Returns the unique ID for this object.
* @return int
*/
abstract public function get_id();
2016-03-11 19:50:44 +00:00
/**
* Creates new object in the database.
*/
abstract public function create();
/**
* Read object from the database.
* @param int ID of the object to load.
*/
abstract public function read( $id );
/**
* Updates object data in the database.
*/
abstract public function update();
/**
* Updates object data in the database.
*/
abstract public function delete();
2016-03-11 19:50:44 +00:00
/**
* Save should create or update based on object existance.
*/
abstract public function save();
/**
* 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() {
return array_merge( $this->_data, array( 'meta_data' => $this->get_meta_data() ) );
}
2016-03-11 19:50:44 +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() {
return $this->_meta_data;
}
/**
* 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() {
return array_merge( array_map( array( $this, 'prefix_key' ), array_keys( $this->_data ) ), $this->_internal_meta_keys );
}
/**
* 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 ) {
$array_keys = array_keys( wp_list_pluck( $this->_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 ) {
$value = $this->_meta_data[ current( $array_keys ) ]->value;
2016-03-11 19:50:44 +00:00
} else {
$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 ) ) {
foreach ( $data as $meta ) {
2016-03-11 19:50:44 +00:00
$meta = (array) $meta;
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
$this->_meta_data[] = (object) array(
'id' => $meta['id'],
'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 ) {
$array_keys = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key );
$this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $array_keys, '' ) );
2016-03-11 19:50:44 +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 = '' ) {
$array_key = '';
if ( $meta_id ) {
$array_key = array_keys( wp_list_pluck( $this->_meta_data, 'id' ), $meta_id );
}
if ( $array_key ) {
$this->_meta_data[ current( $array_key ) ] = (object) array(
'id' => $meta_id,
'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 ) {
$array_keys = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key );
$this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $array_keys, '' ) );
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 ) {
$array_keys = array_keys( wp_list_pluck( $this->_meta_data, 'id' ), $mid );
2016-08-19 12:43:33 +00:00
$this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $array_keys, '' ) );
}
/**
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
*/
protected function read_meta_data() {
$this->_meta_data = array();
$cache_loaded = false;
if ( ! $this->get_id() ) {
return;
}
if ( ! empty( $this->_cache_group ) ) {
2016-03-11 19:50:44 +00:00
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->_cache_group ) . $this->get_id();
$cached_meta = wp_cache_get( $cache_key, $this->_cache_group );
if ( false !== $cached_meta ) {
$this->_meta_data = $cached_meta;
$cache_loaded = true;
}
}
if ( ! $cache_loaded ) {
global $wpdb;
$db_info = $this->_get_db_info();
$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "
SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value
FROM " . $db_info['table'] . "
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() ) );
if ( $raw_meta_data ) {
foreach ( $raw_meta_data as $meta ) {
if ( in_array( $meta->meta_key, $this->get_internal_meta_keys() ) ) {
continue;
}
$this->_meta_data[] = (object) array(
'id' => (int) $meta->{ $db_info['meta_id_field'] },
'key' => $meta->meta_key,
'value' => maybe_unserialize( $meta->meta_value ),
);
2016-03-11 19:50:44 +00:00
}
}
if ( ! empty( $this->_cache_group ) ) {
2016-03-11 19:50:44 +00:00
wp_cache_set( $cache_key, $this->_meta_data, $this->_cache_group );
}
}
}
/**
* 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
*/
protected function save_meta_data() {
global $wpdb;
$db_info = $this->_get_db_info();
$all_meta_ids = array_map( 'absint', $wpdb->get_col( $wpdb->prepare( "
SELECT " . $db_info['meta_id_field'] . " FROM " . $db_info['table'] . "
WHERE " . $db_info['object_id_field'] . " = %d", $this->get_id() ) . "
AND meta_key NOT IN ('" . implode( "','", array_map( 'esc_sql', $this->get_internal_meta_keys() ) ) . "')
2016-08-15 15:53:48 +00:00
AND meta_key NOT LIKE 'wp\_%%';
2016-03-11 19:50:44 +00:00
" ) );
$set_meta_ids = array();
foreach ( $this->_meta_data as $array_key => $meta ) {
if ( empty( $meta->id ) ) {
$new_meta_id = add_metadata( $this->_meta_type, $this->get_id(), $meta->key, $meta->value, false );
$set_meta_ids[] = $new_meta_id;
$this->_meta_data[ $array_key ]->id = $new_meta_id;
2016-03-11 19:50:44 +00:00
} else {
update_metadata_by_mid( $this->_meta_type, $meta->id, $meta->value, $meta->key );
$set_meta_ids[] = absint( $meta->id );
2016-03-11 19:50:44 +00:00
}
}
// Delete no longer set meta data
$delete_meta_ids = array_diff( $all_meta_ids, $set_meta_ids );
foreach ( $delete_meta_ids as $meta_id ) {
delete_metadata_by_mid( $this->_meta_type, $meta_id );
2016-03-11 19:50:44 +00:00
}
if ( ! empty( $this->_cache_group ) ) {
2016-03-11 19:50:44 +00:00
WC_Cache_Helper::incr_cache_prefix( $this->_cache_group );
}
$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-03-15 22:11:07 +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.
if ( ! in_array( $this->_meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) {
$table .= 'woocommerce_';
2016-03-11 19:50:44 +00:00
}
$table .= $this->_meta_type . 'meta';
2016-03-15 22:11:07 +00:00
$object_id_field = $this->_meta_type . '_id';
2016-03-11 19:50:44 +00:00
// Figure out our field names.
2016-03-15 22:11:07 +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,
);
}
/**
* Get internal data prop (raw).
2016-08-23 17:48:48 +00:00
* @param string ...$param Prop keys to retrieve. Supports multiple keys to get nested values.
* @return mixed
*/
2016-08-23 17:48:48 +00:00
protected function get_prop() {
$args = func_get_args();
$target = &$this->_data;
foreach ( $args as $arg ) {
if ( ! isset( $target[ $arg ] ) ) {
return false;
}
$target = &$target[ $arg ];
}
return $target;
}
2016-08-23 14:49:34 +00:00
/**
* Set internal data prop to specified value.
2016-08-23 17:48:48 +00:00
* @param int ...$param Prop keys followed by value to set.
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
*/
2016-08-23 17:48:48 +00:00
protected function set_prop() {
2016-08-24 09:46:29 +00:00
if ( func_num_args() < 2 ) {
$this->throw_exception( 'invalid_value', 'set_prop requires at least 2 parameters' );
2016-08-23 17:48:48 +00:00
}
2016-08-24 09:46:29 +00:00
$args = func_get_args();
$value = array_pop( $args );
$prop = &$this->_data;
2016-08-23 17:48:48 +00:00
foreach ( $args as $arg ) {
2016-08-24 09:46:29 +00:00
$prop = &$prop[ $arg ];
2016-08-23 17:48:48 +00:00
}
2016-08-24 09:46:29 +00:00
$prop = $value;
}
2016-08-17 10:44:56 +00:00
/**
2016-08-23 12:43:26 +00:00
* Returns an invalid data WP_Error object.
* @param string $message Error Message.
* @param mixed $data Data the user tried to set.
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
2016-08-17 10:44:56 +00:00
*/
2016-08-24 09:46:29 +00:00
protected function throw_exception( $error_code, $error_message, $http_status_code = 400 ) {
throw new WC_Data_Exception( $error_code, $error_message, $http_status_code );
2016-08-17 10:44:56 +00:00
}
2016-03-11 19:50:44 +00:00
}