Make WordPress Core

Ticket #38698: 38698.diff

File 38698.diff, 3.3 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index c313784..f19fa17 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    20022002                        );
    20032003                }
    20042004
    2005                 if ( 'page' === $this->post_type ) {
    2006                         $schema['properties']['template'] = array(
    2007                                 'description' => __( 'The theme file to use to display the object.' ),
    2008                                 'type'        => 'string',
    2009                                 'enum'        => array_keys( wp_get_theme()->get_page_templates() ),
    2010                                 'context'     => array( 'view', 'edit' ),
    2011                         );
    2012                 }
     2005                $schema['properties']['template'] = array(
     2006                        'description' => __( 'The theme file to use to display the object.' ),
     2007                        'type'        => 'string',
     2008                        'enum'        => array_keys( wp_get_theme()->get_page_templates( null, $this->post_type ) ),
     2009                        'context'     => array( 'view', 'edit' ),
     2010                );
    20132011
    20142012                $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
    20152013                foreach ( $taxonomies as $taxonomy ) {
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php
    index cdd5ec0..fab605b 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    5656        public function setUp() {
    5757                parent::setUp();
    5858                register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) );
     59                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    5960        }
    6061
    6162        public function test_register_routes() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    10161017                $this->check_create_post_response( $response );
    10171018        }
    10181019
     1020        public function test_create_item_with_template() {
     1021                wp_set_current_user( self::$editor_id );
     1022
     1023                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1024                $params = $this->set_post_data( array(
     1025                        'template' => 'post-my-test-template.php',
     1026                ) );
     1027                $request->set_body_params( $params );
     1028                $response = $this->server->dispatch( $request );
     1029
     1030                $data = $response->get_data();
     1031                $new_post = get_post( $data['id'] );
     1032                $this->assertEquals( 'post-my-test-template.php', $data['template'] );
     1033                $this->assertEquals( 'post-my-test-template.php', get_page_template_slug( $new_post->ID ) );
     1034        }
     1035
    10191036        public function test_rest_create_item() {
    10201037                wp_set_current_user( self::$editor_id );
    10211038
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    22242241                if ( isset( $this->attachment_id ) ) {
    22252242                        $this->remove_added_uploads();
    22262243                }
     2244                remove_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    22272245                parent::tearDown();
    22282246        }
    22292247
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    22382256                return $query;
    22392257        }
    22402258
     2259        public function filter_theme_post_templates( $post_templates ) {
     2260                return array(
     2261                        'post-my-test-template.php' => 'My Test Template',
     2262                );
     2263
     2264                return $post_templates;
     2265        }
    22412266}