2018-06-27 22:30:15 -04:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/autosave.js
|
|
|
|
*/
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/* global tinymce, wpCookies, autosaveL10n, switchEditors */
|
2020-01-28 19:45:18 -05:00
|
|
|
// Back-compat.
|
2014-03-25 12:47:15 -04:00
|
|
|
window.autosave = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2014-01-21 23:56:16 -05:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Adds autosave to the window object on dom ready.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {jQuery} $ jQuery object.
|
|
|
|
* @param {window} The window object.
|
|
|
|
*
|
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
( function( $, window ) {
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Auto saves the post.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Object}
|
2017-08-18 10:24:45 -04:00
|
|
|
* {{
|
|
|
|
* getPostData: getPostData,
|
|
|
|
* getCompareString: getCompareString,
|
|
|
|
* disableButtons: disableButtons,
|
|
|
|
* enableButtons: enableButtons,
|
|
|
|
* local: ({hasStorage, getSavedPostData, save, suspend, resume}|*),
|
2020-09-28 06:46:05 -04:00
|
|
|
* server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)
|
|
|
|
* }}
|
2017-08-18 10:24:45 -04:00
|
|
|
* The object with all functions for autosave.
|
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function autosave() {
|
|
|
|
var initialCompareString,
|
2020-12-16 10:25:09 -05:00
|
|
|
initialCompareData = {},
|
|
|
|
lastTriggerSave = 0,
|
|
|
|
$document = $( document );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the initial compare data.
|
|
|
|
*
|
|
|
|
* @since 5.6.1
|
|
|
|
*/
|
|
|
|
function setInitialCompare() {
|
|
|
|
initialCompareData = {
|
|
|
|
post_title: $( '#title' ).val() || '',
|
|
|
|
content: $( '#content' ).val() || '',
|
|
|
|
excerpt: $( '#excerpt' ).val() || ''
|
|
|
|
};
|
|
|
|
|
|
|
|
initialCompareString = getCompareString( initialCompareData );
|
|
|
|
}
|
2014-01-21 23:56:16 -05:00
|
|
|
|
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Returns the data saved in both local and remote autosave.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {string} type The type of autosave either local or remote.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Object} Object containing the post data.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function getPostData( type ) {
|
|
|
|
var post_name, parent_id, data,
|
|
|
|
time = ( new Date() ).getTime(),
|
|
|
|
cats = [],
|
2016-06-16 23:02:29 -04:00
|
|
|
editor = getEditor();
|
2014-01-21 23:56:16 -05:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
// Don't run editor.save() more often than every 3 seconds.
|
2014-01-21 23:56:16 -05:00
|
|
|
// It is resource intensive and might slow down typing in long posts on slow devices.
|
2016-06-16 23:02:29 -04:00
|
|
|
if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
|
2014-01-21 23:56:16 -05:00
|
|
|
editor.save();
|
|
|
|
lastTriggerSave = time;
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
data = {
|
|
|
|
post_id: $( '#post_ID' ).val() || 0,
|
|
|
|
post_type: $( '#post_type' ).val() || '',
|
|
|
|
post_author: $( '#post_author' ).val() || '',
|
|
|
|
post_title: $( '#title' ).val() || '',
|
|
|
|
content: $( '#content' ).val() || '',
|
|
|
|
excerpt: $( '#excerpt' ).val() || ''
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( type === 'local' ) {
|
|
|
|
return data;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
$( 'input[id^="in-category-"]:checked' ).each( function() {
|
|
|
|
cats.push( this.value );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
2014-01-21 23:56:16 -05:00
|
|
|
data.catslist = cats.join(',');
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( post_name = $( '#post_name' ).val() ) {
|
|
|
|
data.post_name = post_name;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( parent_id = $( '#parent_id' ).val() ) {
|
|
|
|
data.parent_id = parent_id;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( $( '#comment_status' ).prop( 'checked' ) ) {
|
|
|
|
data.comment_status = 'open';
|
TinyMCE 4.0.12, first run.
- Removes wp-tinymce-schema.js and mark-loaded.js, no longer needed.
- Removes the inlinepopups and most of the wpdialogs plugins; wpdialog.js is moved to wp-includes/js.
- Adds charmap, compat3x, image, link and textcolor plugins, previously contained in /themes/advanced.
- Updates the wordpress, wpeditimage, wpfullscreen, wpgallery and wplink plugins.
- Updates DFW, wp-admin/js/wp-fullscreen.js.
See #24067.
Built from https://develop.svn.wordpress.org/trunk@26876
git-svn-id: http://core.svn.wordpress.org/trunk@26759 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-28 18:53:15 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( $( '#ping_status' ).prop( 'checked' ) ) {
|
|
|
|
data.ping_status = 'open';
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( $( '#auto_draft' ).val() === '1' ) {
|
|
|
|
data.auto_draft = '1';
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return data;
|
|
|
|
}
|
2013-04-02 18:51:09 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Concatenates the title, content and excerpt. This is used to track changes
|
|
|
|
* when auto-saving.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {Object} postData The object containing the post data.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {string} A concatenated string with title, content and excerpt.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function getCompareString( postData ) {
|
|
|
|
if ( typeof postData === 'object' ) {
|
|
|
|
return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' );
|
2013-07-18 17:33:59 -04:00
|
|
|
}
|
2014-01-21 23:56:16 -05:00
|
|
|
|
|
|
|
return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Disables save buttons.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function disableButtons() {
|
|
|
|
$document.trigger('autosave-disable-buttons');
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
|
|
|
|
setTimeout( enableButtons, 5000 );
|
|
|
|
}
|
2013-03-16 17:15:43 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Enables save buttons.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function enableButtons() {
|
|
|
|
$document.trigger( 'autosave-enable-buttons' );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-03-16 17:15:43 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Gets the content editor.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 4.6.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean|*} Returns either false if the editor is undefined,
|
|
|
|
* or the instance of the content editor.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2016-06-16 23:02:29 -04:00
|
|
|
function getEditor() {
|
|
|
|
return typeof tinymce !== 'undefined' && tinymce.get('content');
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Autosave in localStorage.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {
|
2017-08-18 10:24:45 -04:00
|
|
|
* {
|
|
|
|
* hasStorage: *,
|
|
|
|
* getSavedPostData: getSavedPostData,
|
|
|
|
* save: save,
|
|
|
|
* suspend: suspend,
|
|
|
|
* resume: resume
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* The object with all functions for local storage autosave.
|
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function autosaveLocal() {
|
2016-06-16 23:02:29 -04:00
|
|
|
var blog_id, post_id, hasStorage, intervalTimer,
|
2014-01-26 19:28:13 -05:00
|
|
|
lastCompareString,
|
|
|
|
isSuspended = false;
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Checks if the browser supports sessionStorage and it's not disabled.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} True if the sessionStorage is supported and enabled.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function checkStorage() {
|
|
|
|
var test = Math.random().toString(),
|
|
|
|
result = false;
|
2013-03-16 17:15:43 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
try {
|
|
|
|
window.sessionStorage.setItem( 'wp-test', test );
|
|
|
|
result = window.sessionStorage.getItem( 'wp-test' ) === test;
|
|
|
|
window.sessionStorage.removeItem( 'wp-test' );
|
|
|
|
} catch(e) {}
|
2013-03-16 17:15:43 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
hasStorage = result;
|
|
|
|
return result;
|
|
|
|
}
|
2013-03-16 17:15:43 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Initializes the local storage.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean|Object} False if no sessionStorage in the browser or an Object
|
|
|
|
* containing all postData for this blog.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function getStorage() {
|
|
|
|
var stored_obj = false;
|
2020-01-28 19:45:18 -05:00
|
|
|
// Separate local storage containers for each blog_id.
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( hasStorage && blog_id ) {
|
|
|
|
stored_obj = sessionStorage.getItem( 'wp-autosave-' + blog_id );
|
|
|
|
|
|
|
|
if ( stored_obj ) {
|
|
|
|
stored_obj = JSON.parse( stored_obj );
|
|
|
|
} else {
|
|
|
|
stored_obj = {};
|
2013-03-28 23:35:41 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2014-01-21 23:56:16 -05:00
|
|
|
|
|
|
|
return stored_obj;
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets the storage for this blog. Confirms that the data was saved
|
|
|
|
* successfully.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} True if the data was saved successfully, false if it wasn't saved.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function setStorage( stored_obj ) {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
if ( hasStorage && blog_id ) {
|
|
|
|
key = 'wp-autosave-' + blog_id;
|
|
|
|
sessionStorage.setItem( key, JSON.stringify( stored_obj ) );
|
|
|
|
return sessionStorage.getItem( key ) !== null;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Gets the saved post data for the current post.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean|Object} False if no storage or no data or the postData as an Object.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function getSavedPostData() {
|
|
|
|
var stored = getStorage();
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( ! stored || ! post_id ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return stored[ 'post_' + post_id ] || false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets (save or delete) post data in the storage.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* If stored_data evaluates to 'false' the storage key for the current post will be removed.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} True if data is stored, false if data was removed.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function setData( stored_data ) {
|
|
|
|
var stored = getStorage();
|
|
|
|
|
|
|
|
if ( ! stored || ! post_id ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( stored_data ) {
|
|
|
|
stored[ 'post_' + post_id ] = stored_data;
|
|
|
|
} else if ( stored.hasOwnProperty( 'post_' + post_id ) ) {
|
|
|
|
delete stored[ 'post_' + post_id ];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return setStorage( stored );
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets isSuspended to true.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-26 19:28:13 -05:00
|
|
|
function suspend() {
|
|
|
|
isSuspended = true;
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets isSuspended to false.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-26 19:28:13 -05:00
|
|
|
function resume() {
|
|
|
|
isSuspended = false;
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Saves post data for the current post.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2020-01-28 19:45:18 -05:00
|
|
|
* Runs on a 15 seconds interval, saves when there are differences in the post title or content.
|
2014-01-21 23:56:16 -05:00
|
|
|
* When the optional data is provided, updates the last saved post data.
|
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {Object} data The post data for saving, minimum 'post_title' and 'content'.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} Returns true when data has been saved, otherwise it returns false.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function save( data ) {
|
|
|
|
var postData, compareString,
|
|
|
|
result = false;
|
|
|
|
|
2014-07-16 14:34:16 -04:00
|
|
|
if ( isSuspended || ! hasStorage ) {
|
2014-01-21 23:56:16 -05:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( data ) {
|
|
|
|
postData = getSavedPostData() || {};
|
|
|
|
$.extend( postData, data );
|
|
|
|
} else {
|
|
|
|
postData = getPostData('local');
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
compareString = getCompareString( postData );
|
2013-07-22 23:05:21 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( typeof lastCompareString === 'undefined' ) {
|
|
|
|
lastCompareString = initialCompareString;
|
|
|
|
}
|
2013-07-22 23:05:21 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
// If the content, title and excerpt did not change since the last save, don't save again.
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( compareString === lastCompareString ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
postData.save_time = ( new Date() ).getTime();
|
|
|
|
postData.status = $( '#post_status' ).val() || '';
|
|
|
|
result = setData( postData );
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( result ) {
|
|
|
|
lastCompareString = compareString;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return result;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Initializes the auto save function.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* Checks whether the editor is active or not to use the editor events
|
|
|
|
* to autosave, or uses the values from the elements to autosave.
|
|
|
|
*
|
|
|
|
* Runs on DOM ready.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function run() {
|
|
|
|
post_id = $('#post_ID').val() || 0;
|
|
|
|
|
|
|
|
// Check if the local post data is different than the loaded post data.
|
|
|
|
if ( $( '#wp-content-wrap' ).hasClass( 'tmce-active' ) ) {
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
/*
|
|
|
|
* If TinyMCE loads first, check the post 1.5 seconds after it is ready.
|
|
|
|
* By this time the content has been loaded in the editor and 'saved' to the textarea.
|
|
|
|
* This prevents false positives.
|
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
$document.on( 'tinymce-editor-init.autosave', function() {
|
|
|
|
window.setTimeout( function() {
|
|
|
|
checkPost();
|
|
|
|
}, 1500 );
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
checkPost();
|
|
|
|
}
|
2013-07-22 23:05:21 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Save every 15 seconds.
|
2014-01-21 23:56:16 -05:00
|
|
|
intervalTimer = window.setInterval( save, 15000 );
|
|
|
|
|
|
|
|
$( 'form#post' ).on( 'submit.autosave-local', function() {
|
2016-06-16 23:02:29 -04:00
|
|
|
var editor = getEditor(),
|
2014-01-21 23:56:16 -05:00
|
|
|
post_id = $('#post_ID').val() || 0;
|
|
|
|
|
|
|
|
if ( editor && ! editor.isHidden() ) {
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
// Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
|
|
|
|
editor.on( 'submit', function() {
|
|
|
|
save({
|
|
|
|
post_title: $( '#title' ).val() || '',
|
|
|
|
content: $( '#content' ).val() || '',
|
|
|
|
excerpt: $( '#excerpt' ).val() || ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
save({
|
|
|
|
post_title: $( '#title' ).val() || '',
|
|
|
|
content: $( '#content' ).val() || '',
|
|
|
|
excerpt: $( '#excerpt' ).val() || ''
|
|
|
|
});
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2015-09-10 18:42:23 -04:00
|
|
|
var secure = ( 'https:' === window.location.protocol );
|
|
|
|
wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60, false, false, secure );
|
2014-01-21 23:56:16 -05:00
|
|
|
});
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Compares 2 strings. Removes whitespaces in the strings before comparing them.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {string} str1 The first string.
|
|
|
|
* @param {string} str2 The second string.
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} True if the strings are the same.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function compare( str1, str2 ) {
|
|
|
|
function removeSpaces( string ) {
|
|
|
|
return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) );
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Checks if the saved data for the current post (if any) is different than the
|
|
|
|
* loaded post data on the screen.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
|
|
|
* Shows a standard message letting the user restore the post data if different.
|
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function checkPost() {
|
|
|
|
var content, post_title, excerpt, $notice,
|
|
|
|
postData = getSavedPostData(),
|
2016-06-16 23:02:29 -04:00
|
|
|
cookie = wpCookies.get( 'wp-saving-post' ),
|
2016-10-27 16:39:39 -04:00
|
|
|
$newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' ),
|
|
|
|
$headerEnd = $( '.wp-header-end' );
|
2014-01-21 23:56:16 -05:00
|
|
|
|
2014-08-21 20:25:16 -04:00
|
|
|
if ( cookie === post_id + '-saved' ) {
|
|
|
|
wpCookies.remove( 'wp-saving-post' );
|
2020-01-28 19:45:18 -05:00
|
|
|
// The post was saved properly, remove old data and bail.
|
2014-08-21 20:25:16 -04:00
|
|
|
setData( false );
|
2014-01-21 23:56:16 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-08-21 20:25:16 -04:00
|
|
|
if ( ! postData ) {
|
|
|
|
return;
|
2014-01-21 23:56:16 -05:00
|
|
|
}
|
2013-07-18 20:22:22 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
content = $( '#content' ).val() || '';
|
|
|
|
post_title = $( '#title' ).val() || '';
|
|
|
|
excerpt = $( '#excerpt' ).val() || '';
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-08-21 20:25:16 -04:00
|
|
|
if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) &&
|
|
|
|
compare( excerpt, postData.excerpt ) ) {
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2016-10-27 16:39:39 -04:00
|
|
|
/*
|
|
|
|
* If '.wp-header-end' is found, append the notices after it otherwise
|
|
|
|
* after the first h1 or h2 heading found within the main content.
|
|
|
|
*/
|
|
|
|
if ( ! $headerEnd.length ) {
|
|
|
|
$headerEnd = $( '.wrap h1, .wrap h2' ).first();
|
|
|
|
}
|
|
|
|
|
2015-10-17 03:04:26 -04:00
|
|
|
$notice = $( '#local-storage-notice' )
|
2016-10-27 16:39:39 -04:00
|
|
|
.insertAfter( $headerEnd )
|
2016-06-16 23:02:29 -04:00
|
|
|
.addClass( 'notice-warning' );
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2016-06-16 23:02:29 -04:00
|
|
|
if ( $newerAutosaveNotice.length ) {
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2016-06-16 23:02:29 -04:00
|
|
|
// If there is a "server" autosave notice, hide it.
|
|
|
|
// The data in the session storage is either the same or newer.
|
|
|
|
$newerAutosaveNotice.slideUp( 150, function() {
|
|
|
|
$notice.slideDown( 150 );
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$notice.slideDown( 200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$notice.find( '.restore-backup' ).on( 'click.autosave-local', function() {
|
|
|
|
restorePost( postData );
|
|
|
|
$notice.fadeTo( 250, 0, function() {
|
|
|
|
$notice.slideUp( 150 );
|
|
|
|
});
|
2014-01-21 23:56:16 -05:00
|
|
|
});
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Restores the current title, content and excerpt from postData.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {Object} postData The object containing all post data.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} True if the post is restored.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function restorePost( postData ) {
|
|
|
|
var editor;
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( postData ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// Set the last saved data.
|
2014-01-21 23:56:16 -05:00
|
|
|
lastCompareString = getCompareString( postData );
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( $( '#title' ).val() !== postData.post_title ) {
|
2021-02-23 14:45:04 -05:00
|
|
|
$( '#title' ).trigger( 'focus' ).val( postData.post_title || '' );
|
2014-01-21 23:56:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$( '#excerpt' ).val( postData.excerpt || '' );
|
2016-06-16 23:02:29 -04:00
|
|
|
editor = getEditor();
|
2014-01-21 23:56:16 -05:00
|
|
|
|
|
|
|
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
|
2016-06-16 23:02:29 -04:00
|
|
|
if ( editor.settings.wpautop && postData.content ) {
|
|
|
|
postData.content = switchEditors.wpautop( postData.content );
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Make sure there's an undo level in the editor.
|
2016-06-16 23:02:29 -04:00
|
|
|
editor.undoManager.transact( function() {
|
|
|
|
editor.setContent( postData.content || '' );
|
|
|
|
editor.nodeChanged();
|
|
|
|
});
|
2014-01-21 23:56:16 -05:00
|
|
|
} else {
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Make sure the Text editor is selected.
|
2021-02-23 14:45:04 -05:00
|
|
|
$( '#content-html' ).trigger( 'click' );
|
|
|
|
$( '#content' ).trigger( 'focus' );
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2016-06-16 23:02:29 -04:00
|
|
|
// Using document.execCommand() will let the user undo.
|
|
|
|
document.execCommand( 'selectAll' );
|
|
|
|
document.execCommand( 'insertText', false, postData.content || '' );
|
2014-01-21 23:56:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
2013-03-13 23:06:07 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id;
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
/*
|
|
|
|
* Check if the browser supports sessionStorage and it's not disabled,
|
|
|
|
* then initialize and run checkPost().
|
|
|
|
* Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
|
|
|
|
*/
|
2014-07-16 14:34:16 -04:00
|
|
|
if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) {
|
2021-03-18 15:01:03 -04:00
|
|
|
$( run );
|
2013-03-25 19:43:06 -04:00
|
|
|
}
|
2014-01-21 23:56:16 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
hasStorage: hasStorage,
|
|
|
|
getSavedPostData: getSavedPostData,
|
2014-01-26 19:28:13 -05:00
|
|
|
save: save,
|
|
|
|
suspend: suspend,
|
|
|
|
resume: resume
|
2014-01-21 23:56:16 -05:00
|
|
|
};
|
2013-03-25 19:43:06 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Auto saves the post on the server.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Object} {
|
2017-08-18 10:24:45 -04:00
|
|
|
* {
|
|
|
|
* tempBlockSave: tempBlockSave,
|
|
|
|
* triggerSave: triggerSave,
|
|
|
|
* postChanged: postChanged,
|
|
|
|
* suspend: suspend,
|
|
|
|
* resume: resume
|
|
|
|
* }
|
|
|
|
* } The object all functions for autosave.
|
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function autosaveServer() {
|
2014-01-26 19:28:13 -05:00
|
|
|
var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
|
|
|
|
nextRun = 0,
|
|
|
|
isSuspended = false;
|
2014-01-21 23:56:16 -05:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
|
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Blocks saving for the next 10 seconds.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function tempBlockSave() {
|
|
|
|
_blockSave = true;
|
|
|
|
window.clearTimeout( _blockSaveTimer );
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
_blockSaveTimer = window.setTimeout( function() {
|
|
|
|
_blockSave = false;
|
|
|
|
}, 10000 );
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets isSuspended to true.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-26 19:28:13 -05:00
|
|
|
function suspend() {
|
|
|
|
isSuspended = true;
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets isSuspended to false.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-26 19:28:13 -05:00
|
|
|
function resume() {
|
|
|
|
isSuspended = false;
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Triggers the autosave with the post data.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param {Object} data The post data.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function response( data ) {
|
|
|
|
_schedule();
|
|
|
|
_blockSave = false;
|
|
|
|
lastCompareString = previousCompareString;
|
|
|
|
previousCompareString = '';
|
2013-03-13 23:06:07 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
$document.trigger( 'after-autosave', [data] );
|
|
|
|
enableButtons();
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( data.success ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// No longer an auto-draft.
|
2014-01-21 23:56:16 -05:00
|
|
|
$( '#auto_draft' ).val('');
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Saves immediately.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* Resets the timing and tells heartbeat to connect now.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function triggerSave() {
|
|
|
|
nextRun = 0;
|
|
|
|
wp.heartbeat.connectNow();
|
|
|
|
}
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Checks if the post content in the textarea has changed since page load.
|
2014-01-21 23:56:16 -05:00
|
|
|
*
|
|
|
|
* This also happens when TinyMCE is active and editor.save() is triggered by
|
|
|
|
* wp.autosave.getPostData().
|
|
|
|
*
|
2017-08-18 10:24:45 -04:00
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @return {boolean} True if the post has been changed.
|
2014-01-21 23:56:16 -05:00
|
|
|
*/
|
|
|
|
function postChanged() {
|
2020-12-16 10:25:09 -05:00
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
// If there are TinyMCE instances, loop through them.
|
|
|
|
if ( window.tinymce ) {
|
|
|
|
window.tinymce.each( [ 'content', 'excerpt' ], function( field ) {
|
|
|
|
var editor = window.tinymce.get( field );
|
|
|
|
|
|
|
|
if ( ! editor || editor.isHidden() ) {
|
Editor: Correct the check for unsaved content in `wp.autosave.server.postChanged()`.
This fixes improper triggering of the "Are you sure?" prompt when navigating away from the old, "classic" Edit Post screen and there are no changes.
The previous check did not account for Pages or any custom post types that don't have a Title, Content, or Excerpt field.
Follow-up to [50031].
Props hwk-fr, mukesh27, audrasjb, archon810, Clorith, ibiza69, tonysandwich, roger995, bartosz777, viablethought, dbtedg, worldedu, hmabpera, magnuswebdesign.
Fixes #52440.
Built from https://develop.svn.wordpress.org/trunk@50232
git-svn-id: http://core.svn.wordpress.org/trunk@49893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-02-06 09:16:07 -05:00
|
|
|
if ( ( $( '#' + field ).val() || '' ) !== initialCompareData[ field ] ) {
|
2020-12-16 10:25:09 -05:00
|
|
|
changed = true;
|
|
|
|
// Break.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( editor.isDirty() ) {
|
|
|
|
changed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
Editor: Correct the check for unsaved content in `wp.autosave.server.postChanged()`.
This fixes improper triggering of the "Are you sure?" prompt when navigating away from the old, "classic" Edit Post screen and there are no changes.
The previous check did not account for Pages or any custom post types that don't have a Title, Content, or Excerpt field.
Follow-up to [50031].
Props hwk-fr, mukesh27, audrasjb, archon810, Clorith, ibiza69, tonysandwich, roger995, bartosz777, viablethought, dbtedg, worldedu, hmabpera, magnuswebdesign.
Fixes #52440.
Built from https://develop.svn.wordpress.org/trunk@50232
git-svn-id: http://core.svn.wordpress.org/trunk@49893 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-02-06 09:16:07 -05:00
|
|
|
if ( ( $( '#title' ).val() || '' ) !== initialCompareData.post_title ) {
|
2020-12-16 10:25:09 -05:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
return getCompareString() !== initialCompareString;
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Checks if the post can be saved or not.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* If the post hasn't changed or it cannot be updated,
|
|
|
|
* because the autosave is blocked or suspended, the function returns false.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Object} Returns the post data.
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function save() {
|
|
|
|
var postData, compareString;
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// window.autosave() used for back-compat.
|
2014-03-25 12:47:15 -04:00
|
|
|
if ( isSuspended || _blockSave || ! window.autosave() ) {
|
2014-01-21 23:56:16 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( new Date() ).getTime() < nextRun ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
postData = getPostData();
|
|
|
|
compareString = getCompareString( postData );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// First check.
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( typeof lastCompareString === 'undefined' ) {
|
|
|
|
lastCompareString = initialCompareString;
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// No change.
|
2014-01-21 23:56:16 -05:00
|
|
|
if ( compareString === lastCompareString ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousCompareString = compareString;
|
|
|
|
tempBlockSave();
|
|
|
|
disableButtons();
|
|
|
|
|
|
|
|
$document.trigger( 'wpcountwords', [ postData.content ] )
|
|
|
|
.trigger( 'before-autosave', [ postData ] );
|
|
|
|
|
|
|
|
postData._wpnonce = $( '#_wpnonce' ).val() || '';
|
|
|
|
|
|
|
|
return postData;
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets the next run, based on the autosave interval.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
function _schedule() {
|
|
|
|
nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000;
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets the autosaveData on the autosave heartbeat.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2021-03-18 15:01:03 -04:00
|
|
|
$( function() {
|
|
|
|
_schedule();
|
|
|
|
}).on( 'heartbeat-send.autosave', function( event, data ) {
|
2014-01-21 23:56:16 -05:00
|
|
|
var autosaveData = save();
|
|
|
|
|
|
|
|
if ( autosaveData ) {
|
|
|
|
data.wp_autosave = autosaveData;
|
|
|
|
}
|
2017-08-18 10:24:45 -04:00
|
|
|
|
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Triggers the autosave of the post with the autosave data on the autosave
|
|
|
|
* heartbeat.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
}).on( 'heartbeat-tick.autosave', function( event, data ) {
|
|
|
|
if ( data.wp_autosave ) {
|
|
|
|
response( data.wp_autosave );
|
|
|
|
}
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Disables buttons and throws a notice when the connection is lost.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
}).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) {
|
2017-08-18 10:24:45 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
// When connection is lost, keep user from submitting changes.
|
|
|
|
if ( 'timeout' === error || 603 === status ) {
|
|
|
|
var $notice = $('#lost-connection-notice');
|
|
|
|
|
|
|
|
if ( ! wp.autosave.local.hasStorage ) {
|
|
|
|
$notice.find('.hide-if-no-sessionstorage').hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
$notice.show();
|
|
|
|
disableButtons();
|
|
|
|
}
|
2017-08-18 10:24:45 -04:00
|
|
|
|
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Enables buttons when the connection is restored.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2014-01-21 23:56:16 -05:00
|
|
|
}).on( 'heartbeat-connection-restored.autosave', function() {
|
|
|
|
$('#lost-connection-notice').hide();
|
|
|
|
enableButtons();
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
tempBlockSave: tempBlockSave,
|
|
|
|
triggerSave: triggerSave,
|
2014-01-26 19:28:13 -05:00
|
|
|
postChanged: postChanged,
|
|
|
|
suspend: suspend,
|
|
|
|
resume: resume
|
2014-01-21 23:56:16 -05:00
|
|
|
};
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 10:24:45 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sets the autosave time out.
|
2017-08-18 10:24:45 -04:00
|
|
|
*
|
|
|
|
* Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading,
|
|
|
|
* then save to the textarea before setting initialCompareString.
|
|
|
|
* This avoids any insignificant differences between the initial textarea content and the content
|
|
|
|
* extracted from the editor.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-08-18 10:24:45 -04:00
|
|
|
*/
|
2021-03-18 15:01:03 -04:00
|
|
|
$( function() {
|
|
|
|
// Set the initial compare string in case TinyMCE is not used or not loaded first.
|
|
|
|
setInitialCompare();
|
|
|
|
}).on( 'tinymce-editor-init.autosave', function( event, editor ) {
|
2020-12-16 10:25:09 -05:00
|
|
|
// Reset the initialCompare data after the TinyMCE instances have been initialized.
|
|
|
|
if ( 'content' === editor.id || 'excerpt' === editor.id ) {
|
2014-01-21 23:56:16 -05:00
|
|
|
window.setTimeout( function() {
|
|
|
|
editor.save();
|
2020-12-16 10:25:09 -05:00
|
|
|
setInitialCompare();
|
2014-01-21 23:56:16 -05:00
|
|
|
}, 1000 );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
getPostData: getPostData,
|
|
|
|
getCompareString: getCompareString,
|
|
|
|
disableButtons: disableButtons,
|
|
|
|
enableButtons: enableButtons,
|
|
|
|
local: autosaveLocal(),
|
|
|
|
server: autosaveServer()
|
|
|
|
};
|
2013-03-13 06:08:16 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 14:42:49 -04:00
|
|
|
/** @namespace wp */
|
2014-01-21 23:56:16 -05:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.autosave = autosave();
|
2013-03-13 06:08:16 -04:00
|
|
|
|
2014-01-21 23:56:16 -05:00
|
|
|
}( jQuery, window ));
|