-
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
Add a Global Styles endpoint and use it in the site editor #35801
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
484e609
Start a Global Styles endpoint and use it in the site editor
youknowriad 369183e
Add the active global styles ID to the themes endpoint
youknowriad efd6dc7
Changes per review
youknowriad 7da64bc
Add unit test
youknowriad 57fbca2
Small tweaks
youknowriad 6532a3a
Remove list endpoint
youknowriad 847abef
changes per review
youknowriad ca75f74
Better entity config
youknowriad d5355dd
Use Full URL instead of path
youknowriad 1c0fa26
Rename link to user global styles
youknowriad a4a579c
Add escaping, version and other fixes
jorgefilipecosta 70a95d6
Create initial global styles object for current theme
youknowriad a498b15
Fix unit tests
youknowriad 2e2caa2
Fix phpcs
youknowriad b91a650
Avoid duplicate queries
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
341 changes: 341 additions & 0 deletions
341
lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,341 @@ | ||
<?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_Controller { | ||
/** | ||
* Post type. | ||
* | ||
* @var string | ||
*/ | ||
protected $post_type; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'global-styles'; | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Registers the controllers routes. | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
// Lists/updates a single gloval style variation based on the given id. | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)', | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_item' ), | ||
'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
'args' => array( | ||
'id' => array( | ||
'description' => __( 'The id of a template', 'gutenberg' ), | ||
'type' => 'string', | ||
), | ||
), | ||
), | ||
array( | ||
'methods' => WP_REST_Server::EDITABLE, | ||
'callback' => array( $this, 'update_item' ), | ||
'permission_callback' => array( $this, 'update_item_permissions_check' ), | ||
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | ||
), | ||
'schema' => array( $this, 'get_public_item_schema' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Checks if the user has permissions to make the request. | ||
* | ||
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
*/ | ||
protected function permissions_check() { | ||
// 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; | ||
} | ||
|
||
/** | ||
* Checks if a given request has access to read a single 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_item_permissions_check( $request ) { | ||
return $this->permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Returns the given global styles config. | ||
* | ||
* @param WP_REST_Request $request The request instance. | ||
* | ||
* @return WP_REST_Response|WP_Error | ||
*/ | ||
public function get_item( $request ) { | ||
$post = get_post( $request['id'] ); | ||
if ( ! $post || 'wp_global_styles' !== $post->post_type ) { | ||
return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
return $this->prepare_item_for_response( $post, $request ); | ||
} | ||
|
||
/** | ||
* Checks if a given request has access to write a single global styles config. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. | ||
*/ | ||
public function update_item_permissions_check( $request ) { | ||
return $this->permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Updates a single global style config. | ||
* | ||
* @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 update_item( $request ) { | ||
$post = get_post( $request['id'] ); | ||
if ( ! $post || 'wp_global_styles' !== $post->post_type ) { | ||
return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
$changes = $this->prepare_item_for_database( $request ); | ||
$result = wp_update_post( wp_slash( (array) $changes ), true ); | ||
if ( is_wp_error( $result ) ) { | ||
return $result; | ||
} | ||
|
||
$post = get_post( $request['id'] ); | ||
$fields_update = $this->update_additional_fields_for_object( $post, $request ); | ||
if ( is_wp_error( $fields_update ) ) { | ||
return $fields_update; | ||
} | ||
|
||
return $this->prepare_item_for_response( | ||
get_post( $request['id'] ), | ||
$request | ||
); | ||
} | ||
|
||
/** | ||
* Prepares a single global styles config for update. | ||
* | ||
* @param WP_REST_Request $request Request object. | ||
* @return stdClass Changes to pass to wp_update_post. | ||
*/ | ||
protected function prepare_item_for_database( $request ) { | ||
$changes = new stdClass(); | ||
$changes->ID = $request['id']; | ||
|
||
$post = get_post( $request['id'] ); | ||
$empty_config = array( | ||
'settings' => new stdClass(), | ||
'styles' => new stdClass(), | ||
); | ||
if ( $post ) { | ||
$existing_config = json_decode( $post->post_content, true ); | ||
if ( ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) || | ||
! $existing_config['isGlobalStylesUserThemeJSON'] ) { | ||
$existing_config = $empty_config; | ||
} | ||
} else { | ||
$existing_config = $empty_config; | ||
} | ||
|
||
if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) { | ||
$config = array(); | ||
if ( isset( $request['styles'] ) ) { | ||
$config['styles'] = $request['styles']; | ||
} else { | ||
$config['styles'] = $existing_config['styles']; | ||
} | ||
if ( isset( $request['settings'] ) ) { | ||
$config['settings'] = $request['settings']; | ||
} else { | ||
$config['settings'] = $existing_config['settings']; | ||
} | ||
$config['isGlobalStylesUserThemeJSON'] = true; | ||
$config['version'] = WP_Theme_JSON_Gutenberg::LATEST_SCHEMA; | ||
$changes->post_content = wp_json_encode( $config ); | ||
} | ||
|
||
// Post title. | ||
if ( isset( $request['title'] ) ) { | ||
if ( is_string( $request['title'] ) ) { | ||
$changes->post_title = $request['title']; | ||
} elseif ( ! empty( $request['title']['raw'] ) ) { | ||
$changes->post_title = $request['title']['raw']; | ||
} | ||
} | ||
|
||
return $changes; | ||
} | ||
|
||
/** | ||
* Prepare a global styles config output for response. | ||
* | ||
* @param WP_Post $post Global Styles post object. | ||
* @param WP_REST_Request $request Request object. | ||
* | ||
* @return WP_REST_Response $data | ||
*/ | ||
public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
$config = json_decode( $post->post_content, true ); | ||
$is_global_styles_user_theme_json = isset( $config['isGlobalStylesUserThemeJSON'] ) && true === $config['isGlobalStylesUserThemeJSON']; | ||
$result = array( | ||
'id' => $post->ID, | ||
'settings' => ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(), | ||
'styles' => ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(), | ||
'title' => array( | ||
'raw' => $post->post_title, | ||
'rendered' => get_the_title( $post ), | ||
), | ||
); | ||
$result = $this->add_additional_fields_to_object( $result, $request ); | ||
$response = rest_ensure_response( $result ); | ||
$links = $this->prepare_links( $post->id ); | ||
$response->add_links( $links ); | ||
if ( ! empty( $links['self']['href'] ) ) { | ||
$actions = $this->get_available_actions(); | ||
$self = $links['self']['href']; | ||
foreach ( $actions as $rel ) { | ||
$response->add_link( $rel, $self ); | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
|
||
/** | ||
* Prepares links for the request. | ||
* | ||
* @param integer $id ID. | ||
* @return array Links for the given post. | ||
*/ | ||
protected function prepare_links( $id ) { | ||
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); | ||
|
||
$links = array( | ||
'self' => array( | ||
'href' => rest_url( trailingslashit( $base ) . $id ), | ||
), | ||
'collection' => array( | ||
'href' => rest_url( $base ), | ||
), | ||
); | ||
|
||
return $links; | ||
} | ||
|
||
/** | ||
* Get the link relations available for the post and current user. | ||
* | ||
* @return array List of link relations. | ||
*/ | ||
protected function get_available_actions() { | ||
$rels = array(); | ||
|
||
$post_type = get_post_type_object( 'wp_global_styles' ); | ||
if ( current_user_can( $post_type->cap->publish_posts ) ) { | ||
$rels[] = 'https://api.w.org/action-publish'; | ||
} | ||
|
||
return $rels; | ||
} | ||
|
||
/** | ||
* Retrieves the query params for the global styles collection. | ||
* | ||
* @return array Collection parameters. | ||
*/ | ||
public function get_collection_params() { | ||
return array(); | ||
} | ||
|
||
/** | ||
* Retrieves the global styles type' schema, conforming to JSON Schema. | ||
* | ||
* @return array Item schema data. | ||
*/ | ||
public function get_item_schema() { | ||
if ( $this->schema ) { | ||
return $this->add_additional_fields_schema( $this->schema ); | ||
} | ||
|
||
$schema = array( | ||
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'title' => 'wp_global_styles', | ||
'type' => 'object', | ||
'properties' => array( | ||
'id' => array( | ||
'description' => __( 'ID of global styles config.', 'gutenberg' ), | ||
'type' => 'string', | ||
'context' => array( 'embed', 'view', 'edit' ), | ||
'readonly' => true, | ||
), | ||
'styles' => array( | ||
'description' => __( 'Global styles.', 'gutenberg' ), | ||
'type' => array( 'object' ), | ||
'context' => array( 'view', 'edit' ), | ||
), | ||
'settings' => array( | ||
'description' => __( 'Global settings.', 'gutenberg' ), | ||
'type' => array( 'object' ), | ||
'context' => array( 'view', 'edit' ), | ||
), | ||
'title' => array( | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'description' => __( 'Title of the global styles variation.', 'gutenberg' ), | ||
'type' => array( 'object', 'string' ), | ||
'default' => '', | ||
'context' => array( 'embed', 'view', 'edit' ), | ||
'properties' => array( | ||
'raw' => array( | ||
'description' => __( 'Title for the global styles variation, as it exists in the database.', 'gutenberg' ), | ||
'type' => 'string', | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
), | ||
'rendered' => array( | ||
'description' => __( 'HTML title for the post, transformed for display.', 'gutenberg' ), | ||
'type' => 'string', | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
'readonly' => true, | ||
), | ||
), | ||
), | ||
), | ||
); | ||
|
||
$this->schema = $schema; | ||
|
||
return $this->add_additional_fields_schema( $this->schema ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is this property used for?