9
|
1 |
<?php |
|
2 |
/** |
|
3 |
* REST API: WP_REST_Themes_Controller class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage REST_API |
|
7 |
* @since 5.0.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
|
11 |
* Core class used to manage themes via the REST API. |
|
12 |
* |
|
13 |
* @since 5.0.0 |
|
14 |
* |
|
15 |
* @see WP_REST_Controller |
|
16 |
*/ |
|
17 |
class WP_REST_Themes_Controller extends WP_REST_Controller { |
|
18 |
|
|
19 |
/** |
|
20 |
* Constructor. |
|
21 |
* |
|
22 |
* @since 5.0.0 |
|
23 |
*/ |
|
24 |
public function __construct() { |
|
25 |
$this->namespace = 'wp/v2'; |
|
26 |
$this->rest_base = 'themes'; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* Registers the routes for the objects of the controller. |
|
31 |
* |
|
32 |
* @since 5.0.0 |
|
33 |
* |
|
34 |
* @see register_rest_route() |
|
35 |
*/ |
|
36 |
public function register_routes() { |
|
37 |
register_rest_route( |
|
38 |
$this->namespace, |
|
39 |
'/' . $this->rest_base, |
|
40 |
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_item_schema' ), |
|
48 |
) |
|
49 |
); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* Checks if a given request has access to read the theme. |
|
54 |
* |
|
55 |
* @since 5.0.0 |
|
56 |
* |
|
57 |
* @param WP_REST_Request $request Full details about the request. |
|
58 |
* @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object. |
|
59 |
*/ |
|
60 |
public function get_items_permissions_check( $request ) { |
16
|
61 |
if ( current_user_can( 'edit_posts' ) ) { |
|
62 |
return true; |
9
|
63 |
} |
|
64 |
|
16
|
65 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
|
66 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
|
67 |
return true; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
return new WP_Error( |
|
72 |
'rest_user_cannot_view', |
|
73 |
__( 'Sorry, you are not allowed to view themes.' ), |
|
74 |
array( 'status' => rest_authorization_required_code() ) |
|
75 |
); |
9
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Retrieves a collection of themes. |
|
80 |
* |
|
81 |
* @since 5.0.0 |
|
82 |
* |
|
83 |
* @param WP_REST_Request $request Full details about the request. |
|
84 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
85 |
*/ |
|
86 |
public function get_items( $request ) { |
|
87 |
// Retrieve the list of registered collection query parameters. |
|
88 |
$registered = $this->get_collection_params(); |
|
89 |
$themes = array(); |
|
90 |
|
|
91 |
if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) { |
|
92 |
$active_theme = wp_get_theme(); |
|
93 |
$active_theme = $this->prepare_item_for_response( $active_theme, $request ); |
|
94 |
$themes[] = $this->prepare_response_for_collection( $active_theme ); |
|
95 |
} |
|
96 |
|
|
97 |
$response = rest_ensure_response( $themes ); |
|
98 |
|
|
99 |
$response->header( 'X-WP-Total', count( $themes ) ); |
|
100 |
$response->header( 'X-WP-TotalPages', count( $themes ) ); |
|
101 |
|
|
102 |
return $response; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Prepares a single theme output for response. |
|
107 |
* |
|
108 |
* @since 5.0.0 |
|
109 |
* |
|
110 |
* @param WP_Theme $theme Theme object. |
|
111 |
* @param WP_REST_Request $request Request object. |
|
112 |
* @return WP_REST_Response Response object. |
|
113 |
*/ |
|
114 |
public function prepare_item_for_response( $theme, $request ) { |
|
115 |
$data = array(); |
|
116 |
$fields = $this->get_fields_for_response( $request ); |
|
117 |
|
16
|
118 |
if ( rest_is_field_included( 'stylesheet', $fields ) ) { |
|
119 |
$data['stylesheet'] = $theme->get_stylesheet(); |
|
120 |
} |
|
121 |
|
|
122 |
if ( rest_is_field_included( 'template', $fields ) ) { |
|
123 |
/** |
|
124 |
* Use the get_template() method, not the 'Template' header, for finding the template. |
|
125 |
* The 'Template' header is only good for what was written in the style.css, while |
|
126 |
* get_template() takes into account where WordPress actually located the theme and |
|
127 |
* whether it is actually valid. |
|
128 |
*/ |
|
129 |
$data['template'] = $theme->get_template(); |
|
130 |
} |
|
131 |
|
|
132 |
$plain_field_mappings = array( |
|
133 |
'requires_php' => 'RequiresPHP', |
|
134 |
'requires_wp' => 'RequiresWP', |
|
135 |
'textdomain' => 'TextDomain', |
|
136 |
'version' => 'Version', |
|
137 |
); |
|
138 |
|
|
139 |
foreach ( $plain_field_mappings as $field => $header ) { |
|
140 |
if ( rest_is_field_included( $field, $fields ) ) { |
|
141 |
$data[ $field ] = $theme->get( $header ); |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
if ( rest_is_field_included( 'screenshot', $fields ) ) { |
|
146 |
// Using $theme->get_screenshot() with no args to get absolute URL. |
|
147 |
$data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : ''; |
|
148 |
} |
|
149 |
|
|
150 |
$rich_field_mappings = array( |
|
151 |
'author' => 'Author', |
|
152 |
'author_uri' => 'AuthorURI', |
|
153 |
'description' => 'Description', |
|
154 |
'name' => 'Name', |
|
155 |
'tags' => 'Tags', |
|
156 |
'theme_uri' => 'ThemeURI', |
|
157 |
); |
9
|
158 |
|
16
|
159 |
foreach ( $rich_field_mappings as $field => $header ) { |
|
160 |
if ( rest_is_field_included( "{$field}.raw", $fields ) ) { |
|
161 |
$data[ $field ]['raw'] = $theme->display( $header, false, true ); |
|
162 |
} |
|
163 |
|
|
164 |
if ( rest_is_field_included( "{$field}.rendered", $fields ) ) { |
|
165 |
$data[ $field ]['rendered'] = $theme->display( $header ); |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
if ( rest_is_field_included( 'theme_supports', $fields ) ) { |
|
170 |
foreach ( get_registered_theme_features() as $feature => $config ) { |
|
171 |
if ( ! is_array( $config['show_in_rest'] ) ) { |
|
172 |
continue; |
|
173 |
} |
|
174 |
|
|
175 |
$name = $config['show_in_rest']['name']; |
9
|
176 |
|
16
|
177 |
if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) { |
|
178 |
continue; |
|
179 |
} |
|
180 |
|
|
181 |
if ( ! current_theme_supports( $feature ) ) { |
|
182 |
$data['theme_supports'][ $name ] = $config['show_in_rest']['schema']['default']; |
|
183 |
continue; |
|
184 |
} |
|
185 |
|
|
186 |
$support = get_theme_support( $feature ); |
|
187 |
|
|
188 |
if ( isset( $config['show_in_rest']['prepare_callback'] ) ) { |
|
189 |
$prepare = $config['show_in_rest']['prepare_callback']; |
|
190 |
} else { |
|
191 |
$prepare = array( $this, 'prepare_theme_support' ); |
|
192 |
} |
|
193 |
|
|
194 |
$prepared = $prepare( $support, $config, $feature, $request ); |
|
195 |
|
|
196 |
if ( is_wp_error( $prepared ) ) { |
|
197 |
continue; |
|
198 |
} |
|
199 |
|
|
200 |
$data['theme_supports'][ $name ] = $prepared; |
9
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
205 |
|
|
206 |
// Wrap the data in a response object. |
|
207 |
$response = rest_ensure_response( $data ); |
|
208 |
|
|
209 |
/** |
|
210 |
* Filters theme data returned from the REST API. |
|
211 |
* |
|
212 |
* @since 5.0.0 |
|
213 |
* |
|
214 |
* @param WP_REST_Response $response The response object. |
|
215 |
* @param WP_Theme $theme Theme object used to create response. |
|
216 |
* @param WP_REST_Request $request Request object. |
|
217 |
*/ |
|
218 |
return apply_filters( 'rest_prepare_theme', $response, $theme, $request ); |
|
219 |
} |
|
220 |
|
|
221 |
/** |
16
|
222 |
* Prepares the theme support value for inclusion in the REST API response. |
|
223 |
* |
|
224 |
* @since 5.5.0 |
|
225 |
* |
|
226 |
* @param mixed $support The raw value from get_theme_support(). |
|
227 |
* @param array $args The feature's registration args. |
|
228 |
* @param string $feature The feature name. |
|
229 |
* @param WP_REST_Request $request The request object. |
|
230 |
* @return mixed The prepared support value. |
|
231 |
*/ |
|
232 |
protected function prepare_theme_support( $support, $args, $feature, $request ) { |
|
233 |
$schema = $args['show_in_rest']['schema']; |
|
234 |
|
|
235 |
if ( 'boolean' === $schema['type'] ) { |
|
236 |
return true; |
|
237 |
} |
|
238 |
|
|
239 |
if ( is_array( $support ) && ! $args['variadic'] ) { |
|
240 |
$support = $support[0]; |
|
241 |
} |
|
242 |
|
|
243 |
return rest_sanitize_value_from_schema( $support, $schema ); |
|
244 |
} |
|
245 |
|
|
246 |
/** |
9
|
247 |
* Retrieves the theme's schema, conforming to JSON Schema. |
|
248 |
* |
|
249 |
* @since 5.0.0 |
|
250 |
* |
|
251 |
* @return array Item schema data. |
|
252 |
*/ |
|
253 |
public function get_item_schema() { |
16
|
254 |
if ( $this->schema ) { |
|
255 |
return $this->add_additional_fields_schema( $this->schema ); |
|
256 |
} |
|
257 |
|
9
|
258 |
$schema = array( |
|
259 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
260 |
'title' => 'theme', |
|
261 |
'type' => 'object', |
|
262 |
'properties' => array( |
16
|
263 |
'stylesheet' => array( |
|
264 |
'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ), |
|
265 |
'type' => 'string', |
|
266 |
'readonly' => true, |
|
267 |
), |
|
268 |
'template' => array( |
|
269 |
'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ), |
|
270 |
'type' => 'string', |
|
271 |
'readonly' => true, |
|
272 |
), |
|
273 |
'author' => array( |
|
274 |
'description' => __( 'The theme author.' ), |
|
275 |
'type' => 'object', |
|
276 |
'readonly' => true, |
|
277 |
'properties' => array( |
|
278 |
'raw' => array( |
|
279 |
'description' => __( 'The theme author\'s name, as found in the theme header.' ), |
|
280 |
'type' => 'string', |
|
281 |
), |
|
282 |
'rendered' => array( |
|
283 |
'description' => __( 'HTML for the theme author, transformed for display.' ), |
|
284 |
'type' => 'string', |
|
285 |
), |
|
286 |
), |
|
287 |
), |
|
288 |
'author_uri' => array( |
|
289 |
'description' => __( 'The website of the theme author.' ), |
|
290 |
'type' => 'object', |
|
291 |
'readonly' => true, |
|
292 |
'properties' => array( |
|
293 |
'raw' => array( |
|
294 |
'description' => __( 'The website of the theme author, as found in the theme header.' ), |
|
295 |
'type' => 'string', |
|
296 |
'format' => 'uri', |
|
297 |
), |
|
298 |
'rendered' => array( |
|
299 |
'description' => __( 'The website of the theme author, transformed for display.' ), |
|
300 |
'type' => 'string', |
|
301 |
'format' => 'uri', |
|
302 |
), |
|
303 |
), |
|
304 |
), |
|
305 |
'description' => array( |
|
306 |
'description' => __( 'A description of the theme.' ), |
|
307 |
'type' => 'object', |
|
308 |
'readonly' => true, |
|
309 |
'properties' => array( |
|
310 |
'raw' => array( |
|
311 |
'description' => __( 'The theme description, as found in the theme header.' ), |
|
312 |
'type' => 'string', |
|
313 |
), |
|
314 |
'rendered' => array( |
|
315 |
'description' => __( 'The theme description, transformed for display.' ), |
|
316 |
'type' => 'string', |
|
317 |
), |
|
318 |
), |
|
319 |
), |
|
320 |
'name' => array( |
|
321 |
'description' => __( 'The name of the theme.' ), |
|
322 |
'type' => 'object', |
9
|
323 |
'readonly' => true, |
|
324 |
'properties' => array( |
16
|
325 |
'raw' => array( |
|
326 |
'description' => __( 'The theme name, as found in the theme header.' ), |
|
327 |
'type' => 'string', |
9
|
328 |
), |
16
|
329 |
'rendered' => array( |
|
330 |
'description' => __( 'The theme name, transformed for display.' ), |
|
331 |
'type' => 'string', |
9
|
332 |
), |
|
333 |
), |
|
334 |
), |
16
|
335 |
'requires_php' => array( |
|
336 |
'description' => __( 'The minimum PHP version required for the theme to work.' ), |
|
337 |
'type' => 'string', |
|
338 |
'readonly' => true, |
|
339 |
), |
|
340 |
'requires_wp' => array( |
|
341 |
'description' => __( 'The minimum WordPress version required for the theme to work.' ), |
|
342 |
'type' => 'string', |
|
343 |
'readonly' => true, |
|
344 |
), |
|
345 |
'screenshot' => array( |
|
346 |
'description' => __( 'The theme\'s screenshot URL.' ), |
|
347 |
'type' => 'string', |
|
348 |
'format' => 'uri', |
|
349 |
'readonly' => true, |
|
350 |
), |
|
351 |
'tags' => array( |
|
352 |
'description' => __( 'Tags indicating styles and features of the theme.' ), |
|
353 |
'type' => 'object', |
|
354 |
'readonly' => true, |
|
355 |
'properties' => array( |
|
356 |
'raw' => array( |
|
357 |
'description' => __( 'The theme tags, as found in the theme header.' ), |
|
358 |
'type' => 'array', |
|
359 |
'items' => array( |
|
360 |
'type' => 'string', |
|
361 |
), |
|
362 |
), |
|
363 |
'rendered' => array( |
|
364 |
'description' => __( 'The theme tags, transformed for display.' ), |
|
365 |
'type' => 'string', |
|
366 |
), |
|
367 |
), |
|
368 |
), |
|
369 |
'textdomain' => array( |
|
370 |
'description' => __( 'The theme\'s text domain.' ), |
|
371 |
'type' => 'string', |
|
372 |
'readonly' => true, |
|
373 |
), |
|
374 |
'theme_supports' => array( |
|
375 |
'description' => __( 'Features supported by this theme.' ), |
|
376 |
'type' => 'object', |
|
377 |
'readonly' => true, |
|
378 |
'properties' => array(), |
|
379 |
), |
|
380 |
'theme_uri' => array( |
|
381 |
'description' => __( 'The URI of the theme\'s webpage.' ), |
|
382 |
'type' => 'object', |
|
383 |
'readonly' => true, |
|
384 |
'properties' => array( |
|
385 |
'raw' => array( |
|
386 |
'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ), |
|
387 |
'type' => 'string', |
|
388 |
'format' => 'uri', |
|
389 |
), |
|
390 |
'rendered' => array( |
|
391 |
'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ), |
|
392 |
'type' => 'string', |
|
393 |
'format' => 'uri', |
|
394 |
), |
|
395 |
), |
|
396 |
), |
|
397 |
'version' => array( |
|
398 |
'description' => __( 'The theme\'s current version.' ), |
|
399 |
'type' => 'string', |
|
400 |
'readonly' => true, |
|
401 |
), |
9
|
402 |
), |
|
403 |
); |
|
404 |
|
16
|
405 |
foreach ( get_registered_theme_features() as $feature => $config ) { |
|
406 |
if ( ! is_array( $config['show_in_rest'] ) ) { |
|
407 |
continue; |
|
408 |
} |
|
409 |
|
|
410 |
$name = $config['show_in_rest']['name']; |
|
411 |
|
|
412 |
$schema['properties']['theme_supports']['properties'][ $name ] = $config['show_in_rest']['schema']; |
|
413 |
} |
|
414 |
|
|
415 |
$this->schema = $schema; |
|
416 |
|
|
417 |
return $this->add_additional_fields_schema( $this->schema ); |
9
|
418 |
} |
|
419 |
|
|
420 |
/** |
|
421 |
* Retrieves the search params for the themes collection. |
|
422 |
* |
|
423 |
* @since 5.0.0 |
|
424 |
* |
|
425 |
* @return array Collection parameters. |
|
426 |
*/ |
|
427 |
public function get_collection_params() { |
|
428 |
$query_params = parent::get_collection_params(); |
|
429 |
|
|
430 |
$query_params['status'] = array( |
|
431 |
'description' => __( 'Limit result set to themes assigned one or more statuses.' ), |
|
432 |
'type' => 'array', |
|
433 |
'items' => array( |
|
434 |
'enum' => array( 'active' ), |
|
435 |
'type' => 'string', |
|
436 |
), |
|
437 |
'required' => true, |
|
438 |
'sanitize_callback' => array( $this, 'sanitize_theme_status' ), |
|
439 |
); |
|
440 |
|
|
441 |
/** |
|
442 |
* Filter collection parameters for the themes controller. |
|
443 |
* |
|
444 |
* @since 5.0.0 |
|
445 |
* |
16
|
446 |
* @param array $query_params JSON Schema-formatted collection parameters. |
9
|
447 |
*/ |
|
448 |
return apply_filters( 'rest_themes_collection_params', $query_params ); |
|
449 |
} |
|
450 |
|
|
451 |
/** |
|
452 |
* Sanitizes and validates the list of theme status. |
|
453 |
* |
|
454 |
* @since 5.0.0 |
|
455 |
* |
16
|
456 |
* @param string|array $statuses One or more theme statuses. |
|
457 |
* @param WP_REST_Request $request Full details about the request. |
|
458 |
* @param string $parameter Additional parameter to pass to validation. |
9
|
459 |
* @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
|
460 |
*/ |
|
461 |
public function sanitize_theme_status( $statuses, $request, $parameter ) { |
|
462 |
$statuses = wp_parse_slug_list( $statuses ); |
|
463 |
|
|
464 |
foreach ( $statuses as $status ) { |
|
465 |
$result = rest_validate_request_arg( $status, $request, $parameter ); |
|
466 |
|
|
467 |
if ( is_wp_error( $result ) ) { |
|
468 |
return $result; |
|
469 |
} |
|
470 |
} |
|
471 |
|
|
472 |
return $statuses; |
|
473 |
} |
|
474 |
} |