2018-12-11 22:33:24 -05:00
< ? php
/**
* REST API : WP_REST_Themes_Controller class
*
* @ package WordPress
* @ subpackage REST_API
* @ since 5.0 . 0
*/
/**
* Core class used to manage themes via the REST API .
*
* @ since 5.0 . 0
*
* @ see WP_REST_Controller
*/
class WP_REST_Themes_Controller extends WP_REST_Controller {
REST API: Support `.` in theme directory names in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Regex changes from [52376] are reverted to restore the original regex patterns. Why? [52376] used an include characters pattern, which was too limiting. It did not account for localized characters, such as `é`, or other valid directory name characters.
The original theme directory regex pattern, i.e. `[^.\/]+(?:\/[^.\/]+)?` excluded the period `.` character. Removing the `.` character resolves the reported issue by allowing matching for `themes/theme-dirname-1.0/` or `themes/<subdirname>/theme-dirname-1.0/`.
As the pattern used an exclude approach, all characters are valid for matching except for `/`. However, not all characters are cross-platform valid for directory names. For example, the characters `/:<>*?"|` are not valid on Windows OS. The pattern now excludes those characters.
The theme's directory (or subdirectory) name pattern matching is now used in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Follow-up to [51003], [52051], [52275], [52376].
Props costdev, hellofromTonya, spacedmonkey, TimothyBlynJacobs, bijayyadav, kafleg.
Fixes #54596.
Built from https://develop.svn.wordpress.org/trunk@52399
git-svn-id: http://core.svn.wordpress.org/trunk@51991 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-20 23:14:00 -05:00
/**
* Matches theme ' s directory : `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/` .
* Excludes invalid directory name characters : `/:<>*?"|` .
*/
const PATTERN = '[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?' ;
2021-11-04 22:30:57 -04:00
2018-12-11 22:33:24 -05:00
/**
* Constructor .
*
* @ since 5.0 . 0
*/
public function __construct () {
$this -> namespace = 'wp/v2' ;
$this -> rest_base = 'themes' ;
}
/**
2021-05-25 09:12:58 -04:00
* Registers the routes for themes .
2018-12-11 22:33:24 -05:00
*
* @ since 5.0 . 0
*
* @ see register_rest_route ()
*/
public function register_routes () {
register_rest_route (
$this -> namespace ,
'/' . $this -> rest_base ,
array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
'schema' => array ( $this , 'get_item_schema' ),
)
);
2021-01-03 16:47:05 -05:00
register_rest_route (
$this -> namespace ,
2021-11-04 22:30:57 -04:00
sprintf ( '/%s/(?P<stylesheet>%s)' , $this -> rest_base , self :: PATTERN ),
2021-01-03 16:47:05 -05:00
array (
'args' => array (
'stylesheet' => array (
REST API: Support `.` in theme directory names in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Regex changes from [52376] are reverted to restore the original regex patterns. Why? [52376] used an include characters pattern, which was too limiting. It did not account for localized characters, such as `é`, or other valid directory name characters.
The original theme directory regex pattern, i.e. `[^.\/]+(?:\/[^.\/]+)?` excluded the period `.` character. Removing the `.` character resolves the reported issue by allowing matching for `themes/theme-dirname-1.0/` or `themes/<subdirname>/theme-dirname-1.0/`.
As the pattern used an exclude approach, all characters are valid for matching except for `/`. However, not all characters are cross-platform valid for directory names. For example, the characters `/:<>*?"|` are not valid on Windows OS. The pattern now excludes those characters.
The theme's directory (or subdirectory) name pattern matching is now used in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Follow-up to [51003], [52051], [52275], [52376].
Props costdev, hellofromTonya, spacedmonkey, TimothyBlynJacobs, bijayyadav, kafleg.
Fixes #54596.
Built from https://develop.svn.wordpress.org/trunk@52399
git-svn-id: http://core.svn.wordpress.org/trunk@51991 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-20 23:14:00 -05:00
'description' => __ ( " The theme's stylesheet. This uniquely identifies the theme. " ),
'type' => 'string' ,
'sanitize_callback' => array ( $this , '_sanitize_stylesheet_callback' ),
2021-01-03 16:47:05 -05:00
),
),
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_item_permissions_check' ),
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
2018-12-11 22:33:24 -05:00
}
REST API: Support `.` in theme directory names in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Regex changes from [52376] are reverted to restore the original regex patterns. Why? [52376] used an include characters pattern, which was too limiting. It did not account for localized characters, such as `é`, or other valid directory name characters.
The original theme directory regex pattern, i.e. `[^.\/]+(?:\/[^.\/]+)?` excluded the period `.` character. Removing the `.` character resolves the reported issue by allowing matching for `themes/theme-dirname-1.0/` or `themes/<subdirname>/theme-dirname-1.0/`.
As the pattern used an exclude approach, all characters are valid for matching except for `/`. However, not all characters are cross-platform valid for directory names. For example, the characters `/:<>*?"|` are not valid on Windows OS. The pattern now excludes those characters.
The theme's directory (or subdirectory) name pattern matching is now used in `WP_REST_Global_Styles_Controller`, `WP_REST_Templates_Controller`, and `WP_REST_Themes_Controller`.
Follow-up to [51003], [52051], [52275], [52376].
Props costdev, hellofromTonya, spacedmonkey, TimothyBlynJacobs, bijayyadav, kafleg.
Fixes #54596.
Built from https://develop.svn.wordpress.org/trunk@52399
git-svn-id: http://core.svn.wordpress.org/trunk@51991 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-20 23:14:00 -05:00
/**
* Sanitize the stylesheet to decode endpoint .
*
* @ since 5.9 . 0
*
* @ param string $stylesheet The stylesheet name .
* @ return string Sanitized stylesheet .
*/
public function _sanitize_stylesheet_callback ( $stylesheet ) {
return urldecode ( $stylesheet );
}
2018-12-11 22:33:24 -05:00
/**
* Checks if a given request has access to read the theme .
*
* @ since 5.0 . 0
*
* @ param WP_REST_Request $request Full details about the request .
* @ return true | WP_Error True if the request has read access for the item , otherwise WP_Error object .
*/
public function get_items_permissions_check ( $request ) {
2021-01-03 16:47:05 -05:00
if ( current_user_can ( 'switch_themes' ) || current_user_can ( 'manage_network_themes' ) ) {
return true ;
}
$registered = $this -> get_collection_params ();
if ( isset ( $registered [ 'status' ], $request [ 'status' ] ) && is_array ( $request [ 'status' ] ) && array ( 'active' ) === $request [ 'status' ] ) {
return $this -> check_read_active_theme_permission ();
}
return new WP_Error (
'rest_cannot_view_themes' ,
__ ( 'Sorry, you are not allowed to view themes.' ),
array ( 'status' => rest_authorization_required_code () )
);
}
/**
* Checks if a given request has access to read the theme .
*
* @ since 5.7 . 0
*
* @ param WP_REST_Request $request Full details about the request .
2023-08-18 13:46:18 -04:00
* @ return true | WP_Error True if the request has read access for the item , otherwise WP_Error object .
2021-01-03 16:47:05 -05:00
*/
public function get_item_permissions_check ( $request ) {
if ( current_user_can ( 'switch_themes' ) || current_user_can ( 'manage_network_themes' ) ) {
return true ;
}
$wp_theme = wp_get_theme ( $request [ 'stylesheet' ] );
$current_theme = wp_get_theme ();
if ( $this -> is_same_theme ( $wp_theme , $current_theme ) ) {
return $this -> check_read_active_theme_permission ();
}
return new WP_Error (
'rest_cannot_view_themes' ,
__ ( 'Sorry, you are not allowed to view themes.' ),
array ( 'status' => rest_authorization_required_code () )
);
}
/**
* Checks if a theme can be read .
*
* @ since 5.7 . 0
*
2023-08-18 13:46:18 -04:00
* @ return true | WP_Error True if the theme can be read , WP_Error object otherwise .
2021-01-03 16:47:05 -05:00
*/
protected function check_read_active_theme_permission () {
2020-02-25 10:20:08 -05:00
if ( current_user_can ( 'edit_posts' ) ) {
return true ;
2018-12-11 22:33:24 -05:00
}
2020-02-25 10:20:08 -05:00
foreach ( get_post_types ( array ( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
if ( current_user_can ( $post_type -> cap -> edit_posts ) ) {
return true ;
}
}
return new WP_Error (
2021-01-03 16:47:05 -05:00
'rest_cannot_view_active_theme' ,
__ ( 'Sorry, you are not allowed to view the active theme.' ),
2020-02-25 10:20:08 -05:00
array ( 'status' => rest_authorization_required_code () )
);
2018-12-11 22:33:24 -05:00
}
2021-01-03 16:47:05 -05:00
/**
* Retrieves a single theme .
*
* @ since 5.7 . 0
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Response | WP_Error Response object on success , or WP_Error object on failure .
*/
public function get_item ( $request ) {
$wp_theme = wp_get_theme ( $request [ 'stylesheet' ] );
if ( ! $wp_theme -> exists () ) {
return new WP_Error (
'rest_theme_not_found' ,
__ ( 'Theme not found.' ),
array ( 'status' => 404 )
);
}
$data = $this -> prepare_item_for_response ( $wp_theme , $request );
return rest_ensure_response ( $data );
}
2018-12-11 22:33:24 -05:00
/**
* Retrieves a collection of themes .
*
* @ since 5.0 . 0
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Response | WP_Error Response object on success , or WP_Error object on failure .
*/
public function get_items ( $request ) {
2021-01-03 16:47:05 -05:00
$themes = array ();
$active_themes = wp_get_themes ();
$current_theme = wp_get_theme ();
$status = $request [ 'status' ];
2018-12-11 22:33:24 -05:00
2024-07-09 09:53:16 -04:00
foreach ( $active_themes as $theme ) {
2021-01-03 16:47:05 -05:00
$theme_status = ( $this -> is_same_theme ( $theme , $current_theme ) ) ? 'active' : 'inactive' ;
if ( is_array ( $status ) && ! in_array ( $theme_status , $status , true ) ) {
continue ;
}
$prepared = $this -> prepare_item_for_response ( $theme , $request );
$themes [] = $this -> prepare_response_for_collection ( $prepared );
2018-12-11 22:33:24 -05:00
}
$response = rest_ensure_response ( $themes );
$response -> header ( 'X-WP-Total' , count ( $themes ) );
2021-01-03 16:47:05 -05:00
$response -> header ( 'X-WP-TotalPages' , 1 );
2018-12-11 22:33:24 -05:00
return $response ;
}
/**
* Prepares a single theme output for response .
*
* @ since 5.0 . 0
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
* @ since 5.9 . 0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support .
2024-06-02 15:27:15 -04:00
* @ since 6.6 . 0 Added `stylesheet_uri` and `template_uri` fields .
2018-12-11 22:33:24 -05:00
*
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
* @ param WP_Theme $item Theme object .
2018-12-11 22:33:24 -05:00
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response Response object .
*/
Code Modernization: Fix parameter name mismatches for parent/child classes in `WP_REST_Controller::prepare_item_for_response()`.
In each child and grandchild class, renames the first parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51786
git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:36:57 -04:00
public function prepare_item_for_response ( $item , $request ) {
// Restores the more descriptive, specific name for use within this method.
2023-09-14 08:46:20 -04:00
$theme = $item ;
2018-12-11 22:33:24 -05:00
$fields = $this -> get_fields_for_response ( $request );
2023-09-14 08:46:20 -04:00
$data = array ();
2018-12-11 22:33:24 -05:00
2020-06-07 02:46:10 -04:00
if ( rest_is_field_included ( 'stylesheet' , $fields ) ) {
$data [ 'stylesheet' ] = $theme -> get_stylesheet ();
}
if ( rest_is_field_included ( 'template' , $fields ) ) {
/**
* Use the get_template () method , not the 'Template' header , for finding the template .
* The 'Template' header is only good for what was written in the style . css , while
* get_template () takes into account where WordPress actually located the theme and
* whether it is actually valid .
*/
$data [ 'template' ] = $theme -> get_template ();
}
$plain_field_mappings = array (
'requires_php' => 'RequiresPHP' ,
'requires_wp' => 'RequiresWP' ,
'textdomain' => 'TextDomain' ,
'version' => 'Version' ,
);
foreach ( $plain_field_mappings as $field => $header ) {
if ( rest_is_field_included ( $field , $fields ) ) {
$data [ $field ] = $theme -> get ( $header );
}
}
if ( rest_is_field_included ( 'screenshot' , $fields ) ) {
// Using $theme->get_screenshot() with no args to get absolute URL.
2020-06-08 17:27:10 -04:00
$data [ 'screenshot' ] = $theme -> get_screenshot () ? $theme -> get_screenshot () : '' ;
2020-06-07 02:46:10 -04:00
}
$rich_field_mappings = array (
'author' => 'Author' ,
'author_uri' => 'AuthorURI' ,
'description' => 'Description' ,
'name' => 'Name' ,
'tags' => 'Tags' ,
'theme_uri' => 'ThemeURI' ,
);
foreach ( $rich_field_mappings as $field => $header ) {
if ( rest_is_field_included ( " { $field } .raw " , $fields ) ) {
$data [ $field ][ 'raw' ] = $theme -> display ( $header , false , true );
}
if ( rest_is_field_included ( " { $field } .rendered " , $fields ) ) {
$data [ $field ][ 'rendered' ] = $theme -> display ( $header );
}
}
2021-01-03 16:47:05 -05:00
$current_theme = wp_get_theme ();
if ( rest_is_field_included ( 'status' , $fields ) ) {
$data [ 'status' ] = ( $this -> is_same_theme ( $theme , $current_theme ) ) ? 'active' : 'inactive' ;
}
if ( rest_is_field_included ( 'theme_supports' , $fields ) && $this -> is_same_theme ( $theme , $current_theme ) ) {
2020-06-25 18:13:10 -04:00
foreach ( get_registered_theme_features () as $feature => $config ) {
if ( ! is_array ( $config [ 'show_in_rest' ] ) ) {
2020-06-07 02:46:10 -04:00
continue ;
}
2020-06-25 18:13:10 -04:00
$name = $config [ 'show_in_rest' ][ 'name' ];
2020-02-10 21:00:07 -05:00
2020-06-25 18:13:10 -04:00
if ( ! rest_is_field_included ( " theme_supports. { $name } " , $fields ) ) {
2020-02-10 21:00:07 -05:00
continue ;
}
2020-06-25 18:13:10 -04:00
if ( ! current_theme_supports ( $feature ) ) {
$data [ 'theme_supports' ][ $name ] = $config [ 'show_in_rest' ][ 'schema' ][ 'default' ];
2020-02-10 21:00:07 -05:00
continue ;
}
2020-06-25 18:13:10 -04:00
$support = get_theme_support ( $feature );
2018-12-11 22:33:24 -05:00
2020-06-25 18:13:10 -04:00
if ( isset ( $config [ 'show_in_rest' ][ 'prepare_callback' ] ) ) {
$prepare = $config [ 'show_in_rest' ][ 'prepare_callback' ];
} else {
$prepare = array ( $this , 'prepare_theme_support' );
2020-02-10 21:00:07 -05:00
}
2020-06-25 18:13:10 -04:00
$prepared = $prepare ( $support , $config , $feature , $request );
2020-02-10 21:00:07 -05:00
2020-06-25 18:13:10 -04:00
if ( is_wp_error ( $prepared ) ) {
continue ;
}
2020-02-10 21:00:07 -05:00
2020-06-25 18:13:10 -04:00
$data [ 'theme_supports' ][ $name ] = $prepared ;
}
2018-12-11 22:33:24 -05:00
}
2023-06-20 18:17:27 -04:00
if ( rest_is_field_included ( 'is_block_theme' , $fields ) ) {
$data [ 'is_block_theme' ] = $theme -> is_block_theme ();
}
2024-06-02 15:27:15 -04:00
if ( rest_is_field_included ( 'stylesheet_uri' , $fields ) ) {
if ( $this -> is_same_theme ( $theme , $current_theme ) ) {
$data [ 'stylesheet_uri' ] = get_stylesheet_directory_uri ();
} else {
$data [ 'stylesheet_uri' ] = $theme -> get_stylesheet_directory_uri ();
}
}
if ( rest_is_field_included ( 'template_uri' , $fields ) ) {
if ( $this -> is_same_theme ( $theme , $current_theme ) ) {
$data [ 'template_uri' ] = get_template_directory_uri ();
} else {
$data [ 'template_uri' ] = $theme -> get_template_directory_uri ();
}
}
2018-12-11 22:33:24 -05:00
$data = $this -> add_additional_fields_to_object ( $data , $request );
// Wrap the data in a response object.
$response = rest_ensure_response ( $data );
2022-07-22 10:00:12 -04:00
if ( rest_is_field_included ( '_links' , $fields ) || rest_is_field_included ( '_embedded' , $fields ) ) {
$response -> add_links ( $this -> prepare_links ( $theme ) );
}
2021-01-03 16:47:05 -05:00
2018-12-11 22:33:24 -05:00
/**
* Filters theme data returned from the REST API .
*
* @ since 5.0 . 0
*
* @ param WP_REST_Response $response The response object .
* @ param WP_Theme $theme Theme object used to create response .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( 'rest_prepare_theme' , $response , $theme , $request );
}
2021-01-03 16:47:05 -05:00
/**
* Prepares links for the request .
*
* @ since 5.7 . 0
*
* @ param WP_Theme $theme Theme data .
* @ return array Links for the given block type .
*/
protected function prepare_links ( $theme ) {
2022-06-21 06:48:21 -04:00
$links = array (
2021-01-03 16:47:05 -05:00
'self' => array (
'href' => rest_url ( sprintf ( '%s/%s/%s' , $this -> namespace , $this -> rest_base , $theme -> get_stylesheet () ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
);
2022-06-21 06:48:21 -04:00
if ( $this -> is_same_theme ( $theme , wp_get_theme () ) ) {
// This creates a record for the active theme if not existent.
$id = WP_Theme_JSON_Resolver :: get_user_global_styles_post_id ();
} else {
$user_cpt = WP_Theme_JSON_Resolver :: get_user_data_from_wp_global_styles ( $theme );
$id = isset ( $user_cpt [ 'ID' ] ) ? $user_cpt [ 'ID' ] : null ;
}
if ( $id ) {
$links [ 'https://api.w.org/user-global-styles' ] = array (
'href' => rest_url ( 'wp/v2/global-styles/' . $id ),
);
}
return $links ;
2021-01-03 16:47:05 -05:00
}
/**
* Helper function to compare two themes .
*
* @ since 5.7 . 0
*
* @ param WP_Theme $theme_a First theme to compare .
* @ param WP_Theme $theme_b Second theme to compare .
* @ return bool
*/
protected function is_same_theme ( $theme_a , $theme_b ) {
return $theme_a -> get_stylesheet () === $theme_b -> get_stylesheet ();
}
2020-06-25 18:13:10 -04:00
/**
* Prepares the theme support value for inclusion in the REST API response .
*
* @ since 5.5 . 0
*
2020-06-27 08:02:03 -04:00
* @ param mixed $support The raw value from get_theme_support () .
2020-06-25 18:13:10 -04:00
* @ param array $args The feature ' s registration args .
* @ param string $feature The feature name .
* @ param WP_REST_Request $request The request object .
* @ return mixed The prepared support value .
*/
protected function prepare_theme_support ( $support , $args , $feature , $request ) {
$schema = $args [ 'show_in_rest' ][ 'schema' ];
if ( 'boolean' === $schema [ 'type' ] ) {
return true ;
}
2020-07-04 21:06:02 -04:00
if ( is_array ( $support ) && ! $args [ 'variadic' ] ) {
$support = $support [ 0 ];
2020-06-25 18:13:10 -04:00
}
return rest_sanitize_value_from_schema ( $support , $schema );
}
2018-12-11 22:33:24 -05:00
/**
* Retrieves the theme ' s schema , conforming to JSON Schema .
*
* @ since 5.0 . 0
*
* @ return array Item schema data .
*/
public function get_item_schema () {
2019-08-15 17:09:55 -04:00
if ( $this -> schema ) {
return $this -> add_additional_fields_schema ( $this -> schema );
}
2018-12-11 22:33:24 -05:00
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'theme' ,
'type' => 'object' ,
'properties' => array (
2020-06-07 02:46:10 -04:00
'stylesheet' => array (
'description' => __ ( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
'type' => 'string' ,
'readonly' => true ,
),
2024-06-02 15:27:15 -04:00
'stylesheet_uri' => array (
'description' => __ ( 'The uri for the theme\'s stylesheet directory.' ),
'type' => 'string' ,
'format' => 'uri' ,
'readonly' => true ,
),
2020-06-07 02:46:10 -04:00
'template' => array (
'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.' ),
'type' => 'string' ,
'readonly' => true ,
),
Coding Standards: Apply changes after running `composer format`.
This applies several formatting related changes made while running `composer format`.
Follow up to [55720], [58171], [58271], [58282], [58283], [58292], [58299], [58303], [58332].
See #51857, #60719, #60895, #61021, #61118, #61228, #61276, #61324.
Built from https://develop.svn.wordpress.org/trunk@58408
git-svn-id: http://core.svn.wordpress.org/trunk@57857 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-13 11:06:07 -04:00
'template_uri' => array (
2024-06-02 15:27:15 -04:00
'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.' ),
'type' => 'string' ,
'format' => 'uri' ,
'readonly' => true ,
),
2020-06-07 02:46:10 -04:00
'author' => array (
'description' => __ ( 'The theme author.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The theme author\'s name, as found in the theme header.' ),
'type' => 'string' ,
),
'rendered' => array (
'description' => __ ( 'HTML for the theme author, transformed for display.' ),
'type' => 'string' ,
),
),
),
'author_uri' => array (
'description' => __ ( 'The website of the theme author.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The website of the theme author, as found in the theme header.' ),
'type' => 'string' ,
'format' => 'uri' ,
),
'rendered' => array (
'description' => __ ( 'The website of the theme author, transformed for display.' ),
'type' => 'string' ,
'format' => 'uri' ,
),
),
),
'description' => array (
'description' => __ ( 'A description of the theme.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The theme description, as found in the theme header.' ),
'type' => 'string' ,
),
'rendered' => array (
'description' => __ ( 'The theme description, transformed for display.' ),
'type' => 'string' ,
),
),
),
2023-06-20 18:17:27 -04:00
'is_block_theme' => array (
'description' => __ ( 'Whether the theme is a block-based theme.' ),
'type' => 'boolean' ,
'readonly' => true ,
),
2020-06-07 02:46:10 -04:00
'name' => array (
'description' => __ ( 'The name of the theme.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The theme name, as found in the theme header.' ),
'type' => 'string' ,
),
'rendered' => array (
'description' => __ ( 'The theme name, transformed for display.' ),
'type' => 'string' ,
),
),
),
'requires_php' => array (
'description' => __ ( 'The minimum PHP version required for the theme to work.' ),
'type' => 'string' ,
'readonly' => true ,
),
'requires_wp' => array (
'description' => __ ( 'The minimum WordPress version required for the theme to work.' ),
'type' => 'string' ,
'readonly' => true ,
),
'screenshot' => array (
'description' => __ ( 'The theme\'s screenshot URL.' ),
'type' => 'string' ,
'format' => 'uri' ,
'readonly' => true ,
),
'tags' => array (
'description' => __ ( 'Tags indicating styles and features of the theme.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The theme tags, as found in the theme header.' ),
'type' => 'array' ,
'items' => array (
'type' => 'string' ,
),
),
'rendered' => array (
'description' => __ ( 'The theme tags, transformed for display.' ),
'type' => 'string' ,
),
),
),
'textdomain' => array (
2020-07-26 17:54:03 -04:00
'description' => __ ( 'The theme\'s text domain.' ),
2020-06-07 02:46:10 -04:00
'type' => 'string' ,
'readonly' => true ,
),
2018-12-11 22:33:24 -05:00
'theme_supports' => array (
'description' => __ ( 'Features supported by this theme.' ),
2020-02-10 21:00:07 -05:00
'type' => 'object' ,
2018-12-11 22:33:24 -05:00
'readonly' => true ,
2020-06-25 18:13:10 -04:00
'properties' => array (),
2018-12-11 22:33:24 -05:00
),
2020-06-07 02:46:10 -04:00
'theme_uri' => array (
'description' => __ ( 'The URI of the theme\'s webpage.' ),
'type' => 'object' ,
'readonly' => true ,
'properties' => array (
'raw' => array (
'description' => __ ( 'The URI of the theme\'s webpage, as found in the theme header.' ),
'type' => 'string' ,
'format' => 'uri' ,
),
'rendered' => array (
'description' => __ ( 'The URI of the theme\'s webpage, transformed for display.' ),
'type' => 'string' ,
'format' => 'uri' ,
),
),
),
'version' => array (
'description' => __ ( 'The theme\'s current version.' ),
'type' => 'string' ,
'readonly' => true ,
),
2021-01-03 16:47:05 -05:00
'status' => array (
'description' => __ ( 'A named status for the theme.' ),
'type' => 'string' ,
'enum' => array ( 'inactive' , 'active' ),
),
2018-12-11 22:33:24 -05:00
),
);
2020-06-25 18:13:10 -04:00
foreach ( get_registered_theme_features () as $feature => $config ) {
if ( ! is_array ( $config [ 'show_in_rest' ] ) ) {
continue ;
}
$name = $config [ 'show_in_rest' ][ 'name' ];
$schema [ 'properties' ][ 'theme_supports' ][ 'properties' ][ $name ] = $config [ 'show_in_rest' ][ 'schema' ];
}
2019-08-15 17:09:55 -04:00
$this -> schema = $schema ;
2020-01-28 19:45:18 -05:00
2019-08-15 17:09:55 -04:00
return $this -> add_additional_fields_schema ( $this -> schema );
2018-12-11 22:33:24 -05:00
}
/**
* Retrieves the search params for the themes collection .
*
* @ since 5.0 . 0
*
* @ return array Collection parameters .
*/
public function get_collection_params () {
2021-01-03 16:47:05 -05:00
$query_params = array (
'status' => array (
'description' => __ ( 'Limit result set to themes assigned one or more statuses.' ),
'type' => 'array' ,
'items' => array (
'enum' => array ( 'active' , 'inactive' ),
'type' => 'string' ,
),
2018-12-11 22:33:24 -05:00
),
);
/**
2021-01-10 17:23:09 -05:00
* Filters REST API collection parameters for the themes controller .
2018-12-11 22:33:24 -05:00
*
* @ since 5.0 . 0
*
2019-09-05 19:05:55 -04:00
* @ param array $query_params JSON Schema - formatted collection parameters .
2018-12-11 22:33:24 -05:00
*/
return apply_filters ( 'rest_themes_collection_params' , $query_params );
}
/**
* Sanitizes and validates the list of theme status .
*
* @ since 5.0 . 0
2021-01-03 16:47:05 -05:00
* @ deprecated 5.7 . 0
2018-12-11 22:33:24 -05:00
*
2019-09-05 19:05:55 -04:00
* @ param string | array $statuses One or more theme statuses .
* @ param WP_REST_Request $request Full details about the request .
* @ param string $parameter Additional parameter to pass to validation .
2018-12-11 22:33:24 -05:00
* @ return array | WP_Error A list of valid statuses , otherwise WP_Error object .
*/
public function sanitize_theme_status ( $statuses , $request , $parameter ) {
2021-01-03 16:47:05 -05:00
_deprecated_function ( __METHOD__ , '5.7.0' );
2018-12-11 22:33:24 -05:00
$statuses = wp_parse_slug_list ( $statuses );
foreach ( $statuses as $status ) {
$result = rest_validate_request_arg ( $status , $request , $parameter );
if ( is_wp_error ( $result ) ) {
return $result ;
}
}
return $statuses ;
}
}