Make WordPress Core

Changeset 59761


Ignore:
Timestamp:
02/04/2025 08:59:04 AM (12 days ago)
Author:
Mamaduka
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r59543 r59761  
    26392639        }
    26402640        if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
    2641             $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) );
     2641            $query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) );
    26422642        }
    26432643    }
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r59116 r59761  
    716716
    717717    /**
     718     * @ticket 62901
     719     */
     720    public function test_build_query_vars_from_query_block_with_top_level_parent() {
     721        $this->registry->register(
     722            'core/example',
     723            array( 'uses_context' => array( 'query' ) )
     724        );
     725
     726        $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     727        $parsed_block  = $parsed_blocks[0];
     728        $context       = array(
     729            'query' => array(
     730                'postType' => 'page',
     731                'parents'  => array( 0 ),
     732            ),
     733        );
     734        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     735        $query         = build_query_vars_from_query_block( $block, 1 );
     736
     737        $this->assertSame(
     738            array(
     739                'post_type'       => 'page',
     740                'order'           => 'DESC',
     741                'orderby'         => 'date',
     742                'post__not_in'    => array(),
     743                'tax_query'       => array(),
     744                'post_parent__in' => array( 0 ),
     745            ),
     746            $query
     747        );
     748    }
     749
     750    /**
    718751     * @ticket 56467
    719752     */
Note: See TracChangeset for help on using the changeset viewer.