Make WordPress Core


Ignore:
Timestamp:
09/30/2024 01:17:40 AM (2 years ago)
Author:
peterwilsoncc
Message:

REST API/Editor: Support post formats in Query Block & Posts API.

Introduces post format support for both the Query Block with the new parameter format. In the build_query_vars_from_query_block() function, this is converted to a post_format taxonomy query passed to WP_Query.

Also introduces the format parameter to the REST API's Posts controller to support the feature in the Query block. The parameter type is an enumerated string accepted the post formats supported by each post type.

Props poena, mukesh27, mamaduka, noisysocks, TimothyBlynJacobs.
Fixes #62014.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r58160 r59115  
    465465
    466466        /**
     467         * @ticket 62014
     468         */
     469        public function test_build_query_vars_from_query_block_standard_post_formats() {
     470                $this->registry->register(
     471                        'core/example',
     472                        array( 'uses_context' => array( 'query' ) )
     473                );
     474
     475                $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     476                $parsed_block  = $parsed_blocks[0];
     477                $context       = array(
     478                        'query' => array(
     479                                'postType' => 'post',
     480                                'format'   => array( 'standard' ),
     481                        ),
     482                );
     483                $block         = new WP_Block( $parsed_block, $context, $this->registry );
     484                $query         = build_query_vars_from_query_block( $block, 1 );
     485
     486                $this->assertSame(
     487                        array(
     488                                'post_type'    => 'post',
     489                                'order'        => 'DESC',
     490                                'orderby'      => 'date',
     491                                'post__not_in' => array(),
     492                                'tax_query'    => array(
     493                                        'relation' => 'OR',
     494                                        array(
     495                                                'taxonomy' => 'post_format',
     496                                                'field'    => 'slug',
     497                                                'operator' => 'NOT EXISTS',
     498                                        ),
     499                                ),
     500                        ),
     501                        $query
     502                );
     503        }
     504
     505        /**
     506         * @ticket 62014
     507         */
     508        public function test_build_query_vars_from_query_block_post_format() {
     509                $this->registry->register(
     510                        'core/example',
     511                        array( 'uses_context' => array( 'query' ) )
     512                );
     513
     514                $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     515                $parsed_block  = $parsed_blocks[0];
     516                $context       = array(
     517                        'query' => array(
     518                                'postType' => 'post',
     519                                'format'   => array( 'aside' ),
     520                        ),
     521                );
     522                $block         = new WP_Block( $parsed_block, $context, $this->registry );
     523                $query         = build_query_vars_from_query_block( $block, 1 );
     524
     525                $this->assertSame(
     526                        array(
     527                                'post_type'    => 'post',
     528                                'order'        => 'DESC',
     529                                'orderby'      => 'date',
     530                                'post__not_in' => array(),
     531                                'tax_query'    => array(
     532                                        'relation' => 'OR',
     533                                        array(
     534                                                'taxonomy' => 'post_format',
     535                                                'field'    => 'slug',
     536                                                'terms'    => array( 'post-format-aside' ),
     537                                                'operator' => 'IN',
     538                                        ),
     539                                ),
     540                        ),
     541                        $query
     542                );
     543        }
     544        /**
     545         * @ticket 62014
     546         */
     547        public function test_build_query_vars_from_query_block_post_formats_with_category() {
     548                $this->registry->register(
     549                        'core/example',
     550                        array( 'uses_context' => array( 'query' ) )
     551                );
     552
     553                $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     554                $parsed_block  = $parsed_blocks[0];
     555                $context       = array(
     556                        'query' => array(
     557                                'postType'    => 'post',
     558                                'format'      => array( 'standard' ),
     559                                'categoryIds' => array( 56 ),
     560                        ),
     561                );
     562                $block         = new WP_Block( $parsed_block, $context, $this->registry );
     563                $query         = build_query_vars_from_query_block( $block, 1 );
     564
     565                $this->assertSame(
     566                        array(
     567                                'post_type'    => 'post',
     568                                'order'        => 'DESC',
     569                                'orderby'      => 'date',
     570                                'post__not_in' => array(),
     571                                'tax_query'    => array(
     572                                        'relation' => 'AND',
     573                                        array(
     574                                                array(
     575                                                        'taxonomy'         => 'category',
     576                                                        'terms'            => array( 56 ),
     577                                                        'include_children' => false,
     578                                                ),
     579                                        ),
     580                                        array(
     581                                                'relation' => 'OR',
     582                                                array(
     583                                                        'taxonomy' => 'post_format',
     584                                                        'field'    => 'slug',
     585                                                        'operator' => 'NOT EXISTS',
     586                                                ),
     587                                        ),
     588                                ),
     589                        ),
     590                        $query
     591                );
     592        }
     593
     594        /**
    467595         * @ticket 52991
    468596         */
    … …  
    482610                                'orderby'      => 'date',
    483611                                'post__not_in' => array(),
     612                                'tax_query'    => array(),
    484613                        )
    485614                );
    … …  
    513642                                'orderby'        => 'date',
    514643                                'post__not_in'   => array(),
     644                                'tax_query'      => array(),
    515645                                'offset'         => 0,
    516646                                'posts_per_page' => 2,
    … …  
    545675                                'orderby'        => 'date',
    546676                                'post__not_in'   => array(),
     677                                'tax_query'      => array(),
    547678                                'offset'         => 10,
    548679                                'posts_per_page' => 5,
    … …  
    577708                                'orderby'        => 'date',
    578709                                'post__not_in'   => array(),
     710                                'tax_query'      => array(),
    579711                                'offset'         => 12,
    580712                                'posts_per_page' => 5,
    … …  
    620752                                'orderby'      => 'title',
    621753                                'post__not_in' => array(),
     754                                'tax_query'    => array(),
    622755                        )
    623756                );
Note: See TracChangeset for help on using the changeset viewer.