mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Post Edit Collision Detection from mdawaffe. fixes #6043
git-svn-id: https://develop.svn.wordpress.org/trunk@7103 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
+42
-8
@@ -470,6 +470,8 @@ case 'add-user' :
|
||||
break;
|
||||
case 'autosave' : // The name of this action is hardcoded in edit_post()
|
||||
check_ajax_referer( 'autosave', 'autosavenonce' );
|
||||
global $current_user;
|
||||
|
||||
$_POST['post_content'] = $_POST['content'];
|
||||
$_POST['post_excerpt'] = $_POST['excerpt'];
|
||||
$_POST['post_status'] = 'draft';
|
||||
@@ -478,17 +480,36 @@ case 'autosave' : // The name of this action is hardcoded in edit_post()
|
||||
if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
|
||||
unset($_POST['post_category']);
|
||||
|
||||
$do_autosave = (bool) $_POST['autosave'];
|
||||
$do_lock = true;
|
||||
|
||||
$data = '<div class="updated"><p>' . sprintf( __('Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) ) . '</p></div>';
|
||||
|
||||
$supplemental = array();
|
||||
|
||||
$id = 0;
|
||||
if($_POST['post_ID'] < 0) {
|
||||
$_POST['temp_ID'] = $_POST['post_ID'];
|
||||
$id = wp_write_post();
|
||||
if( is_wp_error($id) )
|
||||
die($id->get_error_message());
|
||||
else
|
||||
die("$id");
|
||||
if ( $do_autosave )
|
||||
$id = wp_write_post();
|
||||
} else {
|
||||
$post_ID = (int) $_POST['post_ID'];
|
||||
$_POST['ID'] = $post_ID;
|
||||
$post = get_post($post_ID);
|
||||
|
||||
if ( $last = wp_check_post_lock( $post->ID ) ) {
|
||||
$do_autosave = $do_lock = false;
|
||||
|
||||
$last_user = get_userdata( $last );
|
||||
$last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
|
||||
$data = new WP_Error( 'locked', sprintf(
|
||||
$_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
|
||||
wp_specialchars( $last_user_name )
|
||||
) );
|
||||
|
||||
$supplemental['disable_autosave'] = 'disable';
|
||||
}
|
||||
|
||||
if ( 'page' == $post->post_type ) {
|
||||
if ( !current_user_can('edit_page', $post_ID) )
|
||||
die(__('You are not allowed to edit this page.'));
|
||||
@@ -496,10 +517,23 @@ case 'autosave' : // The name of this action is hardcoded in edit_post()
|
||||
if ( !current_user_can('edit_post', $post_ID) )
|
||||
die(__('You are not allowed to edit this post.'));
|
||||
}
|
||||
wp_update_post($_POST);
|
||||
if ( $do_autosave )
|
||||
$id = wp_update_post($_POST);
|
||||
else
|
||||
$id = $post->ID;
|
||||
}
|
||||
die('0');
|
||||
break;
|
||||
|
||||
if ( $do_lock && $id && is_numeric($id) )
|
||||
wp_set_post_lock( $id );
|
||||
|
||||
$x = new WP_Ajax_Response( array(
|
||||
'what' => 'autosave',
|
||||
'id' => $id,
|
||||
'data' => $id ? $data : '',
|
||||
'supplemental' => $supplemental
|
||||
) );
|
||||
$x->send();
|
||||
break;
|
||||
case 'autosave-generate-nonces' :
|
||||
check_ajax_referer( 'autosave', 'autosavenonce' );
|
||||
$ID = (int) $_POST['post_ID'];
|
||||
|
||||
@@ -55,6 +55,15 @@ $saveasdraft = '<input name="save" type="submit" id="save" class="button" tabind
|
||||
<input type="hidden" id="originalaction" name="originalaction" value="<?php echo $form_action ?>" />
|
||||
<input type="hidden" name="post_author" value="<?php echo attribute_escape( $post->post_author ); ?>" />
|
||||
<input type="hidden" id="post_type" name="post_type" value="<?php echo $post->post_type ?>" />
|
||||
<input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo $post->post_status ?>" />
|
||||
<input name="referredby" type="hidden" id="referredby" value="<?php
|
||||
if ( !empty($_REQUEST['popupurl']) )
|
||||
echo clean_url(stripslashes($_REQUEST['popupurl']));
|
||||
else if ( url_to_postid(wp_get_referer()) == $post_ID )
|
||||
echo 'redo';
|
||||
else
|
||||
echo clean_url(stripslashes(wp_get_referer()));
|
||||
?>" />
|
||||
|
||||
<?php echo $form_extra ?>
|
||||
<?php if ((isset($post->post_title) && '' == $post->post_title) || (isset($_GET['message']) && 2 > $_GET['message'])) : ?>
|
||||
@@ -189,19 +198,7 @@ endif; ?>
|
||||
<?php echo $form_pingback ?>
|
||||
<?php echo $form_prevstatus ?>
|
||||
|
||||
<p class="submit">
|
||||
|
||||
<span id="autosave"></span>
|
||||
|
||||
|
||||
<input name="referredby" type="hidden" id="referredby" value="<?php
|
||||
if ( !empty($_REQUEST['popupurl']) )
|
||||
echo clean_url(stripslashes($_REQUEST['popupurl']));
|
||||
else if ( url_to_postid(wp_get_referer()) == $post_ID )
|
||||
echo 'redo';
|
||||
else
|
||||
echo clean_url(stripslashes(wp_get_referer()));
|
||||
?>" /></p>
|
||||
<div id="autosave"></div>
|
||||
|
||||
<div id="tagsdiv" class="postbox <?php echo postbox_classes('tagsdiv', 'post'); ?>">
|
||||
<h3><?php _e('Tags'); ?></h3>
|
||||
|
||||
@@ -36,6 +36,7 @@ if (isset($mode) && 'bookmarklet' == $mode)
|
||||
<input type="hidden" id="originalaction" name="originalaction" value="<?php echo $form_action ?>" />
|
||||
<?php echo $form_extra ?>
|
||||
<input type="hidden" id="post_type" name="post_type" value="<?php echo $post->post_type ?>" />
|
||||
<input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo $post->post_status ?>" />
|
||||
<input name="referredby" type="hidden" id="referredby" value="<?php
|
||||
if ( url_to_postid(wp_get_referer()) == $post_ID )
|
||||
echo 'redo';
|
||||
@@ -169,11 +170,7 @@ endif; ?>
|
||||
<?php wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false ); ?>
|
||||
</div>
|
||||
|
||||
<p class="submit">
|
||||
|
||||
<span id="autosave"></span>
|
||||
|
||||
</p>
|
||||
<div id="autosave"></div>
|
||||
|
||||
<?php do_meta_boxes('page', 'normal', $post); ?>
|
||||
|
||||
|
||||
@@ -18,8 +18,7 @@ function edit_post() {
|
||||
$post =& get_post( $post_ID );
|
||||
$now = time();
|
||||
$then = strtotime($post->post_date_gmt . ' +0000');
|
||||
// Keep autosave_interval in sync with autosave-js.php.
|
||||
$delta = apply_filters( 'autosave_interval', 120 ) / 2;
|
||||
$delta = get_option( 'autosave_interval' ) / 2;
|
||||
if ( ($now - $then) < $delta )
|
||||
return $post_ID;
|
||||
}
|
||||
@@ -619,4 +618,37 @@ function get_sample_permalink_html($id, $new_slug=null) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
// false: not locked or locked by current user
|
||||
// int: user ID of user with lock
|
||||
function wp_check_post_lock( $post_id ) {
|
||||
global $current_user;
|
||||
|
||||
if ( !$post = get_post( $post_id ) )
|
||||
return false;
|
||||
|
||||
$lock = get_post_meta( $post->ID, '_edit_lock', true );
|
||||
$last = get_post_meta( $post->ID, '_edit_last', true );
|
||||
|
||||
$time_window = apply_filters( 'wp_check_post_lock_window', get_option( 'autosave_interval' ) * 2 );
|
||||
|
||||
if ( $lock && $lock > time() - $time_window && $last != $current_user->ID )
|
||||
return $last;
|
||||
return false;
|
||||
}
|
||||
|
||||
function wp_set_post_lock( $post_id ) {
|
||||
global $current_user;
|
||||
if ( !$post = get_post( $post_id ) )
|
||||
return false;
|
||||
if ( !$current_user || !$current_user->ID )
|
||||
return false;
|
||||
|
||||
$now = time();
|
||||
|
||||
if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
|
||||
update_post_meta( $post->ID, '_edit_lock', $now );
|
||||
if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
|
||||
update_post_meta( $post->ID, '_edit_last', $current_user->ID );
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -198,7 +198,7 @@ function upgrade_all() {
|
||||
if ( $wp_current_db_version < 6124 )
|
||||
upgrade_230_old_tables();
|
||||
|
||||
if ( $wp_current_db_version < 6689 )
|
||||
if ( $wp_current_db_version < 7098 )
|
||||
upgrade_250();
|
||||
|
||||
maybe_disable_automattic_widgets();
|
||||
@@ -725,6 +725,8 @@ function upgrade_250() {
|
||||
if ( $wp_current_db_version < 6689 ) {
|
||||
populate_roles_250();
|
||||
}
|
||||
|
||||
add_option('autosave_interval', 60);
|
||||
}
|
||||
|
||||
// The functions we use to actually do stuff
|
||||
|
||||
+9
-2
@@ -57,9 +57,16 @@ case 'edit':
|
||||
wp_enqueue_script('editor');
|
||||
wp_enqueue_script('thickbox');
|
||||
wp_enqueue_script('media-upload');
|
||||
|
||||
if ( 'draft' == $post->post_status )
|
||||
if ( $last = wp_check_post_lock( $post->ID ) ) {
|
||||
$last_user = get_userdata( $last );
|
||||
$last_user_name = $last_user ? $last_user->display_name : __('Somebody');
|
||||
$message = sprintf( __( '%s is currently editing this page' ), wp_specialchars( $last_user_name ) );
|
||||
$message = str_replace( "'", "\'", "<div class='error'><p>$message</p></div>" );
|
||||
add_action('admin_notices', create_function( '', "echo '$message';" ) );
|
||||
} else {
|
||||
wp_set_post_lock( $post->ID );
|
||||
wp_enqueue_script('autosave');
|
||||
}
|
||||
|
||||
require_once('admin-header.php');
|
||||
|
||||
|
||||
+9
-2
@@ -66,9 +66,16 @@ case 'edit':
|
||||
wp_enqueue_script('editor');
|
||||
wp_enqueue_script('thickbox');
|
||||
wp_enqueue_script('media-upload');
|
||||
|
||||
if ( 'draft' == $post->post_status )
|
||||
if ( $last = wp_check_post_lock( $post->ID ) ) {
|
||||
$last_user = get_userdata( $last );
|
||||
$last_user_name = $last_user ? $last_user->display_name : __('Somebody');
|
||||
$message = sprintf( __( '%s is currently editing this post' ), wp_specialchars( $last_user_name ) );
|
||||
$message = str_replace( "'", "\'", "<div class='error'><p>$message</p></div>" );
|
||||
add_action('admin_notices', create_function( '', "echo '$message';" ) );
|
||||
} else {
|
||||
wp_set_post_lock( $post->ID );
|
||||
wp_enqueue_script('autosave');
|
||||
}
|
||||
|
||||
require_once('admin-header.php');
|
||||
|
||||
|
||||
@@ -1033,6 +1033,15 @@ html, body {
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
#poststuff #autosave {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#poststuff #autosave div.updated, #poststuff #autosave div.error {
|
||||
margin: 0 8px 10px 20px;
|
||||
}
|
||||
|
||||
#poststuff .inside {
|
||||
margin: 0 12px 15px;
|
||||
font-size: 11px;
|
||||
|
||||
Reference in New Issue
Block a user