Make WordPress Core

Opened 23 hours ago

Last modified 23 hours ago

#66865 new enhancement

REST API: Add a route to restore a revision

Reported by: adamsilverstein Owned by:
Priority: normal Milestone: Awaiting Review
Component: REST API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The REST API can list, read and delete revisions, but it can not restore one. Gutenberg adds a restore route in ​https://github.com/WordPress/gutenberg/pull/82158 and that route needs to come to core.

Background and proposal, put together by Claude (Fable 5.2):

The in-editor revisions screen of the block editor restores a revision by copying its fields into an ordinary post update. wp_restore_post_revision() never runs, so the wp_restore_post_revision action does not fire and revisioned meta that is not shown in REST is not restored. See ​https://github.com/WordPress/gutenberg/issues/85549.

Proposal

Add POST /wp/v2/<base>/<parent>/revisions/<id>/restore for every post type with revisions, templates and template parts included. The route:

  • requires the edit_post capability on the parent,
  • returns a 404 when the revision does not exist or does not belong to the parent,
  • only restores autosaves while revisions are disabled for the post, as wp-admin/revision.php does,
  • calls wp_restore_post_revision(), so the restore is the same one the classic revisions screen and XML-RPC run,
  • returns the restored parent as its own endpoint returns it in the edit context, so the editor can show it without another request.

Global styles revisions are not included, they are not restored through wp_restore_post_revision().

Related: #66859, where the revision stored by a restore holds the meta from before the restore. The Gutenberg route works around that. The core route does not need to once that ticket is fixed.

A PR is on the way.

AI Use

Claude (Fable 5.2) wrote the Gutenberg route and this ticket. (Ticket only, the core PR follows.)

Generated with ​Claude Code

Change History (1)

This ticket was mentioned in ​PR #16315 on ​WordPress/wordpress-develop by ​@adamsilverstein.


23 hours ago
#1

  • Keywords has-patch has-unit-tests added

Core side of ​https://github.com/WordPress/gutenberg/pull/82158.

_Claude Code (Fable 5.2) ported the route from the Gutenberg PR and wrote this description:_

The block editor restores a revision by copying its fields into an ordinary post update. wp_restore_post_revision() never runs, so the wp_restore_post_revision action does not fire and revisioned meta that is not shown in REST is not restored. See ​https://github.com/WordPress/gutenberg/issues/85549.

This adds POST /wp/v2/<base>/<parent>/revisions/<id>/restore to WP_REST_Revisions_Controller and WP_REST_Template_Revisions_Controller, so every post type with revisions gets it, templates and template parts included. Global styles revisions register their own routes and do not get it.

The route:

  • requires the edit_post capability on the parent,
  • returns a 404 when the revision does not exist, is not a revision, or does not belong to the parent,
  • returns a 400 when revisions are disabled for the post and the revision is not an autosave, as wp-admin/revision.php only restores autosaves then,
  • calls wp_restore_post_revision(), the same restore the revisions screen and XML-RPC run,
  • returns the restored parent as its own endpoint returns it in the edit context.

Differences from the Gutenberg version

  • Gutenberg registers a standalone controller, here the route lives in the revisions controllers. The template controller overrides prepare_restored_parent_for_response() to return the template.
  • The Gutenberg route works around #66859, where the revision stored by a restore holds the meta from before the restore. This PR does not carry that workaround, https://core.trac.wordpress.org/ticket/66859 fixes it in wp_restore_post_revision().

Testing

  1. Save a post twice with different content, so it has two revisions.
  2. POST /wp/v2/posts/<id>/revisions/<first revision id>/restore.
  3. The response is the post with the first content, and the wp_restore_post_revision action has fired.

PHPUnit: 12 tests in Tests_REST_wpRestRevisionsControllerRestore, covering the restore of post fields and meta that is not shown in REST, the action firing once, the returned post, permissions, mismatched and unknown revisions, disabled revisions and a template revision. They pass locally.

tests/qunit/fixtures/wp-api-generated.js was edited by hand to add the six new routes, a local regeneration brought in unrelated differences.

AI Use

Claude Code (Fable 5.2) wrote the route, the tests and this description. I will review and test.

🤖 Generated with ​Claude Code

Note: See TracTickets for help on using tickets.