From 082151025ee96b5a6f03ede1c36410d67ed81b44 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 2 Nov 2016 21:50:08 +0000 Subject: [PATCH] REST API: Include `taxonomies` as an attribute of post types. Add the taxonomies for a post type to the `/wp/v2/types` endpoint, so clients know which taxonomies are available for which post types. Props danielbachhuber. Fixes #38438, #38631. git-svn-id: https://develop.svn.wordpress.org/trunk@39097 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-post-types-controller.php | 15 +++++++++++++++ .../rest-api/rest-post-types-controller.php | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) 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 6f2aeee670..358bad836a 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 @@ -146,6 +146,8 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { * @return WP_REST_Response Response object. */ public function prepare_item_for_response( $post_type, $request ) { + $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) ); + $taxonomies = wp_list_pluck( $taxonomies, 'name' ); $data = array( 'capabilities' => $post_type->cap, 'description' => $post_type->description, @@ -153,6 +155,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'labels' => $post_type->labels, 'name' => $post_type->label, 'slug' => $post_type->name, + 'taxonomies' => array_values( $taxonomies ), ); $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); @@ -203,6 +206,9 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'capabilities' => array( 'description' => __( 'All capabilities used by the resource.' ), 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), 'context' => array( 'edit' ), 'readonly' => true, ), @@ -236,6 +242,15 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), + 'taxonomies' => array( + 'description' => __( 'Taxonomies associated with resource.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), ), ); return $this->add_additional_fields_schema( $schema ); 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 84dca1dc0d..9ae9ead472 100644 --- a/tests/phpunit/tests/rest-api/rest-post-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-post-types-controller.php @@ -58,6 +58,16 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas $request = new WP_REST_Request( 'GET', '/wp/v2/types/post' ); $response = $this->server->dispatch( $request ); $this->check_post_type_object_response( 'view', $response ); + $data = $response->get_data(); + $this->assertEquals( array( 'category', 'post_tag' ), $data['taxonomies'] ); + } + + public function test_get_item_page() { + $request = new WP_REST_Request( 'GET', '/wp/v2/types/page' ); + $response = $this->server->dispatch( $request ); + $this->check_post_type_object_response( 'view', $response, 'page' ); + $data = $response->get_data(); + $this->assertEquals( array(), $data['taxonomies'] ); } public function test_get_item_invalid_type() { @@ -109,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( 6, count( $properties ) ); + $this->assertEquals( 7, 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( 'taxonomies', $properties ); } public function test_get_additional_field_registration() { @@ -172,10 +183,10 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas } } - protected function check_post_type_object_response( $context, $response ) { + protected function check_post_type_object_response( $context, $response, $post_type = 'post' ) { $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $obj = get_post_type_object( 'post' ); + $obj = get_post_type_object( $post_type ); $this->check_post_type_obj( $context, $obj, $data, $response->get_links() ); }