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.


git-svn-id: https://develop.svn.wordpress.org/trunk@39182 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue 2016-11-09 07:12:01 +00:00
parent 8a0c502703
commit 446dc73042
3 changed files with 59 additions and 11 deletions

View File

@ -1094,7 +1094,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
}
/**
* Sets the template for a page.
* Sets the template for a post.
*
* @since 4.7.0
* @access public
@ -1922,14 +1922,12 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
}
if ( 'page' === $this->post_type ) {
$schema['properties']['template'] = array(
'description' => __( 'The theme file to use to display the object.' ),
'type' => 'string',
'enum' => array_keys( wp_get_theme()->get_page_templates() ),
'context' => array( 'view', 'edit' ),
);
}
$schema['properties']['template'] = array(
'description' => __( 'The theme file to use to display the object.' ),
'type' => 'string',
'enum' => array_keys( wp_get_theme()->get_page_templates( null, $this->post_type ) ),
'context' => array( 'view', 'edit' ),
);
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {

View File

@ -1041,7 +1041,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 23, count( $properties ) );
$this->assertEquals( 24, count( $properties ) );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'alt_text', $properties );
$this->assertArrayHasKey( 'caption', $properties );
@ -1067,6 +1067,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'source_url', $properties );
$this->assertArrayHasKey( 'template', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'raw', $properties['title']['properties'] );
$this->assertArrayHasKey( 'rendered', $properties['title']['properties'] );

View File

@ -1025,6 +1025,49 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->check_create_post_response( $response );
}
/**
* @ticket 38698
*/
public function test_create_item_with_template() {
wp_set_current_user( self::$editor_id );
add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$params = $this->set_post_data( array(
'template' => 'post-my-test-template.php',
) );
$request->set_body_params( $params );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$post_template = get_page_template_slug( get_post( $data['id'] ) );
remove_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
$this->assertEquals( 'post-my-test-template.php', $data['template'] );
$this->assertEquals( 'post-my-test-template.php', $post_template );
}
/**
* @ticket 38698
*/
public function test_create_item_with_template_none_available() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$params = $this->set_post_data( array(
'template' => 'post-my-test-template.php',
) );
$request->set_body_params( $params );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$post_template = get_page_template_slug( get_post( $data['id'] ) );
$this->assertEquals( '', $data['template'] );
$this->assertEquals( '', $post_template );
}
public function test_rest_create_item() {
wp_set_current_user( self::$editor_id );
@ -2317,7 +2360,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 23, count( $properties ) );
$this->assertEquals( 24, count( $properties ) );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'comment_status', $properties );
$this->assertArrayHasKey( 'content', $properties );
@ -2337,6 +2380,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'sticky', $properties );
$this->assertArrayHasKey( 'template', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'tags', $properties );
@ -2469,4 +2513,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
return $query;
}
public function filter_theme_post_templates( $post_templates ) {
return array(
'post-my-test-template.php' => 'My Test Template',
);
}
}