Make WordPress Core

Changeset 54175


Ignore:
Timestamp:
09/15/2022 11:39:43 AM (21 months ago)
Author:
audrasjb
Message:

Editor: Backport build_query_vars_from_query_block changes from Gutenberg repository.

This changeset backports changes from the following Gutenberg pull requests:

Props ntsekouras, bernhard-reiter.
See #56467.

Location:
trunk
Files:
2 edited

Legend:

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

    r54155 r54175  
    11901190 *
    11911191 * @since 5.8.0
     1192 * @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
    11921193 *
    11931194 * @param WP_Block $block Block instance.
     
    12901291            $query['s'] = $block->context['query']['search'];
    12911292        }
    1292     }
    1293     return $query;
     1293        if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
     1294            $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) );
     1295        }
     1296    }
     1297
     1298    /**
     1299     * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block.
     1300     *
     1301     * Anything to this filter should be compatible with the `WP_Query` API to form
     1302     * the query context which will be passed down to the Query Loop Block's children.
     1303     * This can help, for example, to include additional settings or meta queries not
     1304     * directly supported by the core Query Loop Block, and extend its capabilities.
     1305     *
     1306     * Please note that this will only influence the query that will be rendered on the
     1307     * front-end. The editor preview is not affected by this filter. Also, worth noting
     1308     * that the editor preview uses the REST API, so, ideally, one should aim to provide
     1309     * attributes which are also compatible with the REST API, in order to be able to
     1310     * implement identical queries on both sides.
     1311     *
     1312     * @since 6.1.0
     1313     *
     1314     * @param array    $query Array containing parameters for `WP_Query` as parsed by the block context.
     1315     * @param WP_Block $block Block instance.
     1316     * @param int      $page  Current query's page.
     1317     */
     1318    return apply_filters( 'query_loop_block_query_vars', $query, $block, $page );
    12941319}
    12951320
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r53268 r54175  
    438438                'orderBy'     => 'title',
    439439                'tagIds'      => array( 3, 11, 10 ),
     440                'parents'     => array( 1, 2 ),
    440441            ),
    441442        );
     
    446447            $query,
    447448            array(
    448                 'post_type'    => 'page',
    449                 'order'        => 'DESC',
    450                 'orderby'      => 'title',
    451                 'post__not_in' => array( 1, 2 ),
    452                 'tax_query'    => array(
     449                'post_type'       => 'page',
     450                'order'           => 'DESC',
     451                'orderby'         => 'title',
     452                'post__not_in'    => array( 1, 2 ),
     453                'tax_query'       => array(
    453454                    array(
    454455                        'taxonomy'         => 'category',
     
    462463                    ),
    463464                ),
     465                'post_parent__in' => array( 1, 2 ),
    464466            )
    465467        );
     
    581583                'offset'         => 12,
    582584                'posts_per_page' => 5,
     585            )
     586        );
     587    }
     588
     589    /**
     590     * @ticket 56467
     591     */
     592    public function test_query_loop_block_query_vars_filter() {
     593        $this->registry->register(
     594            'core/example',
     595            array( 'uses_context' => array( 'query' ) )
     596        );
     597
     598        $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     599        $parsed_block  = $parsed_blocks[0];
     600        $context       = array(
     601            'query' => array(
     602                'postType' => 'page',
     603                'orderBy'  => 'title',
     604            ),
     605        );
     606        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     607        function filterQuery( $query, $block, $page ) {
     608            $query['post_type'] = 'book';
     609            return $query;
     610        }
     611        add_filter( 'query_loop_block_query_vars', 'filterQuery', 10, 3 );
     612        $query = build_query_vars_from_query_block( $block, 1 );
     613        remove_filter( 'query_loop_block_query_vars', 'filterQuery' );
     614        $this->assertSame(
     615            $query,
     616            array(
     617                'post_type'    => 'book',
     618                'order'        => 'DESC',
     619                'orderby'      => 'title',
     620                'post__not_in' => array(),
    583621            )
    584622        );
Note: See TracChangeset for help on using the changeset viewer.