[wp-trac] [WordPress Trac] #56922: Template / Template parts revision / autosave REST API are broken

WordPress Trac noreply at wordpress.org
Fri Oct 28 15:12:18 UTC 2022


#56922: Template / Template parts revision / autosave REST API are broken
--------------------------+-----------------------------
 Reporter:  spacedmonkey  |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  REST API      |     Version:  4.7
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:  rest-api
--------------------------+-----------------------------

Comment (by revgeorge):

 I think that the path `wp/v2/template-parts/1480/revisions` is pretty easy
 to fix, by adding `(?<!/revisions)` to the end of the route regex in
 `WP_REST_Templates_Controller`. This is a negative-lookbehind regex that
 says "the URL can't end in /revisions".

 The harder part is avoiding links to individual revisions like `wp/v2
 /template-parts/1480/revisions/4321`. Negative-lookbehinds must be fixed
 length, we can't do something like `(?<!/revisions/\d+)` because `\d+`
 could be any size and the regex engine doesn't want to backtrack an
 arbitrary number of steps.

 One thought I had was for the REST API to be able to use a callable as a
 route in addition to a regex. This would allow for more complex logic than
 a regex allows. However, it may make it more difficult to use
 `register_rest_route()` to override an existing route and make it easy to
 accidentally add big performance problems.

 Another option, that probably won't work due to backwards compatibility,
 would be to use "The Greatest Regex Trick Ever" (https://www.rexegg.com
 /regex-best-trick.html), which is to force regexes to include at least one
 capture group (something in parenthesis) and to ignore routes that simply
 match. This would allow us to have a regex like

 `\d+/revisions$|\d+/revisions/d+$|/templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)'[\/\w-]+',)`

 which would match on `\d+/revisions$` or `\d+/revisions/d+$` but, since
 they don't have a capture group, the updated router would treat them as
 non-matches. Existing routes without capture groups (e.g. `/templates`)
 would have to be updated though.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56922#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list