-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Global Styles]: Add REST API endpoint to fetch variations #38124
Changes from all commits
c739ceb
362774f
38a8a39
cdd3ccc
f94845c
3751224
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* REST API: Gutenberg_REST_Global_Styles_Controller class | ||
* | ||
* @package Gutenberg | ||
* @subpackage REST_API | ||
*/ | ||
|
||
/** | ||
* Base Global Styles REST API Controller. | ||
*/ | ||
class Gutenberg_REST_Global_Styles_Controller extends WP_REST_Global_Styles_Controller { | ||
/** | ||
* Registers the controllers routes. | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
// List themes global styles. | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations', | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_theme_items' ), | ||
'permission_callback' => array( $this, 'get_theme_items_permissions_check' ), | ||
'args' => array( | ||
'stylesheet' => array( | ||
'description' => __( 'The theme identifier', 'gutenberg' ), | ||
'type' => 'string', | ||
), | ||
), | ||
), | ||
) | ||
); | ||
|
||
parent::register_routes(); | ||
} | ||
|
||
/** | ||
* Checks if a given request has access to read a single theme global styles config. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. | ||
*/ | ||
public function get_theme_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
// Verify if the current user has edit_theme_options capability. | ||
// This capability is required to edit/view/delete templates. | ||
if ( ! current_user_can( 'edit_theme_options' ) ) { | ||
return new WP_Error( | ||
'rest_cannot_manage_global_styles', | ||
__( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ), | ||
array( | ||
'status' => rest_authorization_required_code(), | ||
) | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns the given theme global styles variations. | ||
* | ||
* @param WP_REST_Request $request The request instance. | ||
* | ||
* @return WP_REST_Response|WP_Error | ||
*/ | ||
public function get_theme_items( $request ) { | ||
if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) { | ||
// This endpoint only supports the active theme for now. | ||
return new WP_Error( | ||
'rest_theme_not_found', | ||
__( 'Theme not found.', 'gutenberg' ), | ||
array( 'status' => 404 ) | ||
); | ||
} | ||
|
||
$variations = array(); | ||
$base_directory = get_stylesheet_directory() . '/styles'; | ||
if ( is_dir( $base_directory ) ) { | ||
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); | ||
$nested_html_files = new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ); | ||
foreach ( $nested_html_files as $path => $file ) { | ||
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); | ||
if ( is_array( $decoded_file ) ) { | ||
$variations[] = ( new WP_Theme_JSON_Gutenberg( $decoded_file ) )->get_raw_data(); | ||
} | ||
} | ||
} | ||
$response = rest_ensure_response( $variations ); | ||
|
||
return $response; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Overrides Core's wp-includes/rest-api.php and registers the new endpoint for WP 6.0. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Registers the Global Styles REST API routes. | ||
*/ | ||
function gutenberg_register_global_styles_endpoints() { | ||
$editor_settings = new Gutenberg_REST_Global_Styles_Controller(); | ||
$editor_settings->register_routes(); | ||
} | ||
add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I wonder if we should try to make the variation more "meaningful". For instance add a "dark" variation to the empty theme. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do it an a follow up to unblock the remaining functionality from landing. I'd appreciate if some theme dev could provide a minimal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. So this would be just for the It doesn't even specify a color palette, so making a dark mode version of it is a bit unexpected. 😄 |
||
"version": 2, | ||
"settings": { | ||
"color": { | ||
"palette": [ | ||
{ | ||
"slug": "foreground", | ||
"color": "#3F67C6", | ||
"name": "Foreground" | ||
} | ||
] | ||
} | ||
}, | ||
"styles": { | ||
"blocks": { | ||
"core/post-title": { | ||
"typography": { | ||
"fontWeight": "700" | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should rename
get_theme_items
toget_theme_variations
or change the endpoint to betheme-items
. This all comes down to whether we want to support more things from this endpoint other than fetching the variations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will merge this into
WP_REST_Global_Styles_Controller
in the future, right? In that case usingget_theme_variations
orget_variation_items
make sense.