Skip to content

REST API: Add a route to restore a revision - #16315

Open
adamsilverstein wants to merge 2 commits into
WordPress:trunkfrom
adamsilverstein:add/rest-revision-restore-route
Open

adamsilverstein wants to merge 2 commits into
WordPress:trunkfrom
adamsilverstein:add/rest-revision-restore-route

Conversation

@adamsilverstein

Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/66865

Core side of WordPress/gutenberg#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 WordPress/gutenberg#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

The block editor restores a revision by copying its fields into a post update,
so `wp_restore_post_revision()` does not run: the `wp_restore_post_revision`
action does not fire and revisioned meta that is not shown in REST is not
restored.

Add `POST /wp/v2/<base>/<parent>/revisions/<id>/restore`, which restores through
`wp_restore_post_revision()` as the revisions screen does and returns the
restored parent in the `edit` context.

See #66865.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

A request value is untyped, and a template is addressed by a string ID where
other post types use an integer. Read the parent through a method that each
revisions controller implements for its own ID type, so that the restore route
passes PHPStan at level 10.

See #66865.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant