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

WordPress Trac noreply at wordpress.org
Mon Oct 9 22:25: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 needs-      |     Focuses:  rest-api
  testing changes-requested                      |
-------------------------------------------------+-------------------------

Comment (by antonvlasenko):

 == Test Report ==
 This report confirms that the specified PR addresses the issue.

 PR tested: https://github.com/WordPress/wordpress-develop/pull/3533

 === Environment ===
 - WordPress: 6.4-beta2-56769-src
 - PHP: 7.3.33
 - Server: Apache/2.4.57 (Unix) PHP/7.3.33
 - Database: mysqli (Server: 5.7.43)
 - Browser: Safari 17.0 (macOS 13.6)
 - Theme: Twenty Twenty-Three 1.2
 - MU-Plugins: None activated
 - Plugins:
   * JSON Basic Authentication 0.1

 === Steps to Reproduce ===
 1. Navigate to Appearance -> Editor.
 2. Create a new template part (also known as "pattern").
 3. Save the template part multiple times, making slight modifications to
 the template before each save. For instance, add new blocks or alter the
 text.
 4. Identify the ID of your template part in the `wp_posts` table. You can
 use the following SQL query to find the ID of the template part you just
 created:
 {{{
 SELECT
     CONCAT_WS('/',
             (SELECT
                     option_value
                 FROM
                     wp_options
                 WHERE
                     option_name = 'stylesheet'),
             post_name) AS template_part_id
 FROM
     wp_posts
 WHERE
     post_type = 'wp_template_part'
         AND post_status = 'publish'
 ORDER BY post_date DESC
 LIMIT 1;
 }}}
 5. Record the template part ID value.
 6. Install and activate the https://github.com/WP-API/Basic-Auth plugin to
 simplify the authentication of REST requests.
 7. Make a REST request to the template parts endpoint. Replace
 `admin:password` with your admin credentials and `<template_part_id>` with
 the ID value from step 5 (slash included).
 {{{
 #!bash
 curl http://path.to.your.wordpress.server/wp-json/wp/v2/template-
 parts/<template_part_id>/revisions -u "admin:password"
 }}}
 8. 🐞 The following response will be received:
 {{{
 #!js
 {"code":"rest_template_not_found","message":"No templates exist with that
 id.","data":{"status":404}}
 }}}

 === Actual Results ===
 When testing the patch to ensure it functions as intended:
 - ✅ A REST request should return a list of the revisions for your
 template part. For example:
 {{{
 #!js
 [{"author":1,"date":"2023-09-12T12:14:30","date_gmt":"2023-09-12T12:14:30","id":10,"modified":"2023-09-12T12:14:30","modified_gmt":"2023-09-12T12:14:30","parent":7,"slug":"7-revision-v1","guid":{"rendered":"http:\/\/wordpress.test\/?p=10","raw":"http:\/\/wordpress.test\/?p=10"},"title":{"raw":"Template
 Part","rendered":"Template Part"},"content":{"raw":"<!-- wp:paragraph
 -->\n<p>A simple p. #3<\/p>\n<!-- \/wp:paragraph -->","rendered":"\n<p>A
 simple p. #3<\/p>\n"},"_links":{"parent":[{"href":"http:\/\/wordpress.test
 \/wp-json\/wp\/v2\/template-
 parts\/7"}]}},{"author":1,"date":"2023-09-12T12:14:21","date_gmt":"2023-09-12T12:14:21","id":9,"modified":"2023-09-12T12:14:21","modified_gmt":"2023-09-12T12:14:21","parent":7,"slug":"7-revision-v1","guid":{"rendered":"http:\/\/wordpress.test\/?p=9","raw":"http:\/\/wordpress.test\/?p=9"},"title":{"raw":"Template
 Part","rendered":"Template Part"},"content":{"raw":"<!-- wp:paragraph
 -->\n<p>A simple p. #2<\/p>\n<!-- \/wp:paragraph -->","rendered":"\n<p>A
 simple p. #2<\/p>\n"},"_links":{"parent":[{"href":"http:\/\/wordpress.test
 \/wp-json\/wp\/v2\/template-
 parts\/7"}]}},{"author":1,"date":"2023-09-12T12:14:08","date_gmt":"2023-09-12T12:14:08","id":8,"modified":"2023-09-12T12:14:08","modified_gmt":"2023-09-12T12:14:08","parent":7,"slug":"7-revision-v1","guid":{"rendered":"http:\/\/wordpress.test\/?p=8","raw":"http:\/\/wordpress.test\/?p=8"},"title":{"raw":"Template
 Part","rendered":"Template Part"},"content":{"raw":"<!-- wp:paragraph
 -->\n<p>A simple p.<\/p>\n<!-- \/wp:paragraph -->","rendered":"\n<p>A
 simple p.<\/p>\n"},"_links":{"parent":[{"href":"http:\/\/wordpress.test
 \/wp-json\/wp\/v2\/template-parts\/7"}]}}]
 }}}

 === Notes===
 ⚠️ In conclusion, the proposed patch successfully addresses the issue.
 However, the controllers expect the `template_id` and `template_part_id`
 to always follow the format `theme_name//<template(_part)_name`. In
 practice, it's not possible to retrieve a template or template part using
 only their template name (i.e., `post_name`). The `get_block_template()`
 function, employed by both REST controllers, presumes that the ID is
 divided into two parts separated by `//`. The theme's name must always be
 included in the request alongside the template (part) name. Should we
 adjust the regex expressions for the REST routes to consistently expect
 the presence of the `theme_name`?

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


More information about the wp-trac mailing list