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
This commit is contained in:
Joe Hoyle 2016-11-02 21:50:08 +00:00
parent e78230b39a
commit 082151025e
2 changed files with 29 additions and 3 deletions

View File

@ -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 );

View File

@ -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() );
}