Skip to content

Commit 27b130e

Browse files
committed
add the code and tests
1 parent 26ce245 commit 27b130e

File tree

2 files changed

+145
-9
lines changed

2 files changed

+145
-9
lines changed

src/wp-includes/blocks.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,18 +2714,58 @@ function build_query_vars_from_query_block( $block, $page ) {
27142714
}
27152715
$query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
27162716
}
2717-
if ( ! empty( $block->context['query']['taxQuery'] ) ) {
2717+
$tax_query_input = $block->context['query']['taxQuery'];
2718+
if ( ! empty( $tax_query_input ) && is_array( $tax_query_input ) ) {
27182719
$tax_query = array();
2719-
foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
2720-
if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
2721-
$tax_query[] = array(
2722-
'taxonomy' => $taxonomy,
2723-
'terms' => array_filter( array_map( 'intval', $terms ) ),
2724-
'include_children' => false,
2725-
);
2720+
// If there are keys other than include/exclude, it's the old
2721+
// format e.g. "taxQuery":{"category":[4]}
2722+
if ( ! empty( array_diff( array_keys( $tax_query_input ), array( 'include', 'exclude' ) ) ) ) {
2723+
foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
2724+
if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
2725+
$tax_query[] = array(
2726+
'taxonomy' => $taxonomy,
2727+
'terms' => array_filter( array_map( 'intval', $terms ) ),
2728+
'include_children' => false,
2729+
);
2730+
}
27262731
}
2732+
} else {
2733+
// This is the new format e.g. "taxQuery":{"include":{"category":[4]},"exclude":{"post_tag":[5]}}
2734+
2735+
// Helper function to build tax_query conditions from taxonomy terms.
2736+
$build_conditions = static function ( $terms, $operator = 'IN' ) {
2737+
$conditions = array();
2738+
foreach ( $terms as $taxonomy => $terms ) {
2739+
if ( ! empty( $terms ) && is_taxonomy_viewable( $taxonomy ) ) {
2740+
$conditions[] = array(
2741+
'taxonomy' => $taxonomy,
2742+
'terms' => array_filter( array_map( 'intval', $terms ) ),
2743+
'operator' => $operator,
2744+
'include_children' => false,
2745+
);
2746+
}
2747+
}
2748+
return $conditions;
2749+
};
2750+
2751+
// Separate exclude from include terms.
2752+
$exclude_terms = isset( $tax_query_input['exclude'] ) && is_array( $tax_query_input['exclude'] )
2753+
? $tax_query_input['exclude']
2754+
: array();
2755+
$include_terms = isset( $tax_query_input['include'] ) && is_array( $tax_query_input['include'] )
2756+
? $tax_query_input['include']
2757+
: array();
2758+
2759+
$tax_query = array_merge(
2760+
$build_conditions( $include_terms ),
2761+
$build_conditions( $exclude_terms, 'NOT IN' )
2762+
);
2763+
}
2764+
2765+
if ( ! empty( $tax_query ) ) {
2766+
// Merge with any existing `tax_query` conditions.
2767+
$query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
27272768
}
2728-
$query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
27292769
}
27302770
if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
27312771
$formats = $block->context['query']['format'];

tests/phpunit/tests/blocks/wpBlock.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,102 @@ public function test_build_query_vars_from_query_block() {
797797
);
798798
}
799799

800+
/**
801+
* @ticket 64416
802+
*/
803+
public function test_build_query_vars_from_query_block_tax_query_old_format() {
804+
$this->registry->register(
805+
'core/example',
806+
array( 'uses_context' => array( 'query' ) )
807+
);
808+
809+
$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
810+
$parsed_block = $parsed_blocks[0];
811+
$context = array(
812+
'query' => array(
813+
'taxQuery' => array(
814+
'category' => array( 1, 2, 3 ),
815+
'post_tag' => array( 10, 20 ),
816+
),
817+
),
818+
);
819+
$block = new WP_Block( $parsed_block, $context, $this->registry );
820+
$query = build_query_vars_from_query_block( $block, 1 );
821+
822+
$this->assertSame(
823+
array(
824+
'post_type' => 'post',
825+
'order' => 'DESC',
826+
'orderby' => 'date',
827+
'post__not_in' => array(),
828+
'tax_query' => array(
829+
array(
830+
'taxonomy' => 'category',
831+
'terms' => array( 1, 2, 3 ),
832+
'include_children' => false,
833+
),
834+
array(
835+
'taxonomy' => 'post_tag',
836+
'terms' => array( 10, 20 ),
837+
'include_children' => false,
838+
),
839+
),
840+
),
841+
$query
842+
);
843+
}
844+
845+
/**
846+
* @ticket 64416
847+
*/
848+
public function test_build_query_vars_from_query_block_tax_query_include_exclude() {
849+
$this->registry->register(
850+
'core/example',
851+
array( 'uses_context' => array( 'query' ) )
852+
);
853+
854+
$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
855+
$parsed_block = $parsed_blocks[0];
856+
$context = array(
857+
'query' => array(
858+
'taxQuery' => array(
859+
'include' => array(
860+
'category' => array( 1, 2, 3 ),
861+
),
862+
'exclude' => array(
863+
'post_tag' => array( 15 ),
864+
),
865+
),
866+
),
867+
);
868+
$block = new WP_Block( $parsed_block, $context, $this->registry );
869+
$query = build_query_vars_from_query_block( $block, 1 );
870+
871+
$this->assertSame(
872+
array(
873+
'post_type' => 'post',
874+
'order' => 'DESC',
875+
'orderby' => 'date',
876+
'post__not_in' => array(),
877+
'tax_query' => array(
878+
array(
879+
'taxonomy' => 'category',
880+
'terms' => array( 1, 2, 3 ),
881+
'operator' => 'IN',
882+
'include_children' => false,
883+
),
884+
array(
885+
'taxonomy' => 'post_tag',
886+
'terms' => array( 15 ),
887+
'operator' => 'NOT IN',
888+
'include_children' => false,
889+
),
890+
),
891+
),
892+
$query
893+
);
894+
}
895+
800896
/**
801897
* @ticket 62014
802898
*/

0 commit comments

Comments
 (0)