85 if ( ! empty( $request['type'] ) ) { |
85 if ( ! empty( $request['type'] ) ) { |
86 $taxonomies = get_object_taxonomies( $request['type'], 'objects' ); |
86 $taxonomies = get_object_taxonomies( $request['type'], 'objects' ); |
87 } else { |
87 } else { |
88 $taxonomies = get_taxonomies( '', 'objects' ); |
88 $taxonomies = get_taxonomies( '', 'objects' ); |
89 } |
89 } |
|
90 |
90 foreach ( $taxonomies as $taxonomy ) { |
91 foreach ( $taxonomies as $taxonomy ) { |
91 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) { |
92 if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) { |
92 return true; |
93 return true; |
93 } |
94 } |
94 } |
95 } |
95 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 |
96 } |
97 return new WP_Error( |
|
98 'rest_cannot_view', |
|
99 __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), |
|
100 array( 'status' => rest_authorization_required_code() ) |
|
101 ); |
|
102 } |
|
103 |
97 return true; |
104 return true; |
98 } |
105 } |
99 |
106 |
100 /** |
107 /** |
101 * Retrieves all public taxonomies. |
108 * Retrieves all public taxonomies. |
113 if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) { |
120 if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) { |
114 $taxonomies = get_object_taxonomies( $request['type'], 'objects' ); |
121 $taxonomies = get_object_taxonomies( $request['type'], 'objects' ); |
115 } else { |
122 } else { |
116 $taxonomies = get_taxonomies( '', 'objects' ); |
123 $taxonomies = get_taxonomies( '', 'objects' ); |
117 } |
124 } |
|
125 |
118 $data = array(); |
126 $data = array(); |
|
127 |
119 foreach ( $taxonomies as $tax_type => $value ) { |
128 foreach ( $taxonomies as $tax_type => $value ) { |
120 if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) { |
129 if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) { |
121 continue; |
130 continue; |
122 } |
131 } |
|
132 |
123 $tax = $this->prepare_item_for_response( $value, $request ); |
133 $tax = $this->prepare_item_for_response( $value, $request ); |
124 $tax = $this->prepare_response_for_collection( $tax ); |
134 $tax = $this->prepare_response_for_collection( $tax ); |
125 $data[ $tax_type ] = $tax; |
135 $data[ $tax_type ] = $tax; |
126 } |
136 } |
127 |
137 |
136 /** |
146 /** |
137 * Checks if a given request has access to a taxonomy. |
147 * Checks if a given request has access to a taxonomy. |
138 * |
148 * |
139 * @since 4.7.0 |
149 * @since 4.7.0 |
140 * |
150 * |
141 * @param WP_REST_Request $request Full details about the request. |
151 * @param WP_REST_Request $request Full details about the request. |
142 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object. |
152 * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object. |
143 */ |
153 */ |
144 public function get_item_permissions_check( $request ) { |
154 public function get_item_permissions_check( $request ) { |
145 |
155 |
146 $tax_obj = get_taxonomy( $request['taxonomy'] ); |
156 $tax_obj = get_taxonomy( $request['taxonomy'] ); |
147 |
157 |
148 if ( $tax_obj ) { |
158 if ( $tax_obj ) { |
149 if ( empty( $tax_obj->show_in_rest ) ) { |
159 if ( empty( $tax_obj->show_in_rest ) ) { |
150 return false; |
160 return false; |
151 } |
161 } |
|
162 |
152 if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) { |
163 if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) { |
153 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) ); |
164 return new WP_Error( |
|
165 'rest_forbidden_context', |
|
166 __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), |
|
167 array( 'status' => rest_authorization_required_code() ) |
|
168 ); |
154 } |
169 } |
155 } |
170 } |
156 |
171 |
157 return true; |
172 return true; |
158 } |
173 } |
165 * @param WP_REST_Request $request Full details about the request. |
180 * @param WP_REST_Request $request Full details about the request. |
166 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
181 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
167 */ |
182 */ |
168 public function get_item( $request ) { |
183 public function get_item( $request ) { |
169 $tax_obj = get_taxonomy( $request['taxonomy'] ); |
184 $tax_obj = get_taxonomy( $request['taxonomy'] ); |
|
185 |
170 if ( empty( $tax_obj ) ) { |
186 if ( empty( $tax_obj ) ) { |
171 return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) ); |
187 return new WP_Error( |
172 } |
188 'rest_taxonomy_invalid', |
|
189 __( 'Invalid taxonomy.' ), |
|
190 array( 'status' => 404 ) |
|
191 ); |
|
192 } |
|
193 |
173 $data = $this->prepare_item_for_response( $tax_obj, $request ); |
194 $data = $this->prepare_item_for_response( $tax_obj, $request ); |
|
195 |
174 return rest_ensure_response( $data ); |
196 return rest_ensure_response( $data ); |
175 } |
197 } |
176 |
198 |
177 /** |
199 /** |
178 * Prepares a taxonomy object for serialization. |
200 * Prepares a taxonomy object for serialization. |
179 * |
201 * |
180 * @since 4.7.0 |
202 * @since 4.7.0 |
181 * |
203 * |
182 * @param stdClass $taxonomy Taxonomy data. |
204 * @param WP_Taxonomy $taxonomy Taxonomy data. |
183 * @param WP_REST_Request $request Full details about the request. |
205 * @param WP_REST_Request $request Full details about the request. |
184 * @return WP_REST_Response Response object. |
206 * @return WP_REST_Response Response object. |
185 */ |
207 */ |
186 public function prepare_item_for_response( $taxonomy, $request ) { |
208 public function prepare_item_for_response( $taxonomy, $request ) { |
187 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; |
209 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; |
208 if ( in_array( 'labels', $fields, true ) ) { |
230 if ( in_array( 'labels', $fields, true ) ) { |
209 $data['labels'] = $taxonomy->labels; |
231 $data['labels'] = $taxonomy->labels; |
210 } |
232 } |
211 |
233 |
212 if ( in_array( 'types', $fields, true ) ) { |
234 if ( in_array( 'types', $fields, true ) ) { |
213 $data['types'] = $taxonomy->object_type; |
235 $data['types'] = array_values( $taxonomy->object_type ); |
214 } |
236 } |
215 |
237 |
216 if ( in_array( 'show_cloud', $fields, true ) ) { |
238 if ( in_array( 'show_cloud', $fields, true ) ) { |
217 $data['show_cloud'] = $taxonomy->show_tagcloud; |
239 $data['show_cloud'] = $taxonomy->show_tagcloud; |
218 } |
240 } |
260 * Allows modification of the taxonomy data right before it is returned. |
282 * Allows modification of the taxonomy data right before it is returned. |
261 * |
283 * |
262 * @since 4.7.0 |
284 * @since 4.7.0 |
263 * |
285 * |
264 * @param WP_REST_Response $response The response object. |
286 * @param WP_REST_Response $response The response object. |
265 * @param object $item The original taxonomy object. |
287 * @param WP_Taxonomy $item The original taxonomy object. |
266 * @param WP_REST_Request $request Request used to generate the response. |
288 * @param WP_REST_Request $request Request used to generate the response. |
267 */ |
289 */ |
268 return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request ); |
290 return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request ); |
269 } |
291 } |
270 |
292 |
274 * @since 4.7.0 |
296 * @since 4.7.0 |
275 * |
297 * |
276 * @return array Item schema data. |
298 * @return array Item schema data. |
277 */ |
299 */ |
278 public function get_item_schema() { |
300 public function get_item_schema() { |
|
301 if ( $this->schema ) { |
|
302 return $this->add_additional_fields_schema( $this->schema ); |
|
303 } |
|
304 |
279 $schema = array( |
305 $schema = array( |
280 '$schema' => 'http://json-schema.org/draft-04/schema#', |
306 '$schema' => 'http://json-schema.org/draft-04/schema#', |
281 'title' => 'taxonomy', |
307 'title' => 'taxonomy', |
282 'type' => 'object', |
308 'type' => 'object', |
283 'properties' => array( |
309 'properties' => array( |