Make WordPress Core

Changeset 59544


Ignore:
Timestamp:
12/19/2024 09:41:19 PM (23 months ago)
Author:
peterwilsoncc
Message:

REST API: Protect against fatal error for post types without format support.

Ignore the format parameter introduced in WordPress 6.7 for post types that do not support post formats. This protects against a fatal error being thrown in later version of PHP or a warning in earlier versions of PHP.

Follow up to r59115.

Props dd32, sergeybiryukov, yogeshbhutkar.
Fixes #62646.
See #62014.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r59453 r59544  
    347347                $args = $this->prepare_tax_query( $args, $request );
    348348
    349                 if ( ! empty( $request['format'] ) ) {
     349                if ( isset( $registered['format'], $request['format'] ) ) {
    350350                        $formats = $request['format'];
    351351                        /*
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59235 r59544  
    55095509
    55105510        /**
    5511          * Test the REST API support for the standard post format.
     5511         * Test the REST API ignores the post format parameter for post types that do not support them.
    55125512         *
     5513         * @ticket 62646
    55135514         * @ticket 62014
    55145515         *
    55155516         * @covers WP_REST_Posts_Controller::get_items
    55165517         */
    5517         public function test_standard_post_format_support() {
     5518        public function test_standard_post_format_ignored_for_post_types_that_do_not_support_them() {
    55185519                $initial_theme_support = get_theme_support( 'post-formats' );
    55195520                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
    55205521
    5521                 $post_id = self::factory()->post->create(
    5522                         array(
    5523                                 'post_type'   => 'post',
     5522                self::factory()->post->create(
     5523                        array(
     5524                                'post_type'   => 'page',
    55245525                                'post_status' => 'publish',
    55255526                        )
    55265527                );
    5527                 set_post_format( $post_id, 'aside' );
    5528 
    5529                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    5530                 $request->set_param( 'format', array( 'standard' ) );
    5531                 $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     5528
     5529                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
     5530                $request->set_param( 'format', 'invalid_type' );
    55325531
    55335532                $response = rest_get_server()->dispatch( $request );
    … …  
    55455544                }
    55465545
    5547                 $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' );
    5548         }
    5549 
    5550         /**
    5551          * Test the REST API support for post formats.
     5546                $this->assertCount( 1, $response->get_data(), 'The response should ignore the post format parameter' );
     5547        }
     5548
     5549        /**
     5550         * Test the REST API support for the standard post format.
    55525551         *
    55535552         * @ticket 62014
    … …  
    55555554         * @covers WP_REST_Posts_Controller::get_items
    55565555         */
    5557         public function test_post_format_support() {
     5556        public function test_standard_post_format_support() {
    55585557                $initial_theme_support = get_theme_support( 'post-formats' );
    55595558                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
    … …  
    55685567
    55695568                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    5570                 $request->set_param( 'format', array( 'aside' ) );
    5571 
    5572                 $response_aside = rest_get_server()->dispatch( $request );
    5573 
    5574                 $request->set_param( 'format', array( 'invalid_format' ) );
    5575                 $response_invalid = rest_get_server()->dispatch( $request );
     5569                $request->set_param( 'format', array( 'standard' ) );
     5570                $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
     5571
     5572                $response = rest_get_server()->dispatch( $request );
    55765573
    55775574                /*
    … …  
    55875584                }
    55885585
    5589                 $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' );
    5590                 $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' );
    5591         }
    5592 
    5593         /**
    5594          * Test the REST API support for multiple post formats.
     5586                $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' );
     5587        }
     5588
     5589        /**
     5590         * Test the REST API support for post formats.
    55955591         *
    55965592         * @ticket 62014
    … …  
    55985594         * @covers WP_REST_Posts_Controller::get_items
    55995595         */
    5600         public function test_multiple_post_format_support() {
     5596        public function test_post_format_support() {
    56015597                $initial_theme_support = get_theme_support( 'post-formats' );
    56025598                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
    … …  
    56105606                set_post_format( $post_id, 'aside' );
    56115607
    5612                 $post_id_2 = self::factory()->post->create(
    5613                         array(
    5614                                 'post_type'   => 'post',
    5615                                 'post_status' => 'publish',
    5616                         )
    5617                 );
    5618                 set_post_format( $post_id_2, 'gallery' );
    5619 
    5620                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    5621                 $request->set_param( 'format', array( 'aside', 'gallery' ) );
    5622 
    5623                 $response = rest_get_server()->dispatch( $request );
     5608                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5609                $request->set_param( 'format', array( 'aside' ) );
     5610
     5611                $response_aside = rest_get_server()->dispatch( $request );
     5612
     5613                $request->set_param( 'format', array( 'invalid_format' ) );
     5614                $response_invalid = rest_get_server()->dispatch( $request );
    56245615
    56255616                /*
    … …  
    56355626                }
    56365627
     5628                $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' );
     5629                $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' );
     5630        }
     5631
     5632        /**
     5633         * Test the REST API support for multiple post formats.
     5634         *
     5635         * @ticket 62014
     5636         *
     5637         * @covers WP_REST_Posts_Controller::get_items
     5638         */
     5639        public function test_multiple_post_format_support() {
     5640                $initial_theme_support = get_theme_support( 'post-formats' );
     5641                add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
     5642
     5643                $post_id = self::factory()->post->create(
     5644                        array(
     5645                                'post_type'   => 'post',
     5646                                'post_status' => 'publish',
     5647                        )
     5648                );
     5649                set_post_format( $post_id, 'aside' );
     5650
     5651                $post_id_2 = self::factory()->post->create(
     5652                        array(
     5653                                'post_type'   => 'post',
     5654                                'post_status' => 'publish',
     5655                        )
     5656                );
     5657                set_post_format( $post_id_2, 'gallery' );
     5658
     5659                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     5660                $request->set_param( 'format', array( 'aside', 'gallery' ) );
     5661
     5662                $response = rest_get_server()->dispatch( $request );
     5663
     5664                /*
     5665                 * Restore the initial post formats support.
     5666                 *
     5667                 * This needs to be done prior to the assertions to avoid unexpected
     5668                 * results for other tests should an assertion fail.
     5669                 */
     5670                if ( $initial_theme_support ) {
     5671                        add_theme_support( 'post-formats', $initial_theme_support[0] );
     5672                } else {
     5673                        remove_theme_support( 'post-formats' );
     5674                }
     5675
    56375676                $this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' );
    56385677        }
Note: See TracChangeset for help on using the changeset viewer.