From 7381a8273a3c5c3e63a194dace734612a610a135 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 19 Dec 2024 21:41:19 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@59544 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 2 +- .../tests/rest-api/rest-posts-controller.php | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 8852519ec45c9..95851199c6e4d 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -346,7 +346,7 @@ public function get_items( $request ) { $args = $this->prepare_tax_query( $args, $request ); - if ( ! empty( $request['format'] ) ) { + if ( isset( $registered['format'], $request['format'] ) ) { $formats = $request['format']; /* * The relation needs to be set to `OR` since the request can contain diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 4574bde83621e..e2fd7139f3f54 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -5507,6 +5507,45 @@ public function test_draft_post_does_not_have_the_same_slug_as_existing_post() { ); } + /** + * Test the REST API ignores the post format parameter for post types that do not support them. + * + * @ticket 62646 + * @ticket 62014 + * + * @covers WP_REST_Posts_Controller::get_items + */ + public function test_standard_post_format_ignored_for_post_types_that_do_not_support_them() { + $initial_theme_support = get_theme_support( 'post-formats' ); + add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) ); + + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); + $request->set_param( 'format', 'invalid_type' ); + + $response = rest_get_server()->dispatch( $request ); + + /* + * Restore the initial post formats support. + * + * This needs to be done prior to the assertions to avoid unexpected + * results for other tests should an assertion fail. + */ + if ( $initial_theme_support ) { + add_theme_support( 'post-formats', $initial_theme_support[0] ); + } else { + remove_theme_support( 'post-formats' ); + } + + $this->assertCount( 1, $response->get_data(), 'The response should ignore the post format parameter' ); + } + /** * Test the REST API support for the standard post format. *