-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Rest API: add /revisions endpoint for global styles #4606
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
Closed
ramonjd
wants to merge
21
commits into
WordPress:trunk
from
ramonjd:add/rest-api-global-revisions-controller
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e421e6f
Adds an endpoint that returns revisions for the global styles custom …
ramonjd f24d1a5
Rolled back unnecessary custom post changes
ramonjd 9b44f21
Added rest endpoint to reset schema
ramonjd 5323981
generated fixtures via npm run test:php -- --filter WP_Test_REST_Sche…
ramonjd f51484e
Adds prepare_date_response to parse and format the date in the respon…
ramonjd 4b1876c
tearing down after tests: deleting users
ramonjd 6a67883
post name should be different to the one created in global styles res…
ramonjd 3a8c04a
Ticket no in tests
ramonjd 8ff9685
Adding feedback:
ramonjd bed64da
Updated annotations that refer to duplicates
ramonjd 13a7ad1
Returning error if there is one from prepare_item_for_response
ramonjd 05d4247
updated fixtures
ramonjd a348276
updated fixtures
ramonjd c5cc8c5
Reinstate suppress filter.
ramonjd f9bb652
Only run and set styles and/or settings through WP_Theme_JSON if one …
ramonjd a00dbc5
forgot contect
ramonjd 6c4e410
updated fixtures
ramonjd c050b15
updated fixtures
ramonjd a2a26ec
Update class-wp-rest-global-styles-revisions-controller.php
ramonjd cfd08e8
Update rest-global-styles-revisions-controller.php
ramonjd 5c8052b
linter!
ramonjd 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
Next
Next commit
Adds an endpoint that returns revisions for the global styles custom …
…post. Adds tests
- Loading branch information
commit e421e6fae2cc977abaf61af73e06338e0ebebcc9
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
311 changes: 311 additions & 0 deletions
311
src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php
This file contains hidden or 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,311 @@ | ||
| <?php | ||
| /** | ||
| * REST API: WP_REST_Global_Styles_Revisions_Controller class | ||
| * | ||
| * @package WordPress | ||
| * @subpackage REST_API | ||
| * @since 6.3.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Core class used to access global styles revisions via the REST API. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @see WP_REST_Controller | ||
| */ | ||
| class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { | ||
| /** | ||
| * Parent post type. | ||
| * | ||
| * @since 6.3.0 | ||
| * @var string | ||
| */ | ||
| private $parent_post_type; | ||
|
|
||
| /** | ||
| * The base of the parent controller's route. | ||
| * | ||
| * @since 6.3.0 | ||
| * @var string | ||
| */ | ||
| private $parent_base; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @since 6.3.0 | ||
| */ | ||
| public function __construct() { | ||
| $this->parent_post_type = 'wp_global_styles'; | ||
| $this->rest_base = 'revisions'; | ||
| $this->parent_base = 'global-styles'; | ||
| $this->namespace = 'wp/v2'; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the controllers routes. | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @return void | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public function register_routes() { | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base, | ||
| array( | ||
| 'args' => array( | ||
| 'parent' => array( | ||
| 'description' => __( 'The ID for the parent of the revision.' ), | ||
| 'type' => 'integer', | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::READABLE, | ||
| 'callback' => array( $this, 'get_items' ), | ||
| 'permission_callback' => array( $this, 'get_item_permissions_check' ), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns revisions of the given global styles config custom post type. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @param WP_REST_Request $request The request instance. | ||
| * | ||
| * @return WP_REST_Response|WP_Error | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public function get_items( $request ) { | ||
| $parent = $this->get_parent( $request['parent'] ); | ||
|
|
||
| if ( is_wp_error( $parent ) ) { | ||
| return $parent; | ||
| } | ||
| $response = array(); | ||
| $raw_config = json_decode( $parent->post_content, true ); | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; | ||
|
|
||
| if ( $is_global_styles_user_theme_json ) { | ||
| $user_theme_revisions = wp_get_post_revisions( | ||
| $parent->ID, | ||
| array( | ||
| 'posts_per_page' => 100, | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! empty( $user_theme_revisions ) ) { | ||
| foreach ( $user_theme_revisions as $revision ) { | ||
| $revision = $this->prepare_item_for_response( $revision, $request ); | ||
| $response[] = $this->prepare_response_for_collection( $revision ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return rest_ensure_response( $response ); | ||
| } | ||
|
|
||
| /** | ||
| * Prepares the revision for the REST response. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @param WP_Post $item Post revision object. | ||
| * @param WP_REST_Request $request Request object. | ||
| * @return WP_REST_Response Response object. | ||
| */ | ||
| public function prepare_item_for_response( $item, $request ) { | ||
| $parent = $this->get_parent( $request['parent'] ); | ||
| // Retrieves global styles config as JSON. | ||
| $raw_revision_config = json_decode( $item->post_content, true ); | ||
| $config = ( new WP_Theme_JSON( $raw_revision_config, 'custom' ) )->get_raw_data(); | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Prepares item data. | ||
| $data = array(); | ||
| $fields = $this->get_fields_for_response( $request ); | ||
|
|
||
| if ( rest_is_field_included( 'author', $fields ) ) { | ||
| $data['author'] = (int) $item->post_author; | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'date', $fields ) ) { | ||
| $data['date'] = $item->post_date; | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if ( rest_is_field_included( 'date_gmt', $fields ) ) { | ||
| $data['date_gmt'] = $item->post_date_gmt; | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if ( rest_is_field_included( 'id', $fields ) ) { | ||
| $data['id'] = (int) $item->ID; | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'modified', $fields ) ) { | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $data['modified'] = $item->post_modified; | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'modified_gmt', $fields ) ) { | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $data['modified_gmt'] = $item->post_modified_gmt; | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'parent', $fields ) ) { | ||
| $data['parent'] = (int) $parent->ID; | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'settings', $fields ) ) { | ||
| $data['settings'] = ! empty( $config['settings'] ) ? $config['settings'] : new stdClass(); | ||
| } | ||
|
|
||
| if ( rest_is_field_included( 'styles', $fields ) ) { | ||
| $data['styles'] = ! empty( $config['styles'] ) ? $config['styles'] : new stdClass(); | ||
| } | ||
|
|
||
| $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | ||
| $data = $this->add_additional_fields_to_object( $data, $request ); | ||
| $data = $this->filter_response_by_context( $data, $context ); | ||
|
|
||
| return rest_ensure_response( $data ); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the revision's schema, conforming to JSON Schema. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
| public function get_item_schema() { | ||
| if ( $this->schema ) { | ||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
|
|
||
| $schema = array( | ||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'title' => "{$this->parent_post_type}-revision", | ||
| 'type' => 'object', | ||
| // Base properties for every revision. | ||
| 'properties' => array( | ||
|
|
||
| /* | ||
| * Adds settings and styles from the WP_REST_Revisions_Controller item fields. | ||
| * Leaves out GUID as global styles shouldn't be accessible via URL. | ||
| */ | ||
| 'author' => array( | ||
| 'description' => __( 'The ID for the author of the revision.' ), | ||
| 'type' => 'integer', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'date' => array( | ||
| 'description' => __( "The date the revision was published, in the site's timezone." ), | ||
| 'type' => 'string', | ||
| 'format' => 'date-time', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'date_gmt' => array( | ||
| 'description' => __( 'The date the revision was published, as GMT.' ), | ||
| 'type' => 'string', | ||
| 'format' => 'date-time', | ||
| 'context' => array( 'view', 'edit' ), | ||
| ), | ||
| 'id' => array( | ||
| 'description' => __( 'Unique identifier for the revision.' ), | ||
| 'type' => 'integer', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
| 'modified' => array( | ||
| 'description' => __( "The date the revision was last modified, in the site's timezone." ), | ||
| 'type' => 'string', | ||
| 'format' => 'date-time', | ||
| 'context' => array( 'view', 'edit' ), | ||
| ), | ||
| 'modified_gmt' => array( | ||
| 'description' => __( 'The date the revision was last modified, as GMT.' ), | ||
| 'type' => 'string', | ||
| 'format' => 'date-time', | ||
| 'context' => array( 'view', 'edit' ), | ||
| ), | ||
| 'parent' => array( | ||
| 'description' => __( 'The ID for the parent of the revision.' ), | ||
| 'type' => 'integer', | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
| ), | ||
|
|
||
| // Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema. | ||
| 'styles' => array( | ||
| 'description' => __( 'Global styles.' ), | ||
| 'type' => array( 'object' ), | ||
| 'context' => array( 'view', 'edit' ), | ||
| ), | ||
| 'settings' => array( | ||
| 'description' => __( 'Global settings.' ), | ||
| 'type' => array( 'object' ), | ||
| 'context' => array( 'view', 'edit' ), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| $this->schema = $schema; | ||
|
|
||
| return $this->add_additional_fields_schema( $this->schema ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a given request has access to read a single global style. | ||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return true|WP_Error True if the request has read access, WP_Error object otherwise. | ||
| */ | ||
| public function get_item_permissions_check( $request ) { | ||
| $post = $this->get_parent( $request['parent'] ); | ||
| if ( is_wp_error( $post ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
| /* | ||
| * The same check as WP_REST_Global_Styles_Controller->get_item_permissions_check. | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| if ( ! current_user_can( 'read_post', $post->ID ) ) { | ||
| return new WP_Error( | ||
| 'rest_cannot_view', | ||
| __( 'Sorry, you are not allowed to view revisions for this global style.' ), | ||
| array( 'status' => rest_authorization_required_code() ) | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the parent post, if the ID is valid. Copied from WP_REST_Revisions_Controller. | ||
ramonjd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @since 6.3.0 | ||
| * | ||
| * @param int $parent_post_id Supplied ID. | ||
| * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. | ||
| */ | ||
| protected function get_parent( $parent_post_id ) { | ||
| $error = new WP_Error( | ||
| 'rest_post_invalid_parent', | ||
| __( 'Invalid post parent ID.' ), | ||
| array( 'status' => 404 ) | ||
| ); | ||
|
|
||
| if ( (int) $parent_post_id <= 0 ) { | ||
| return $error; | ||
| } | ||
|
|
||
| $parent_post = get_post( (int) $parent_post_id ); | ||
|
|
||
| if ( empty( $parent_post ) || empty( $parent_post->ID ) | ||
| || $this->parent_post_type !== $parent_post->post_type | ||
| ) { | ||
| return $error; | ||
| } | ||
|
|
||
| return $parent_post; | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.