Skip to content

REST API: Fix WP_REST_Autosaves_Controller silently preserving stale revision field#12675

Open
himanshupathak95 wants to merge 8 commits into
WordPress:trunkfrom
himanshupathak95:fix/65694
Open

REST API: Fix WP_REST_Autosaves_Controller silently preserving stale revision field#12675
himanshupathak95 wants to merge 8 commits into
WordPress:trunkfrom
himanshupathak95:fix/65694

Conversation

@himanshupathak95

@himanshupathak95 himanshupathak95 commented Jul 24, 2026

Copy link
Copy Markdown

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

When a post field (e.g. post_title) is reverted to the published value and an autosave is triggered via the REST API, WP_REST_Autosaves_Controller::create_post_autosave() silently skips the write if the submitted value matches the parent post — even when the existing autosave still holds a stale value from a prior edit.

The guard:

if ( ! $autosave_is_different && $old_autosave ) {
    return $old_autosave->ID;
}

compares incoming data against the parent post only. When that comparison returns equal, it returns the existing autosave ID without checking whether that autosave itself is already consistent with what was submitted.

Reproduce -

  • Save an autosave with title "My Post !!", then submit another with "My Post" (matching the published title).
  • The controller returns the old revision still holding "My Post !!" and no subsequent autosave can fix it, because every request with the correct title hits the same early return.

This PR expands the early-return guard to additionally compare the submitted fields against the existing autosave revision's fields using the same normalize_whitespace() loop already used earlier in the method. Only skip the update when the autosave is already consistent with the submitted data.

Testing

  • Manually reproduced the stale-title state and confirmed it is resolved with the fix.
  • New PHPUnit test covers the exact sequence from the ticket: modified autosave → revert → assert autosave is updated, tested by -
npm run test:php -- --filter=WP_Test_REST_Autosaves_Controller

Copilot AI review requested due to automatic review settings July 24, 2026 07:02
@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.

Pull request overview

This PR updates WP_REST_Autosaves_Controller::create_post_autosave() to prevent REST autosave updates from leaving stale values in an existing autosave revision (notably when a field is reverted back to the published post’s value), which impacts consumers like Preview that read from the autosave revision.

Changes:

  • Switches the “is this autosave different?” comparison to use the existing autosave revision (when present) rather than the published post.
  • Adds a meta-diff path against the existing autosave revision’s meta (when present) before deciding “nothing to save”.
  • Updates inline documentation to reflect the new comparison behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +415 to 419
if ( ! $autosave_is_different && ! empty( $meta ) ) {
foreach ( $revisioned_meta_keys as $meta_key ) {
$old_meta_value = get_metadata_raw( 'post', $old_autosave->ID, $meta_key, true );
$new_meta_value = $meta[ $meta_key ] ?? '';

Comment on lines +399 to +406
if ( $old_autosave ) {
/*
* When an autosave revision already exists, compare the incoming data
* against its current field values rather than the published post. A field
* reverted to the published value - such as a title edit that was undone -
* must still overwrite any stale value in the existing autosave, otherwise
* consumers like Preview will display incorrect content indefinitely.
*/
Copilot AI review requested due to automatic review settings July 24, 2026 07:38
@himanshupathak95
himanshupathak95 marked this pull request as ready for review July 24, 2026 07:39
@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. You can also manually ask me to refresh this list by adding the props-bot label.

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

Props abcd95.

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

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +373 to +374
* @since 7.1.0 Compares incoming data against the existing autosave instead
* of the published post.
Comment on lines +938 to +949
$controller->create_post_autosave( array(
'ID' => $post_id,
'post_title' => 'Original Title !!',
) );

$controller->create_post_autosave( array(
'ID' => $post_id,
'post_title' => 'Original Title',
) );

$autosave = wp_get_post_autosave( $post_id, self::$editor_id );
$this->assertSame( 'Original Title', $autosave->post_title );
Copilot AI review requested due to automatic review settings July 24, 2026 07:42

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php:374

  • The new @since note is misleading: this method does not always compare against an existing autosave, and it can apply to any saved post status (not just published). Updating this wording will keep the inline docs accurate for future maintainers.
	 * @since 7.1.0 Compares incoming data against the existing autosave instead
	 *              of the published post.

Comment on lines +416 to +419
foreach ( $revisioned_meta_keys as $meta_key ) {
if ( ! array_key_exists( $meta_key, $meta ) ) {
continue;
}
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