Revisions: return existing autosave after saving with unchanged data.

Correctly return the existing autosave when an unchanged autosave is saved, instead of returning an error.

Fix regressions after r55154 where an error and not the original autosave was returned when saving with unchanged data (for example, clicking the preview button repeatedly). Returning the autosave (ID) is the expected behavior for the endpoint.

Follow up to [55154]

Props Mamaduka, jeroenrotty, mrfoxtalbot.
Fixes #58739.



git-svn-id: https://develop.svn.wordpress.org/trunk@56163 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2023-07-07 17:51:11 +00:00
parent a3b80f551f
commit 9781ffbca5
2 changed files with 19 additions and 15 deletions

View File

@ -371,19 +371,16 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
}
}
if ( ! $autosave_is_different ) {
return new WP_Error(
'rest_autosave_no_changes',
__( 'There is nothing to save. The autosave and the post content are the same.' ),
array( 'status' => 400 )
);
}
$user_id = get_current_user_id();
// Store one autosave per author. If there is already an autosave, overwrite it.
$old_autosave = wp_get_post_autosave( $post_id, $user_id );
if ( ! $autosave_is_different && $old_autosave ) {
// Nothing to save, return the existing autosave.
return $old_autosave->ID;
}
if ( $old_autosave ) {
$new_autosave['ID'] = $old_autosave->ID;
$new_autosave['post_author'] = $user_id;

View File

@ -688,22 +688,29 @@ class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle
'post_status' => 'publish',
);
$post_id = wp_insert_post( $post_data );
wp_set_current_user( self::$editor_id );
// Make a small change create the initial autosave.
$autosave_data = array(
'post_content' => $post_data['post_content'],
'post_content' => 'Test post content changed',
);
// Create autosaves response.
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( wp_json_encode( $autosave_data ) );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
// Store the first autosave ID.
$autosave = $response->get_data();
// Try creating an autosave using the REST endpoint with unchanged content.
$request->set_body( wp_json_encode( $autosave_data ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 400, $response->get_status(), 'Response status is not 400.' );
$this->assertSame( 'rest_autosave_no_changes', $data['code'], 'Response "code" is not "rest_autosave_no_changes"' );
$this->assertSame( 200, $response->get_status() );
$this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' );
}
}