From 67da42825eeb1fe59db3bb5bc906a679c83f7b93 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Thu, 10 Nov 2016 02:21:30 +0000 Subject: [PATCH] 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 --- .../endpoints/class-wp-rest-post-types-controller.php | 10 ++++++++-- .../endpoints/class-wp-rest-taxonomies-controller.php | 10 ++++++++-- wp-includes/version.php | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php index 534aa8ef68..5394d16fdb 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php @@ -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 ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php index 21c0b0e32b..11c306c988 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index dceb84a7ec..f907c52aa8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.