Skip to content

Commit

Permalink
REST API: Protect against fatal error for post types without format s…
Browse files Browse the repository at this point in the history
…upport.

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
  • Loading branch information
peterwilsoncc committed Dec 19, 2024
1 parent ef76060 commit 7381a82
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 7381a82

Please sign in to comment.