Make WordPress Core


Ignore:
Timestamp:
01/27/2023 10:01:59 PM (16 months ago)
Author:
adamsilverstein
Message:

Revisions: only create autosave when content changed.

In the autosave REST API endpoint, avoid excessive database writes when an autosave is sent with content that is unchanged from the saved post.

Before this fix, clicking "preview" in the editor (which uses the autosave feature) multiple times would cause an identical autosave entry to be deleted and re-created repeatedly.

Props inwerpsel, aduth, mukesh27, ironprogrammer.
Fixes #49532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r55104 r55154  
    675675        wp_delete_post( $post_id );
    676676    }
     677
     678    /**
     679     * @ticket 49532
     680     *
     681     * @covers WP_REST_Autosaves_Controller::create_post_autosave
     682     */
     683    public function test_rest_autosave_do_not_create_autosave_when_post_is_unchanged() {
     684        // Create a post by the editor.
     685        $post_data = array(
     686            'post_content' => 'Test post content',
     687            'post_title'   => 'Test post title',
     688            'post_excerpt' => 'Test post excerpt',
     689            'post_author'  => self::$editor_id,
     690            'post_status'  => 'publish',
     691        );
     692        $post_id   = wp_insert_post( $post_data );
     693
     694        wp_set_current_user( self::$editor_id );
     695
     696        $autosave_data = array(
     697            'post_content' => $post_data['post_content'],
     698        );
     699
     700        // Create autosaves response.
     701        $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' );
     702        $request->add_header( 'content-type', 'application/json' );
     703        $request->set_body( wp_json_encode( $autosave_data ) );
     704
     705        $response = rest_get_server()->dispatch( $request );
     706        $data     = $response->get_data();
     707
     708        $this->assertSame( 400, $response->get_status(), 'Response status is not 400.' );
     709        $this->assertSame( 'rest_autosave_no_changes', $data['code'], 'Response "code" is not "rest_autosave_no_changes"' );
     710    }
    677711}
Note: See TracChangeset for help on using the changeset viewer.