Skip to content

Revisions: Only include revisioned meta in revision REST responses - #85509

Merged
adamsilverstein merged 9 commits into
WordPress:trunkfrom
DevinWalker:fix/revisions-only-revisioned-meta
Oct 26, 2026
Merged

adamsilverstein merged 9 commits into
WordPress:trunkfrom
DevinWalker:fix/revisions-only-revisioned-meta

Conversation

@DevinWalker

Copy link
Copy Markdown
Contributor

What?

Closes #84809.

Only includes revisioned meta in revision and autosave REST responses, so restoring a revision in the editor stops overwriting meta that isn't revisioned. This brings the Core fix in WordPress/wordpress-develop#14359 to WordPress 7.0 and 7.1.

Why?

Visual Revisions restores a revision by reading it from the REST API and saving its meta onto the post. The revisions endpoint returns every show_in_rest meta key of the parent post type, but only the keys from wp_post_revision_meta_keys() are stored on a revision. The others come back as their default, or null, and the restore saves those over the post's real values.

For patterns, wp_pattern_sync_status comes back null. Restoring an unsynced pattern makes it synced, and restoring a synced pattern fails with "The wp_pattern_sync_status property has an invalid stored value, and cannot be updated to null." On other post types, any meta that isn't revisioned is quietly reset.

