Make WordPress Core

Changeset 54130

Timestamp:
09/11/2022 10:33:29 PM (2 years ago)
Author:
antpb
Message:

Autosave/REST API: Block autosaving from overwriting changes when locked from editing.

Previously when a user was locked from editing a post in the block editor, autosave functionality was allowed to overwrite changes made by the editor that has taken control. This patch honors the lock status keeping autosave from conflicitng with other content editors.

Props jhart35, adamsilverstein, sathyapulse, chanthaboune, primetimejas, joemcgill, kadamwhite.
Fixes #55659.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    r51962 r54130  
    221221        $user_id           = get_current_user_id();
    222222
    223         if ( ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) && $post->post_author == $user_id ) {
     223        // We need to check post lock to ensure the original author didn't leave their browser tab open.
     224        if ( ! function_exists( 'wp_check_post_lock' ) ) {
     225            require_once ABSPATH . 'wp-admin/includes/post.php';
     226        }
     227
     228        $post_lock = wp_check_post_lock( $post->ID );
     229        $is_draft  = 'draft' === $post->post_status || 'auto-draft' === $post->post_status;
     230
     231        if ( $is_draft && (int) $post->post_author === $user_id && ! $post_lock ) {
    224232            // Draft posts for the same author: autosaving updates the post and does not create a revision.
    225233            // Convert the post object to an array and add slashes, wp_update_post() expects escaped array.
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r54058 r54130  
    608608        $this->assertNotEquals( 'garbage', get_post( self::$draft_page_id )->comment_status );
    609609    }
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
    610677}
Note: See TracChangeset for help on using the changeset viewer.