]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
WordPress 4.7.2
[autoinstalls/wordpress.git] / wp-includes / rest-api / endpoints / class-wp-rest-taxonomies-controller.php
1 <?php
2 /**
3  * REST API: WP_REST_Taxonomies_Controller class
4  *
5  * @package WordPress
6  * @subpackage REST_API
7  * @since 4.7.0
8  */
9
10 /**
11  * Core class used to manage taxonomies via the REST API.
12  *
13  * @since 4.7.0
14  *
15  * @see WP_REST_Controller
16  */
17 class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
18
19         /**
20          * Constructor.
21          *
22          * @since 4.7.0
23          * @access public
24          */
25         public function __construct() {
26                 $this->namespace = 'wp/v2';
27                 $this->rest_base = 'taxonomies';
28         }
29
30         /**
31          * Registers the routes for the objects of the controller.
32          *
33          * @since 4.7.0
34          * @access public
35          *
36          * @see register_rest_route()
37          */
38         public function register_routes() {
39
40                 register_rest_route( $this->namespace, '/' . $this->rest_base, array(
41                         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(),
46                         ),
47                         'schema' => array( $this, 'get_public_item_schema' ),
48                 ) );
49
50                 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<taxonomy>[\w-]+)', array(
51                         'args' => array(
52                                 'taxonomy' => array(
53                                         'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
54                                         'type'         => 'string',
55                                 ),
56                         ),
57                         array(
58                                 'methods'         => WP_REST_Server::READABLE,
59                                 'callback'        => array( $this, 'get_item' ),
60                                 'permission_callback' => array( $this, 'get_item_permissions_check' ),
61                                 'args'            => array(
62                                         'context'     => $this->get_context_param( array( 'default' => 'view' ) ),
63                                 ),
64                         ),
65                         'schema' => array( $this, 'get_public_item_schema' ),
66                 ) );
67         }
68
69         /**
70          * Checks whether a given request has permission to read taxonomies.
71          *
72          * @since 4.7.0
73          * @access public
74          *
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.
77          */
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' );
82                         } else {
83                                 $taxonomies = get_taxonomies( '', 'objects' );
84                         }
85                         foreach ( $taxonomies as $taxonomy ) {
86                                 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->manage_terms ) ) {
87                                         return true;
88                                 }
89                         }
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() ) );
91                 }
92                 return true;
93         }
94
95         /**
96          * Retrieves all public taxonomies.
97          *
98          * @since 4.7.0
99          * @access public
100          *
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.
103          */
104         public function get_items( $request ) {
105
106                 // Retrieve the list of registered collection query parameters.
107                 $registered = $this->get_collection_params();
108
109                 if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
110                         $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
111                 } else {
112                         $taxonomies = get_taxonomies( '', 'objects' );
113                 }
114                 $data = array();
115                 foreach ( $taxonomies as $tax_type => $value ) {
116                         if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->manage_terms ) ) ) {
117                                 continue;
118                         }
119                         $tax = $this->prepare_item_for_response( $value, $request );
120                         $tax = $this->prepare_response_for_collection( $tax );
121                         $data[ $tax_type ] = $tax;
122                 }
123
124                 if ( empty( $data ) ) {
125                         // Response should still be returned as a JSON object when it is empty.
126                         $data = (object) $data;
127                 }
128
129                 return rest_ensure_response( $data );
130         }
131
132         /**
133          * Checks if a given request has access to a taxonomy.
134          *
135          * @since 4.7.0
136          * @access public
137          *
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.
140          */
141         public function get_item_permissions_check( $request ) {
142
143                 $tax_obj = get_taxonomy( $request['taxonomy'] );
144
145                 if ( $tax_obj ) {
146                         if ( empty( $tax_obj->show_in_rest ) ) {
147                                 return false;
148                         }
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() ) );
151                         }
152                 }
153
154                 return true;
155         }
156
157         /**
158          * Retrieves a specific taxonomy.
159          *
160          * @since 4.7.0
161          * @access public
162          *
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.
165          */
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 ) );
170                 }
171                 $data = $this->prepare_item_for_response( $tax_obj, $request );
172                 return rest_ensure_response( $data );
173         }
174
175         /**
176          * Prepares a taxonomy object for serialization.
177          *
178          * @since 4.7.0
179          * @access public
180          *
181          * @param stdClass        $taxonomy Taxonomy data.
182          * @param WP_REST_Request $request  Full details about the request.
183          * @return WP_REST_Response Response object.
184          */
185         public function prepare_item_for_response( $taxonomy, $request ) {
186                 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
187                 $data = array(
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,
197                 );
198
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 );
202
203                 // Wrap the data in a response object.
204                 $response = rest_ensure_response( $data );
205
206                 $response->add_links( array(
207                         'collection'                => array(
208                                 'href'                  => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
209                         ),
210                         'https://api.w.org/items'   => array(
211                                 'href'                  => rest_url( sprintf( 'wp/v2/%s', $base ) ),
212                         ),
213                 ) );
214
215                 /**
216                  * Filters a taxonomy returned from the REST API.
217                  *
218                  * Allows modification of the taxonomy data right before it is returned.
219                  *
220                  * @since 4.7.0
221                  *
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.
225                  */
226                 return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
227         }
228
229         /**
230          * Retrieves the taxonomy's schema, conforming to JSON Schema.
231          *
232          * @since 4.7.0
233          * @access public
234          *
235          * @return array Item schema data.
236          */
237         public function get_item_schema() {
238                 $schema = array(
239                         '$schema'              => 'http://json-schema.org/schema#',
240                         'title'                => 'taxonomy',
241                         'type'                 => 'object',
242                         'properties'           => array(
243                                 'capabilities'     => array(
244                                         'description'  => __( 'All capabilities used by the taxonomy.' ),
245                                         'type'         => 'object',
246                                         'context'      => array( 'edit' ),
247                                         'readonly'     => true,
248                                 ),
249                                 'description'      => array(
250                                         'description'  => __( 'A human-readable description of the taxonomy.' ),
251                                         'type'         => 'string',
252                                         'context'      => array( 'view', 'edit' ),
253                                         'readonly'     => true,
254                                 ),
255                                 'hierarchical'     => array(
256                                         'description'  => __( 'Whether or not the taxonomy should have children.' ),
257                                         'type'         => 'boolean',
258                                         'context'      => array( 'view', 'edit' ),
259                                         'readonly'     => true,
260                                 ),
261                                 'labels'           => array(
262                                         'description'  => __( 'Human-readable labels for the taxonomy for various contexts.' ),
263                                         'type'         => 'object',
264                                         'context'      => array( 'edit' ),
265                                         'readonly'     => true,
266                                 ),
267                                 'name'             => array(
268                                         'description'  => __( 'The title for the taxonomy.' ),
269                                         'type'         => 'string',
270                                         'context'      => array( 'view', 'edit', 'embed' ),
271                                         'readonly'     => true,
272                                 ),
273                                 'slug'             => array(
274                                         'description'  => __( 'An alphanumeric identifier for the taxonomy.' ),
275                                         'type'         => 'string',
276                                         'context'      => array( 'view', 'edit', 'embed' ),
277                                         'readonly'     => true,
278                                 ),
279                                 'show_cloud'       => array(
280                                         'description'  => __( 'Whether or not the term cloud should be displayed.' ),
281                                         'type'         => 'boolean',
282                                         'context'      => array( 'edit' ),
283                                         'readonly'     => true,
284                                 ),
285                                 'types'            => array(
286                                         'description'  => __( 'Types associated with the taxonomy.' ),
287                                         'type'         => 'array',
288                                         'items'        => array(
289                                                 'type' => 'string',
290                                         ),
291                                         'context'      => array( 'view', 'edit' ),
292                                         'readonly'     => true,
293                                 ),
294                                 'rest_base'            => array(
295                                         'description'  => __( 'REST base route for the taxonomy.' ),
296                                         'type'         => 'string',
297                                         'context'      => array( 'view', 'edit', 'embed' ),
298                                         'readonly'     => true,
299                                 ),
300                         ),
301                 );
302                 return $this->add_additional_fields_schema( $schema );
303         }
304
305         /**
306          * Retrieves the query params for collections.
307          *
308          * @since 4.7.0
309          * @access public
310          *
311          * @return array Collection parameters.
312          */
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.' ),
318                         'type'         => 'string',
319                 );
320                 return $new_params;
321         }
322
323 }