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
This commit is contained in:
Dominik Schilling (ocean90) 2017-02-27 20:02:43 +00:00
parent 6bfb976577
commit a2c633fdc7
3 changed files with 31 additions and 21 deletions

View File

@ -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;

View File

@ -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() {

View File

@ -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"