Release a user's post lock when the user leaves a post. see #18515.

git-svn-id: http://svn.automattic.com/wordpress/trunk@18796 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2011-09-27 20:52:07 +00:00
parent b171681162
commit de8cc0eae1
7 changed files with 56 additions and 7 deletions

View File

@ -988,8 +988,10 @@ case 'autosave' : // The name of this action is hardcoded in edit_post()
$id = $post->ID;
}
if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) )
wp_set_post_lock( $id );
if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) ) {
$lock_result = wp_set_post_lock( $id );
$supplemental['active-post-lock'] = implode( ':', $lock_result );
}
if ( $nonce_age == 2 ) {
$supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
@ -1551,6 +1553,26 @@ case 'wp-fullscreen-save-post' :
echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) );
die();
break;
case 'wp-remove-post-lock' :
if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) )
die( '0' );
$post_id = (int) $_POST['post_ID'];
if ( ! $post = get_post( $post_id ) )
die( '0' );
check_ajax_referer( 'update-' . $post->post_type . '_' . $post_id );
if ( ! current_user_can( 'edit_post', $post_id ) )
die( '-1' );
$active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) );
if ( $active_lock[1] != get_current_user_id() )
die( '0' );
$new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ) + 5 ) . ':' . $active_lock[1];
update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) );
die( '1' );
default :
do_action( 'wp_ajax_' . $_POST['action'] );
die('0');

View File

@ -231,7 +231,10 @@ require_once('./admin-header.php');
<input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post_type ) ?>" />
<input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status) ?>" />
<input type="hidden" id="referredby" name="referredby" value="<?php echo esc_url(stripslashes(wp_get_referer())); ?>" />
<?php if ( ! empty( $active_post_lock ) ) { ?>
<input type="hidden" id="active_post_lock" value="<?php echo esc_attr( implode( ':', $active_post_lock ) ); ?>" />
<?php
}
if ( 'draft' != $post->post_status )
wp_original_referer_field(true, 'previous');

View File

@ -1239,7 +1239,8 @@ function wp_check_post_lock( $post_id ) {
* @since 2.5.0
*
* @param int $post_id ID of the post to being edited
* @return bool Returns false if the post doesn't exist of there is no current user
* @return bool|array Returns false if the post doesn't exist of there is no current user, or
* an array of the lock time and the user ID.
*/
function wp_set_post_lock( $post_id ) {
if ( !$post = get_post( $post_id ) )
@ -1251,6 +1252,7 @@ function wp_set_post_lock( $post_id ) {
$lock = "$now:$user_id";
update_post_meta( $post->ID, '_edit_lock', $lock );
return array( $now, $user_id );
}
/**

View File

@ -174,7 +174,7 @@ case 'edit':
if ( $last = wp_check_post_lock( $post->ID ) ) {
add_action('admin_notices', '_admin_notice_post_locked' );
} else {
wp_set_post_lock( $post->ID );
$active_post_lock = wp_set_post_lock( $post->ID );
wp_enqueue_script('autosave');
}

View File

@ -1,4 +1,4 @@
var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen;
var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
jQuery(document).ready( function($) {
var dotabkey = true;
@ -9,6 +9,7 @@ jQuery(document).ready( function($) {
//Disable autosave after the form has been submitted
$("#post").submit(function() {
$.cancel(autosavePeriodical);
autosaveLockRelease = false;
});
$('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
@ -47,6 +48,22 @@ jQuery(document).ready( function($) {
}
};
$(window).unload( function() {
if ( ! autosaveLockRelease )
return;
$.ajax({
type: 'POST',
url: ajaxurl,
async: false,
data: {
action: 'wp-remove-post-lock',
_wpnonce: $('#_wpnonce').val(),
post_ID: $('#post_ID').val(),
active_post_lock: $('#active_post_lock').val()
},
});
} );
// preview
$('#post-preview').click(function(){
if ( $('#auto_draft').val() == '1' && notSaved ) {
@ -99,9 +116,14 @@ function autosave_parse_response(response) {
sup = res.responses[0].supplemental;
if ( 'disable' == sup['disable_autosave'] ) {
autosave = function() {};
autosaveLockRelease = false;
res = { errors: true };
}
if ( sup['active-post-lock'] ) {
jQuery('#active_post_lock').val( sup['active-post-lock'] );
}
if ( sup['alert'] ) {
jQuery('#autosave-alert').remove();
jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>');

File diff suppressed because one or more lines are too long

View File

@ -101,7 +101,7 @@ function wp_default_scripts( &$scripts ) {
'close' => __('Close'),
) );
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20110524', 1 );
$scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), '20110927', 1 );
$scripts->add( 'wp-lists', "/wp-includes/js/wp-lists$suffix.js", array('wp-ajax-response'), '20110521', 1 );