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

Commit e6df617

Browse files
committed
simplify merge logic to unpack/destructure arrays
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.
1 parent a9ffac4 commit e6df617

File tree

1 file changed

+7
-43
lines changed

1 file changed

+7
-43
lines changed

src/BlockTypes/ProductQuery.php

+7-43
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ class ProductQuery extends AbstractBlock {
3939
*/
4040
protected $attributes_filter_query_args = array();
4141

42-
/**
43-
* All query args from WP_Query.
44-
*
45-
* @var array
46-
*/
47-
protected $valid_query_vars;
48-
4942
/**
5043
* Initialize this block type.
5144
*
@@ -187,8 +180,8 @@ function( $acc, $query ) {
187180
if ( ! is_array( $query ) ) {
188181
return $acc;
189182
}
190-
// If the $query doesn't contain any valid query keys, we unpack/destructure it then merge.
191-
if ( empty( array_intersect( $this->get_valid_query_vars(), array_keys( $query ) ) ) ) {
183+
// If the $query doesn't contain any keys, we unpack/destructure it then merge.
184+
if ( ! $this->array_has_string_keys( $query ) ) {
192185
return $this->merge_queries( $acc, ...array_values( $query ) );
193186
}
194187
return $this->array_merge_recursive_replace_non_array_properties( $acc, $query );
@@ -517,42 +510,13 @@ function( $stock_status ) {
517510
}
518511

519512
/**
520-
* Return or initialize $valid_query_vars.
513+
* Check if array has string key.
521514
*
522-
* @return array
515+
* @param array $array Input array.
516+
* @return bool
523517
*/
524-
private function get_valid_query_vars() {
525-
if ( ! empty( $this->valid_query_vars ) ) {
526-
return $this->valid_query_vars;
527-
}
528-
529-
$valid_query_vars = array_keys( ( new WP_Query() )->fill_query_vars( array() ) );
530-
$this->valid_query_vars = array_merge(
531-
$valid_query_vars,
532-
// fill_query_vars doesn't include these vars so we need to add them manually.
533-
array(
534-
'date_query',
535-
'exact',
536-
'ignore_sticky_posts',
537-
'lazy_load_term_meta',
538-
'meta_compare_key',
539-
'meta_compare',
540-
'meta_query',
541-
'meta_type_key',
542-
'meta_type',
543-
'nopaging',
544-
'offset',
545-
'order',
546-
'orderby',
547-
'page',
548-
'post_type',
549-
'posts_per_page',
550-
'suppress_filters',
551-
'tax_query',
552-
)
553-
);
554-
555-
return $this->valid_query_vars;
518+
private function array_has_string_keys( $array ) {
519+
return count( array_filter( array_keys( $array ), 'is_string' ) ) > 0;
556520
}
557521

558522
/**

0 commit comments

Comments
 (0)