diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php index 6b2da40eb3..1eacba38e8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php @@ -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; diff --git a/tests/phpunit/tests/rest-api/rest-autosaves-controller.php b/tests/phpunit/tests/rest-api/rest-autosaves-controller.php index b043b5e073..e987a5f699 100644 --- a/tests/phpunit/tests/rest-api/rest-autosaves-controller.php +++ b/tests/phpunit/tests/rest-api/rest-autosaves-controller.php @@ -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' ); } }