WordPress.org

Make WordPress Core

Changeset 40137


Ignore:
Timestamp:
02/27/2017 08:02:43 PM (2 years ago)
Author:
ocean90
Message:

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.

Location:
branches/4.7
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

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

    r40136 r40137  
    19841984
    19851985                case 'post-formats':
    1986                     $supports_formats = get_theme_support( 'post-formats' );
    1987 
    1988                     // Force to an array. Supports formats can return true even if empty in some cases.
    1989                     $supports_formats = is_array( $supports_formats ) ? array_values( $supports_formats[0] ) : array();
    1990 
    1991                     $supported_formats = array_merge( array( 'standard' ), $supports_formats );
     1986                    // Get the native post formats and remove the array keys.
     1987                    $formats = array_values( get_post_format_slugs() );
    19921988
    19931989                    $schema['properties']['format'] = array(
    19941990                        'description' => __( 'The format for the object.' ),
    19951991                        'type'        => 'string',
    1996                         'enum'        => $supported_formats,
     1992                        'enum'        => $formats,
    19971993                        'context'     => array( 'view', 'edit' ),
    19981994                    );
  • branches/4.7/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r40136 r40137  
    991991    }
    992992
    993     public function test_get_items_no_supported_post_formats() {
    994         // This causes get_theme_support( 'post-formats' ) to return `true` (not an array)
    995         add_theme_support( 'post-formats' );
    996 
     993    public function test_get_items_all_post_formats() {
    997994        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
    998995        $response = $this->server->dispatch( $request );
    999996        $data = $response->get_data();
    1000997
    1001         // Set the expected state back for the rest of the tests.
    1002         global $_wp_theme_features;
    1003         unset( $_wp_theme_features['post-formats'] );
    1004         add_theme_support( 'post-formats', array( 'post', 'gallery' ) );
    1005 
    1006         $formats = array( 'standard' );
     998        $formats = array_values( get_post_format_slugs() );
    1007999
    10081000        $this->assertEquals( $formats, $data['schema']['properties']['format']['enum'] );
     
    16521644        $request->set_body_params( $params );
    16531645        $response = $this->server->dispatch( $request );
    1654 
    1655         $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1646        $this->assertEquals( 201, $response->get_status() );
     1647
     1648        $data = $response->get_data();
     1649        $this->assertEquals( 'link', $data['format'] );
    16561650    }
    16571651
     
    21502144        $request->set_body_params( $params );
    21512145        $response = $this->server->dispatch( $request );
    2152 
    2153         $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     2146        $this->assertEquals( 200, $response->get_status() );
     2147
     2148        $data = $response->get_data();
     2149        $this->assertEquals( 'link', $data['format'] );
    21542150    }
    21552151
  • branches/4.7/tests/qunit/fixtures/wp-api-generated.js

    r40116 r40137  
    389389                            "required": false,
    390390                            "enum": [
    391                                 "standard"
     391                                "standard",
     392                                "aside",
     393                                "chat",
     394                                "gallery",
     395                                "link",
     396                                "image",
     397                                "quote",
     398                                "status",
     399                                "video",
     400                                "audio"
    392401                            ],
    393402                            "description": "The format for the object.",
     
    563572                            "required": false,
    564573                            "enum": [
    565                                 "standard"
     574                                "standard",
     575                                "aside",
     576                                "chat",
     577                                "gallery",
     578                                "link",
     579                                "image",
     580                                "quote",
     581                                "status",
     582                                "video",
     583                                "audio"
    566584                            ],
    567585                            "description": "The format for the object.",
Note: See TracChangeset for help on using the changeset viewer.