REST API: Include `viewable` attribute on Post Type resource for `edit` context

For the block editor to be able to expose the Preview button correctly, it needs to know the `is_post_type_viewable()` setting, this change adds it to the Post Type response.

Props danielbachhuber.
Fixes #43739.



git-svn-id: https://develop.svn.wordpress.org/trunk@43007 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2018-04-27 03:05:40 +00:00
parent 41b6e0da34
commit 0907ed4894
2 changed files with 16 additions and 1 deletions

View File

@ -159,6 +159,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
'capabilities' => $post_type->cap,
'description' => $post_type->description,
'hierarchical' => $post_type->hierarchical,
'viewable' => is_post_type_viewable( $post_type ),
'labels' => $post_type->labels,
'name' => $post_type->label,
'slug' => $post_type->name,
@ -229,6 +230,12 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'viewable' => array(
'description' => __( 'Whether or not the post type can be viewed.' ),
'type' => 'boolean',
'context' => array( 'edit' ),
'readonly' => true,
),
'labels' => array(
'description' => __( 'Human-readable labels for the post type for various contexts.' ),
'type' => 'object',

View File

@ -128,10 +128,11 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 9, count( $properties ) );
$this->assertEquals( 10, count( $properties ) );
$this->assertArrayHasKey( 'capabilities', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'hierarchical', $properties );
$this->assertArrayHasKey( 'viewable', $properties );
$this->assertArrayHasKey( 'labels', $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'slug', $properties );
@ -191,9 +192,16 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
if ( 'edit' === $context ) {
$this->assertEquals( $post_type_obj->cap, $data['capabilities'] );
$this->assertEquals( $post_type_obj->labels, $data['labels'] );
if ( in_array( $post_type_obj->name, array( 'post', 'page' ), true ) ) {
$viewable = true;
} else {
$viewable = is_post_type_viewable( $post_type_obj );
}
$this->assertEquals( $viewable, $data['viewable'] );
$this->assertEquals( get_all_post_type_supports( $post_type_obj->name ), $data['supports'] );
} else {
$this->assertFalse( isset( $data['capabilities'] ) );
$this->assertFalse( isset( $data['viewable'] ) );
$this->assertFalse( isset( $data['labels'] ) );
$this->assertFalse( isset( $data['supports'] ) );
}