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 f99a201974..3ed68e6a2e 100644
|
|
class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { |
360 | 360 | return $post; |
361 | 361 | } |
362 | 362 | |
| 363 | // Only create an autosave when the it is different than the saved post. |
| 364 | $autosave_is_different = false; |
| 365 | $new_autosave = _wp_post_revision_data( $post_data, true ); |
| 366 | |
| 367 | foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
| 368 | if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) { |
| 369 | $autosave_is_different = true; |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if ( ! $autosave_is_different ) { |
| 375 | return new WP_Error( |
| 376 | 'rest_autosave_no_changes', |
| 377 | __( 'There is nothing to save. The autosave and the post content are the same.' ), |
| 378 | array( 'status' => 400 ) |
| 379 | ); |
| 380 | } |
| 381 | |
363 | 382 | $user_id = get_current_user_id(); |
364 | 383 | |
365 | 384 | // Store one autosave per author. If there is already an autosave, overwrite it. |
366 | 385 | $old_autosave = wp_get_post_autosave( $post_id, $user_id ); |
367 | 386 | |
368 | 387 | if ( $old_autosave ) { |
369 | | $new_autosave = _wp_post_revision_data( $post_data, true ); |
370 | 388 | $new_autosave['ID'] = $old_autosave->ID; |
371 | 389 | $new_autosave['post_author'] = $user_id; |
372 | 390 | |
373 | | // If the new autosave has the same content as the post, delete the autosave. |
374 | | $autosave_is_different = false; |
375 | | |
376 | | foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { |
377 | | if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) { |
378 | | $autosave_is_different = true; |
379 | | break; |
380 | | } |
381 | | } |
382 | | |
383 | | if ( ! $autosave_is_different ) { |
384 | | wp_delete_post_revision( $old_autosave->ID ); |
385 | | return new WP_Error( |
386 | | 'rest_autosave_no_changes', |
387 | | __( 'There is nothing to save. The autosave and the post content are the same.' ), |
388 | | array( 'status' => 400 ) |
389 | | ); |
390 | | } |
391 | | |
392 | 391 | /** This filter is documented in wp-admin/post.php */ |
393 | 392 | do_action( 'wp_creating_autosave', $new_autosave ); |
394 | 393 | |