Local autosave: set a temp cookie on submitting the form and change it on redirecting after the post is saved/updated, then use it to determine if saving worked properly. Removes the chance for false positives after saving/updating a post. See #23220

git-svn-id: https://develop.svn.wordpress.org/trunk@23693 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2013-03-14 03:06:07 +00:00
parent a1ae5c0d28
commit 5e71fb3992
2 changed files with 24 additions and 37 deletions

View File

@ -210,6 +210,10 @@ case 'editpost':
$post_id = edit_post(); $post_id = edit_post();
// Session cookie flag that the post was saved
if ( isset( $_COOKIE['wp-saving-post-' . $post_id] ) )
setcookie( 'wp-saving-post-' . $post_id, 'saved' );
redirect_post($post_id); // Send user on their way while we keep working redirect_post($post_id); // Send user on their way while we keep working
exit(); exit();

View File

@ -475,7 +475,7 @@ wp.autosave.local = {
$.extend( post_data, data ); $.extend( post_data, data );
} }
// If the content and title are empty or did not change since the last save, don't save again // If the content and title did not change since the last save, don't save again
if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata ) if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata )
return false; return false;
@ -537,7 +537,7 @@ wp.autosave.local = {
}); });
$('form#post').on('submit.autosave-local', function() { $('form#post').on('submit.autosave-local', function() {
var editor = typeof tinymce != 'undefined' && tinymce.get('content'); var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0;
if ( editor && ! editor.isHidden() ) { if ( editor && ! editor.isHidden() ) {
// Last onSubmit event in the editor, needs to run after the content has been moved to the textarea. // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
@ -555,21 +555,18 @@ wp.autosave.local = {
excerpt: $('#excerpt').val() || '' excerpt: $('#excerpt').val() || ''
}); });
} }
wpCookies.set( 'wp-saving-post-' + post_id, 'check' );
}); });
}, },
// Strip whitespace and compare two strings // Strip whitespace and compare two strings
compare: function( str1, str2, strip_tags ) { compare: function( str1, str2 ) {
function remove( string, strip_tags ) { function remove( string ) {
string = string.toString(); return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
if ( strip_tags )
string = string.replace(/<[^<>]+>/g, '');
return string.replace(/[\x20\t\r\n\f]+/g, '');
} }
return ( remove( str1 || '', strip_tags ) == remove( str2 || '', strip_tags ) ); return ( remove( str1 || '' ) == remove( str2 || '' ) );
}, },
/** /**
@ -580,7 +577,8 @@ wp.autosave.local = {
* @return void * @return void
*/ */
checkPost: function() { checkPost: function() {
var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice; var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice,
post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
if ( ! post_data ) if ( ! post_data )
return; return;
@ -589,37 +587,22 @@ wp.autosave.local = {
if ( $('#has-newer-autosave').length ) if ( $('#has-newer-autosave').length )
return; return;
content = $('#content').val(); if ( cookie == 'saved' ) {
check_data = $.extend( {}, post_data );
if ( $('#wp-content-wrap').hasClass('tmce-active') )
content = switchEditors.pre_wpautop( content );
// The post has just been published, only compare text
if ( $('#post_status').val() == 'publish' && check_data.status != 'publish' )
strip_tags = true;
if ( this.compare( content, check_data.content, strip_tags ) && this.compare( $('#title').val(), check_data.post_title, strip_tags ) && this.compare( $('#excerpt').val(), check_data.excerpt, strip_tags ) )
return; return;
} else if ( cookie != 'check' ) {
content = $('#content').val();
check_data = $.extend( {}, post_data );
// We have three choices here: if ( $('#wp-content-wrap').hasClass('tmce-active') )
// - Do an autosave and then show the standard notice "There is an autosave newer than...". content = switchEditors.pre_wpautop( content );
// - Offer to load/restore the backed up post data.
// - Restore the post_data without asking, then show a notice with an Undo link/button. if ( this.compare( content, check_data.content ) && this.compare( $('#title').val(), check_data.post_title ) && this.compare( $('#excerpt').val(), check_data.excerpt ) )
// Doing an autosave will take few seconds and may take up to 30 and fail if network connectivity is bad return;
// Restoring the post will leave the user with the proper content, but it won't be saved to the server until the next autosave. }
this.restore_post_data = post_data; this.restore_post_data = post_data;
this.undo_post_data = wp.autosave.getPostData(); this.undo_post_data = wp.autosave.getPostData();
/*
if ( $('#post_status').val() == 'publish' ) {
// Different message when a post is published?
// Comparing the current and saved post data may fail (false positive) when the post is published
// as in some cases there are changes to post_content on publishing and updating before saving to the DB.
}
*/
notice = $('#local-storage-notice'); notice = $('#local-storage-notice');
$('form#post').before( notice.addClass('updated').show() ); $('form#post').before( notice.addClass('updated').show() );