Make WordPress Core

Changeset 39182


Ignore:
Timestamp:
11/09/2016 07:12:01 AM (8 years ago)
Author:
rmccue
Message:

REST API: Include template in all post type schemas.

[38951] added templates to all post types, but didn't add them to the schema.

Props swissspidy.
Fixes #38698.

Location:
trunk
Files:
3 edited

Legend:

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

    r39162 r39182  
    10951095
    10961096    /**
    1097      * Sets the template for a page.
     1097     * Sets the template for a post.
    10981098     *
    10991099     * @since 4.7.0
     
    19231923        }
    19241924
    1925         if ( 'page' === $this->post_type ) {
    1926             $schema['properties']['template'] = array(
    1927                 'description' => __( 'The theme file to use to display the object.' ),
    1928                 'type'        => 'string',
    1929                 'enum'        => array_keys( wp_get_theme()->get_page_templates() ),
    1930                 'context'     => array( 'view', 'edit' ),
    1931             );
    1932         }
     1925        $schema['properties']['template'] = array(
     1926            'description' => __( 'The theme file to use to display the object.' ),
     1927            'type'        => 'string',
     1928            'enum'        => array_keys( wp_get_theme()->get_page_templates( null, $this->post_type ) ),
     1929            'context'     => array( 'view', 'edit' ),
     1930        );
    19331931
    19341932        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r39160 r39182  
    10421042        $data = $response->get_data();
    10431043        $properties = $data['schema']['properties'];
    1044         $this->assertEquals( 23, count( $properties ) );
     1044        $this->assertEquals( 24, count( $properties ) );
    10451045        $this->assertArrayHasKey( 'author', $properties );
    10461046        $this->assertArrayHasKey( 'alt_text', $properties );
     
    10681068        $this->assertArrayHasKey( 'slug', $properties );
    10691069        $this->assertArrayHasKey( 'source_url', $properties );
     1070        $this->assertArrayHasKey( 'template', $properties );
    10701071        $this->assertArrayHasKey( 'title', $properties );
    10711072        $this->assertArrayHasKey( 'raw', $properties['title']['properties'] );
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39156 r39182  
    10241024
    10251025        $this->check_create_post_response( $response );
     1026    }
     1027
     1028    /**
     1029     * @ticket 38698
     1030     */
     1031    public function test_create_item_with_template() {
     1032        wp_set_current_user( self::$editor_id );
     1033        add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
     1034
     1035        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1036        $params = $this->set_post_data( array(
     1037            'template' => 'post-my-test-template.php',
     1038        ) );
     1039        $request->set_body_params( $params );
     1040        $response = $this->server->dispatch( $request );
     1041
     1042        $data = $response->get_data();
     1043        $post_template = get_page_template_slug( get_post( $data['id'] ) );
     1044
     1045        remove_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
     1046
     1047        $this->assertEquals( 'post-my-test-template.php', $data['template'] );
     1048        $this->assertEquals( 'post-my-test-template.php', $post_template );
     1049    }
     1050
     1051    /**
     1052     * @ticket 38698
     1053     */
     1054    public function test_create_item_with_template_none_available() {
     1055        wp_set_current_user( self::$editor_id );
     1056
     1057        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1058        $params = $this->set_post_data( array(
     1059            'template' => 'post-my-test-template.php',
     1060        ) );
     1061        $request->set_body_params( $params );
     1062        $response = $this->server->dispatch( $request );
     1063
     1064        $data = $response->get_data();
     1065        $post_template = get_page_template_slug( get_post( $data['id'] ) );
     1066
     1067        $this->assertEquals( '', $data['template'] );
     1068        $this->assertEquals( '', $post_template );
    10261069    }
    10271070
     
    23182361        $data = $response->get_data();
    23192362        $properties = $data['schema']['properties'];
    2320         $this->assertEquals( 23, count( $properties ) );
     2363        $this->assertEquals( 24, count( $properties ) );
    23212364        $this->assertArrayHasKey( 'author', $properties );
    23222365        $this->assertArrayHasKey( 'comment_status', $properties );
     
    23382381        $this->assertArrayHasKey( 'status', $properties );
    23392382        $this->assertArrayHasKey( 'sticky', $properties );
     2383        $this->assertArrayHasKey( 'template', $properties );
    23402384        $this->assertArrayHasKey( 'title', $properties );
    23412385        $this->assertArrayHasKey( 'type', $properties );
     
    24702514    }
    24712515
     2516    public function filter_theme_post_templates( $post_templates ) {
     2517        return array(
     2518            'post-my-test-template.php' => 'My Test Template',
     2519        );
     2520    }
    24722521}
Note: See TracChangeset for help on using the changeset viewer.