Implemented PHPCS Fixes in OrdersTableQuery.php and ProductQuery.php (#51281)
* Implemented PHPCS Fixes in OrdersTableQuery.php and ProductQuery.php * Reverted gmdate() to date() * Add changelog * Fixed linting issue in Update OrdersTableQuery.php --------- Co-authored-by: Alex Florisca <alex.florisca@automattic.com>
This commit is contained in:
parent
ae20724210
commit
63d93d7355
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: tweak
|
||||||
|
|
||||||
|
Fix PHPCS warnings in OrdersTableQuery.php and ProductQuery.php
|
|
@ -336,11 +336,11 @@ class OrdersTableQuery {
|
||||||
* Generates a `WP_Date_Query` compatible query from a given date.
|
* Generates a `WP_Date_Query` compatible query from a given date.
|
||||||
* YYYY-MM-DD queries have 'day' precision for backwards compatibility.
|
* YYYY-MM-DD queries have 'day' precision for backwards compatibility.
|
||||||
*
|
*
|
||||||
* @param mixed $date The date. Can be a {@see \WC_DateTime}, a timestamp or a string.
|
* @param mixed $date The date. Can be a {@see \WC_DateTime}, a timestamp or a string.
|
||||||
* @return array An array with keys 'year', 'month', 'day' and possibly 'hour', 'minute' and 'second'.
|
* @return array An array with keys 'year', 'month', 'day' and possibly 'hour', 'minute' and 'second'.
|
||||||
*/
|
*/
|
||||||
private function date_to_date_query_arg( $date ): array {
|
private function date_to_date_query_arg( $date ): array {
|
||||||
$result = array(
|
$result = array(
|
||||||
'year' => '',
|
'year' => '',
|
||||||
'month' => '',
|
'month' => '',
|
||||||
'day' => '',
|
'day' => '',
|
||||||
|
@ -373,10 +373,12 @@ class OrdersTableQuery {
|
||||||
/**
|
/**
|
||||||
* Returns UTC-based date query arguments for a combination of local time dates and a date shorthand operator.
|
* Returns UTC-based date query arguments for a combination of local time dates and a date shorthand operator.
|
||||||
*
|
*
|
||||||
* @param array $dates_raw Array of dates (in local time) to use in combination with the operator.
|
* @param array $dates_raw Array of dates (in local time) to use in combination with the operator.
|
||||||
* @param string $operator One of the operators supported by date queries (<, <=, =, ..., >, >=).
|
* @param string $operator One of the operators supported by date queries (<, <=, =, ..., >, >=).
|
||||||
* @return array Partial date query arg with relevant dates now UTC-based.
|
* @return array Partial date query arg with relevant dates now UTC-based.
|
||||||
*
|
*
|
||||||
|
* @throws \Exception If an invalid date shorthand operator is specified.
|
||||||
|
*
|
||||||
* @since 8.2.0
|
* @since 8.2.0
|
||||||
*/
|
*/
|
||||||
private function local_time_to_gmt_date_query( $dates_raw, $operator ) {
|
private function local_time_to_gmt_date_query( $dates_raw, $operator ) {
|
||||||
|
@ -387,7 +389,7 @@ class OrdersTableQuery {
|
||||||
$raw_date = is_numeric( $raw_date ) ? $raw_date : strtotime( get_gmt_from_date( date( 'Y-m-d', strtotime( $raw_date ) ) ) );
|
$raw_date = is_numeric( $raw_date ) ? $raw_date : strtotime( get_gmt_from_date( date( 'Y-m-d', strtotime( $raw_date ) ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$date1 = end( $dates_raw );
|
$date1 = end( $dates_raw );
|
||||||
|
|
||||||
switch ( $operator ) {
|
switch ( $operator ) {
|
||||||
case '>':
|
case '>':
|
||||||
|
@ -410,9 +412,9 @@ class OrdersTableQuery {
|
||||||
'inclusive' => true,
|
'inclusive' => true,
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'before' => $this->date_to_date_query_arg( $date1 + DAY_IN_SECONDS ),
|
'before' => $this->date_to_date_query_arg( $date1 + DAY_IN_SECONDS ),
|
||||||
'inclusive' => false,
|
'inclusive' => false,
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case '<=':
|
case '<=':
|
||||||
|
@ -474,7 +476,6 @@ class OrdersTableQuery {
|
||||||
foreach ( $date_keys as $date_key ) {
|
foreach ( $date_keys as $date_key ) {
|
||||||
$is_local = in_array( $date_key, $local_date_keys, true );
|
$is_local = in_array( $date_key, $local_date_keys, true );
|
||||||
$date_value = $this->args[ $date_key ];
|
$date_value = $this->args[ $date_key ];
|
||||||
|
|
||||||
$operator = '=';
|
$operator = '=';
|
||||||
$dates_raw = array();
|
$dates_raw = array();
|
||||||
$dates = array();
|
$dates = array();
|
||||||
|
|
|
@ -16,7 +16,7 @@ class ProductQuery {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function prepare_objects_query( $request ) {
|
public function prepare_objects_query( $request ) {
|
||||||
$args = [
|
$args = array(
|
||||||
'offset' => $request['offset'],
|
'offset' => $request['offset'],
|
||||||
'order' => $request['order'],
|
'order' => $request['order'],
|
||||||
'orderby' => $request['orderby'],
|
'orderby' => $request['orderby'],
|
||||||
|
@ -31,17 +31,17 @@ class ProductQuery {
|
||||||
'fields' => 'ids',
|
'fields' => 'ids',
|
||||||
'ignore_sticky_posts' => true,
|
'ignore_sticky_posts' => true,
|
||||||
'post_status' => 'publish',
|
'post_status' => 'publish',
|
||||||
'date_query' => [],
|
'date_query' => array(),
|
||||||
'post_type' => 'product',
|
'post_type' => 'product',
|
||||||
];
|
);
|
||||||
|
|
||||||
// If searching for a specific SKU or slug, allow any post type.
|
// If searching for a specific SKU or slug, allow any post type.
|
||||||
if ( ! empty( $request['sku'] ) || ! empty( $request['slug'] ) ) {
|
if ( ! empty( $request['sku'] ) || ! empty( $request['slug'] ) ) {
|
||||||
$args['post_type'] = [ 'product', 'product_variation' ];
|
$args['post_type'] = array( 'product', 'product_variation' );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Taxonomy query to filter products by type, category, tag, shipping class, and attribute.
|
// Taxonomy query to filter products by type, category, tag, shipping class, and attribute.
|
||||||
$tax_query = [];
|
$tax_query = array();
|
||||||
|
|
||||||
// Filter product type by slug.
|
// Filter product type by slug.
|
||||||
if ( ! empty( $request['type'] ) ) {
|
if ( ! empty( $request['type'] ) ) {
|
||||||
|
@ -49,11 +49,11 @@ class ProductQuery {
|
||||||
$args['post_type'] = 'product_variation';
|
$args['post_type'] = 'product_variation';
|
||||||
} else {
|
} else {
|
||||||
$args['post_type'] = 'product';
|
$args['post_type'] = 'product';
|
||||||
$tax_query[] = [
|
$tax_query[] = array(
|
||||||
'taxonomy' => 'product_type',
|
'taxonomy' => 'product_type',
|
||||||
'field' => 'slug',
|
'field' => 'slug',
|
||||||
'terms' => $request['type'],
|
'terms' => $request['type'],
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,12 +77,12 @@ class ProductQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set custom args to handle later during clauses.
|
// Set custom args to handle later during clauses.
|
||||||
$custom_keys = [
|
$custom_keys = array(
|
||||||
'sku',
|
'sku',
|
||||||
'min_price',
|
'min_price',
|
||||||
'max_price',
|
'max_price',
|
||||||
'stock_status',
|
'stock_status',
|
||||||
];
|
);
|
||||||
|
|
||||||
foreach ( $custom_keys as $key ) {
|
foreach ( $custom_keys as $key ) {
|
||||||
if ( ! empty( $request[ $key ] ) ) {
|
if ( ! empty( $request[ $key ] ) ) {
|
||||||
|
@ -90,11 +90,11 @@ class ProductQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$operator_mapping = [
|
$operator_mapping = array(
|
||||||
'in' => 'IN',
|
'in' => 'IN',
|
||||||
'not_in' => 'NOT IN',
|
'not_in' => 'NOT IN',
|
||||||
'and' => 'AND',
|
'and' => 'AND',
|
||||||
];
|
);
|
||||||
|
|
||||||
// Gets all registered product taxonomies and prefixes them with `tax_`.
|
// Gets all registered product taxonomies and prefixes them with `tax_`.
|
||||||
// This is needed to avoid situations where a user registers a new product taxonomy with the same name as default field.
|
// This is needed to avoid situations where a user registers a new product taxonomy with the same name as default field.
|
||||||
|
@ -107,10 +107,10 @@ class ProductQuery {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Map between taxonomy name and arg key.
|
// Map between taxonomy name and arg key.
|
||||||
$default_taxonomies = [
|
$default_taxonomies = array(
|
||||||
'product_cat' => 'category',
|
'product_cat' => 'category',
|
||||||
'product_tag' => 'tag',
|
'product_tag' => 'tag',
|
||||||
];
|
);
|
||||||
|
|
||||||
$taxonomies = array_merge( $all_product_taxonomies, $default_taxonomies );
|
$taxonomies = array_merge( $all_product_taxonomies, $default_taxonomies );
|
||||||
|
|
||||||
|
@ -118,18 +118,18 @@ class ProductQuery {
|
||||||
foreach ( $taxonomies as $taxonomy => $key ) {
|
foreach ( $taxonomies as $taxonomy => $key ) {
|
||||||
if ( ! empty( $request[ $key ] ) ) {
|
if ( ! empty( $request[ $key ] ) ) {
|
||||||
$operator = $request->get_param( $key . '_operator' ) && isset( $operator_mapping[ $request->get_param( $key . '_operator' ) ] ) ? $operator_mapping[ $request->get_param( $key . '_operator' ) ] : 'IN';
|
$operator = $request->get_param( $key . '_operator' ) && isset( $operator_mapping[ $request->get_param( $key . '_operator' ) ] ) ? $operator_mapping[ $request->get_param( $key . '_operator' ) ] : 'IN';
|
||||||
$tax_query[] = [
|
$tax_query[] = array(
|
||||||
'taxonomy' => $taxonomy,
|
'taxonomy' => $taxonomy,
|
||||||
'field' => 'term_id',
|
'field' => 'term_id',
|
||||||
'terms' => $request[ $key ],
|
'terms' => $request[ $key ],
|
||||||
'operator' => $operator,
|
'operator' => $operator,
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by attributes.
|
// Filter by attributes.
|
||||||
if ( ! empty( $request['attributes'] ) ) {
|
if ( ! empty( $request['attributes'] ) ) {
|
||||||
$att_queries = [];
|
$att_queries = array();
|
||||||
|
|
||||||
foreach ( $request['attributes'] as $attribute ) {
|
foreach ( $request['attributes'] as $attribute ) {
|
||||||
if ( empty( $attribute['term_id'] ) && empty( $attribute['slug'] ) ) {
|
if ( empty( $attribute['term_id'] ) && empty( $attribute['slug'] ) ) {
|
||||||
|
@ -137,22 +137,22 @@ class ProductQuery {
|
||||||
}
|
}
|
||||||
if ( in_array( $attribute['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
|
if ( in_array( $attribute['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
|
||||||
$operator = isset( $attribute['operator'], $operator_mapping[ $attribute['operator'] ] ) ? $operator_mapping[ $attribute['operator'] ] : 'IN';
|
$operator = isset( $attribute['operator'], $operator_mapping[ $attribute['operator'] ] ) ? $operator_mapping[ $attribute['operator'] ] : 'IN';
|
||||||
$att_queries[] = [
|
$att_queries[] = array(
|
||||||
'taxonomy' => $attribute['attribute'],
|
'taxonomy' => $attribute['attribute'],
|
||||||
'field' => ! empty( $attribute['term_id'] ) ? 'term_id' : 'slug',
|
'field' => ! empty( $attribute['term_id'] ) ? 'term_id' : 'slug',
|
||||||
'terms' => ! empty( $attribute['term_id'] ) ? $attribute['term_id'] : $attribute['slug'],
|
'terms' => ! empty( $attribute['term_id'] ) ? $attribute['term_id'] : $attribute['slug'],
|
||||||
'operator' => $operator,
|
'operator' => $operator,
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 1 < count( $att_queries ) ) {
|
if ( 1 < count( $att_queries ) ) {
|
||||||
// Add relation arg when using multiple attributes.
|
// Add relation arg when using multiple attributes.
|
||||||
$relation = $request->get_param( 'attribute_relation' ) && isset( $operator_mapping[ $request->get_param( 'attribute_relation' ) ] ) ? $operator_mapping[ $request->get_param( 'attribute_relation' ) ] : 'IN';
|
$relation = $request->get_param( 'attribute_relation' ) && isset( $operator_mapping[ $request->get_param( 'attribute_relation' ) ] ) ? $operator_mapping[ $request->get_param( 'attribute_relation' ) ] : 'IN';
|
||||||
$tax_query[] = [
|
$tax_query[] = array(
|
||||||
'relation' => $relation,
|
'relation' => $relation,
|
||||||
$att_queries,
|
$att_queries,
|
||||||
];
|
);
|
||||||
} else {
|
} else {
|
||||||
$tax_query = array_merge( $tax_query, $att_queries );
|
$tax_query = array_merge( $tax_query, $att_queries );
|
||||||
}
|
}
|
||||||
|
@ -176,12 +176,12 @@ class ProductQuery {
|
||||||
|
|
||||||
// Filter featured.
|
// Filter featured.
|
||||||
if ( is_bool( $request['featured'] ) ) {
|
if ( is_bool( $request['featured'] ) ) {
|
||||||
$args['tax_query'][] = [
|
$args['tax_query'][] = array(
|
||||||
'taxonomy' => 'product_visibility',
|
'taxonomy' => 'product_visibility',
|
||||||
'field' => 'name',
|
'field' => 'name',
|
||||||
'terms' => 'featured',
|
'terms' => 'featured',
|
||||||
'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
|
'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by on sale products.
|
// Filter by on sale products.
|
||||||
|
@ -190,7 +190,7 @@ class ProductQuery {
|
||||||
$on_sale_ids = wc_get_product_ids_on_sale();
|
$on_sale_ids = wc_get_product_ids_on_sale();
|
||||||
|
|
||||||
// Use 0 when there's no on sale products to avoid return all products.
|
// Use 0 when there's no on sale products to avoid return all products.
|
||||||
$on_sale_ids = empty( $on_sale_ids ) ? [ 0 ] : $on_sale_ids;
|
$on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids;
|
||||||
|
|
||||||
$args[ $on_sale_key ] += $on_sale_ids;
|
$args[ $on_sale_key ] += $on_sale_ids;
|
||||||
}
|
}
|
||||||
|
@ -203,25 +203,25 @@ class ProductQuery {
|
||||||
$exclude_from_catalog = 'search' === $catalog_visibility ? '' : 'exclude-from-catalog';
|
$exclude_from_catalog = 'search' === $catalog_visibility ? '' : 'exclude-from-catalog';
|
||||||
$exclude_from_search = 'catalog' === $catalog_visibility ? '' : 'exclude-from-search';
|
$exclude_from_search = 'catalog' === $catalog_visibility ? '' : 'exclude-from-search';
|
||||||
|
|
||||||
$args['tax_query'][] = [
|
$args['tax_query'][] = array(
|
||||||
'taxonomy' => 'product_visibility',
|
'taxonomy' => 'product_visibility',
|
||||||
'field' => 'name',
|
'field' => 'name',
|
||||||
'terms' => [ $exclude_from_catalog, $exclude_from_search ],
|
'terms' => array( $exclude_from_catalog, $exclude_from_search ),
|
||||||
'operator' => 'hidden' === $catalog_visibility ? 'AND' : 'NOT IN',
|
'operator' => 'hidden' === $catalog_visibility ? 'AND' : 'NOT IN',
|
||||||
'rating_filter' => true,
|
'rating_filter' => true,
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $rating ) {
|
if ( $rating ) {
|
||||||
$rating_terms = [];
|
$rating_terms = array();
|
||||||
foreach ( $rating as $value ) {
|
foreach ( $rating as $value ) {
|
||||||
$rating_terms[] = 'rated-' . $value;
|
$rating_terms[] = 'rated-' . $value;
|
||||||
}
|
}
|
||||||
$args['tax_query'][] = [
|
$args['tax_query'][] = array(
|
||||||
'taxonomy' => 'product_visibility',
|
'taxonomy' => 'product_visibility',
|
||||||
'field' => 'name',
|
'field' => 'name',
|
||||||
'terms' => $rating_terms,
|
'terms' => $rating_terms,
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$orderby = $request->get_param( 'orderby' );
|
$orderby = $request->get_param( 'orderby' );
|
||||||
|
@ -283,7 +283,7 @@ class ProductQuery {
|
||||||
public function get_results( $request ) {
|
public function get_results( $request ) {
|
||||||
$query_args = $this->prepare_objects_query( $request );
|
$query_args = $this->prepare_objects_query( $request );
|
||||||
|
|
||||||
add_filter( 'posts_clauses', [ $this, 'add_query_clauses' ], 10, 2 );
|
add_filter( 'posts_clauses', array( $this, 'add_query_clauses' ), 10, 2 );
|
||||||
|
|
||||||
$query = new \WP_Query();
|
$query = new \WP_Query();
|
||||||
$results = $query->query( $query_args );
|
$results = $query->query( $query_args );
|
||||||
|
@ -297,13 +297,13 @@ class ProductQuery {
|
||||||
$total_posts = $count_query->found_posts;
|
$total_posts = $count_query->found_posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
remove_filter( 'posts_clauses', [ $this, 'add_query_clauses' ], 10 );
|
remove_filter( 'posts_clauses', array( $this, 'add_query_clauses' ), 10 );
|
||||||
|
|
||||||
return [
|
return array(
|
||||||
'results' => $results,
|
'results' => $results,
|
||||||
'total' => (int) $total_posts,
|
'total' => (int) $total_posts,
|
||||||
'pages' => $query->query_vars['posts_per_page'] > 0 ? (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ) : 1,
|
'pages' => $query->query_vars['posts_per_page'] > 0 ? (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ) : 1,
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -315,11 +315,11 @@ class ProductQuery {
|
||||||
public function get_objects( $request ) {
|
public function get_objects( $request ) {
|
||||||
$results = $this->get_results( $request );
|
$results = $this->get_results( $request );
|
||||||
|
|
||||||
return [
|
return array(
|
||||||
'objects' => array_map( 'wc_get_product', $results['results'] ),
|
'objects' => array_map( 'wc_get_product', $results['results'] ),
|
||||||
'total' => $results['total'],
|
'total' => $results['total'],
|
||||||
'pages' => $results['pages'],
|
'pages' => $results['pages'],
|
||||||
];
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -442,7 +442,7 @@ class ProductQuery {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$or_queries = [];
|
$or_queries = array();
|
||||||
|
|
||||||
// We need to adjust the filter for each possible tax class and combine the queries into one.
|
// We need to adjust the filter for each possible tax class and combine the queries into one.
|
||||||
foreach ( $product_tax_classes as $tax_class ) {
|
foreach ( $product_tax_classes as $tax_class ) {
|
||||||
|
|
Loading…
Reference in New Issue