116 * Checks if a given request has access to read the theme. |
116 * Checks if a given request has access to read the theme. |
117 * |
117 * |
118 * @since 5.7.0 |
118 * @since 5.7.0 |
119 * |
119 * |
120 * @param WP_REST_Request $request Full details about the request. |
120 * @param WP_REST_Request $request Full details about the request. |
121 * @return bool|WP_Error True if the request has read access for the item, otherwise WP_Error object. |
121 * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object. |
122 */ |
122 */ |
123 public function get_item_permissions_check( $request ) { |
123 public function get_item_permissions_check( $request ) { |
124 if ( current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' ) ) { |
124 if ( current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' ) ) { |
125 return true; |
125 return true; |
126 } |
126 } |
222 /** |
222 /** |
223 * Prepares a single theme output for response. |
223 * Prepares a single theme output for response. |
224 * |
224 * |
225 * @since 5.0.0 |
225 * @since 5.0.0 |
226 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. |
226 * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. |
|
227 * @since 6.6.0 Added `stylesheet_uri` and `template_uri` fields. |
227 * |
228 * |
228 * @param WP_Theme $item Theme object. |
229 * @param WP_Theme $item Theme object. |
229 * @param WP_REST_Request $request Request object. |
230 * @param WP_REST_Request $request Request object. |
230 * @return WP_REST_Response Response object. |
231 * @return WP_REST_Response Response object. |
231 */ |
232 */ |
232 public function prepare_item_for_response( $item, $request ) { |
233 public function prepare_item_for_response( $item, $request ) { |
233 // Restores the more descriptive, specific name for use within this method. |
234 // Restores the more descriptive, specific name for use within this method. |
234 $theme = $item; |
235 $theme = $item; |
|
236 |
|
237 $fields = $this->get_fields_for_response( $request ); |
235 $data = array(); |
238 $data = array(); |
236 $fields = $this->get_fields_for_response( $request ); |
|
237 |
239 |
238 if ( rest_is_field_included( 'stylesheet', $fields ) ) { |
240 if ( rest_is_field_included( 'stylesheet', $fields ) ) { |
239 $data['stylesheet'] = $theme->get_stylesheet(); |
241 $data['stylesheet'] = $theme->get_stylesheet(); |
240 } |
242 } |
241 |
243 |
324 |
326 |
325 $data['theme_supports'][ $name ] = $prepared; |
327 $data['theme_supports'][ $name ] = $prepared; |
326 } |
328 } |
327 } |
329 } |
328 |
330 |
|
331 if ( rest_is_field_included( 'is_block_theme', $fields ) ) { |
|
332 $data['is_block_theme'] = $theme->is_block_theme(); |
|
333 } |
|
334 |
|
335 if ( rest_is_field_included( 'stylesheet_uri', $fields ) ) { |
|
336 if ( $this->is_same_theme( $theme, $current_theme ) ) { |
|
337 $data['stylesheet_uri'] = get_stylesheet_directory_uri(); |
|
338 } else { |
|
339 $data['stylesheet_uri'] = $theme->get_stylesheet_directory_uri(); |
|
340 } |
|
341 } |
|
342 |
|
343 if ( rest_is_field_included( 'template_uri', $fields ) ) { |
|
344 if ( $this->is_same_theme( $theme, $current_theme ) ) { |
|
345 $data['template_uri'] = get_template_directory_uri(); |
|
346 } else { |
|
347 $data['template_uri'] = $theme->get_template_directory_uri(); |
|
348 } |
|
349 } |
|
350 |
329 $data = $this->add_additional_fields_to_object( $data, $request ); |
351 $data = $this->add_additional_fields_to_object( $data, $request ); |
330 |
352 |
331 // Wrap the data in a response object. |
353 // Wrap the data in a response object. |
332 $response = rest_ensure_response( $data ); |
354 $response = rest_ensure_response( $data ); |
333 |
355 |
334 $response->add_links( $this->prepare_links( $theme ) ); |
356 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { |
335 |
357 $response->add_links( $this->prepare_links( $theme ) ); |
336 if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) { |
|
337 // This creates a record for the active theme if not existent. |
|
338 $id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); |
|
339 } else { |
|
340 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); |
|
341 $id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null; |
|
342 } |
|
343 |
|
344 if ( $id ) { |
|
345 $response->add_link( |
|
346 'https://api.w.org/user-global-styles', |
|
347 rest_url( 'wp/v2/global-styles/' . $id ) |
|
348 ); |
|
349 } |
358 } |
350 |
359 |
351 /** |
360 /** |
352 * Filters theme data returned from the REST API. |
361 * Filters theme data returned from the REST API. |
353 * |
362 * |
367 * |
376 * |
368 * @param WP_Theme $theme Theme data. |
377 * @param WP_Theme $theme Theme data. |
369 * @return array Links for the given block type. |
378 * @return array Links for the given block type. |
370 */ |
379 */ |
371 protected function prepare_links( $theme ) { |
380 protected function prepare_links( $theme ) { |
372 return array( |
381 $links = array( |
373 'self' => array( |
382 'self' => array( |
374 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ), |
383 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ), |
375 ), |
384 ), |
376 'collection' => array( |
385 'collection' => array( |
377 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
386 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
378 ), |
387 ), |
379 ); |
388 ); |
|
389 |
|
390 if ( $this->is_same_theme( $theme, wp_get_theme() ) ) { |
|
391 // This creates a record for the active theme if not existent. |
|
392 $id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); |
|
393 } else { |
|
394 $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); |
|
395 $id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null; |
|
396 } |
|
397 |
|
398 if ( $id ) { |
|
399 $links['https://api.w.org/user-global-styles'] = array( |
|
400 'href' => rest_url( 'wp/v2/global-styles/' . $id ), |
|
401 ); |
|
402 } |
|
403 |
|
404 return $links; |
380 } |
405 } |
381 |
406 |
382 /** |
407 /** |
383 * Helper function to compare two themes. |
408 * Helper function to compare two themes. |
384 * |
409 * |
437 'stylesheet' => array( |
462 'stylesheet' => array( |
438 'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ), |
463 'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ), |
439 'type' => 'string', |
464 'type' => 'string', |
440 'readonly' => true, |
465 'readonly' => true, |
441 ), |
466 ), |
|
467 'stylesheet_uri' => array( |
|
468 'description' => __( 'The uri for the theme\'s stylesheet directory.' ), |
|
469 'type' => 'string', |
|
470 'format' => 'uri', |
|
471 'readonly' => true, |
|
472 ), |
442 'template' => array( |
473 'template' => array( |
443 '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.' ), |
474 '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.' ), |
444 'type' => 'string', |
475 'type' => 'string', |
|
476 'readonly' => true, |
|
477 ), |
|
478 'template_uri' => array( |
|
479 'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.' ), |
|
480 'type' => 'string', |
|
481 'format' => 'uri', |
445 'readonly' => true, |
482 'readonly' => true, |
446 ), |
483 ), |
447 'author' => array( |
484 'author' => array( |
448 'description' => __( 'The theme author.' ), |
485 'description' => __( 'The theme author.' ), |
449 'type' => 'object', |
486 'type' => 'object', |
488 'rendered' => array( |
525 'rendered' => array( |
489 'description' => __( 'The theme description, transformed for display.' ), |
526 'description' => __( 'The theme description, transformed for display.' ), |
490 'type' => 'string', |
527 'type' => 'string', |
491 ), |
528 ), |
492 ), |
529 ), |
|
530 ), |
|
531 'is_block_theme' => array( |
|
532 'description' => __( 'Whether the theme is a block-based theme.' ), |
|
533 'type' => 'boolean', |
|
534 'readonly' => true, |
493 ), |
535 ), |
494 'name' => array( |
536 'name' => array( |
495 'description' => __( 'The name of the theme.' ), |
537 'description' => __( 'The name of the theme.' ), |
496 'type' => 'object', |
538 'type' => 'object', |
497 'readonly' => true, |
539 'readonly' => true, |