-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Global styles controllers: extend with WP_REST_Posts_Controller and WP_REST_Revisions_Controller #5699
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
14
commits into
WordPress:trunk
from
ramonjd:update/global-styles-revision-extends-revisions-controller
Closed
Global styles controllers: extend with WP_REST_Posts_Controller and WP_REST_Revisions_Controller #5699
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7d192d8
Initial commit.
ramonjd d7b4c1c
Adding title to schema.
ramonjd 41c98a5
Remove permissions checks and favour parent's
ramonjd 59d6953
Remove duplicated tests
ramonjd dbcc3b5
Reverting post type registration changes as it assigns an autosave co…
ramonjd 25cbe83
Regenerated fixture
ramonjd e9d1f10
Revert introducing title in schema
ramonjd ec4229f
Reinstating global styles revision controller tests that were duplica…
ramonjd ca524b2
Update src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles…
ramonjd 93190ae
Excluding search, include and exclude from the collection query params
ramonjd b43344d
Now that https://github.com/WordPress/wordpress-develop/pull/5655 has…
ramonjd dac8e0b
Pulling across changes from https://github.com/WordPress/wordpress-de…
ramonjd acd2590
Implementing latest round of feedback:
ramonjd dfa02f8
Apply suggestions from code review
spacedmonkey 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 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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,25 +10,14 @@ | |||||
| /** | ||||||
| * Base Global Styles REST API Controller. | ||||||
| */ | ||||||
| class WP_REST_Global_Styles_Controller extends WP_REST_Controller { | ||||||
|
|
||||||
| class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller { | ||||||
| /** | ||||||
| * Post type. | ||||||
| * Whether the controller supports batching. | ||||||
| * | ||||||
| * @since 5.9.0 | ||||||
| * @var string | ||||||
| */ | ||||||
| protected $post_type; | ||||||
|
|
||||||
| /** | ||||||
| * Constructor. | ||||||
| * @since 5.9.0 | ||||||
| * @since 6.5.0 | ||||||
| * @var array | ||||||
| */ | ||||||
| public function __construct() { | ||||||
| $this->namespace = 'wp/v2'; | ||||||
| $this->rest_base = 'global-styles'; | ||||||
| $this->post_type = 'wp_global_styles'; | ||||||
| } | ||||||
| protected $allow_batch = array( 'v1' => false ); | ||||||
|
|
||||||
| /** | ||||||
| * Registers the controllers routes. | ||||||
|
|
@@ -194,28 +183,10 @@ public function get_item_permissions_check( $request ) { | |||||
| * @param WP_Post $post Post object. | ||||||
| * @return bool Whether the post can be read. | ||||||
| */ | ||||||
| protected function check_read_permission( $post ) { | ||||||
| public function check_read_permission( $post ) { | ||||||
| return current_user_can( 'read_post', $post->ID ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Returns the given global styles config. | ||||||
| * | ||||||
| * @since 5.9.0 | ||||||
| * | ||||||
| * @param WP_REST_Request $request The request instance. | ||||||
| * | ||||||
| * @return WP_REST_Response|WP_Error | ||||||
| */ | ||||||
| public function get_item( $request ) { | ||||||
| $post = $this->get_post( $request['id'] ); | ||||||
| if ( is_wp_error( $post ) ) { | ||||||
| return $post; | ||||||
| } | ||||||
|
|
||||||
| return $this->prepare_item_for_response( $post, $request ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a given request has access to write a single global styles config. | ||||||
| * | ||||||
|
|
@@ -241,55 +212,6 @@ public function update_item_permissions_check( $request ) { | |||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a global style can be edited. | ||||||
| * | ||||||
| * @since 5.9.0 | ||||||
| * | ||||||
| * @param WP_Post $post Post object. | ||||||
| * @return bool Whether the post can be edited. | ||||||
| */ | ||||||
| protected function check_update_permission( $post ) { | ||||||
| return current_user_can( 'edit_post', $post->ID ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Updates a single global style config. | ||||||
| * | ||||||
| * @since 5.9.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 update_item( $request ) { | ||||||
| $post_before = $this->get_post( $request['id'] ); | ||||||
| if ( is_wp_error( $post_before ) ) { | ||||||
| return $post_before; | ||||||
| } | ||||||
|
|
||||||
| $changes = $this->prepare_item_for_database( $request ); | ||||||
| if ( is_wp_error( $changes ) ) { | ||||||
| return $changes; | ||||||
| } | ||||||
|
|
||||||
| $result = wp_update_post( wp_slash( (array) $changes ), true, false ); | ||||||
| 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; | ||||||
| } | ||||||
|
|
||||||
| wp_after_insert_post( $post, true, $post_before ); | ||||||
|
|
||||||
| $response = $this->prepare_item_for_response( $post, $request ); | ||||||
|
|
||||||
| return rest_ensure_response( $response ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Prepares a single global styles config for update. | ||||||
| * | ||||||
|
|
@@ -407,7 +329,7 @@ public function prepare_item_for_response( $post, $request ) { | |||||
| $links = $this->prepare_links( $post->ID ); | ||||||
| $response->add_links( $links ); | ||||||
| if ( ! empty( $links['self']['href'] ) ) { | ||||||
| $actions = $this->get_available_actions(); | ||||||
| $actions = $this->get_available_actions( $post, $request ); | ||||||
| $self = $links['self']['href']; | ||||||
| foreach ( $actions as $rel ) { | ||||||
| $response->add_link( $rel, $self ); | ||||||
|
|
@@ -431,9 +353,12 @@ protected function prepare_links( $id ) { | |||||
| $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); | ||||||
|
|
||||||
| $links = array( | ||||||
| 'self' => array( | ||||||
| 'self' => array( | ||||||
| 'href' => rest_url( trailingslashit( $base ) . $id ), | ||||||
| ), | ||||||
| 'about' => array( | ||||||
| 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), | ||||||
| ), | ||||||
| ); | ||||||
|
|
||||||
| if ( post_type_supports( $this->post_type, 'revisions' ) ) { | ||||||
|
|
@@ -454,13 +379,16 @@ protected function prepare_links( $id ) { | |||||
| * | ||||||
| * @since 5.9.0 | ||||||
| * @since 6.2.0 Added 'edit-css' action. | ||||||
| * @since 6.5.0 Add $post and $request parameters. | ||||||
|
Member
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.
Suggested change
|
||||||
| * | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| * @param WP_Post $post Post object. | ||||||
| * @param WP_REST_Request $request Request object. | ||||||
| * @return array List of link relations. | ||||||
| */ | ||||||
| protected function get_available_actions() { | ||||||
| protected function get_available_actions( $post, $request ) { | ||||||
|
Member
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. Missing |
||||||
| $rels = array(); | ||||||
|
|
||||||
| $post_type = get_post_type_object( $this->post_type ); | ||||||
| $post_type = get_post_type_object( $post->post_type ); | ||||||
| if ( current_user_can( $post_type->cap->publish_posts ) ) { | ||||||
| $rels[] = 'https://api.w.org/action-publish'; | ||||||
| } | ||||||
|
|
@@ -472,21 +400,6 @@ protected function get_available_actions() { | |||||
| return $rels; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Overwrites the default protected title format. | ||||||
| * | ||||||
| * By default, WordPress will show password protected posts with a title of | ||||||
| * "Protected: %s", as the REST API communicates the protected status of a post | ||||||
| * in a machine readable format, we remove the "Protected: " prefix. | ||||||
| * | ||||||
| * @since 5.9.0 | ||||||
| * | ||||||
| * @return string Protected title format. | ||||||
| */ | ||||||
| public function protected_title_format() { | ||||||
| return '%s'; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Retrieves the query params for the global styles collection. | ||||||
| * | ||||||
|
|
||||||
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.