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

Revisions Controller: return single revision only when it matches the parent id #5655

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Nov 10, 2023

This patch adds a condition to WP_REST_Revisions_Controller->get_item that checks the revision parent id against the supplied parent id.

If it doesn't match, the method returns a 404.

This is to ensure that get_item does not return a revision whose parent does not match the parent route fragment.

Test coverage added.

Run tests: npm run test:php -- --filter WP_Test_REST_Revisions_Controller

Commit 1a7ff6c demonstrates that currently, get_item() can return the revision of different parent post of that same type.

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for following up with this fix! Looks like a good approach to me, just left a tiny comment about the value used in the error message.

…se parent does not match the `parent` route fragment.
…t the supplied parent id.

If it doesn't match, return a 404
Updated test coverage

Update error message in class-wp-rest-revisions-controller.php
@ramonjd ramonjd force-pushed the try/revisions-controller-get-item-valid-parent branch from 9f528bf to 6fdc982 Compare November 13, 2023 23:11
Copy link
Member

@danielbachhuber danielbachhuber 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! Thanks for your work on this, @ramonjd

I'll let @adamsilverstein give it a final review.

string replacement type signifier %d

Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co>
@ramonjd
Copy link
Member Author

ramonjd commented Nov 19, 2023

Thanks, folks. I appreciate the testing and reviews!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Nice work!

@jonnynews
Copy link

@ramonjd Has this change been tested with https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php controller that extends this controller?

@ramonjd
Copy link
Member Author

ramonjd commented Nov 27, 2023

Has this change been tested with https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php controller that extends this controller?

Good point. I can add a relevant unit test to the WP_REST_Template_Revisions_Controller test suite.

@ramonjd
Copy link
Member Author

ramonjd commented Nov 28, 2023

I can confirm that we'll need to overwrite get_item() in WP_REST_Template_Revisions_Controller to account for the wp_id > parent relationship between template record and their revisions.

Screenshot 2023-11-28 at 1 56 16 pm

I'll do this and add a test to this PR since it's related, and also check any other post types that extend this controller.

No. I was mixing up the results of get_post and WP_REST_Template_Revisions_Controller.

See below.

@ramonjd
Copy link
Member Author

ramonjd commented Nov 28, 2023

I can confirm that we'll need to overwrite get_item() in WP_REST_Template_Revisions_Controller to account for the wp_id > parent relationship between template record and their revisions.

Actually, withdraw that, I should have checked WP_REST_Revisions_Controller::get_item before posting.

The check in get_item that this PR introduces does so by comparing the results of get_post for both the parent and the revision.

The results of get_post is independent of the WP_REST_Template_Revisions_Controller which performs all the wp_id faffery: get_post will return integer IDs and parent IDs for template posts and revisions alike.

Regardless, WP_REST_Template_Revisions_Controller has unit tests for get_item, which would have failed otherwise.

I've also double checked other registered post types, none of which, wp_template and wp_template_part notwithstanding, define a custom revisions_rest_controller_class.

I'd submit, therefore, that this PR is fine as it is.

Thanks for the reviews!

spacedmonkey

This comment was marked as outdated.

@ramonjd
Copy link
Member Author

ramonjd commented Dec 19, 2023

Thanks a lot for the feedback @spacedmonkey! 🙇🏻 I'll get it done today or tomorrow.

Adding parent id revision parent_id mismatch invalid test to the templates revisions controller tests

Remove specific mention of "post" to discern between other post types, e.g., "template"
@ramonjd ramonjd force-pushed the try/revisions-controller-get-item-valid-parent branch from ae49d86 to ff84ef6 Compare December 20, 2023 19:59
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.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Excellent, thanks for all the tests!

@tellthemachines
Copy link
Contributor

Committed in r57222.

ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Jan 31, 2024
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Feb 7, 2024
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Feb 14, 2024
ramonjd added a commit to WordPress/gutenberg that referenced this pull request Feb 15, 2024
ramonjd added a commit to WordPress/gutenberg that referenced this pull request Feb 15, 2024
…59049)

This is required to sync with WordPress/wordpress-develop#5655

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request May 18, 2024
ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants