REST API: Add `rest_base` to response objects of `wp/v2/taxonomies` and `wp/v2/types`

Though we have the `_links.collection` available, having this value can be useful to know post type / taxonomy urls if you need to build them another way.

Props youknowriad, jnylen0.
Fixes #38607.


git-svn-id: https://develop.svn.wordpress.org/trunk@39191 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Hoyle 2016-11-10 02:20:09 +00:00
parent ec2ffb45c1
commit c67401baf1
4 changed files with 23 additions and 6 deletions

View File

@ -148,6 +148,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
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' );
$base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
$data = array(
'capabilities' => $post_type->cap,
'description' => $post_type->description,
@ -156,6 +157,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
'name' => $post_type->label,
'slug' => $post_type->name,
'taxonomies' => array_values( $taxonomies ),
'rest_base' => $base,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
@ -164,8 +166,6 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
$response->add_links( array(
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
@ -251,6 +251,12 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'rest_base' => array(
'description' => __( 'REST base route for the resource.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );

View File

@ -177,7 +177,7 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $taxonomy, $request ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$data = array(
'name' => $taxonomy->label,
'slug' => $taxonomy->name,
@ -187,6 +187,7 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
'types' => $taxonomy->object_type,
'show_cloud' => $taxonomy->show_tagcloud,
'hierarchical' => $taxonomy->hierarchical,
'rest_base' => $base,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
@ -196,7 +197,6 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$response->add_links( array(
'collection' => array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
@ -285,6 +285,12 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'rest_base' => array(
'description' => __( 'REST base route for the resource.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );

View File

@ -119,7 +119,7 @@ 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( 7, count( $properties ) );
$this->assertEquals( 8, count( $properties ) );
$this->assertArrayHasKey( 'capabilities', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'hierarchical', $properties );
@ -127,6 +127,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'taxonomies', $properties );
$this->assertArrayHasKey( 'rest_base', $properties );
}
public function test_get_additional_field_registration() {
@ -170,6 +171,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
$this->assertEquals( $post_type_obj->name, $data['slug'] );
$this->assertEquals( $post_type_obj->description, $data['description'] );
$this->assertEquals( $post_type_obj->hierarchical, $data['hierarchical'] );
$this->assertEquals( $post_type_obj->rest_base, $data['rest_base'] );
$links = test_rest_expand_compact_links( $links );
$this->assertEquals( rest_url( 'wp/v2/types' ), $links['collection'][0]['href'] );

View File

@ -45,6 +45,7 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
$this->assertEquals( 'Tags', $data['post_tag']['name'] );
$this->assertEquals( 'post_tag', $data['post_tag']['slug'] );
$this->assertEquals( false, $data['post_tag']['hierarchical'] );
$this->assertEquals( 'tags', $data['post_tag']['rest_base'] );
}
public function test_get_items_invalid_permission_for_context() {
@ -134,7 +135,7 @@ class WP_Test_REST_Taxonomies_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 );
@ -143,6 +144,7 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'show_cloud', $properties );
$this->assertArrayHasKey( 'types', $properties );
$this->assertArrayHasKey( 'rest_base', $properties );
}
public function tearDown() {
@ -168,6 +170,7 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
$this->assertEquals( $tax_obj->name, $data['slug'] );
$this->assertEquals( $tax_obj->description, $data['description'] );
$this->assertEquals( $tax_obj->hierarchical, $data['hierarchical'] );
$this->assertEquals( $tax_obj->rest_base, $data['rest_base'] );
$this->assertEquals( rest_url( 'wp/v2/taxonomies' ), $links['collection'][0]['href'] );
$this->assertArrayHasKey( 'https://api.w.org/items', $links );
if ( 'edit' === $context ) {