From a2c633fdc7deec124268d28fbd87c066d7d67a9d Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Mon, 27 Feb 2017 20:02:43 +0000 Subject: [PATCH] REST API: Allow setting post formats even if they are not supported by the theme. A `post_format` not used by the current theme, but supported by core is not a wrong/broken piece of information. It's just not used at this point in time. Therefore we should allow setting and retrieving any of the standard post formats supported in core, even if the current theme doesn't use them. After this commit, a post's `format` value can survive a round trip through the API, which is a good general design principle for an API. Merge of [40120] and [40121] to the 4.7 branch. Props JPry, iseulde, davidakennedy, Drivingralle. Fixes #39232. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40137 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 10 +++------ .../tests/rest-api/rest-posts-controller.php | 20 +++++++---------- tests/qunit/fixtures/wp-api-generated.js | 22 +++++++++++++++++-- 3 files changed, 31 insertions(+), 21 deletions(-) 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 9e726b4891..b55620db73 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 @@ -1983,17 +1983,13 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { break; case 'post-formats': - $supports_formats = get_theme_support( 'post-formats' ); - - // Force to an array. Supports formats can return true even if empty in some cases. - $supports_formats = is_array( $supports_formats ) ? array_values( $supports_formats[0] ) : array(); - - $supported_formats = array_merge( array( 'standard' ), $supports_formats ); + // Get the native post formats and remove the array keys. + $formats = array_values( get_post_format_slugs() ); $schema['properties']['format'] = array( 'description' => __( 'The format for the object.' ), 'type' => 'string', - 'enum' => $supported_formats, + 'enum' => $formats, 'context' => array( 'view', 'edit' ), ); break; diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index f7b7a74206..db94b71d5f 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -990,20 +990,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $post2, $data[0]['id'] ); } - public function test_get_items_no_supported_post_formats() { - // This causes get_theme_support( 'post-formats' ) to return `true` (not an array) - add_theme_support( 'post-formats' ); - + public function test_get_items_all_post_formats() { $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); - // Set the expected state back for the rest of the tests. - global $_wp_theme_features; - unset( $_wp_theme_features['post-formats'] ); - add_theme_support( 'post-formats', array( 'post', 'gallery' ) ); - - $formats = array( 'standard' ); + $formats = array_values( get_post_format_slugs() ); $this->assertEquals( $formats, $data['schema']['properties']['format']['enum'] ); } @@ -1651,8 +1643,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); + $this->assertEquals( 201, $response->get_status() ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $data = $response->get_data(); + $this->assertEquals( 'link', $data['format'] ); } public function test_create_update_post_with_featured_media() { @@ -2149,8 +2143,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $data = $response->get_data(); + $this->assertEquals( 'link', $data['format'] ); } public function test_update_post_ignore_readonly() { diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index a7f253282f..23a2fb918b 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -388,7 +388,16 @@ mockedApiResponse.Schema = { "format": { "required": false, "enum": [ - "standard" + "standard", + "aside", + "chat", + "gallery", + "link", + "image", + "quote", + "status", + "video", + "audio" ], "description": "The format for the object.", "type": "string" @@ -562,7 +571,16 @@ mockedApiResponse.Schema = { "format": { "required": false, "enum": [ - "standard" + "standard", + "aside", + "chat", + "gallery", + "link", + "image", + "quote", + "status", + "video", + "audio" ], "description": "The format for the object.", "type": "string"