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/rest-api/rest-posts-controller.php

    r59036 r59115  
    197197                                'context',
    198198                                'exclude',
     199                                'format',
    199200                                'include',
    200201                                'modified_after',
    … …  
    54985499
    54995500        /**
     5501         * Test the REST API support for the standard post format.
     5502         *
     5503         * @ticket 62014
     5504         *
     5505         * @covers WP_REST_Posts_Controller::get_items
     5506         */
     5507        public function test_standard_post_format_support() {
     5508                $initial_theme_support = get_theme_support( 'post-formats' );
     5509                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
     5510
     5511                $post_id = self::factory()->post->create(
     5512                        array(
     5513                                'post_type'   => 'post',
     5514                                'post_status' => 'publish',
     5515                        )
     5516                );
     5517                set_post_format( $post_id, 'aside' );
     5518
     5519                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5520                $request->set_param( 'format', array( 'standard' ) );
     5521                $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     5522
     5523                $response = rest_get_server()->dispatch( $request );
     5524
     5525                /*
     5526                 * Restore the initial post formats support.
     5527                 *
     5528                 * This needs to be done prior to the assertions to avoid unexpected
     5529                 * results for other tests should an assertion fail.
     5530                 */
     5531                if ( $initial_theme_support ) {
     5532                        add_theme_support( 'post-formats', $initial_theme_support[0] );
     5533                } else {
     5534                        remove_theme_support( 'post-formats' );
     5535                }
     5536
     5537                $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' );
     5538        }
     5539
     5540        /**
     5541         * Test the REST API support for post formats.
     5542         *
     5543         * @ticket 62014
     5544         *
     5545         * @covers WP_REST_Posts_Controller::get_items
     5546         */
     5547        public function test_post_format_support() {
     5548                $initial_theme_support = get_theme_support( 'post-formats' );
     5549                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
     5550
     5551                $post_id = self::factory()->post->create(
     5552                        array(
     5553                                'post_type'   => 'post',
     5554                                'post_status' => 'publish',
     5555                        )
     5556                );
     5557                set_post_format( $post_id, 'aside' );
     5558
     5559                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5560                $request->set_param( 'format', array( 'aside' ) );
     5561
     5562                $response_aside = rest_get_server()->dispatch( $request );
     5563
     5564                $request->set_param( 'format', array( 'invalid_format' ) );
     5565                $response_invalid = rest_get_server()->dispatch( $request );
     5566
     5567                /*
     5568                 * Restore the initial post formats support.
     5569                 *
     5570                 * This needs to be done prior to the assertions to avoid unexpected
     5571                 * results for other tests should an assertion fail.
     5572                 */
     5573                if ( $initial_theme_support ) {
     5574                        add_theme_support( 'post-formats', $initial_theme_support[0] );
     5575                } else {
     5576                        remove_theme_support( 'post-formats' );
     5577                }
     5578
     5579                $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' );
     5580                $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' );
     5581        }
     5582
     5583        /**
     5584         * Test the REST API support for multiple post formats.
     5585         *
     5586         * @ticket 62014
     5587         *
     5588         * @covers WP_REST_Posts_Controller::get_items
     5589         */
     5590        public function test_multiple_post_format_support() {
     5591                $initial_theme_support = get_theme_support( 'post-formats' );
     5592                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
     5593
     5594                $post_id = self::factory()->post->create(
     5595                        array(
     5596                                'post_type'   => 'post',
     5597                                'post_status' => 'publish',
     5598                        )
     5599                );
     5600                set_post_format( $post_id, 'aside' );
     5601
     5602                $post_id_2 = self::factory()->post->create(
     5603                        array(
     5604                                'post_type'   => 'post',
     5605                                'post_status' => 'publish',
     5606                        )
     5607                );
     5608                set_post_format( $post_id_2, 'gallery' );
     5609
     5610                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5611                $request->set_param( 'format', array( 'aside', 'gallery' ) );
     5612
     5613                $response = rest_get_server()->dispatch( $request );
     5614
     5615                /*
     5616                 * Restore the initial post formats support.
     5617                 *
     5618                 * This needs to be done prior to the assertions to avoid unexpected
     5619                 * results for other tests should an assertion fail.
     5620                 */
     5621                if ( $initial_theme_support ) {
     5622                        add_theme_support( 'post-formats', $initial_theme_support[0] );
     5623                } else {
     5624                        remove_theme_support( 'post-formats' );
     5625                }
     5626
     5627                $this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' );
     5628        }
     5629
     5630        /**
    55005631         * Internal function used to disable an insert query which
    55015632         * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.