2016-11-21 23:48:49 +00:00
< ? php
/**
* 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
*
2018-02-22 18:30:49 +00:00
* @ version 3.0 . 0
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Classes
2018-02-22 18:30:49 +00:00
*/
defined ( 'ABSPATH' ) || exit ;
/**
* WC_Data_Store_WP class .
2016-11-21 23:48:49 +00:00
*/
class WC_Data_Store_WP {
/**
* Meta type . This should match up with
2019-06-12 22:31:01 +00:00
* the types available at https :// developer . wordpress . org / reference / functions / add_metadata /.
2016-11-21 23:48:49 +00:00
* WP defines 'post' , 'user' , 'comment' , and 'term' .
2018-02-22 18:30:49 +00:00
*
* @ var string
2016-11-21 23:48:49 +00:00
*/
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 .
2018-02-22 18:30:49 +00:00
*
2016-11-21 23:48:49 +00:00
* @ 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 .
2018-02-22 18:30:49 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2019-02-19 13:23:09 +00:00
*
2016-11-21 23:48:49 +00:00
* @ var array
*/
protected $internal_meta_keys = array ();
2019-02-19 13:23:09 +00:00
/**
* Meta data which should exist in the DB , even if empty .
*
* @ since 3.6 . 0
*
* @ var array
*/
protected $must_exist_meta_keys = array ();
2016-11-21 23:48:49 +00:00
/**
* Get and store terms from a taxonomy .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2018-03-08 18:31:19 +00:00
* @ param WC_Data | integer $object WC_Data object or object ID .
2018-02-22 18:30:49 +00:00
* @ param string $taxonomy Taxonomy name e . g . product_cat .
2016-11-21 23:48:49 +00:00
* @ 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
2018-03-08 18:31:19 +00:00
* @ param WC_Data $object WC_Data object .
2016-11-22 11:34:14 +00:00
* @ return array
*/
2016-11-21 23:48:49 +00:00
public function read_meta ( & $object ) {
global $wpdb ;
$db_info = $this -> get_db_info ();
2018-03-07 19:16:01 +00:00
$raw_meta_data = $wpdb -> get_results (
$wpdb -> prepare (
2020-03-11 20:56:02 +00:00
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
2018-03-07 19:16:01 +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' ]} " ,
2018-03-08 18:31:19 +00:00
// phpcs:enable
2018-03-07 19:16:01 +00:00
$object -> get_id ()
)
);
2020-10-21 09:23:28 +00:00
return $this -> filter_raw_meta_data ( $object , $raw_meta_data );
2020-06-24 14:09:11 +00:00
}
2016-11-21 23:48:49 +00:00
2020-06-24 14:09:11 +00:00
/**
* Helper method to filter internal meta keys from all meta data rows for the object .
*
2020-09-16 15:54:12 +00:00
* @ since 4.7 . 0
*
2020-06-24 14:09:11 +00:00
* @ param WC_Data $object WC_Data object .
* @ param array $raw_meta_data Array of std object of meta data to be filtered .
*
* @ return mixed | void
*/
2020-10-21 09:23:28 +00:00
public function filter_raw_meta_data ( & $object , $raw_meta_data ) {
2016-11-21 23:48:49 +00:00
$this -> internal_meta_keys = array_merge ( array_map ( array ( $this , 'prefix_key' ), $object -> get_data_keys () ), $this -> internal_meta_keys );
2017-07-17 09:34:44 +00:00
$meta_data = array_filter ( $raw_meta_data , array ( $this , 'exclude_internal_meta_keys' ) );
return apply_filters ( " woocommerce_data_store_wp_ { $this -> meta_type } _read_meta " , $meta_data , $object , $this );
2016-11-21 23:48:49 +00:00
}
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
2018-03-08 18:31:19 +00:00
* @ param WC_Data $object WC_Data object .
* @ param stdClass $meta ( containing at least -> id ) .
2016-11-22 11:34:14 +00:00
*/
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
2018-03-08 18:31:19 +00:00
* @ param WC_Data $object WC_Data object .
* @ param stdClass $meta ( 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 ) {
2020-03-11 20:56:02 +00:00
return add_metadata ( $this -> meta_type , $object -> get_id (), wp_slash ( $meta -> key ), is_string ( $meta -> value ) ? wp_slash ( $meta -> value ) : $meta -> value , false );
2016-11-21 23:48:49 +00:00
}
2016-11-22 11:34:14 +00:00
/**
* Update meta .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2018-03-08 18:31:19 +00:00
* @ param WC_Data $object WC_Data object .
* @ param stdClass $meta ( containing -> id , -> key and -> value ) .
2016-11-22 11:34:14 +00:00
*/
2016-11-21 23:48:49 +00:00
public function update_meta ( & $object , $meta ) {
2017-06-29 21:41:11 +00:00
update_metadata_by_mid ( $this -> meta_type , $meta -> id , $meta -> value , $meta -> key );
2016-11-21 23:48:49 +00:00
}
/**
* 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 ;
2018-02-22 18:30:49 +00:00
$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-11-21 23:48:49 +00:00
// If we are dealing with a type of metadata that is not a core type, the table should be prefixed.
2018-02-22 18:30:49 +00:00
if ( ! in_array ( $this -> meta_type , array ( 'post' , 'user' , 'comment' , 'term' ), true ) ) {
2016-11-21 23:48:49 +00:00
$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 .
2017-05-15 11:50:52 +00:00
*
2018-02-22 18:30:49 +00:00
* @ since 2.6 . 0
2017-05-15 11:50:52 +00:00
*
2018-03-08 18:31:19 +00:00
* @ param string $key Prefix to be added to meta keys .
2017-05-15 11:50:52 +00:00
* @ return string
2016-11-21 23:48:49 +00:00
*/
protected function prefix_key ( $key ) {
return '_' === substr ( $key , 0 , 1 ) ? $key : '_' . $key ;
}
/**
* Callback to remove unwanted meta data .
*
2018-03-08 18:31:19 +00:00
* @ param object $meta Meta object to check if it should be excluded or not .
2016-11-21 23:48:49 +00:00
* @ return bool
*/
protected function exclude_internal_meta_keys ( $meta ) {
2018-02-22 18:30:49 +00:00
return ! in_array ( $meta -> meta_key , $this -> internal_meta_keys , true ) && 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 ;
}
2019-02-19 13:23:09 +00:00
/**
2019-02-19 15:35:05 +00:00
* Update meta data in , or delete it from , the database .
2019-02-19 13:23:09 +00:00
*
2019-02-19 15:35:05 +00:00
* Avoids storing meta when it ' s either an empty string or empty array .
* Other empty values such as numeric 0 and null should still be stored .
* Data - stores can force meta to exist using `must_exist_meta_keys` .
*
* Note : WordPress `get_metadata` function returns an empty string when meta data does not exist .
2019-02-19 13:23:09 +00:00
*
* @ param WC_Data $object The WP_Data object ( WC_Coupon for coupons , etc ) .
* @ param string $meta_key Meta key to update .
* @ param mixed $meta_value Value to save .
*
* @ since 3.6 . 0 Added to prevent empty meta being stored unless required .
*
* @ return bool True if updated / deleted .
*/
protected function update_or_delete_post_meta ( $object , $meta_key , $meta_value ) {
2019-02-19 15:35:05 +00:00
if ( in_array ( $meta_value , array ( array (), '' ), true ) && ! in_array ( $meta_key , $this -> must_exist_meta_keys , true ) ) {
2019-02-19 13:23:09 +00:00
$updated = delete_post_meta ( $object -> get_id (), $meta_key );
2019-02-19 15:35:05 +00:00
} else {
$updated = update_post_meta ( $object -> get_id (), $meta_key , $meta_value );
2019-02-19 13:23:09 +00:00
}
return ( bool ) $updated ;
}
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
2018-02-22 18:30:49 +00:00
* @ param array $query_vars query vars from a WC_Object_Query .
2017-04-26 17:49:19 +00:00
* @ 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 );
2018-02-22 18:30:49 +00:00
$wp_query_args = array (
2017-06-13 20:32:21 +00:00
'errors' => array (),
2019-02-19 13:23:09 +00:00
'meta_query' => array (), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2017-04-25 21:52:17 +00:00
);
2017-05-09 21:55:39 +00:00
foreach ( $query_vars as $key => $value ) {
2017-04-25 21:52:17 +00:00
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.
2018-02-22 18:30:49 +00:00
if ( in_array ( '_' . $key , $this -> internal_meta_keys , true ) ) {
2018-08-22 23:59:39 +00:00
// Check for existing values if wildcard is used.
if ( '*' === $value ) {
$wp_query_args [ 'meta_query' ][] = array (
2018-08-24 03:03:09 +00:00
array (
'key' => '_' . $key ,
'compare' => 'EXISTS' ,
),
array (
'key' => '_' . $key ,
'value' => '' ,
'compare' => '!=' ,
),
2018-08-22 23:59:39 +00:00
);
} else {
$wp_query_args [ 'meta_query' ][] = array (
2018-08-23 05:23:35 +00:00
'key' => '_' . $key ,
2018-08-22 23:59:39 +00:00
'value' => $value ,
'compare' => is_array ( $value ) ? 'IN' : '=' ,
);
}
2018-03-07 19:16:01 +00:00
} else { // Other vars get mapped to wp_query args or just left alone.
2017-05-09 21:55:39 +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
2017-05-09 18:37:45 +00:00
/**
2017-05-09 21:49:45 +00:00
* Map a valid date query var to WP_Query arguments .
* Valid date formats : YYYY - MM - DD or timestamp , possibly combined with an operator from $valid_operators .
2017-05-09 18:37:45 +00:00
* Also accepts a WC_DateTime object .
2017-09-26 19:03:11 +00:00
*
* @ since 3.2 . 0
2018-02-22 18:30:49 +00:00
* @ param mixed $query_var A valid date format .
* @ param string $key meta or db column key .
* @ param array $wp_query_args WP_Query args .
2017-05-09 21:49:45 +00:00
* @ return array Modified $wp_query_args
2017-05-09 18:37:45 +00:00
*/
2017-09-26 19:03:11 +00:00
public function parse_date_for_wp_query ( $query_var , $key , $wp_query_args = array () ) {
2017-05-09 21:49:45 +00:00
$query_parse_regex = '/([^.<>]*)(>=|<=|>|<|\.\.\.)([^.<>]+)/' ;
2017-05-10 21:33:29 +00:00
$valid_operators = array ( '>' , '>=' , '=' , '<=' , '<' , '...' );
2017-05-09 21:49:45 +00:00
// YYYY-MM-DD queries have 'day' precision. Timestamp/WC_DateTime queries have 'second' precision.
2017-05-09 18:37:45 +00:00
$precision = 'second' ;
2017-05-10 21:33:29 +00:00
$dates = array ();
2017-05-09 18:37:45 +00:00
$operator = '=' ;
2017-05-10 20:08:47 +00:00
try {
// Specific time query with a WC_DateTime.
if ( is_a ( $query_var , 'WC_DateTime' ) ) {
$dates [] = $query_var ;
2018-03-07 19:16:01 +00:00
} elseif ( is_numeric ( $query_var ) ) { // Specific time query with a timestamp.
2017-05-10 20:08:47 +00:00
$dates [] = new WC_DateTime ( " @ { $query_var } " , new DateTimeZone ( 'UTC' ) );
2018-03-07 19:16:01 +00:00
} elseif ( preg_match ( $query_parse_regex , $query_var , $sections ) ) { // Query with operators and possible range of dates.
2017-05-10 20:08:47 +00:00
if ( ! empty ( $sections [ 1 ] ) ) {
$dates [] = is_numeric ( $sections [ 1 ] ) ? new WC_DateTime ( " @ { $sections [ 1 ] } " , new DateTimeZone ( 'UTC' ) ) : wc_string_to_datetime ( $sections [ 1 ] );
}
2017-05-09 18:37:45 +00:00
2018-02-22 18:30:49 +00:00
$operator = in_array ( $sections [ 2 ], $valid_operators , true ) ? $sections [ 2 ] : '' ;
$dates [] = is_numeric ( $sections [ 3 ] ) ? new WC_DateTime ( " @ { $sections [ 3 ] } " , new DateTimeZone ( 'UTC' ) ) : wc_string_to_datetime ( $sections [ 3 ] );
2017-05-10 20:08:47 +00:00
if ( ! is_numeric ( $sections [ 1 ] ) && ! is_numeric ( $sections [ 3 ] ) ) {
$precision = 'day' ;
}
2018-03-07 19:16:01 +00:00
} else { // Specific time query with a string.
2018-02-22 18:30:49 +00:00
$dates [] = wc_string_to_datetime ( $query_var );
2017-05-09 18:37:45 +00:00
$precision = 'day' ;
}
2017-05-10 20:08:47 +00:00
} catch ( Exception $e ) {
return $wp_query_args ;
2017-05-09 18:37:45 +00:00
}
// Check for valid inputs.
2017-05-10 18:49:11 +00:00
if ( ! $operator || empty ( $dates ) || ( '...' === $operator && count ( $dates ) < 2 ) ) {
2017-05-09 18:37:45 +00:00
return $wp_query_args ;
}
// Build date query for 'post_date' or 'post_modified' keys.
2017-05-10 18:49:11 +00:00
if ( 'post_date' === $key || 'post_modified' === $key ) {
2017-05-09 18:37:45 +00:00
if ( ! isset ( $wp_query_args [ 'date_query' ] ) ) {
$wp_query_args [ 'date_query' ] = array ();
}
$query_arg = array (
2017-05-10 21:33:29 +00:00
'column' => 'day' === $precision ? $key : $key . '_gmt' ,
2017-05-09 18:37:45 +00:00
'inclusive' => '>' !== $operator && '<' !== $operator ,
);
2017-05-10 19:02:15 +00:00
// Add 'before'/'after' query args.
$comparisons = array ();
if ( '>' === $operator || '>=' === $operator || '...' === $operator ) {
$comparisons [] = 'after' ;
}
if ( '<' === $operator || '<=' === $operator || '...' === $operator ) {
$comparisons [] = 'before' ;
}
foreach ( $comparisons as $index => $comparison ) {
2017-05-12 22:25:15 +00:00
if ( 'day' === $precision ) {
2018-03-07 19:16:01 +00:00
/**
* WordPress doesn 't generate the correct SQL for inclusive day queries with both a ' before ' and
* 'after' string query , so we have to use the array format in 'day' precision .
*
* @ see https :// core . trac . wordpress . org / ticket / 29908
*/
2017-05-12 22:25:15 +00:00
$query_arg [ $comparison ][ 'year' ] = $dates [ $index ] -> date ( 'Y' );
$query_arg [ $comparison ][ 'month' ] = $dates [ $index ] -> date ( 'n' );
$query_arg [ $comparison ][ 'day' ] = $dates [ $index ] -> date ( 'j' );
} else {
2018-02-22 18:30:49 +00:00
/**
* WordPress doesn 't support ' hour '/' second '/' minute ' in array format ' before '/' after ' queries ,
* so we have to use a string query .
*/
2017-05-12 22:25:15 +00:00
$query_arg [ $comparison ] = gmdate ( 'm/d/Y H:i:s' , $dates [ $index ] -> getTimestamp () );
2017-05-09 18:37:45 +00:00
}
2017-05-10 19:02:15 +00:00
}
if ( empty ( $comparisons ) ) {
2017-05-10 21:33:29 +00:00
$query_arg [ 'year' ] = $dates [ 0 ] -> date ( 'Y' );
2017-05-10 18:49:11 +00:00
$query_arg [ 'month' ] = $dates [ 0 ] -> date ( 'n' );
2017-05-10 21:33:29 +00:00
$query_arg [ 'day' ] = $dates [ 0 ] -> date ( 'j' );
2017-05-09 18:37:45 +00:00
if ( 'second' === $precision ) {
2017-05-12 22:25:15 +00:00
$query_arg [ 'hour' ] = $dates [ 0 ] -> date ( 'H' );
2017-05-10 18:49:11 +00:00
$query_arg [ 'minute' ] = $dates [ 0 ] -> date ( 'i' );
$query_arg [ 'second' ] = $dates [ 0 ] -> date ( 's' );
2017-05-09 18:37:45 +00:00
}
}
$wp_query_args [ 'date_query' ][] = $query_arg ;
return $wp_query_args ;
}
// Build meta query for unrecognized keys.
if ( ! isset ( $wp_query_args [ 'meta_query' ] ) ) {
2019-02-19 13:23:09 +00:00
$wp_query_args [ 'meta_query' ] = array (); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
2017-05-09 18:37:45 +00:00
}
2017-05-09 21:49:45 +00:00
// Meta dates are stored as timestamps in the db.
2017-07-10 05:56:28 +00:00
// Check against beginning/end-of-day timestamps when using 'day' precision.
2017-05-09 21:49:45 +00:00
if ( 'day' === $precision ) {
2017-05-10 20:08:47 +00:00
$start_timestamp = strtotime ( gmdate ( 'm/d/Y 00:00:00' , $dates [ 0 ] -> getTimestamp () ) );
2018-02-22 18:30:49 +00:00
$end_timestamp = '...' !== $operator ? ( $start_timestamp + DAY_IN_SECONDS ) : strtotime ( gmdate ( 'm/d/Y 00:00:00' , $dates [ 1 ] -> getTimestamp () ) );
2017-05-09 21:49:45 +00:00
switch ( $operator ) {
case '>' :
case '<=' :
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $end_timestamp ,
2017-05-09 21:49:45 +00:00
'compare' => $operator ,
);
2018-02-22 18:30:49 +00:00
break ;
2017-05-09 21:49:45 +00:00
case '<' :
case '>=' :
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $start_timestamp ,
2017-05-09 21:49:45 +00:00
'compare' => $operator ,
);
2018-02-22 18:30:49 +00:00
break ;
2017-05-09 21:49:45 +00:00
default :
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $start_timestamp ,
2017-05-09 21:49:45 +00:00
'compare' => '>=' ,
);
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $end_timestamp ,
2017-05-09 21:49:45 +00:00
'compare' => '<=' ,
);
}
2017-04-28 21:01:06 +00:00
} else {
2017-05-09 21:49:45 +00:00
if ( '...' !== $operator ) {
$wp_query_args [ 'meta_query' ][] = array (
'key' => $key ,
2017-05-10 18:49:11 +00:00
'value' => $dates [ 0 ] -> getTimestamp (),
2017-05-09 21:49:45 +00:00
'compare' => $operator ,
);
2017-04-28 21:01:06 +00:00
} else {
2017-05-09 21:49:45 +00:00
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $dates [ 0 ] -> getTimestamp (),
2017-05-09 21:49:45 +00:00
'compare' => '>=' ,
);
$wp_query_args [ 'meta_query' ][] = array (
2017-05-10 21:33:29 +00:00
'key' => $key ,
'value' => $dates [ 1 ] -> getTimestamp (),
2017-05-09 21:49:45 +00:00
'compare' => '<=' ,
);
2017-04-28 21:01:06 +00:00
}
}
2017-05-09 21:49:45 +00:00
return $wp_query_args ;
2017-04-28 21:01:06 +00:00
}
2017-06-06 17:10:24 +00:00
/**
* Return list of internal meta keys .
*
2017-06-07 14:20:09 +00:00
* @ since 3.2 . 0
2017-06-06 17:10:24 +00:00
* @ return array
*/
public function get_internal_meta_keys () {
return $this -> internal_meta_keys ;
}
2018-02-22 18:22:55 +00:00
/**
* Check if the terms are suitable for searching .
*
* Uses an array of stopwords ( terms ) that are excluded from the separate
* term matching when searching for posts . The list of English stopwords is
* the approximate search engines list , and is translatable .
*
* @ since 3.4 . 0
* @ param array $terms Terms to check .
* @ return array Terms that are not stopwords .
*/
protected function get_valid_search_terms ( $terms ) {
$valid_terms = array ();
$stopwords = $this -> get_search_stopwords ();
foreach ( $terms as $term ) {
// keep before/after spaces when term is for exact match, otherwise trim quotes and spaces.
if ( preg_match ( '/^".+"$/' , $term ) ) {
$term = trim ( $term , " \" ' " );
} else {
$term = trim ( $term , " \" ' " );
}
// Avoid single A-Z and single dashes.
if ( empty ( $term ) || ( 1 === strlen ( $term ) && preg_match ( '/^[a-z\-]$/i' , $term ) ) ) {
continue ;
}
if ( in_array ( wc_strtolower ( $term ), $stopwords , true ) ) {
continue ;
}
$valid_terms [] = $term ;
}
return $valid_terms ;
}
/**
* Retrieve stopwords used when parsing search terms .
*
* @ since 3.4 . 0
* @ return array Stopwords .
*/
protected function get_search_stopwords () {
// Translators: This is a comma-separated list of very common words that should be excluded from a search, like a, an, and the. These are usually called "stopwords". You should not simply translate these individual words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2018-08-22 23:59:39 +00:00
$stopwords = array_map (
2018-11-23 14:57:51 +00:00
'wc_strtolower' ,
array_map (
'trim' ,
explode (
',' ,
_x (
2018-08-22 23:59:39 +00:00
'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www' ,
'Comma-separated list of search stopwords in your language' ,
'woocommerce'
)
)
2018-02-22 18:22:55 +00:00
)
2018-08-22 23:59:39 +00:00
);
2018-02-22 18:22:55 +00:00
2018-02-27 19:22:15 +00:00
return apply_filters ( 'wp_search_stopwords' , $stopwords );
2018-02-22 18:22:55 +00:00
}
2019-02-12 13:22:51 +00:00
2019-03-07 15:08:22 +00:00
/**
* Get data to save to a lookup table .
*
* @ since 3.6 . 0
* @ param int $id ID of object to update .
* @ param string $table Lookup table name .
* @ return array
*/
2019-05-15 12:20:27 +00:00
protected function get_data_for_lookup_table ( $id , $table ) {
2019-03-07 15:08:22 +00:00
return array ();
}
/**
* Get primary key name for lookup table .
*
* @ since 3.6 . 0
* @ param string $table Lookup table name .
* @ return string
*/
protected function get_primary_key_for_lookup_table ( $table ) {
return '' ;
}
2019-02-12 13:22:51 +00:00
/**
* Update a lookup table for an object .
*
* @ since 3.6 . 0
* @ param int $id ID of object to update .
* @ param string $table Lookup table name .
2019-05-02 17:29:28 +00:00
*
* @ return NULL
2019-02-12 13:22:51 +00:00
*/
2019-05-15 12:20:27 +00:00
protected function update_lookup_table ( $id , $table ) {
2019-02-12 13:22:51 +00:00
global $wpdb ;
$id = absint ( $id );
$table = sanitize_key ( $table );
if ( empty ( $id ) || empty ( $table ) ) {
return false ;
}
$existing_data = wp_cache_get ( 'lookup_table' , 'object_' . $id );
2019-05-15 12:20:27 +00:00
$update_data = $this -> get_data_for_lookup_table ( $id , $table );
2019-02-12 13:22:51 +00:00
if ( ! empty ( $update_data ) && $update_data !== $existing_data ) {
$wpdb -> replace (
2019-02-12 14:04:22 +00:00
$wpdb -> $table ,
2019-02-12 13:22:51 +00:00
$update_data
);
wp_cache_set ( 'lookup_table' , $update_data , 'object_' . $id );
}
}
2019-03-07 15:08:22 +00:00
/**
* Delete lookup table data for an ID .
*
* @ since 3.6 . 0
* @ param int $id ID of object to update .
* @ param string $table Lookup table name .
*/
public function delete_from_lookup_table ( $id , $table ) {
global $wpdb ;
$id = absint ( $id );
$table = sanitize_key ( $table );
if ( empty ( $id ) || empty ( $table ) ) {
return false ;
}
$pk = $this -> get_primary_key_for_lookup_table ( $table );
$wpdb -> delete (
$wpdb -> $table ,
array (
$pk => $id ,
)
);
wp_cache_delete ( 'lookup_table' , 'object_' . $id );
}
2020-10-22 07:21:15 +00:00
/**
* Converts a WP post date string into a timestamp .
*
2020-11-04 12:09:52 +00:00
* @ since 4.8 . 0
2020-10-22 07:21:15 +00:00
*
* @ param string $time_string The WP post date string .
* @ return int | null The date string converted to a timestamp or null .
*/
protected function string_to_timestamp ( $time_string ) {
return '0000-00-00 00:00:00' !== $time_string ? wc_string_to_timestamp ( $time_string ) : null ;
}
2016-11-21 23:48:49 +00:00
}