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

Fix route conflicts for the auto-save and revisions endpoints of the wp_template and wp_template_part post types #5194

Conversation

anton-vlasenko
Copy link

@anton-vlasenko anton-vlasenko commented Sep 12, 2023

What and why?

Fixes https://core.trac.wordpress.org/ticket/56922.

This PR addresses route conflicts between WP_REST_Templates_Controller and both WP_REST_Revisions_Controller and WP_REST_Autosaves_Controller.

When making REST requests to the following endpoints:

  • wp/v2/templates/<parent_post_id>/revisions
  • wp/v2/templates/<parent_post_id>/autosaves
  • wp/v2/template-parts/<parent_post_id>/revisions
  • wp/v2/template-parts/<parent_post_id>/autosaves

the WP_REST_Templates_Controller::get_item() method is incorrectly matched as the callback method to process these requests. This misidentification arises from the regular expression that defines the problematic route in the WP_REST_Templates_Controller class, specifically:
/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+).

In actuality, the aforementioned requests should be handled by the WP_REST_Revisions_Controller::get_items() and WP_REST_Autosaves_Controller::get_items() callbacks, depending on whether it's a revisions or autosaves request.

How

This PR modifies the order in which the routes for endpoints in WP_REST_Templates_Controller, WP_REST_Revisions_Controller, and WP_REST_Autosaves_Controller are registered.

Routes in WP_REST_Revisions_Controller and WP_REST_Autosaves_Controller are now registered prior to those in WP_REST_Templates_Controller.

This ensures the correct callback is matched, preventing WP_REST_Templates_Controller::get_item() from being mistakenly matched for the endpoints listed above.

However, this change is limited to the wp_template and wp_template_parts post types to mitigate the risk of introducing new bugs.
Snímek obrazovky 2023-09-13 v 1 27 44

Snímek obrazovky 2023-09-13 v 1 28 13

@anton-vlasenko anton-vlasenko changed the title Enhanced route registration logic to introduce late registration for … Fix route conflicts for the auto-save and revisions endpoints of the wp_template and wp_template_part post types Sep 12, 2023
@anton-vlasenko anton-vlasenko force-pushed the fix/template-revision-autosave-endpoints branch from 6e20189 to 0d6ab54 Compare September 12, 2023 23:11
@anton-vlasenko anton-vlasenko marked this pull request as ready for review September 12, 2023 23:48
@spacedmonkey
Copy link
Member

Why register late? Why not register all post type controller like this?

@anton-vlasenko
Copy link
Author

Why register late? Why not register all post type controller like this?

I've replied in the ticket: https://core.trac.wordpress.org/ticket/56922#comment:13
Thank you.

@@ -5074,6 +5074,338 @@ mockedApiResponse.Schema = {
}
]
},
"/wp/v2/templates/(?P<parent>[\\d]+)/revisions": {
Copy link
Author

Choose a reason for hiding this comment

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

The changes in the wp-api-generated.js file are auto-generated, so probably it makes no sense to review them.

@anton-vlasenko anton-vlasenko force-pushed the fix/template-revision-autosave-endpoints branch 2 times, most recently from 343426f to e31904c Compare September 29, 2023 10:12
@anton-vlasenko anton-vlasenko force-pushed the fix/template-revision-autosave-endpoints branch from e31904c to 9f8d174 Compare September 29, 2023 10:12
@anton-vlasenko
Copy link
Author

Closed in favor of #3533.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants