[wp-trac] [WordPress Trac] #56922: Template / Template parts revision / autosave REST API are broken
WordPress Trac
noreply at wordpress.org
Tue Sep 12 23:42:16 UTC 2023
#56922: Template / Template parts revision / autosave REST API are broken
--------------------------------------+-----------------------
Reporter: spacedmonkey | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: 6.4
Component: REST API | Version: 4.7
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses: rest-api
--------------------------------------+-----------------------
Comment (by antonvlasenko):
Thank you for the information, @spacedmonkey.
I suggest considering the PR I've just submitted -
https://github.com/WordPress/wordpress-develop/pull/5194 - as an
alternative solution.
I believe it addresses the issue, assuming my understanding of it is
accurate.
=== What and why?
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 handle 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.
[[Image(https://cldup.com/QSPhTXRi05.png)]]
[[Image(https://cldup.com/UwM6gol7iu.png)]]
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56922#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list