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(
52 'methods' => WP_REST_Server::READABLE,
53 'callback' => array( $this, 'get_item' ),
54 'permission_callback' => array( $this, 'get_item_permissions_check' ),
56 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
59 'schema' => array( $this, 'get_public_item_schema' ),
64 * Checks whether a given request has permission to read taxonomies.
69 * @param WP_REST_Request $request Full details about the request.
70 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
72 public function get_items_permissions_check( $request ) {
73 if ( 'edit' === $request['context'] ) {
74 if ( ! empty( $request['type'] ) ) {
75 $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
77 $taxonomies = get_taxonomies( '', 'objects' );
79 foreach ( $taxonomies as $taxonomy ) {
80 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->manage_terms ) ) {
84 return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
90 * Retrieves all public taxonomies.
95 * @param WP_REST_Request $request Full details about the request.
96 * @return WP_REST_Response Response object on success, or WP_Error object on failure.
98 public function get_items( $request ) {
100 // Retrieve the list of registered collection query parameters.
101 $registered = $this->get_collection_params();
103 if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
104 $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
106 $taxonomies = get_taxonomies( '', 'objects' );
109 foreach ( $taxonomies as $tax_type => $value ) {
110 if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->manage_terms ) ) ) {
113 $tax = $this->prepare_item_for_response( $value, $request );
114 $tax = $this->prepare_response_for_collection( $tax );
115 $data[ $tax_type ] = $tax;
118 if ( empty( $data ) ) {
119 // Response should still be returned as a JSON object when it is empty.
120 $data = (object) $data;
123 return rest_ensure_response( $data );
127 * Checks if a given request has access to a taxonomy.
132 * @param WP_REST_Request $request Full details about the request.
133 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
135 public function get_item_permissions_check( $request ) {
137 $tax_obj = get_taxonomy( $request['taxonomy'] );
140 if ( empty( $tax_obj->show_in_rest ) ) {
143 if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->manage_terms ) ) {
144 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
152 * Retrieves a specific taxonomy.
157 * @param WP_REST_Request $request Full details about the request.
158 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
160 public function get_item( $request ) {
161 $tax_obj = get_taxonomy( $request['taxonomy'] );
162 if ( empty( $tax_obj ) ) {
163 return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) );
165 $data = $this->prepare_item_for_response( $tax_obj, $request );
166 return rest_ensure_response( $data );
170 * Prepares a taxonomy object for serialization.
175 * @param stdClass $taxonomy Taxonomy data.
176 * @param WP_REST_Request $request Full details about the request.
177 * @return WP_REST_Response Response object.
179 public function prepare_item_for_response( $taxonomy, $request ) {
180 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
182 'name' => $taxonomy->label,
183 'slug' => $taxonomy->name,
184 'capabilities' => $taxonomy->cap,
185 'description' => $taxonomy->description,
186 'labels' => $taxonomy->labels,
187 'types' => $taxonomy->object_type,
188 'show_cloud' => $taxonomy->show_tagcloud,
189 'hierarchical' => $taxonomy->hierarchical,
190 'rest_base' => $base,
193 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
194 $data = $this->add_additional_fields_to_object( $data, $request );
195 $data = $this->filter_response_by_context( $data, $context );
197 // Wrap the data in a response object.
198 $response = rest_ensure_response( $data );
200 $response->add_links( array(
201 'collection' => array(
202 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
204 'https://api.w.org/items' => array(
205 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
210 * Filters a taxonomy returned from the REST API.
212 * Allows modification of the taxonomy data right before it is returned.
216 * @param WP_REST_Response $response The response object.
217 * @param object $item The original taxonomy object.
218 * @param WP_REST_Request $request Request used to generate the response.
220 return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
224 * Retrieves the taxonomy's schema, conforming to JSON Schema.
229 * @return array Item schema data.
231 public function get_item_schema() {
233 '$schema' => 'http://json-schema.org/schema#',
234 'title' => 'taxonomy',
236 'properties' => array(
237 'capabilities' => array(
238 'description' => __( 'All capabilities used by the taxonomy.' ),
240 'context' => array( 'edit' ),
243 'description' => array(
244 'description' => __( 'A human-readable description of the taxonomy.' ),
246 'context' => array( 'view', 'edit' ),
249 'hierarchical' => array(
250 'description' => __( 'Whether or not the taxonomy should have children.' ),
252 'context' => array( 'view', 'edit' ),
256 'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
258 'context' => array( 'edit' ),
262 'description' => __( 'The title for the taxonomy.' ),
264 'context' => array( 'view', 'edit', 'embed' ),
268 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
270 'context' => array( 'view', 'edit', 'embed' ),
273 'show_cloud' => array(
274 'description' => __( 'Whether or not the term cloud should be displayed.' ),
276 'context' => array( 'edit' ),
280 'description' => __( 'Types associated with the taxonomy.' ),
285 'context' => array( 'view', 'edit' ),
288 'rest_base' => array(
289 'description' => __( 'REST base route for the taxonomy.' ),
291 'context' => array( 'view', 'edit', 'embed' ),
296 return $this->add_additional_fields_schema( $schema );
300 * Retrieves the query params for collections.
305 * @return array Collection parameters.
307 public function get_collection_params() {
308 $new_params = array();
309 $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
310 $new_params['type'] = array(
311 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),