Repository navigation
Revisions: Only include revisioned meta in revision REST responses - #85509
adamsilverstein merged 9 commits into
Conversation
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.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
|
@adamsilverstein could you add |
ramonjd
left a comment
There was a problem hiding this comment.
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
talldan
left a comment
There was a problem hiding this comment.
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.
|
@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! |
|
Want to shout out @adamsilverstein, @DevinWalker, and @talldan for this and related work. Awesome to see everyone moving quickly to squash the bug! |
|
I'm going to review the remaining feedback points to get this merged. |
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
|
Planning to merge this once CI is green, doing some manual testing in the meantime. |
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
metaonto the post. The revisions endpoint returns everyshow_in_restmeta key of the parent post type, but only the keys fromwp_post_revision_meta_keys()are stored on a revision. The others come back as their default, ornull, and the restore saves those over the post's real values.For patterns,
wp_pattern_sync_statuscomes backnull. 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()tolib/compat/wordpress-7.2/rest-api.php, on therest_prepare_revisionfilter.metakeys that aren't inwp_post_revision_meta_keys()for the parent post type, including keys registered with a custom REST name.wp_post_revision_meta_keysfilter that returnsfalsedoesn't break responses.Unlike the Core PR, this only changes the response, not the
metaschema.Testing Instructions
npm run wp-env run cli -- wp post meta update <ID> kept_meta hello.kept_metais empty.kept_metais stillhello.New tests in
phpunit/rest-only-include-revisioned-meta-test.php(8 cases), run withnpm 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
restoreRevisiondoes it (read the revision withcontext=edit, then save its content andmetato the post) on WordPress 7.2-beta1, which doesn't include the Core fix yet:kept_metaset tohellokept_metais''after restoringhellounsyncedrest_invalid_stored_valueTesting 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.