From 9781ffbca5086b604bd334e9230f274e3d890eda Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 7 Jul 2023 17:51:11 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-autosaves-controller.php | 13 +++++------- .../rest-api/rest-autosaves-controller.php | 21 ++++++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) 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' ); } }