2016-11-21 23:48:49 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shared logic for WP based data.
|
2016-11-22 11:34:14 +00:00
|
|
|
* Contains functions like meta handling for all default data stores.
|
|
|
|
* Your own data store doesn't need to use WC_Data_Store_WP -- you can write
|
|
|
|
* your own meta handling functions.
|
2016-11-21 23:48:49 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-11-21 23:48:49 +00:00
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Data_Store_WP {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta type. This should match up with
|
2017-03-20 10:03:14 +00:00
|
|
|
* the types available at https://codex.wordpress.org/Function_Reference/add_metadata.
|
2016-11-21 23:48:49 +00:00
|
|
|
* 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 = '';
|
|
|
|
|
|
|
|
/**
|
2016-11-22 11:34:14 +00:00
|
|
|
* Data stored in meta keys, but not considered "meta" for an object.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-21 23:48:49 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $internal_meta_keys = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get and store terms from a taxonomy.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-02-07 12:35:24 +00:00
|
|
|
* @param WC_Data|integer $object
|
2016-11-21 23:48:49 +00:00
|
|
|
* @param string $taxonomy Taxonomy name e.g. product_cat
|
|
|
|
* @return array of terms
|
|
|
|
*/
|
|
|
|
protected function get_term_ids( $object, $taxonomy ) {
|
2017-02-07 12:35:24 +00:00
|
|
|
if ( is_numeric( $object ) ) {
|
|
|
|
$object_id = $object;
|
|
|
|
} else {
|
|
|
|
$object_id = $object->get_id();
|
|
|
|
}
|
|
|
|
$terms = get_the_terms( $object_id, $taxonomy );
|
2016-11-21 23:48:49 +00:00
|
|
|
if ( false === $terms || is_wp_error( $terms ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return wp_list_pluck( $terms, 'term_id' );
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of meta for an object.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-22 11:34:14 +00:00
|
|
|
* @param WC_Data
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-21 23:48:49 +00:00
|
|
|
public function read_meta( &$object ) {
|
|
|
|
global $wpdb;
|
|
|
|
$db_info = $this->get_db_info();
|
|
|
|
$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "
|
2017-02-01 00:43:52 +00:00
|
|
|
SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value
|
|
|
|
FROM {$db_info['table']}
|
|
|
|
WHERE {$db_info['object_id_field']} = %d
|
|
|
|
ORDER BY {$db_info['meta_id_field']}
|
2016-11-21 23:48:49 +00:00
|
|
|
", $object->get_id() ) );
|
|
|
|
|
|
|
|
$this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys );
|
|
|
|
return array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* Deletes meta based on meta ID.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-22 11:34:14 +00:00
|
|
|
* @param WC_Data
|
|
|
|
* @param stdClass (containing at least ->id)
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-21 23:48:49 +00:00
|
|
|
public function delete_meta( &$object, $meta ) {
|
|
|
|
delete_metadata_by_mid( $this->meta_type, $meta->id );
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* Add new piece of meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-22 11:34:14 +00:00
|
|
|
* @param WC_Data
|
|
|
|
* @param stdClass (containing ->key and ->value)
|
2017-03-20 10:03:14 +00:00
|
|
|
* @return int meta ID
|
2016-11-22 11:34:14 +00:00
|
|
|
*/
|
2016-11-21 23:48:49 +00:00
|
|
|
public function add_meta( &$object, $meta ) {
|
|
|
|
return add_metadata( $this->meta_type, $object->get_id(), $meta->key, $meta->value, false );
|
|
|
|
}
|
|
|
|
|
2016-11-22 11:34:14 +00:00
|
|
|
/**
|
|
|
|
* Update meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-22 11:34:14 +00:00
|
|
|
* @param WC_Data
|
|
|
|
* @param stdClass (containing ->id, ->key and ->value)
|
|
|
|
*/
|
2016-11-21 23:48:49 +00:00
|
|
|
public function update_meta( &$object, $meta ) {
|
|
|
|
update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table structure is slightly different between meta types, this function will return what we need to know.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-21 23:48:49 +00:00
|
|
|
* @return array Array elements: table, object_id_field, meta_id_field
|
|
|
|
*/
|
|
|
|
protected function get_db_info() {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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_';
|
|
|
|
}
|
|
|
|
|
|
|
|
$table .= $this->meta_type . 'meta';
|
|
|
|
$object_id_field = $this->meta_type . '_id';
|
|
|
|
|
|
|
|
// Figure out our field names.
|
|
|
|
if ( 'user' === $this->meta_type ) {
|
|
|
|
$meta_id_field = 'umeta_id';
|
2017-01-18 17:11:21 +00:00
|
|
|
$table = $wpdb->usermeta;
|
2016-11-21 23:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $this->object_id_field_for_meta ) ) {
|
|
|
|
$object_id_field = $this->object_id_field_for_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'table' => $table,
|
|
|
|
'object_id_field' => $object_id_field,
|
|
|
|
'meta_id_field' => $meta_id_field,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. This is in
|
|
|
|
* addition to all data props with _ prefix.
|
|
|
|
* @since 2.6.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prefix_key( $key ) {
|
|
|
|
return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to remove unwanted meta data.
|
|
|
|
*
|
|
|
|
* @param object $meta
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function exclude_internal_meta_keys( $meta ) {
|
2017-02-01 00:43:52 +00:00
|
|
|
return ! in_array( $meta->meta_key, $this->internal_meta_keys ) && 0 !== stripos( $meta->meta_key, 'wp_' );
|
2016-11-21 23:48:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:20:29 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of props and meta keys that need updated based on change state
|
|
|
|
* or if they are present in the database or not.
|
|
|
|
*
|
|
|
|
* @param WC_Data $object The WP_Data object (WC_Coupon for coupons, etc).
|
|
|
|
* @param array $meta_key_to_props A mapping of meta keys => prop names.
|
|
|
|
* @param string $meta_type The internal WP meta type (post, user, etc).
|
|
|
|
* @return array A mapping of meta keys => prop names, filtered by ones that should be updated.
|
|
|
|
*/
|
|
|
|
protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) {
|
|
|
|
$props_to_update = array();
|
|
|
|
$changed_props = $object->get_changes();
|
|
|
|
|
|
|
|
// Props should be updated if they are a part of the $changed array or don't exist yet.
|
|
|
|
foreach ( $meta_key_to_props as $meta_key => $prop ) {
|
2017-01-23 21:08:55 +00:00
|
|
|
if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) {
|
2017-01-23 20:20:29 +00:00
|
|
|
$props_to_update[ $meta_key ] = $prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $props_to_update;
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
/**
|
|
|
|
* Get valid WP_Query args from a WC_Object_Query's query variables.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @param array $query_vars query vars from a WC_Object_Query
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-27 18:07:03 +00:00
|
|
|
protected function get_wp_query_args( $query_vars ) {
|
2017-04-25 21:52:17 +00:00
|
|
|
|
|
|
|
$skipped_values = array( '', array(), null );
|
|
|
|
$wp_query_args = array(
|
2017-04-28 02:14:48 +00:00
|
|
|
'meta_query' => array(),
|
2017-04-25 21:52:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach( $query_vars as $key => $value ) {
|
|
|
|
if ( in_array( $value, $skipped_values, true ) || 'meta_query' === $key ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:04:28 +00:00
|
|
|
// Build meta queries out of vars that are stored in internal meta keys.
|
2017-04-25 21:52:17 +00:00
|
|
|
if ( in_array( '_' . $key, $this->internal_meta_keys ) ) {
|
|
|
|
$wp_query_args['meta_query'][] = array(
|
|
|
|
'key' => '_' . $key,
|
|
|
|
'value' => $value,
|
|
|
|
'compare' => '=',
|
|
|
|
);
|
2017-04-27 21:09:10 +00:00
|
|
|
// Other vars get mapped to wp_query args or just left alone.
|
2017-04-25 21:52:17 +00:00
|
|
|
} else {
|
2017-04-26 17:49:19 +00:00
|
|
|
$key_mapping = array (
|
2017-04-26 21:20:44 +00:00
|
|
|
'parent' => 'post_parent',
|
2017-04-27 21:09:10 +00:00
|
|
|
'parent_exclude' => 'post_parent__not_in',
|
|
|
|
'exclude' => 'post__not_in',
|
|
|
|
'limit' => 'posts_per_page',
|
2017-04-26 21:20:44 +00:00
|
|
|
'type' => 'post_type',
|
2017-04-27 15:38:18 +00:00
|
|
|
'return' => 'fields',
|
2017-04-26 17:49:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $key_mapping[ $key ] ) ) {
|
|
|
|
$wp_query_args[ $key_mapping[ $key ] ] = $value;
|
|
|
|
} else {
|
|
|
|
$wp_query_args[ $key ] = $value;
|
2017-04-25 21:52:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:19:54 +00:00
|
|
|
return apply_filters( 'woocommerce_get_wp_query_args', $wp_query_args, $query_vars );
|
2017-04-25 21:52:17 +00:00
|
|
|
}
|
2017-04-28 21:01:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a valid date for use in WP_Query date queries.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @param mixed $query_var Value from a WC_Object_Query's query variable.
|
|
|
|
*/
|
|
|
|
protected function get_date_for_wp_query( $query_var, $as_timestamp = false ) {
|
|
|
|
if ( ! $query_var ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_a( $query_var, 'WC_DateTime' ) ) {
|
|
|
|
$datetime = $query_var;
|
|
|
|
} elseif ( is_numeric( $query_var ) ) {
|
|
|
|
// Timestamps are handled as UTC timestamps in all cases.
|
|
|
|
$datetime = new WC_DateTime( "@{$value}", new DateTimeZone( 'UTC' ) );
|
|
|
|
} else {
|
|
|
|
// Strings are defined in local WP timezone. Convert to UTC.
|
|
|
|
if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $query_var, $date_bits ) ) {
|
|
|
|
$offset = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
|
|
|
|
$timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
|
|
|
|
} else {
|
|
|
|
$timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $query_var ) ) ) );
|
|
|
|
}
|
|
|
|
$datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set local timezone or offset.
|
|
|
|
if ( get_option( 'timezone_string' ) ) {
|
|
|
|
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
|
|
|
|
} else {
|
|
|
|
$datetime->set_utc_offset( wc_timezone_offset() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $as_timestamp ? $datetime->getTimestamp() : (string) $datetime;
|
|
|
|
}
|
2016-11-21 23:48:49 +00:00
|
|
|
}
|