Make WordPress Core

Ticket #49532: 49532.diff

File 49532.diff, 2.5 KB (added by adamsilverstein, 4 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php
    index 0a2cf5d49b..2d0efefaf7 100644
    class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 
    352352                        return $post;
    353353                }
    354354
    355                 $user_id = get_current_user_id();
     355                // If the autosave content matches the post content, do not create an autosave and delete any existing autosave.
     356                $autosave_is_different = false;
     357                $new_autosave          = _wp_post_revision_data( $post_data, true );
     358
     359                foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
     360                        if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
     361                                $autosave_is_different = true;
     362                                break;
     363                        }
     364                }
    356365
    357                 // Store one autosave per author. If there is already an autosave, overwrite it.
     366                // Store one autosave per author. If there is already an autosave for the current author, overwrite it.
     367                $user_id      = get_current_user_id();
    358368                $old_autosave = wp_get_post_autosave( $post_id, $user_id );
    359369
    360370                if ( $old_autosave ) {
    361                         $new_autosave                = _wp_post_revision_data( $post_data, true );
    362371                        $new_autosave['ID']          = $old_autosave->ID;
    363372                        $new_autosave['post_author'] = $user_id;
    364373
    365                         // If the new autosave has the same content as the post, delete the autosave.
    366                         $autosave_is_different = false;
    367 
    368                         foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
    369                                 if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
    370                                         $autosave_is_different = true;
    371                                         break;
    372                                 }
    373                         }
    374 
    375374                        if ( ! $autosave_is_different ) {
    376375                                wp_delete_post_revision( $old_autosave->ID );
    377376                                return new WP_Error(
    class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 
    388387                        return wp_update_post( wp_slash( $new_autosave ) );
    389388                }
    390389
     390                if ( ! $autosave_is_different ) {
     391                        return new WP_Error(
     392                                'rest_autosave_no_changes',
     393                                __( 'There is nothing to save. The autosave and the post content are the same.' ),
     394                                array( 'status' => 400 )
     395                        );
     396                }
     397
    391398                // Create the new autosave as a special post revision.
    392399                return _wp_put_post_revision( $post_data, true );
    393400        }