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 { |
352 | 352 | return $post; |
353 | 353 | } |
354 | 354 | |
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 | } |
356 | 365 | |
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(); |
358 | 368 | $old_autosave = wp_get_post_autosave( $post_id, $user_id ); |
359 | 369 | |
360 | 370 | if ( $old_autosave ) { |
361 | | $new_autosave = _wp_post_revision_data( $post_data, true ); |
362 | 371 | $new_autosave['ID'] = $old_autosave->ID; |
363 | 372 | $new_autosave['post_author'] = $user_id; |
364 | 373 | |
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 | | |
375 | 374 | if ( ! $autosave_is_different ) { |
376 | 375 | wp_delete_post_revision( $old_autosave->ID ); |
377 | 376 | return new WP_Error( |
… |
… |
class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { |
388 | 387 | return wp_update_post( wp_slash( $new_autosave ) ); |
389 | 388 | } |
390 | 389 | |
| 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 | |
391 | 398 | // Create the new autosave as a special post revision. |
392 | 399 | return _wp_put_post_revision( $post_data, true ); |
393 | 400 | } |