Skip to content

Commit

Permalink
When autosave is newer than the post, use that for comparison before …
Browse files Browse the repository at this point in the history
…autosaving

This change replicates how the frontend compares against the autosave when checking
if a post can be autosaved.
  • Loading branch information
talldan committed Sep 3, 2018
1 parent 78bb1ef commit 847c699
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/class-wp-rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,19 @@ public function create_post_autosave( $post_data ) {
$new_autosave['ID'] = $old_autosave->ID;
$new_autosave['post_author'] = $user_id;

// If the new autosave has the same content as the post, delete the autosave.
$autosave_is_different = false;
$is_autosave_newer_than_post = mysql2date( 'U', $old_autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false );

foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
// Check if the autosave is different to either the current post or the current autosave, whichever is newer.
$comparison_fields = $is_autosave_newer_than_post ? $old_autosave : $post;
$autosave_is_different = false;
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $comparison_fields ) ) ) as $field ) {
if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
$autosave_is_different = true;
break;
}
}

// If the new autosave has the same content as the post, delete the autosave.
if ( ! $autosave_is_different ) {
wp_delete_post_revision( $old_autosave->ID );
return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.', 'gutenberg' ), array( 'status' => 400 ) );
Expand Down

0 comments on commit 847c699

Please sign in to comment.