3 * REST API: WP_REST_Taxonomies_Controller class
11 * Core class used to manage taxonomies via the REST API.
15 * @see WP_REST_Controller
17 class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
25 public function __construct() {
26 $this->namespace = 'wp/v2';
27 $this->rest_base = 'taxonomies';
31 * Registers the routes for the objects of the controller.
36 * @see register_rest_route()
38 public function register_routes() {
40 register_rest_route( $this->namespace, '/' . $this->rest_base, array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get_items' ),
44 'permission_callback' => array( $this, 'get_items_permissions_check' ),
45 'args' => $this->get_collection_params(),
47 'schema' => array( $this, 'get_public_item_schema' ),
50 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)', array(
53 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => array( $this, 'get_item' ),
60 'permission_callback' => array( $this, 'get_item_permissions_check' ),
62 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
65 'schema' => array( $this, 'get_public_item_schema' ),
70 * Checks whether a given request has permission to read taxonomies.
75 * @param WP_REST_Request $request Full details about the request.
76 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
78 public function get_items_permissions_check( $request ) {
79 if ( 'edit' === $request['context'] ) {
80 if ( ! empty( $request['type'] ) ) {
81 $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
83 $taxonomies = get_taxonomies( '', 'objects' );
85 foreach ( $taxonomies as $taxonomy ) {
86 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->manage_terms ) ) {
90 return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
96 * Retrieves all public taxonomies.
101 * @param WP_REST_Request $request Full details about the request.
102 * @return WP_REST_Response Response object on success, or WP_Error object on failure.
104 public function get_items( $request ) {
106 // Retrieve the list of registered collection query parameters.
107 $registered = $this->get_collection_params();
109 if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
110 $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
112 $taxonomies = get_taxonomies( '', 'objects' );
115 foreach ( $taxonomies as $tax_type => $value ) {
116 if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->manage_terms ) ) ) {
119 $tax = $this->prepare_item_for_response( $value, $request );
120 $tax = $this->prepare_response_for_collection( $tax );
121 $data[ $tax_type ] = $tax;
124 if ( empty( $data ) ) {
125 // Response should still be returned as a JSON object when it is empty.
126 $data = (object) $data;
129 return rest_ensure_response( $data );
133 * Checks if a given request has access to a taxonomy.
138 * @param WP_REST_Request $request Full details about the request.
139 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
141 public function get_item_permissions_check( $request ) {
143 $tax_obj = get_taxonomy( $request['taxonomy'] );
146 if ( empty( $tax_obj->show_in_rest ) ) {
149 if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->manage_terms ) ) {
150 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
158 * Retrieves a specific taxonomy.
163 * @param WP_REST_Request $request Full details about the request.
164 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
166 public function get_item( $request ) {
167 $tax_obj = get_taxonomy( $request['taxonomy'] );
168 if ( empty( $tax_obj ) ) {
169 return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) );
171 $data = $this->prepare_item_for_response( $tax_obj, $request );
172 return rest_ensure_response( $data );
176 * Prepares a taxonomy object for serialization.
181 * @param stdClass $taxonomy Taxonomy data.
182 * @param WP_REST_Request $request Full details about the request.
183 * @return WP_REST_Response Response object.
185 public function prepare_item_for_response( $taxonomy, $request ) {
186 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
188 'name' => $taxonomy->label,
189 'slug' => $taxonomy->name,
190 'capabilities' => $taxonomy->cap,
191 'description' => $taxonomy->description,
192 'labels' => $taxonomy->labels,
193 'types' => $taxonomy->object_type,
194 'show_cloud' => $taxonomy->show_tagcloud,
195 'hierarchical' => $taxonomy->hierarchical,
196 'rest_base' => $base,
199 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
200 $data = $this->add_additional_fields_to_object( $data, $request );
201 $data = $this->filter_response_by_context( $data, $context );
203 // Wrap the data in a response object.
204 $response = rest_ensure_response( $data );
206 $response->add_links( array(
207 'collection' => array(
208 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
210 'https://api.w.org/items' => array(
211 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
216 * Filters a taxonomy returned from the REST API.
218 * Allows modification of the taxonomy data right before it is returned.
222 * @param WP_REST_Response $response The response object.
223 * @param object $item The original taxonomy object.
224 * @param WP_REST_Request $request Request used to generate the response.
226 return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
230 * Retrieves the taxonomy's schema, conforming to JSON Schema.
235 * @return array Item schema data.
237 public function get_item_schema() {
239 '$schema' => 'http://json-schema.org/schema#',
240 'title' => 'taxonomy',
242 'properties' => array(
243 'capabilities' => array(
244 'description' => __( 'All capabilities used by the taxonomy.' ),
246 'context' => array( 'edit' ),
249 'description' => array(
250 'description' => __( 'A human-readable description of the taxonomy.' ),
252 'context' => array( 'view', 'edit' ),
255 'hierarchical' => array(
256 'description' => __( 'Whether or not the taxonomy should have children.' ),
258 'context' => array( 'view', 'edit' ),
262 'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
264 'context' => array( 'edit' ),
268 'description' => __( 'The title for the taxonomy.' ),
270 'context' => array( 'view', 'edit', 'embed' ),
274 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
276 'context' => array( 'view', 'edit', 'embed' ),
279 'show_cloud' => array(
280 'description' => __( 'Whether or not the term cloud should be displayed.' ),
282 'context' => array( 'edit' ),
286 'description' => __( 'Types associated with the taxonomy.' ),
291 'context' => array( 'view', 'edit' ),
294 'rest_base' => array(
295 'description' => __( 'REST base route for the taxonomy.' ),
297 'context' => array( 'view', 'edit', 'embed' ),
302 return $this->add_additional_fields_schema( $schema );
306 * Retrieves the query params for collections.
311 * @return array Collection parameters.
313 public function get_collection_params() {
314 $new_params = array();
315 $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
316 $new_params['type'] = array(
317 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),