Skip to content
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 controllers: extend with WP_REST_Posts_Controller and WP_REST_Revisions_Controller #5699

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Nov 23, 2023

This change:

  • extends WP_REST_Global_Styles_Revisions_Controller with WP_REST_Revisions_Controller
  • extends WP_REST_Global_Styles_Controller with WP_REST_Posts_Controller

As part of this work, the PR also:

  • Removes shared public methods
  • Updates schema to use parent and parent controller's properties. Unsets unnecessary properties.
  • Updates relevant tests. Deletes dupes.

Trac ticket: https://core.trac.wordpress.org/ticket/60131

Related PRs/discussions

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ramonjd ramonjd force-pushed the update/global-styles-revision-extends-revisions-controller branch from 7e40ac4 to 4ffbee2 Compare December 22, 2023 03:44
@ramonjd
Copy link
Member Author

ramonjd commented Dec 22, 2023

@adamsilverstein No huge rush, but if you get a chance to take this for a test drive, I'd be very grateful 🙇🏻

@ramonjd
Copy link
Member Author

ramonjd commented Dec 22, 2023

I double checked and the failing E2E test is also failing on trunk:

[[chromium] › gutenberg-plugin.test.js:25:6 › Gutenberg plugin › should activate: tests/e2e/specs/gutenberg-plugin.test.js#L12](https://github.com/WordPress/wordpress-develop/pull/5699/files#annotation_16570999825)
  1) [chromium] › gutenberg-plugin.test.js:25:6 › Gutenberg plugin › should activate ───────────────

    Retry #1 ───────────────────────────────────────────────────────────────────────────────────────
    SyntaxError: Unexpected token '<', "<br />
    <b>"... is not valid JSON

      10 | 	test.beforeAll( async ( { requestUtils } ) => {
      11 | 		// Install Gutenberg plugin if it's not yet installed.
    > 12 | 		const pluginsMap = await requestUtils.getPluginsMap();
         | 		                   ^
      13 | 		if ( ! pluginsMap.gutenberg ) {
      14 | 			await requestUtils.rest( {
      15 | 				method: 'POST',

@spacedmonkey spacedmonkey self-requested a review December 27, 2023 21:41
Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave the unit tests and not delete them.

One issue here is the revisions controller currently guesses that the parent controller is a instance or sub class of post controller. Means means it expects some classes and properties that might not exist in WP_REST_Global_Styles_Controller.

@ramonjd ramonjd force-pushed the update/global-styles-revision-extends-revisions-controller branch from edc8162 to 1120502 Compare January 3, 2024 01:51
Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest worry about this WP_REST_Global_Styles_Controller does not extend the WP_REST_Posts_Controller. Future refactors would might meaning breaks here, as class methods / properties do not match. I feel like if we are going to make this change, we go the whole way and convert WP_REST_Global_Styles_Controller to extend WP_REST_Posts_Controller. Then we can use the work done in 1f51e1f, to regsiter custom post / revision controllers when registering the global post type. Then this manaul regsiteration can be removed here.

// Global Styles revisions.
$controller = new WP_REST_Global_Styles_Revisions_Controller();
$controller->register_routes();
// Global Styles.
$controller = new WP_REST_Global_Styles_Controller();
$controller->register_routes();

@ramonjd
Copy link
Member Author

ramonjd commented Jan 4, 2024

I feel like if we are going to make this change, we go the whole way and convert WP_REST_Global_Styles_Controller to extend WP_REST_Posts_Controller.

Thanks for the feedback.

Looks like this point was raised by @TimothyBJacobs back when the endpoint was introduced: WordPress/gutenberg#35801 (review)

I think in this case, considering how custom the global styles API is, I think it is fine to extend the base controller instead of using WP_REST_Posts_Controller.

@TimothyBJacobs do you think this is still the case? The "custom" nature of global styles, I think, still stands.

I'm happy to take a stab at it if we think inheriting WP_REST_Posts_Controller will bring more benefits as described above.

@jonnynews
Copy link

I'm happy to take a stab at it if we think inheriting WP_REST_Posts_Controller will bring more benefits as described above.

The big difference is now 1f51e1f is committed, we can make custom revisions endpoints. I think now we can have custom revisions, it register this as a post endpoint and revisions endpoint.

Let's try this change in another PR, see if it makes sense.

@ramonjd ramonjd force-pushed the update/global-styles-revision-extends-revisions-controller branch from 1120502 to 2275567 Compare January 31, 2024 21:34
@ramonjd
Copy link
Member Author

ramonjd commented Feb 2, 2024

Let's try this change in another PR, see if it makes sense.

Started here. More stuff and tests to come 👍🏻

Copy link

github-actions bot commented Feb 2, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @jonnynews.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, spacedmonkey, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd
Copy link
Member Author

ramonjd commented Feb 2, 2024

The big difference is now 1f51e1f is committed, we can make custom revisions endpoints. I think now we can have custom revisions, it register this as a post endpoint and revisions endpoint.

Works pretty well over at #6008, but I'm wondering if registering endpoints should also be optional - e.g., Global styles should be able to opt out of autosaves routes

Perhaps explicitly setting '${ autosave | revision }_rest_controller_class' to false when registering the post?

Ora a supports_autosaves, similar to supports_revisions? 🤷🏻

I'm happy to get a PR up to test this out.

@spacedmonkey
Copy link
Member

Closing in favour of #6022

@ramonjd
Copy link
Member Author

ramonjd commented Feb 5, 2024

Closing in favour of #6022

@spacedmonkey Just checking, this PR was working as a stand alone patch.

Did you close with the intention to port these changes over to #6022? Otherwise I think we should reopen.

@spacedmonkey
Copy link
Member

@spacedmonkey Just checking, this PR was working as a stand alone patch.

@ramonjd I thought we were aligned on #6022? I feel like #6022 is good to commit.

@ramonjd
Copy link
Member Author

ramonjd commented Feb 5, 2024

@ramonjd I thought we were aligned on #6022? I feel like #6022 is good to commit.

Definitely, but just not sure why this PR needed to be closed?

I think maybe our wires just got crossed 😅

This PR affects WP_REST_Global_Styles_Revisions_Controller (Revisions) not WP_REST_Global_Styles_Controller. I just assumed you were going to bundle them into #6022.

Happy to reapprove #6022 - it's good to go, but we still need this PR I think.

@ramonjd
Copy link
Member Author

ramonjd commented Feb 6, 2024

To be clear, #6022 needs to be part of this PR. Let's do this change in one commit.

Ah, got it. Thanks, for clearing that up.

#6022 seemed to work well on its own, but I can pull over the changes into this PR if that's your advice.

Thanks again.

@ramonjd ramonjd force-pushed the update/global-styles-revision-extends-revisions-controller branch from d4d7e82 to bf20794 Compare February 7, 2024 01:51
Updating generated API file
@ramonjd ramonjd force-pushed the update/global-styles-revision-extends-revisions-controller branch from bf20794 to dac8e0b Compare February 7, 2024 02:05
@ramonjd ramonjd changed the title Global styles revisions controller: extend with WP_REST_Revisions_Controller Global styles controllers: extend with WP_REST_Posts_Controller and WP_REST_Revisions_Controller Feb 7, 2024
Copy link

@jonnynews jonnynews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the contractor, get_item and update_item methods no longer need to be overridden in the global style controller.

I also had a note about marking this endpoint as not support batching.

I love this change, as it allows the gutenberg plugin to override these classes easier.

removing __construct, get_item and update_item in favour of the parent class methods

Add $allow_batch property, setting it to false
@ramonjd
Copy link
Member Author

ramonjd commented Feb 8, 2024

I believe the contractor, get_item and update_item methods no longer need to be overridden in the global style controller.

👍🏻 Done

What do you think about get_post() as well?

The functions are the identical, the only difference being the error message, which seems fairly samey.

In global styles controller:

		$error = new WP_Error(
			'rest_global_styles_not_found',
			__( 'No global styles config exist with that id.' ),
			array( 'status' => 404 )
		);

In post controller:

		$error = new WP_Error(
			'rest_post_invalid_id',
			__( 'Invalid post ID.' ),
			array( 'status' => 404 )
		);

Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@@ -457,10 +382,10 @@ protected function prepare_links( $id ) {
*
* @return array List of link relations.
*/
protected function get_available_actions() {
protected function get_available_actions( $post, $request ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @param documentation for new variables.

$this->rest_base = 'revisions';
$this->parent_base = 'global-styles';
$this->namespace = 'wp/v2';
public function __construct( $parent_post_type ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @param documentation for new variables.

Co-authored-by: Jonny Harris <148061917+jonnynews@users.noreply.github.com>
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 6.5.0 Add $post and $request parameters.
* @since 6.5.0 Added $post and $request parameters.

@spacedmonkey
Copy link
Member

Committed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants