From fa3f97449e8494379bf551cacaaa324e56bd79c8 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Thu, 29 Dec 2016 17:27:37 +0000 Subject: [PATCH] REST API: Add the `supports` property to the Post Type response object. Includes a new `supports` property in the response object and schema for the `/types` endpoints for users with the `edit_posts` capability for the given post type. The `supports` property returns an object of the features the given post type *supports*. Props timmydcrawford, tyxla. Fixes #39033. git-svn-id: https://develop.svn.wordpress.org/trunk@39647 602fd350-edb4-49c9-b593-d223f7449a82 --- .../endpoints/class-wp-rest-post-types-controller.php | 9 +++++++++ .../tests/rest-api/rest-post-types-controller.php | 5 ++++- 2 files changed, 13 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 a1437dd597..4da4b52578 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 @@ -149,6 +149,8 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) ); $taxonomies = wp_list_pluck( $taxonomies, 'name' ); $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; + $supports = get_all_post_type_supports( $post_type->name ); + $data = array( 'capabilities' => $post_type->cap, 'description' => $post_type->description, @@ -156,6 +158,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'labels' => $post_type->labels, 'name' => $post_type->label, 'slug' => $post_type->name, + 'supports' => $supports, 'taxonomies' => array_values( $taxonomies ), 'rest_base' => $base, ); @@ -239,6 +242,12 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), + 'supports' => array( + 'description' => __( 'All features, supported by the post type.' ), + 'type' => 'object', + 'context' => array( 'edit' ), + 'readonly' => true, + ), 'taxonomies' => array( 'description' => __( 'Taxonomies associated with post type.' ), 'type' => 'array', 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 49c9ebb26f..b0bdd8c9ae 100644 --- a/tests/phpunit/tests/rest-api/rest-post-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-post-types-controller.php @@ -119,13 +119,14 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertEquals( 8, count( $properties ) ); + $this->assertEquals( 9, count( $properties ) ); $this->assertArrayHasKey( 'capabilities', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'hierarchical', $properties ); $this->assertArrayHasKey( 'labels', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'slug', $properties ); + $this->assertArrayHasKey( 'supports', $properties ); $this->assertArrayHasKey( 'taxonomies', $properties ); $this->assertArrayHasKey( 'rest_base', $properties ); } @@ -179,9 +180,11 @@ 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'] ); + $this->assertEquals( get_all_post_type_supports( $post_type_obj->name ), $data['supports'] ); } else { $this->assertFalse( isset( $data['capabilities'] ) ); $this->assertFalse( isset( $data['labels'] ) ); + $this->assertFalse( isset( $data['supports'] ) ); } }