From f185aeb638028276b98a8b7b6718fcac29f2d4c3 Mon Sep 17 00:00:00 2001 From: antpb Date: Sun, 11 Sep 2022 22:35:08 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@54130 git-svn-id: http://core.svn.wordpress.org/trunk@53689 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../endpoints/class-wp-rest-autosaves-controller.php | 10 +++++++++- wp-includes/version.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php index 0a2cf5d49b..f99a201974 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php @@ -220,7 +220,15 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { $prepared_post->ID = $post->ID; $user_id = get_current_user_id(); - if ( ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) && $post->post_author == $user_id ) { + // We need to check post lock to ensure the original author didn't leave their browser tab open. + if ( ! function_exists( 'wp_check_post_lock' ) ) { + require_once ABSPATH . 'wp-admin/includes/post.php'; + } + + $post_lock = wp_check_post_lock( $post->ID ); + $is_draft = 'draft' === $post->post_status || 'auto-draft' === $post->post_status; + + if ( $is_draft && (int) $post->post_author === $user_id && ! $post_lock ) { // Draft posts for the same author: autosaving updates the post and does not create a revision. // Convert the post object to an array and add slashes, wp_update_post() expects escaped array. $autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 58cc44c676..200c08bb1f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54129'; +$wp_version = '6.1-alpha-54130'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.