WordPress.org

Make WordPress Core

Changeset 39084


Ignore:
Timestamp:
11/02/2016 03:36:40 AM (5 years ago)
Author:
rmccue
Message:

REST API: Only expose formats supported by the current theme.

While it's valid to save any format to the database, and WordPress is totally fine with that, we should only include the formats specified by the theme in the schema.

Props danielbachhuber.
Fixes #38610.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39055 r39084  
    19241924
    19251925                case 'post-formats':
     1926                    $supports_formats = get_theme_support( 'post-formats' );
    19261927                    $schema['properties']['format'] = array(
    19271928                        'description' => __( 'The format for the object.' ),
    19281929                        'type'        => 'string',
    1929                         'enum'        => array_values( get_post_format_slugs() ),
     1930                        'enum'        => $supports_formats ? array_values( $supports_formats[0] ) : array(),
    19301931                        'context'     => array( 'view', 'edit' ),
    19311932                    );
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39055 r39084  
    1717    protected static $contributor_id;
    1818
     19    protected static $supported_formats;
     20
    1921    public static function wpSetUpBeforeClass( $factory ) {
    2022        self::$post_id = $factory->post->create();
     
    2931            'role' => 'contributor',
    3032        ) );
     33
     34        // Only support 'post' and 'gallery'
     35        self::$supported_formats = get_theme_support( 'post-formats' );
     36        add_theme_support( 'post-formats', array( 'post', 'gallery' ) );
    3137    }
    3238
    3339    public static function wpTearDownAfterClass() {
     40        // Restore theme support for formats.
     41        if ( self::$supported_formats ) {
     42            add_theme_support( 'post-formats', self::$supported_formats );
     43        } else {
     44            remove_theme_support( 'post-formats' );
     45        }
     46
    3447        wp_delete_post( self::$post_id, true );
    3548
     
    10791092    }
    10801093
     1094    /**
     1095     * Test with a valid format, but one unsupported by the theme.
     1096     *
     1097     * https://core.trac.wordpress.org/ticket/38610
     1098     */
     1099    public function test_create_post_with_unsupported_format() {
     1100        wp_set_current_user( self::$editor_id );
     1101
     1102        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1103        $params = $this->set_post_data( array(
     1104            'format' => 'link',
     1105        ) );
     1106        $request->set_body_params( $params );
     1107        $response = $this->server->dispatch( $request );
     1108
     1109        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1110    }
     1111
    10811112    public function test_create_update_post_with_featured_media() {
    10821113
     
    14911522        $params = $this->set_post_data( array(
    14921523            'format' => 'testformat',
     1524        ) );
     1525        $request->set_body_params( $params );
     1526        $response = $this->server->dispatch( $request );
     1527
     1528        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1529    }
     1530
     1531    /**
     1532     * Test with a valid format, but one unsupported by the theme.
     1533     *
     1534     * https://core.trac.wordpress.org/ticket/38610
     1535     */
     1536    public function test_update_post_with_unsupported_format() {
     1537        wp_set_current_user( self::$editor_id );
     1538
     1539        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1540        $params = $this->set_post_data( array(
     1541            'format' => 'link',
    14931542        ) );
    14941543        $request->set_body_params( $params );
Note: See TracChangeset for help on using the changeset viewer.