From 7d10dd7b0fde2a782395887c2d66439481440f9b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 4 Feb 2025 08:59:04 +0000 Subject: [PATCH] Editor: Fix `parents` argument validation for Query block. Allow passing zero (`0`) via the `parents` argument. It is a valid value for hierarchical post types, often used to display top-level items. Props mamaduka, audrasjb, peterwilsoncc. Fixes #62901. git-svn-id: https://develop.svn.wordpress.org/trunk@59761 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 2 +- tests/phpunit/tests/blocks/wpBlock.php | 33 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 25a4a7e3b29a1..c09321edfa5bd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2638,7 +2638,7 @@ static function ( $format ) { $query['s'] = $block->context['query']['search']; } if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { - $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) ); + $query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) ); } } diff --git a/tests/phpunit/tests/blocks/wpBlock.php b/tests/phpunit/tests/blocks/wpBlock.php index 8eff0e66d013a..bfde02b4dc44c 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -714,6 +714,39 @@ public function test_build_query_vars_from_query_block_page_with_offset() { ); } + /** + * @ticket 62901 + */ + public function test_build_query_vars_from_query_block_with_top_level_parent() { + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'postType' => 'page', + 'parents' => array( 0 ), + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query = build_query_vars_from_query_block( $block, 1 ); + + $this->assertSame( + array( + 'post_type' => 'page', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'tax_query' => array(), + 'post_parent__in' => array( 0 ), + ), + $query + ); + } + /** * @ticket 56467 */