Skip to content

Commit 2b063ba

Browse files
committed
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. Reviewed by jorbin. Merges [59544] to the 6.7 branch. Props dd32, sergeybiryukov, yogeshbhutkar. Fixes #62646. See #62014. git-svn-id: https://develop.svn.wordpress.org/branches/6.7@59744 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6711402 commit 2b063ba

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public function get_items( $request ) {
346346

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
/*
352352
* The relation needs to be set to `OR` since the request can contain

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5507,6 +5507,45 @@ public function test_draft_post_does_not_have_the_same_slug_as_existing_post() {
55075507
);
55085508
}
55095509

5510+
/**
5511+
* Test the REST API ignores the post format parameter for post types that do not support them.
5512+
*
5513+
* @ticket 62646
5514+
* @ticket 62014
5515+
*
5516+
* @covers WP_REST_Posts_Controller::get_items
5517+
*/
5518+
public function test_standard_post_format_ignored_for_post_types_that_do_not_support_them() {
5519+
$initial_theme_support = get_theme_support( 'post-formats' );
5520+
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
5521+
5522+
self::factory()->post->create(
5523+
array(
5524+
'post_type' => 'page',
5525+
'post_status' => 'publish',
5526+
)
5527+
);
5528+
5529+
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
5530+
$request->set_param( 'format', 'invalid_type' );
5531+
5532+
$response = rest_get_server()->dispatch( $request );
5533+
5534+
/*
5535+
* Restore the initial post formats support.
5536+
*
5537+
* This needs to be done prior to the assertions to avoid unexpected
5538+
* results for other tests should an assertion fail.
5539+
*/
5540+
if ( $initial_theme_support ) {
5541+
add_theme_support( 'post-formats', $initial_theme_support[0] );
5542+
} else {
5543+
remove_theme_support( 'post-formats' );
5544+
}
5545+
5546+
$this->assertCount( 1, $response->get_data(), 'The response should ignore the post format parameter' );
5547+
}
5548+
55105549
/**
55115550
* Test the REST API support for the standard post format.
55125551
*

0 commit comments

Comments
 (0)