Skip to content

Revisions: Store the restored meta in the revision of a restore - #16314

Open
adamsilverstein wants to merge 2 commits into
WordPress:trunkfrom
adamsilverstein:fix/66859-restore-revision-stale-meta
Open

adamsilverstein wants to merge 2 commits into
WordPress:trunkfrom
adamsilverstein:fix/66859-restore-revision-stale-meta

Conversation

@adamsilverstein

Copy link
Copy Markdown
Member

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

Claude Code (Fable 5.2) wrote the tests, the fix and this description:

Restoring a revision stores a new revision that records the restore. That revision holds the restored post fields, and the revisioned meta from before the restore. Footnotes are revisioned meta, so a restored post with footnotes gets a revision with the wrong footnotes.

Steps to reproduce

  1. Add a post with a paragraph that reads "First text." and add a footnote to it that reads "First footnote". Publish.
  2. Change the paragraph to "Second text." and the footnote to "Second footnote". Save.
  3. Open the revisions screen, select the revision with "First text." and click "Restore This Revision".
  4. The post is right: "First text." with "First footnote".
  5. Open the revisions screen again and look at the newest revision, the one the restore stored.

On trunk the comparison shows the content going from "Second text." to "First text." and no Footnotes change: the new revision still holds "Second footnote". Over REST, GET /wp/v2/posts/<id>/revisions shows the same thing in meta.footnotes of the newest revision. It should hold "First footnote", like the post.

Cause

wp_restore_post_revision() calls wp_update_post(), which stores the revision on wp_after_insert_post, and only then fires the wp_restore_post_revision action where wp_restore_post_revision_meta() restores the meta. The revision is stored while the post still has its old meta.

Fix

wp_restore_post_revision() now updates the post without firing the after-insert hooks, and calls wp_after_insert_post() itself once the action has run. This is what WP_REST_Posts_Controller::update_item() does to get meta into the revision of a REST save.

It also fixes the related case from the ticket: a restore that only changes meta stored no revision, and now stores one.

One change in behavior: during a restore the wp_after_insert_post action now fires after the wp_restore_post_revision action and after _edit_last is updated, where it used to fire before both. It still fires once for the post.

Tests

  • The first commit adds an e2e test, tests/e2e/specs/restore-revision-footnotes.test.js. It saves a post with a footnote twice, restores the first revision on the revisions screen and checks the footnotes of the newest revision. It fails on trunk with Expected "First footnote", Received "Second footnote" and passes with the fix.
  • Three PHPUnit tests in Tests_Post_MetaRevisions: the new revision holds the restored meta, a meta only restore stores a revision, and wp_after_insert_post fires once with the meta restored. All three fail without the fix.

The e2e test was run against a nightly build (7.3-alpha-65819) with and without the patched revision.php. The revision, post, xmlrpc and restapi PHPUnit groups pass locally.

AI Use

Claude Code (Fable 5.2) found the cause, wrote the tests and the fix, and drafted this description. I will review and test.

🤖 Generated with Claude Code

Restoring a revision stores a new revision that holds the restored content
and the footnotes from before the restore. The test fails until that is fixed.

See #66859.
`wp_restore_post_revision()` updated the post, which stored a new revision,
before the `wp_restore_post_revision` action restored the revisioned meta. The
new revision was left with the meta from before the restore, and a restore that
only changed meta stored no revision at all.

Fire the after-insert hooks once the meta is restored, as the REST API does
when it updates a post, so that the revision is stored with the restored meta.

Fixes #66859.
@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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The attachment restore path still fires wp_after_insert_post during wp_insert_attachment and again afterward, potentially storing a revision before metadata restoration and violating the required single post-restore hook invocation.

1 open issue
What changed in this PR

Updates revision restoration so the newly stored revision captures restored post metadata, including footnotes, while ensuring metadata-only restores create a revision.

Changes:

  • Defers wp_after_insert_post until after revisioned metadata is restored.
  • Adds PHPUnit coverage for restored metadata, metadata-only restores, and hook ordering.
  • Adds an end-to-end footnotes regression test covering the admin restore flow and REST revision data.
File Description
tests/​phpunit/​tests/​post/​metaRevisions.php Adds PHPUnit tests for restored revision metadata, metadata-only restores, and wp_after_insert_post behavior.
tests/​e2e/​specs/​restore-revision-footnotes.test.js Adds an end-to-end test verifying restored footnotes through the revisions interface and REST API.
src/​wp-includes/​revision.php Adjusts revision restoration sequencing so restored metadata is applied before the resulting revision and post-insert hook are processed.

Review effort: Lite

Comment thread src/wp-includes/revision.php Resolved
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.

2 participants