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.

Built from https://develop.svn.wordpress.org/trunk@39191


git-svn-id: http://core.svn.wordpress.org/trunk@39131 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe Hoyle 2016-11-10 02:21:30 +00:00
parent e66d2478ee
commit 67da42825e
3 changed files with 17 additions and 5 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

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-beta2-39190';
$wp_version = '4.7-beta2-39191';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.