The Core fix (Trac #66329) is in review for 7.2. Gutenberg supports the last two WordPress releases, 7.0 and 7.1, and both have this bug, so @adamsilverstein suggested a compat filter in his review of the Core PR.

How?

Adds gutenberg_rest_only_include_revisioned_meta() to lib/compat/wordpress-7.2/rest-api.php, on the rest_prepare_revision filter.

  • It drops the meta keys that aren't in wp_post_revision_meta_keys() for the parent post type, including keys registered with a custom REST name.
  • Autosave responses are prepared through the revisions controller, so they're filtered too.
  • It skips objects that aren't revisions. When an author autosaves their own draft, the autosaves controller returns the post itself, which keeps all of its meta.
  • The keys are cast to an array and limited to strings, so a wp_post_revision_meta_keys filter that returns false doesn't break responses.
  • Once the Core fix lands, responses already only have revisioned keys, so this leaves them unchanged.

Unlike the Core PR, this only changes the response, not the meta schema.

Testing Instructions

  1. Add this as an mu-plugin:
    <?php
    add_action( 'init', function () {
    	register_post_meta( 'post', 'kept_meta', array(
    		'show_in_rest' => true,
    		'single'       => true,
    		'type'         => 'string',
    	) );
    } );
  2. Create a post and update it a couple of times so it has revisions, then run npm run wp-env run cli -- wp post meta update <ID> kept_meta hello.
  3. With "Custom fields" turned off in the editor Preferences, open Revisions, move to an earlier revision, and click "Restore".
    • Before: kept_meta is empty.
    • After: kept_meta is still hello.
  4. For patterns, create an unsynced pattern, save a few edits, and restore an earlier revision.
    • Before: the pattern becomes synced. On a synced pattern, the restore shows an error.
    • After: the pattern keeps its sync status and the restore works.

New tests in phpunit/rest-only-include-revisioned-meta-test.php (8 cases), run with npm run test:unit:php:base -- --filter Gutenberg_REST_Only_Include_Revisioned_Meta_Test. Without the filter, the 5 that cover the bug fail. The other 3 check that keys aren't dropped: an author autosaving their own draft, keys added with the filter, and keys with a custom REST name.

I also ran the restore the way restoreRevision does it (read the revision with context=edit, then save its content and meta to the post) on WordPress 7.2-beta1, which doesn't include the Core fix yet:

without this PR with this PR
Post with a non-revisioned kept_meta set to hello kept_meta is '' after restoring stays hello
Unsynced pattern becomes synced stays unsynced
Synced pattern 500 rest_invalid_stored_value 200, restored

Testing Instructions for Keyboard

No UI changes.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Writing the compat filter and the PHPUnit tests, testing the restore against the REST API, and drafting this description. I reviewed the code and the test results before opening this PR.

The revisions endpoint returns every `show_in_rest` meta key of the parent post type, but only revisioned keys are stored on a revision. The others come back as their default, and restoring the revision in the editor saves those defaults over the post's values. Restoring an unsynced pattern makes it synced, and restoring a synced pattern fails.

Filter `rest_prepare_revision` to only keep the keys from `wp_post_revision_meta_keys()`, so WordPress 7.0 and 7.1 behave like the Core fix in WordPress/wordpress-develop#14359. Objects that aren't revisions are left alone, since an author autosaving their own draft gets the post itself back.
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: DevinWalker <dlocc@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: smithjw1 <smithjw1@git.wordpress.org>
Co-authored-by: umeshnevase <umeshnevase@git.wordpress.org>
Co-authored-by: prasadkarmalkar <prasadkarmalkar@git.wordpress.org>
Co-authored-by: davidsword <davidsword@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>

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 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.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@DevinWalker

Copy link
Copy Markdown
Contributor Author

@adamsilverstein could you add [Type] Bug here when you get a chance? I can't add labels on this repo. [Feature] History and Backport from WordPress Core probably fit too.

@adamsilverstein adamsilverstein added [Type] Bug An existing feature does not function as intended [Feature] History History, undo, redo, revisions, autosave. labels Oct 25, 2026

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR @DevinWalker

I tested things against WordPress trunk (doesn't have the fix in yet)

I went through the test steps in #84809

Some other test results:

Before After
Post with a non-revisioned kept_meta set to hello Restoring saves kept_meta as '' Stays hello
Unsynced pattern Becomes synced Stays unsynced
Synced pattern Error code 500: "The wp_pattern_sync_status property has an invalid stored value, and cannot be updated to null." Restores without errors

Looks faithful to the Core backport too.

Nit:

This PR says it brings the Core fix to 7.0 and 7.1, but it only reaches sites running the Gutenberg plugin, and WordPress/wordpress-develop#14359 is still open.

LGTM

Comment thread lib/compat/wordpress-7.2/rest-api.php Resolved

@talldan talldan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! A few small nits, and then I have some concerns that are a bit outside the scope of this PR in one of the comments. I guess I'll make an issue for that.

Thanks for tackling this and the core ticket.

Comment thread lib/compat/wordpress-7.2/rest-api.php Resolved
Comment thread lib/compat/wordpress-7.2/rest-api.php Resolved
Comment thread lib/compat/wordpress-7.2/rest-api.php Resolved
@adamsilverstein
adamsilverstein self-requested a review October 26, 2026 17:22
@adamsilverstein

Copy link
Copy Markdown
Member

@DevinWalker It would be great to address the final review items from @talldan and then we can land this ahead of 7.2 Beta 2 tomorrow. Thanks for all your work here so far, happy to help wrap this up if you don't have the bandwidth, just let me know!

@smithjw1

Copy link
Copy Markdown
Contributor

Want to shout out @adamsilverstein, @DevinWalker, and @talldan for this and related work. Awesome to see everyone moving quickly to squash the bug!

@adamsilverstein

Copy link
Copy Markdown
Member

I'm going to review the remaining feedback points to get this merged.

adamsilverstein and others added 3 commits October 26, 2026 14:01
Return early when the filtered response isn't a WP_REST_Response,
check for non-string meta keys inside the loop instead of a separate
array_filter() pass, and rename $meta_keys to $meta_args since
get_registered_meta_keys() returns the registration args.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WiQMWxychhmQU1tfEJdsBM
get_post_type() returns false for an orphaned revision, which was passed
to get_registered_meta_keys() and the wp_post_revision_meta_keys filter
and left the response with no meta.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WiQMWxychhmQU1tfEJdsBM
Check that revisioned meta is still returned for autosaves, so a filter
that drops all meta would fail, and cover draft autosaves that create a
revision: another user's draft, and the author's own draft under
real-time collaboration. Also cover a revision with no parent post.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WiQMWxychhmQU1tfEJdsBM

@adamsilverstein adamsilverstein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Delightful

@adamsilverstein

Copy link
Copy Markdown
Member

Planning to merge this once CI is green, doing some manual testing in the meantime.

@adamsilverstein
adamsilverstein enabled auto-merge (squash) October 26, 2026 22:25
@adamsilverstein
adamsilverstein merged commit 9c1c428 into WordPress:trunk Oct 26, 2026
74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] History History, undo, redo, revisions, autosave. [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Patterns: Restoring revisions is broken

5 participants