Post locks and autosave:

- Move nonces refreshing from autosave to lock checking.
- Do autosave only when there is something to save.
See #23295

git-svn-id: http://core.svn.wordpress.org/trunk@24209 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2013-05-08 22:52:55 +00:00
parent 19c3b4bfdc
commit d0c5c59c94
4 changed files with 39 additions and 25 deletions

View File

@ -1038,7 +1038,7 @@ function wp_ajax_add_user( $action ) {
function wp_ajax_autosave() { function wp_ajax_autosave() {
define( 'DOING_AUTOSAVE', true ); define( 'DOING_AUTOSAVE', true );
$nonce_age = check_ajax_referer( 'autosave', 'autosavenonce' ); check_ajax_referer( 'autosave', 'autosavenonce' );
$_POST['post_category'] = explode(",", $_POST['catslist']); $_POST['post_category'] = explode(",", $_POST['catslist']);
if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) ) if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
@ -1090,15 +1090,6 @@ function wp_ajax_autosave() {
$id = $post->ID; $id = $post->ID;
} }
if ( $nonce_age == 2 ) {
$supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
$supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
$supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink');
$supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
$supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' );
$supplemental['replace-_wpnonce'] = wp_create_nonce( 'update-post_' . $post->ID );
}
$x = new WP_Ajax_Response( array( $x = new WP_Ajax_Response( array(
'what' => 'autosave', 'what' => 'autosave',
'id' => $id, 'id' => $id,

View File

@ -623,6 +623,17 @@ function wp_refresh_post_lock( $response, $data, $screen_id ) {
$send['new_lock'] = implode( ':', $new_lock ); $send['new_lock'] = implode( ':', $new_lock );
} }
if ( ! empty( $received['post_nonce'] ) && 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
$send['update_nonces'] = array(
'replace-autosavenonce' => wp_create_nonce('autosave'),
'replace-getpermalinknonce' => wp_create_nonce('getpermalink'),
'replace-samplepermalinknonce' => wp_create_nonce('samplepermalink'),
'replace-closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
'replace-_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
'replace-_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
);
}
$response['wp-refresh-post-lock'] = $send; $response['wp-refresh-post-lock'] = $send;
} }

View File

@ -252,7 +252,10 @@ WPRemoveThumbnail = function(nonce){
}; };
$(document).on( 'heartbeat-send.refresh-lock', function( e, data ) { $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
var lock = $('#active_post_lock').val(), post_id = $('#post_ID').val(), send = {}; var lock = $('#active_post_lock').val(),
post_id = $('#post_ID').val(),
post_nonce = $('#_wpnonce').val(),
send = {};
if ( !post_id ) if ( !post_id )
return; return;
@ -262,6 +265,9 @@ $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
if ( lock ) if ( lock )
send['lock'] = lock; send['lock'] = lock;
if ( post_nonce )
send['post_nonce'] = post_nonce;
data['wp-refresh-post-lock'] = send; data['wp-refresh-post-lock'] = send;
}); });
@ -286,7 +292,9 @@ $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
}); });
// Save the latest changes and disable // Save the latest changes and disable
autosave(); if ( ! autosave() )
window.onbeforeunload = null;
autosave = function(){}; autosave = function(){};
} }
@ -301,6 +309,13 @@ $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
} else if ( received.new_lock ) { } else if ( received.new_lock ) {
$('#active_post_lock').val( received.new_lock ); $('#active_post_lock').val( received.new_lock );
} }
if ( received.update_nonces ) {
$.each( received.update_nonces, function( selector, value ) {
if ( selector.match(/^replace-/) )
$( '#' + selector.replace('replace-', '') ).val( value );
});
}
} }
}); });

View File

@ -254,19 +254,11 @@ function delayed_autosave() {
} }
autosave = function() { autosave = function() {
// (bool) is rich editor enabled and active var post_data = wp.autosave.getPostData(),
doAutoSave = post_data.autosave,
successCallback;
blockSave = true; blockSave = true;
var rich = (typeof tinymce != "undefined") && tinymce.activeEditor && !tinymce.activeEditor.isHidden(),
post_data, doAutoSave, ed, origStatus, successCallback;
// Disable buttons until we know the save completed.
autosave_disable_buttons();
post_data = wp.autosave.getPostData();
// We always send the ajax request in order to keep the post lock fresh.
// This (bool) tells whether or not to write the post to the DB during the ajax request.
doAutoSave = post_data.autosave;
// No autosave while thickbox is open (media buttons) // No autosave while thickbox is open (media buttons)
if ( jQuery("#TB_window").css('display') == 'block' ) if ( jQuery("#TB_window").css('display') == 'block' )
@ -281,9 +273,12 @@ autosave = function() {
autosaveLast = post_data["post_title"] + post_data["content"]; autosaveLast = post_data["post_title"] + post_data["content"];
jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
} else { } else {
post_data['autosave'] = 0; return false;
} }
// Disable buttons until we know the save completed.
autosave_disable_buttons();
if ( post_data["auto_draft"] == '1' ) { if ( post_data["auto_draft"] == '1' ) {
successCallback = autosave_saved_new; // new post successCallback = autosave_saved_new; // new post
} else { } else {
@ -297,6 +292,8 @@ autosave = function() {
url: ajaxurl, url: ajaxurl,
success: successCallback success: successCallback
}); });
return true;
} }
// Autosave in localStorage // Autosave in localStorage