diff --git a/src/wp-includes/js/autosave.js b/src/wp-includes/js/autosave.js
index e88943d6d..8dc83d47a 100644
|
a
|
b
|
window.autosave = function() { |
| 4 | 4 | return true; |
| 5 | 5 | }; |
| 6 | 6 | |
| | 7 | /** |
| | 8 | * @summary Adds autosave to the window object on dom ready. |
| | 9 | * |
| | 10 | * @since 3.9.0 |
| | 11 | * |
| | 12 | * @param {jQuery} $ jQuery object. |
| | 13 | * @param {window} The window object. |
| | 14 | * |
| | 15 | */ |
| 7 | 16 | ( function( $, window ) { |
| | 17 | /** |
| | 18 | * @summary Auto saves the post. |
| | 19 | * |
| | 20 | * @since 3.9.0 |
| | 21 | * |
| | 22 | * @returns {Object} |
| | 23 | * {{ |
| | 24 | * getPostData: getPostData, |
| | 25 | * getCompareString: getCompareString, |
| | 26 | * disableButtons: disableButtons, |
| | 27 | * enableButtons: enableButtons, |
| | 28 | * local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), |
| | 29 | * server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)} |
| | 30 | * } |
| | 31 | * The object with all functions for autosave. |
| | 32 | */ |
| 8 | 33 | function autosave() { |
| 9 | 34 | var initialCompareString, |
| 10 | 35 | lastTriggerSave = 0, |
| 11 | 36 | $document = $(document); |
| 12 | 37 | |
| 13 | 38 | /** |
| 14 | | * Returns the data saved in both local and remote autosave |
| | 39 | * @summary Returns the data saved in both local and remote autosave. |
| 15 | 40 | * |
| 16 | | * @return object Object containing the post data |
| | 41 | * @since 3.9.0 |
| | 42 | * |
| | 43 | * @param {string} type The type of autosave either local or remote. |
| | 44 | * |
| | 45 | * @returns {Object} Object containing the post data. |
| 17 | 46 | */ |
| 18 | 47 | function getPostData( type ) { |
| 19 | 48 | var post_name, parent_id, data, |
| … |
… |
window.autosave = function() { |
| 69 | 98 | return data; |
| 70 | 99 | } |
| 71 | 100 | |
| 72 | | // Concatenate title, content and excerpt. Used to track changes when auto-saving. |
| | 101 | /** |
| | 102 | * @summary Concatenates the title, content and excerpt. |
| | 103 | * |
| | 104 | * This is used to track changes when auto-saving. |
| | 105 | * |
| | 106 | * @since 3.9.0 |
| | 107 | * |
| | 108 | * @param {Object} postData The object containing the post data. |
| | 109 | * @returns {string} A concatenated string with title, content and excerpt. |
| | 110 | */ |
| 73 | 111 | function getCompareString( postData ) { |
| 74 | 112 | if ( typeof postData === 'object' ) { |
| 75 | 113 | return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' ); |
| … |
… |
window.autosave = function() { |
| 78 | 116 | return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); |
| 79 | 117 | } |
| 80 | 118 | |
| | 119 | /** |
| | 120 | * @summary Disables save buttons. |
| | 121 | * |
| | 122 | * @since 3.9.0 |
| | 123 | * |
| | 124 | * @returns {void} |
| | 125 | */ |
| 81 | 126 | function disableButtons() { |
| 82 | 127 | $document.trigger('autosave-disable-buttons'); |
| 83 | 128 | // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. |
| 84 | 129 | setTimeout( enableButtons, 5000 ); |
| 85 | 130 | } |
| 86 | 131 | |
| | 132 | /** |
| | 133 | * @summary Enables save buttons. |
| | 134 | * |
| | 135 | * @since 3.9.0 |
| | 136 | * |
| | 137 | * @returns {void} |
| | 138 | */ |
| 87 | 139 | function enableButtons() { |
| 88 | 140 | $document.trigger( 'autosave-enable-buttons' ); |
| 89 | 141 | } |
| 90 | 142 | |
| | 143 | /** |
| | 144 | * @summary Gets the content editor. |
| | 145 | * |
| | 146 | * @since 4.6.0 |
| | 147 | * |
| | 148 | * @returns {boolean|*} Returns either false if the editor is undefined, |
| | 149 | * or the instance of the content editor. |
| | 150 | */ |
| 91 | 151 | function getEditor() { |
| 92 | 152 | return typeof tinymce !== 'undefined' && tinymce.get('content'); |
| 93 | 153 | } |
| 94 | 154 | |
| 95 | | // Autosave in localStorage |
| | 155 | /** |
| | 156 | * @summary Autosaves in the localStorage. |
| | 157 | * |
| | 158 | * @since 3.9.0 |
| | 159 | * |
| | 160 | * @returns { |
| | 161 | * { |
| | 162 | * hasStorage: *, |
| | 163 | * getSavedPostData: getSavedPostData, |
| | 164 | * save: save, |
| | 165 | * suspend: suspend, |
| | 166 | * resume: resume |
| | 167 | * } |
| | 168 | * } |
| | 169 | * The object with all functions for local storage autosave. |
| | 170 | */ |
| 96 | 171 | function autosaveLocal() { |
| 97 | 172 | var blog_id, post_id, hasStorage, intervalTimer, |
| 98 | 173 | lastCompareString, |
| 99 | 174 | isSuspended = false; |
| 100 | 175 | |
| 101 | | // Check if the browser supports sessionStorage and it's not disabled |
| | 176 | /** |
| | 177 | * @summary Checks if the browser supports sessionStorage and it's not disabled. |
| | 178 | * |
| | 179 | * @since 3.9.0 |
| | 180 | * |
| | 181 | * @returns {boolean} True if the sessionStorage is supported and enabled. |
| | 182 | */ |
| 102 | 183 | function checkStorage() { |
| 103 | 184 | var test = Math.random().toString(), |
| 104 | 185 | result = false; |
| … |
… |
window.autosave = function() { |
| 114 | 195 | } |
| 115 | 196 | |
| 116 | 197 | /** |
| 117 | | * Initialize the local storage |
| | 198 | * @summary Initializes the local storage. |
| 118 | 199 | * |
| 119 | | * @return mixed False if no sessionStorage in the browser or an Object containing all postData for this blog |
| | 200 | * @since 3.9.0 |
| | 201 | * |
| | 202 | * @returns {boolean|Object} False if no sessionStorage in the browser or an Object containing all postData for this blog. |
| 120 | 203 | */ |
| 121 | 204 | function getStorage() { |
| 122 | 205 | var stored_obj = false; |
| … |
… |
window.autosave = function() { |
| 135 | 218 | } |
| 136 | 219 | |
| 137 | 220 | /** |
| 138 | | * Set the storage for this blog |
| | 221 | * @summary Sets the storage for this blog. |
| 139 | 222 | * |
| 140 | 223 | * Confirms that the data was saved successfully. |
| 141 | 224 | * |
| 142 | | * @return bool |
| | 225 | * @since 3.9.0 |
| | 226 | * |
| | 227 | * @returns {boolean} True if the data was saved successfully, false if it wasn't saved. |
| 143 | 228 | */ |
| 144 | 229 | function setStorage( stored_obj ) { |
| 145 | 230 | var key; |
| … |
… |
window.autosave = function() { |
| 154 | 239 | } |
| 155 | 240 | |
| 156 | 241 | /** |
| 157 | | * Get the saved post data for the current post |
| | 242 | * @summary Gets the saved post data for the current post. |
| 158 | 243 | * |
| 159 | | * @return mixed False if no storage or no data or the postData as an Object |
| | 244 | * @since 3.9.0 |
| | 245 | * |
| | 246 | * @returns {boolean|Object} False if no storage or no data or the postData as an Object. |
| 160 | 247 | */ |
| 161 | 248 | function getSavedPostData() { |
| 162 | 249 | var stored = getStorage(); |
| … |
… |
window.autosave = function() { |
| 169 | 256 | } |
| 170 | 257 | |
| 171 | 258 | /** |
| 172 | | * Set (save or delete) post data in the storage. |
| | 259 | * @summary Sets (save or delete) post data in the storage. |
| | 260 | * |
| | 261 | * If stored_data evaluates to 'false' the storage key for the current post will be removed. |
| 173 | 262 | * |
| 174 | | * If stored_data evaluates to 'false' the storage key for the current post will be removed |
| | 263 | * @since 3.9.0 |
| 175 | 264 | * |
| 176 | | * $param stored_data The post data to store or null/false/empty to delete the key |
| 177 | | * @return bool |
| | 265 | * @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key. |
| | 266 | * |
| | 267 | * @returns {boolean} True if data is stored, false if data was removed. |
| 178 | 268 | */ |
| 179 | 269 | function setData( stored_data ) { |
| 180 | 270 | var stored = getStorage(); |
| … |
… |
window.autosave = function() { |
| 194 | 284 | return setStorage( stored ); |
| 195 | 285 | } |
| 196 | 286 | |
| | 287 | /** |
| | 288 | * @summary Sets isSuspended to true. |
| | 289 | * |
| | 290 | * @since 3.9.0 |
| | 291 | * |
| | 292 | * @returns {void} |
| | 293 | */ |
| 197 | 294 | function suspend() { |
| 198 | 295 | isSuspended = true; |
| 199 | 296 | } |
| 200 | 297 | |
| | 298 | /** |
| | 299 | * @summary Sets isSuspended to false. |
| | 300 | * |
| | 301 | * @since 3.9.0 |
| | 302 | * |
| | 303 | * @returns {void} |
| | 304 | */ |
| 201 | 305 | function resume() { |
| 202 | 306 | isSuspended = false; |
| 203 | 307 | } |
| 204 | 308 | |
| 205 | 309 | /** |
| 206 | | * Save post data for the current post |
| | 310 | * @summary Saves post data for the current post. |
| 207 | 311 | * |
| 208 | 312 | * Runs on a 15 sec. interval, saves when there are differences in the post title or content. |
| 209 | 313 | * When the optional data is provided, updates the last saved post data. |
| 210 | 314 | * |
| 211 | | * $param data optional Object The post data for saving, minimum 'post_title' and 'content' |
| 212 | | * @return bool |
| | 315 | * @since 3.9.0 |
| | 316 | * |
| | 317 | * @param {Object} data The post data for saving, minimum 'post_title' and 'content'. |
| | 318 | * |
| | 319 | * @returns {boolean} Returns true when data has been saved, otherwise it returns false. |
| 213 | 320 | */ |
| 214 | 321 | function save( data ) { |
| 215 | 322 | var postData, compareString, |
| … |
… |
window.autosave = function() { |
| 248 | 355 | return result; |
| 249 | 356 | } |
| 250 | 357 | |
| 251 | | // Run on DOM ready |
| | 358 | /** |
| | 359 | * @summary Initializes the auto save function. |
| | 360 | * |
| | 361 | * Checks whether the editor is active or not to use the editor events |
| | 362 | * to autosave, or uses the values from the elements to autosave. |
| | 363 | * |
| | 364 | * Runs on DOM ready. |
| | 365 | * |
| | 366 | * @since 3.9.0 |
| | 367 | * |
| | 368 | * @returns {void} |
| | 369 | */ |
| 252 | 370 | function run() { |
| 253 | 371 | post_id = $('#post_ID').val() || 0; |
| 254 | 372 | |
| … |
… |
window.autosave = function() { |
| 295 | 413 | }); |
| 296 | 414 | } |
| 297 | 415 | |
| 298 | | // Strip whitespace and compare two strings |
| | 416 | /** |
| | 417 | * @summary Compares 2 strings. |
| | 418 | * |
| | 419 | * Removes whitespaces in the strings before comparing them. |
| | 420 | * |
| | 421 | * @since 3.9.0 |
| | 422 | * |
| | 423 | * @param {string} str1 The first string. |
| | 424 | * @param {string} str2 The second string. |
| | 425 | * @returns {boolean} True if the strings are the same. |
| | 426 | */ |
| 299 | 427 | function compare( str1, str2 ) { |
| 300 | 428 | function removeSpaces( string ) { |
| 301 | 429 | return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); |
| … |
… |
window.autosave = function() { |
| 305 | 433 | } |
| 306 | 434 | |
| 307 | 435 | /** |
| 308 | | * Check if the saved data for the current post (if any) is different than the loaded post data on the screen |
| | 436 | * @summary Checks if the saved data for the current post (if any) is different |
| | 437 | * than the loaded post data on the screen. |
| 309 | 438 | * |
| 310 | 439 | * Shows a standard message letting the user restore the post data if different. |
| 311 | 440 | * |
| 312 | | * @return void |
| | 441 | * @since 3.9.0 |
| | 442 | * |
| | 443 | * @returns {void} |
| 313 | 444 | */ |
| 314 | 445 | function checkPost() { |
| 315 | 446 | var content, post_title, excerpt, $notice, |
| … |
… |
window.autosave = function() { |
| 369 | 500 | }); |
| 370 | 501 | } |
| 371 | 502 | |
| 372 | | // Restore the current title, content and excerpt from postData. |
| | 503 | /** |
| | 504 | * @summary Restores the current title, content and excerpt from postData. |
| | 505 | * |
| | 506 | * @since 3.9.0 |
| | 507 | * |
| | 508 | * @param {Object} postData The object containing all post data. |
| | 509 | * @returns {boolean} True if the post is restored. |
| | 510 | */ |
| 373 | 511 | function restorePost( postData ) { |
| 374 | 512 | var editor; |
| 375 | 513 | |
| … |
… |
window.autosave = function() { |
| 427 | 565 | }; |
| 428 | 566 | } |
| 429 | 567 | |
| 430 | | // Autosave on the server |
| | 568 | /** |
| | 569 | * @summary Auto saves the post on the server. |
| | 570 | * |
| | 571 | * @since 3.9.0 |
| | 572 | * |
| | 573 | * @returns {Object} { |
| | 574 | * { |
| | 575 | * tempBlockSave: tempBlockSave, |
| | 576 | * triggerSave: triggerSave, |
| | 577 | * postChanged: postChanged, |
| | 578 | * suspend: suspend, |
| | 579 | * resume: resume |
| | 580 | * } |
| | 581 | * } The object all functions for autosave. |
| | 582 | */ |
| 431 | 583 | function autosaveServer() { |
| 432 | 584 | var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString, |
| 433 | 585 | nextRun = 0, |
| 434 | 586 | isSuspended = false; |
| 435 | 587 | |
| 436 | | // Block saving for the next 10 sec. |
| | 588 | |
| | 589 | /** |
| | 590 | * @summary Blocks saving for the next 10 seconds. |
| | 591 | * |
| | 592 | * @since 3.9.0 |
| | 593 | * |
| | 594 | * @returns {void} |
| | 595 | */ |
| 437 | 596 | function tempBlockSave() { |
| 438 | 597 | _blockSave = true; |
| 439 | 598 | window.clearTimeout( _blockSaveTimer ); |
| … |
… |
window.autosave = function() { |
| 443 | 602 | }, 10000 ); |
| 444 | 603 | } |
| 445 | 604 | |
| | 605 | /** |
| | 606 | * @summary Sets isSuspended to true. |
| | 607 | * |
| | 608 | * @since 3.9.0 |
| | 609 | * |
| | 610 | * @returns {void} |
| | 611 | */ |
| 446 | 612 | function suspend() { |
| 447 | 613 | isSuspended = true; |
| 448 | 614 | } |
| 449 | 615 | |
| | 616 | /** |
| | 617 | * @summary Sets isSuspended to false. |
| | 618 | * |
| | 619 | * @since 3.9.0 |
| | 620 | * |
| | 621 | * @returns {void} |
| | 622 | */ |
| 450 | 623 | function resume() { |
| 451 | 624 | isSuspended = false; |
| 452 | 625 | } |
| 453 | 626 | |
| 454 | | // Runs on heartbeat-response |
| | 627 | /** |
| | 628 | * @summary Triggers the autosave with the post data. |
| | 629 | * |
| | 630 | * @since 3.9.0 |
| | 631 | * |
| | 632 | * @param {Object} data The post data. |
| | 633 | * |
| | 634 | * @returns {void} |
| | 635 | */ |
| 455 | 636 | function response( data ) { |
| 456 | 637 | _schedule(); |
| 457 | 638 | _blockSave = false; |
| … |
… |
window.autosave = function() { |
| 468 | 649 | } |
| 469 | 650 | |
| 470 | 651 | /** |
| 471 | | * Save immediately |
| | 652 | * @summary Saves immediately. |
| | 653 | * |
| | 654 | * Resets the timing and tells heartbeat to connect now. |
| 472 | 655 | * |
| 473 | | * Resets the timing and tells heartbeat to connect now |
| | 656 | * @since 3.9.0 |
| 474 | 657 | * |
| 475 | | * @return void |
| | 658 | * @returns {void} |
| 476 | 659 | */ |
| 477 | 660 | function triggerSave() { |
| 478 | 661 | nextRun = 0; |
| … |
… |
window.autosave = function() { |
| 480 | 663 | } |
| 481 | 664 | |
| 482 | 665 | /** |
| 483 | | * Checks if the post content in the textarea has changed since page load. |
| | 666 | * @summary Checks if the post content in the textarea has changed since page load. |
| 484 | 667 | * |
| 485 | 668 | * This also happens when TinyMCE is active and editor.save() is triggered by |
| 486 | 669 | * wp.autosave.getPostData(). |
| 487 | 670 | * |
| 488 | | * @return bool |
| | 671 | * @since 3.9.0 |
| | 672 | * |
| | 673 | * @return {boolean} True if the post has been changed. |
| 489 | 674 | */ |
| 490 | 675 | function postChanged() { |
| 491 | 676 | return getCompareString() !== initialCompareString; |
| 492 | 677 | } |
| 493 | 678 | |
| 494 | | // Runs on 'heartbeat-send' |
| | 679 | /** |
| | 680 | * @summary Checks if the post can be saved or not. |
| | 681 | * |
| | 682 | * If the post hasn't changed or it cannot be updated, |
| | 683 | * because the autosave is blocked or suspended, the function returns false. |
| | 684 | * |
| | 685 | * @since 3.9.0 |
| | 686 | * |
| | 687 | * @returns {Object} Returns the post data. |
| | 688 | */ |
| 495 | 689 | function save() { |
| 496 | 690 | var postData, compareString; |
| 497 | 691 | |
| … |
… |
window.autosave = function() { |
| 529 | 723 | return postData; |
| 530 | 724 | } |
| 531 | 725 | |
| | 726 | /** |
| | 727 | * @summary Sets the next run, based on the autosave interval. |
| | 728 | * |
| | 729 | * @private |
| | 730 | * |
| | 731 | * @since 3.9.0 |
| | 732 | * |
| | 733 | * @returns {void} |
| | 734 | */ |
| 532 | 735 | function _schedule() { |
| 533 | 736 | nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000; |
| 534 | 737 | } |
| 535 | 738 | |
| | 739 | /** |
| | 740 | * @summary Sets the autosaveData on the autosave heartbeat. |
| | 741 | * |
| | 742 | * @since 3.9.0 |
| | 743 | * |
| | 744 | * @returns {void} |
| | 745 | */ |
| 536 | 746 | $document.on( 'heartbeat-send.autosave', function( event, data ) { |
| 537 | 747 | var autosaveData = save(); |
| 538 | 748 | |
| 539 | 749 | if ( autosaveData ) { |
| 540 | 750 | data.wp_autosave = autosaveData; |
| 541 | 751 | } |
| | 752 | |
| | 753 | /** |
| | 754 | * @summary Triggers the autosave of the post with the autosave data |
| | 755 | * on the autosave heartbeat. |
| | 756 | * |
| | 757 | * @since 3.9.0 |
| | 758 | * |
| | 759 | * @returns {void} |
| | 760 | */ |
| 542 | 761 | }).on( 'heartbeat-tick.autosave', function( event, data ) { |
| 543 | 762 | if ( data.wp_autosave ) { |
| 544 | 763 | response( data.wp_autosave ); |
| 545 | 764 | } |
| | 765 | /** |
| | 766 | * @summary Disables buttons and throws a notice when the connection is lost. |
| | 767 | * |
| | 768 | * @since 3.9.0 |
| | 769 | * |
| | 770 | * @returns {void} |
| | 771 | */ |
| 546 | 772 | }).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) { |
| | 773 | |
| 547 | 774 | // When connection is lost, keep user from submitting changes. |
| 548 | 775 | if ( 'timeout' === error || 603 === status ) { |
| 549 | 776 | var $notice = $('#lost-connection-notice'); |
| … |
… |
window.autosave = function() { |
| 555 | 782 | $notice.show(); |
| 556 | 783 | disableButtons(); |
| 557 | 784 | } |
| | 785 | |
| | 786 | /** |
| | 787 | * @summary Enables buttons when the connection is restored. |
| | 788 | * |
| | 789 | * @since 3.9.0 |
| | 790 | * |
| | 791 | * @returns {void} |
| | 792 | */ |
| 558 | 793 | }).on( 'heartbeat-connection-restored.autosave', function() { |
| 559 | 794 | $('#lost-connection-notice').hide(); |
| 560 | 795 | enableButtons(); |
| … |
… |
window.autosave = function() { |
| 571 | 806 | }; |
| 572 | 807 | } |
| 573 | 808 | |
| 574 | | // Wait for TinyMCE to initialize plus 1 sec. for any external css to finish loading, |
| 575 | | // then 'save' to the textarea before setting initialCompareString. |
| 576 | | // This avoids any insignificant differences between the initial textarea content and the content |
| 577 | | // extracted from the editor. |
| | 809 | /** |
| | 810 | * @summary Sets the autosave time out. |
| | 811 | * |
| | 812 | * Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading, |
| | 813 | * then save to the textarea before setting initialCompareString. |
| | 814 | * This avoids any insignificant differences between the initial textarea content and the content |
| | 815 | * extracted from the editor. |
| | 816 | * |
| | 817 | * @since 3.9.0 |
| | 818 | * |
| | 819 | * @returns {void} |
| | 820 | */ |
| 578 | 821 | $document.on( 'tinymce-editor-init.autosave', function( event, editor ) { |
| 579 | 822 | if ( editor.id === 'content' ) { |
| 580 | 823 | window.setTimeout( function() { |