Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
simplify merge logic to unpack/destructure arrays
Browse files Browse the repository at this point in the history
Instead of checking if array contains built-in query variables, we only
check if the array contains string keys as we can control the inputs of
merge_queries.
  • Loading branch information
dinhtungdu committed Nov 28, 2022
1 parent a9ffac4 commit e6df617
Showing 1 changed file with 7 additions and 43 deletions.
50 changes: 7 additions & 43 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ class ProductQuery extends AbstractBlock {
*/
protected $attributes_filter_query_args = array();

/**
* All query args from WP_Query.
*
* @var array
*/
protected $valid_query_vars;

/**
* Initialize this block type.
*
Expand Down Expand Up @@ -187,8 +180,8 @@ function( $acc, $query ) {
if ( ! is_array( $query ) ) {
return $acc;
}
// If the $query doesn't contain any valid query keys, we unpack/destructure it then merge.
if ( empty( array_intersect( $this->get_valid_query_vars(), array_keys( $query ) ) ) ) {
// If the $query doesn't contain any keys, we unpack/destructure it then merge.
if ( ! $this->array_has_string_keys( $query ) ) {
return $this->merge_queries( $acc, ...array_values( $query ) );
}
return $this->array_merge_recursive_replace_non_array_properties( $acc, $query );
Expand Down Expand Up @@ -517,42 +510,13 @@ function( $stock_status ) {
}

/**
* Return or initialize $valid_query_vars.
* Check if array has string key.
*
* @return array
* @param array $array Input array.
* @return bool
*/
private function get_valid_query_vars() {
if ( ! empty( $this->valid_query_vars ) ) {
return $this->valid_query_vars;
}

$valid_query_vars = array_keys( ( new WP_Query() )->fill_query_vars( array() ) );
$this->valid_query_vars = array_merge(
$valid_query_vars,
// fill_query_vars doesn't include these vars so we need to add them manually.
array(
'date_query',
'exact',
'ignore_sticky_posts',
'lazy_load_term_meta',
'meta_compare_key',
'meta_compare',
'meta_query',
'meta_type_key',
'meta_type',
'nopaging',
'offset',
'order',
'orderby',
'page',
'post_type',
'posts_per_page',
'suppress_filters',
'tax_query',
)
);

return $this->valid_query_vars;
private function array_has_string_keys( $array ) {
return count( array_filter( array_keys( $array ), 'is_string' ) ) > 0;
}

/**
Expand Down

0 comments on commit e6df617

Please sign in to comment.