From 0907ed4894c156522726124c3dcb489c79af33c6 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 27 Apr 2018 03:05:40 +0000 Subject: [PATCH] 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 --- .../endpoints/class-wp-rest-post-types-controller.php | 7 +++++++ .../tests/rest-api/rest-post-types-controller.php | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php index 203cea2530..de0ccab598 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php @@ -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', diff --git a/tests/phpunit/tests/rest-api/rest-post-types-controller.php b/tests/phpunit/tests/rest-api/rest-post-types-controller.php index dfb233bf2e..8ef7f4202c 100644 --- a/tests/phpunit/tests/rest-api/rest-post-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-post-types-controller.php @@ -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'] ) ); }