Ticket #24756: 24756.3.diff
File 24756.3.diff, 23.0 KB (added by , 11 years ago) |
---|
-
wp-includes/js/autosave.js
1 1 var autosave, autosaveLast = '', autosavePeriodical, autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true; 2 2 3 3 jQuery(document).ready( function($) { 4 4 5 5 if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof tinymce != 'undefined' ) { 6 6 tinymce.onAddEditor.add( function( tinymce, editor ) { 7 7 if ( 'content' == editor.id ) { 8 8 editor.onLoad.add( function() { 9 9 editor.save(); 10 autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '');10 autosaveLast = wp.autosave.getLastSavedCompareString(); 11 11 }); 12 12 } 13 13 }); 14 14 } else { 15 autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '');15 autosaveLast = wp.autosave.getLastSavedCompareString(); 16 16 } 17 17 18 18 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); 19 19 20 20 //Disable autosave after the form has been submitted 21 21 $("#post").submit(function() { 22 22 $.cancel(autosavePeriodical); 23 23 autosaveLockRelease = false; 24 24 }); 25 25 26 26 $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){ 27 27 blockSave = true; 28 28 window.onbeforeunload = null; 29 29 $(':button, :submit', '#submitpost').each(function(){ 30 30 var t = $(this); 31 31 if ( t.hasClass('button-primary') ) 32 32 t.addClass('button-primary-disabled'); 33 33 else 34 34 t.addClass('button-disabled'); 35 35 }); 36 36 if ( $(this).attr('id') == 'publish' ) 37 37 $('#major-publishing-actions .spinner').show(); 38 38 else 39 39 $('#minor-publishing .spinner').show(); 40 40 }); 41 41 42 42 window.onbeforeunload = function(){ 43 var mce = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, title, content;43 var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString; 44 44 45 if ( mce && !mce.isHidden() ) {46 if ( mce.isDirty() )45 if ( editor && ! editor.isHidden() ) { 46 if ( editor.isDirty() ) 47 47 return autosaveL10n.saveAlert; 48 48 } else { 49 49 if ( fullscreen && fullscreen.settings.visible ) { 50 title = $('#wp-fullscreen-title').val() || ''; 51 content = $("#wp_mce_fullscreen").val() || ''; 50 compareString = wp.autosave.getLastSavedCompareString({ 51 post_title: $('#wp-fullscreen-title').val() || '', 52 content: $('#wp_mce_fullscreen').val() || '', 53 excerpt: $('#excerpt').val() || '' 54 }); 52 55 } else { 53 title = $('#post #title').val() || ''; 54 content = $('#post #content').val() || ''; 56 compareString = wp.autosave.getLastSavedCompareString(); 55 57 } 56 58 57 if ( ( title || content ) && title + content!= autosaveLast )59 if ( compareString != autosaveLast ) 58 60 return autosaveL10n.saveAlert; 59 61 } 60 62 }; 61 63 62 64 $(window).unload( function(e) { 63 65 if ( ! autosaveLockRelease ) 64 66 return; 65 67 66 68 // unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload. 67 69 if ( e.target && e.target.nodeName != '#document' ) 68 70 return; 69 71 70 72 $.ajax({ 71 73 type: 'POST', 72 74 url: ajaxurl, 73 75 async: false, 74 76 data: { 75 77 action: 'wp-remove-post-lock', 76 78 _wpnonce: $('#_wpnonce').val(), 77 79 post_ID: $('#post_ID').val(), 78 80 active_post_lock: $('#active_post_lock').val() 79 81 } 80 82 }); 81 83 } ); 82 84 83 85 // preview 84 86 $('#post-preview').click(function(){ 85 87 if ( $('#auto_draft').val() == '1' && notSaved ) { 86 88 autosaveDelayPreview = true; 87 89 autosave(); 88 90 return false; 89 91 } 90 92 doPreview(); 91 93 return false; 92 94 }); 93 95 94 96 doPreview = function() { 95 97 $('input#wp-preview').val('dopreview'); 96 98 $('form#post').attr('target', 'wp-preview').submit().attr('target', ''); 97 99 98 100 /* 99 101 * Workaround for WebKit bug preventing a form submitting twice to the same action. 100 102 * https://bugs.webkit.org/show_bug.cgi?id=28633 101 103 */ 102 104 var ua = navigator.userAgent.toLowerCase(); 103 105 if ( ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1 ) { 104 106 $('form#post').attr('action', function(index, value) { 105 107 return value + '?t=' + new Date().getTime(); 106 108 }); 107 109 } 108 110 109 111 $('input#wp-preview').val(''); 110 112 } 111 113 112 114 // This code is meant to allow tabbing from Title to Post content. 113 115 $('#title').on('keydown.editor-focus', function(e) { 114 116 var ed; 115 117 116 118 if ( e.which != 9 ) 117 119 return; 118 120 119 121 if ( !e.ctrlKey && !e.altKey && !e.shiftKey ) { 120 122 if ( typeof(tinymce) != 'undefined' ) 121 123 ed = tinymce.get('content'); 122 124 123 125 if ( ed && !ed.isHidden() ) { 124 126 $(this).one('keyup', function(e){ 125 127 $('#content_tbl td.mceToolbar > a').focus(); 126 128 }); 127 129 } else { 128 130 $('#content').focus(); 129 131 } 130 132 131 133 e.preventDefault(); 132 134 } 133 135 }); 134 136 135 137 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked 136 138 if ( '1' == $('#auto_draft').val() ) { 137 139 $('#title').blur( function() { 138 140 if ( !this.value || $('#auto_draft').val() != '1' ) 139 141 return; 140 142 delayed_autosave(); 141 143 }); 142 144 } 143 145 144 146 // When connection is lost, keep user from submitting changes. 145 147 $(document).on('heartbeat-connection-lost.autosave', function( e, error ) { 146 148 if ( 'timeout' === error ) { 147 149 var notice = $('#lost-connection-notice'); 148 150 if ( ! wp.autosave.local.hasStorage ) { 149 151 notice.find('.hide-if-no-sessionstorage').hide(); 150 152 } 151 153 notice.show(); 152 154 autosave_disable_buttons(); 153 155 } 154 156 }).on('heartbeat-connection-restored.autosave', function() { 155 157 $('#lost-connection-notice').hide(); 156 158 autosave_enable_buttons(); 157 159 }); 158 160 }); 159 161 160 162 function autosave_parse_response( response ) { 161 163 var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup; 162 164 163 165 if ( res && res.responses && res.responses.length ) { 164 166 if ( res.responses[0].supplemental ) { 165 167 sup = res.responses[0].supplemental; 166 168 167 169 jQuery.each( sup, function( selector, value ) { 168 170 if ( selector.match(/^replace-/) ) 169 171 jQuery( '#' + selector.replace('replace-', '') ).val( value ); 170 172 }); 171 173 } 172 174 173 175 // if no errors: add slug UI and update autosave-message 174 176 if ( !res.errors ) { 175 177 if ( post_id = parseInt( res.responses[0].id, 10 ) ) 176 178 autosave_update_slug( post_id ); 177 179 178 180 if ( res.responses[0].data ) // update autosave message 179 181 jQuery('.autosave-message').text( res.responses[0].data ); 180 182 } 181 183 } 182 184 183 185 return res; 184 186 } 185 187 186 188 // called when autosaving pre-existing post 187 189 function autosave_saved(response) { 188 190 blockSave = false; 189 191 autosave_parse_response(response); // parse the ajax response 190 192 autosave_enable_buttons(); // re-enable disabled form buttons 191 193 } 192 194 193 195 // called when autosaving new post 194 196 function autosave_saved_new(response) { 195 197 blockSave = false; 196 198 var res = autosave_parse_response(response), post_id; 197 199 198 200 if ( res && res.responses.length && !res.errors ) { 199 201 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves 200 202 post_id = parseInt( res.responses[0].id, 10 ); 201 203 202 204 if ( post_id ) { 203 205 notSaved = false; 204 206 jQuery('#auto_draft').val('0'); // No longer an auto-draft 205 207 } 206 208 207 209 autosave_enable_buttons(); 208 210 209 211 if ( autosaveDelayPreview ) { 210 212 autosaveDelayPreview = false; 211 213 doPreview(); 212 214 } 213 215 } else { 214 216 autosave_enable_buttons(); // re-enable disabled form buttons 215 217 } 216 218 } 217 219 218 220 function autosave_update_slug(post_id) { 219 221 // create slug area only if not already there 220 222 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) { 221 223 jQuery.post( ajaxurl, { 222 224 action: 'sample-permalink', 223 225 post_id: post_id, 224 226 new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(), 225 227 samplepermalinknonce: jQuery('#samplepermalinknonce').val() 226 228 }, 227 229 function(data) { 228 230 if ( data !== '-1' ) { 229 231 var box = jQuery('#edit-slug-box'); 230 232 box.html(data); 231 233 if (box.hasClass('hidden')) { 232 234 box.fadeIn('fast', function () { 233 235 box.removeClass('hidden'); 234 236 }); 235 237 } 236 238 makeSlugeditClickable(); 237 239 } 238 240 } 239 241 ); 240 242 } 241 243 } 242 244 243 245 function autosave_loading() { 244 246 jQuery('.autosave-message').html(autosaveL10n.savingText); 245 247 } 246 248 247 249 function autosave_enable_buttons() { 248 250 jQuery(document).trigger('autosave-enable-buttons'); 249 if ( ! wp.heartbeat .hasConnectionError() ) {251 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) { 250 252 // delay that a bit to avoid some rare collisions while the DOM is being updated. 251 253 setTimeout(function(){ 252 254 var parent = jQuery('#submitpost'); 253 255 parent.find(':button, :submit').removeAttr('disabled'); 254 256 parent.find('.spinner').hide(); 255 257 }, 500); 256 258 } 257 259 } 258 260 259 261 function autosave_disable_buttons() { 260 262 jQuery(document).trigger('autosave-disable-buttons'); 261 263 jQuery('#submitpost').find(':button, :submit').prop('disabled', true); 262 264 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. 263 265 setTimeout( autosave_enable_buttons, 5000 ); 264 266 } 265 267 266 268 function delayed_autosave() { 267 269 setTimeout(function(){ 268 270 if ( blockSave ) 269 271 return; 270 272 autosave(); 271 273 }, 200); 272 274 } 273 275 274 276 autosave = function() { 275 277 var post_data = wp.autosave.getPostData(), 276 doAutoSave = post_data.autosave,278 compareString, 277 279 successCallback; 278 280 279 281 blockSave = true; 280 282 283 // post_data.content cannot be retrieved at the moment 284 if ( ! post_data.autosave ) 285 return false; 286 281 287 // No autosave while thickbox is open (media buttons) 282 288 if ( jQuery("#TB_window").css('display') == 'block' ) 283 doAutoSave =false;289 return false; 284 290 285 // Nothing to save or no change. 286 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) { 287 doAutoSave = false; 288 } 291 compareString = wp.autosave.getLastSavedCompareString( post_data ); 289 292 290 if ( doAutoSave ) { 291 autosaveLast = post_data["post_title"] + post_data["content"]; 292 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); 293 } else { 293 // Nothing to save or no change. 294 if ( compareString == autosaveLast ) 294 295 return false; 295 } 296 297 autosaveLast = compareString; 298 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); 296 299 297 300 // Disable buttons until we know the save completed. 298 301 autosave_disable_buttons(); 299 302 300 303 if ( post_data["auto_draft"] == '1' ) { 301 304 successCallback = autosave_saved_new; // new post 302 305 } else { 303 306 successCallback = autosave_saved; // pre-existing post 304 307 } 305 308 306 309 jQuery.ajax({ 307 310 data: post_data, 308 beforeSend: doAutoSave ? autosave_loading : null,311 beforeSend: autosave_loading, 309 312 type: "POST", 310 313 url: ajaxurl, 311 314 success: successCallback 312 315 }); 313 316 314 317 return true; 315 318 } 316 319 317 320 // Autosave in localStorage 318 321 // set as simple object/mixin for now 319 322 window.wp = window.wp || {}; 320 323 wp.autosave = wp.autosave || {}; 321 324 322 325 (function($){ 323 326 // Returns the data for saving in both localStorage and autosaves to the server 324 327 wp.autosave.getPostData = function() { 325 328 var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [], 326 329 data = { 327 330 action: 'autosave', 328 331 autosave: true, 329 332 post_id: $('#post_ID').val() || 0, 330 333 autosavenonce: $('#autosavenonce').val() || '', 331 334 post_type: $('#post_type').val() || '', 332 335 post_author: $('#post_author').val() || '', 333 336 excerpt: $('#excerpt').val() || '' 334 337 }; 335 338 336 339 if ( ed && !ed.isHidden() ) { 337 340 // Don't run while the tinymce spellcheck is on. It resets all found words. 338 341 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) { 339 342 data.autosave = false; 340 343 return data; 341 344 } else { 342 345 if ( 'mce_fullscreen' == ed.id ) 343 346 tinymce.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'}); 344 347 345 348 tinymce.triggerSave(); 346 349 } 347 350 } 348 351 349 352 if ( typeof fullscreen != 'undefined' && fullscreen.settings.visible ) { 350 353 data['post_title'] = $('#wp-fullscreen-title').val() || ''; 351 354 data['content'] = $('#wp_mce_fullscreen').val() || ''; 352 355 } else { 353 356 data['post_title'] = $('#title').val() || ''; 354 357 data['content'] = $('#content').val() || ''; 355 358 } 356 359 357 360 /* 358 361 // We haven't been saving tags with autosave since 2.8... Start again? 359 362 $('.the-tags').each( function() { 360 363 data[this.name] = this.value; 361 364 }); 362 365 */ 363 366 364 367 $('input[id^="in-category-"]:checked').each( function() { 365 368 cats.push(this.value); 366 369 }); 367 370 data['catslist'] = cats.join(','); 368 371 369 372 if ( post_name = $('#post_name').val() ) 370 373 data['post_name'] = post_name; 371 374 372 375 if ( parent_id = $('#parent_id').val() ) 373 376 data['parent_id'] = parent_id; 374 377 375 378 if ( $('#comment_status').prop('checked') ) 376 379 data['comment_status'] = 'open'; 377 380 378 381 if ( $('#ping_status').prop('checked') ) 379 382 data['ping_status'] = 'open'; 380 383 381 384 if ( $('#auto_draft').val() == '1' ) 382 385 data['auto_draft'] = '1'; 383 386 384 387 return data; 385 } 388 }; 389 390 // Concatenate title, content and excerpt. Used to track changes when auto-saving. 391 wp.autosave.getLastSavedCompareString = function( post_data ) { 392 if ( typeof post_data === 'object' ) { 393 return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' ); 394 } 395 396 return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); 397 }; 386 398 387 399 wp.autosave.local = { 388 400 389 401 lastsaveddata: '', 390 402 blog_id: 0, 391 ajaxurl: window.ajaxurl || 'wp-admin/admin-ajax.php',392 403 hasStorage: false, 393 404 394 405 // Check if the browser supports sessionStorage and it's not disabled 395 406 checkStorage: function() { 396 407 var test = Math.random(), result = false; 397 408 398 409 try { 399 410 sessionStorage.setItem('wp-test', test); 400 411 result = sessionStorage.getItem('wp-test') == test; 401 412 sessionStorage.removeItem('wp-test'); 402 413 } catch(e) {} 403 414 404 415 this.hasStorage = result; 405 416 return result; 406 417 }, 407 418 408 419 /** 409 420 * Initialize the local storage 410 421 * 411 422 * @return mixed False if no sessionStorage in the browser or an Object containing all post_data for this blog 412 423 */ 413 424 getStorage: function() { 414 425 var stored_obj = false; 415 426 // Separate local storage containers for each blog_id 416 427 if ( this.hasStorage && this.blog_id ) { 417 428 stored_obj = sessionStorage.getItem( 'wp-autosave-' + this.blog_id ); 418 429 419 430 if ( stored_obj ) 420 431 stored_obj = JSON.parse( stored_obj ); 421 432 else 422 433 stored_obj = {}; 423 434 } 424 435 425 436 return stored_obj; 426 437 }, 427 438 428 439 /** 429 440 * Set the storage for this blog 430 441 * 431 442 * Confirms that the data was saved successfully. 432 443 * 433 444 * @return bool 434 445 */ 435 446 setStorage: function( stored_obj ) { 436 447 var key; 437 448 438 449 if ( this.hasStorage && this.blog_id ) { 439 450 key = 'wp-autosave-' + this.blog_id; 440 451 sessionStorage.setItem( key, JSON.stringify( stored_obj ) ); 441 452 return sessionStorage.getItem( key ) !== null; 442 453 } 443 454 444 455 return false; 445 456 }, 446 457 447 458 /** 448 459 * Get the saved post data for the current post 449 460 * 450 461 * @return mixed False if no storage or no data or the post_data as an Object 451 462 */ 452 463 getData: function() { 453 464 var stored = this.getStorage(), post_id = $('#post_ID').val(); 454 465 455 466 if ( !stored || !post_id ) 456 467 return false; 457 468 458 469 return stored[ 'post_' + post_id ] || false; 459 470 }, 460 471 461 472 /** 462 473 * Set (save or delete) post data in the storage. 463 474 * 464 475 * If stored_data evaluates to 'false' the storage key for the current post will be removed 465 476 * 466 477 * $param stored_data The post data to store or null/false/empty to delete the key 467 478 * @return bool 468 479 */ 469 480 setData: function( stored_data ) { 470 481 var stored = this.getStorage(), post_id = $('#post_ID').val(); 471 482 472 483 if ( !stored || !post_id ) 473 484 return false; 474 485 475 486 if ( stored_data ) 476 487 stored[ 'post_' + post_id ] = stored_data; 477 488 else if ( stored.hasOwnProperty( 'post_' + post_id ) ) 478 489 delete stored[ 'post_' + post_id ]; 479 490 else 480 491 return false; 481 492 482 493 return this.setStorage(stored); 483 494 }, 484 495 485 496 /** 486 497 * Save post data for the current post 487 498 * 488 499 * Runs on a 15 sec. schedule, saves when there are differences in the post title or content. 489 500 * When the optional data is provided, updates the last saved post data. 490 501 * 491 502 * $param data optional Object The post data for saving, minimum 'post_title' and 'content' 492 503 * @return bool 493 504 */ 494 505 save: function( data ) { 495 var result = false ;506 var result = false, post_data, compareString; 496 507 497 508 if ( ! data ) { 498 509 post_data = wp.autosave.getPostData(); 499 510 } else { 500 511 post_data = this.getData() || {}; 501 512 $.extend( post_data, data ); 502 513 post_data.autosave = true; 503 514 } 504 515 505 // If the content and title did not change since the last save, don't save again506 if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata)516 // Cannot get the post data at the moment 517 if ( ! post_data.autosave ) 507 518 return false; 508 519 509 // Cannot get the post data at the moment 510 if ( !post_data.autosave ) 520 compareString = wp.autosave.getLastSavedCompareString( post_data ); 521 522 // If the content, title and excerpt did not change since the last save, don't save again 523 if ( compareString == this.lastsaveddata ) 511 524 return false; 512 525 513 526 post_data['save_time'] = (new Date()).getTime(); 514 527 post_data['status'] = $('#post_status').val() || ''; 515 528 result = this.setData( post_data ); 516 529 517 530 if ( result ) 518 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;531 this.lastsaveddata = compareString; 519 532 520 533 return result; 521 534 }, 522 535 523 536 // Initialize and run checkPost() on loading the script (before TinyMCE init) 524 537 init: function( settings ) { 525 538 var self = this; 526 539 527 // Check if the browser supports sessionStorage and editor.js is loaded528 if ( ! this.checkStorage() || typeof switchEditors == 'undefined')540 // Check if the browser supports sessionStorage and it's not disabled 541 if ( ! this.checkStorage() ) 529 542 return; 530 543 531 544 // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. 532 545 if ( ! $('#content').length && ! $('#excerpt').length ) 533 546 return; 534 547 535 548 if ( settings ) 536 549 $.extend( this, settings ); 537 550 538 551 if ( !this.blog_id ) 539 552 this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0; 540 553 541 this.checkPost();542 554 $(document).ready( function(){ self.run(); } ); 543 555 }, 544 556 545 557 // Run on DOM ready 546 558 run: function() { 547 var self = this, post_data; 548 549 // Set the comparison string 550 if ( !this.lastsaveddata ) { 551 post_data = wp.autosave.getPostData(); 559 var self = this; 552 560 553 if ( post_data.content && $('#wp-content-wrap').hasClass('tmce-active') ) 554 this.lastsaveddata = post_data.post_title + ': ' + switchEditors.pre_wpautop( post_data.content ); 555 else 556 this.lastsaveddata = post_data.post_title + ': ' + post_data.content; 557 } 561 // Check if the local post data is different than the loaded post data. 562 this.checkPost(); 558 563 559 564 // Set the schedule 560 565 this.schedule = $.schedule({ 561 566 time: 15 * 1000, 562 567 func: function() { wp.autosave.local.save(); }, 563 568 repeat: true, 564 569 protect: true 565 570 }); 566 571 567 572 $('form#post').on('submit.autosave-local', function() { 568 573 var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0; 569 574 570 575 if ( editor && ! editor.isHidden() ) { 571 576 // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea. 572 577 editor.onSubmit.add( function() { 573 578 wp.autosave.local.save({ 574 579 post_title: $('#title').val() || '', 575 580 content: $('#content').val() || '', 576 581 excerpt: $('#excerpt').val() || '' 577 582 }); 578 583 }); 579 584 } else { 580 585 self.save({ 581 586 post_title: $('#title').val() || '', 582 587 content: $('#content').val() || '', 583 588 excerpt: $('#excerpt').val() || '' 584 589 }); 585 590 } 586 591 587 592 wpCookies.set( 'wp-saving-post-' + post_id, 'check' ); 588 593 }); 589 594 }, 590 595 591 596 // Strip whitespace and compare two strings 592 597 compare: function( str1, str2 ) { 593 598 function remove( string ) { 594 599 return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); 595 600 } 596 601 597 602 return ( remove( str1 || '' ) == remove( str2 || '' ) ); 598 603 }, 599 604 600 605 /** 601 606 * Check if the saved data for the current post (if any) is different than the loaded post data on the screen 602 607 * 603 608 * Shows a standard message letting the user restore the post data if different. 604 609 * 605 610 * @return void 606 611 */ 607 612 checkPost: function() { 608 var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice,613 var self = this, post_data = this.getData(), content, post_title, excerpt, notice, 609 614 post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id ); 610 615 611 616 if ( ! post_data ) 612 617 return; 613 618 614 619 if ( cookie ) { 615 620 wpCookies.remove( 'wp-saving-post-' + post_id ); 616 621 617 622 if ( cookie == 'saved' ) { 618 623 // The post was saved properly, remove old data and bail 619 624 this.setData( false ); 620 625 return; 621 626 } 622 627 } 623 628 624 629 // There is a newer autosave. Don't show two "restore" notices at the same time. 625 630 if ( $('#has-newer-autosave').length ) 626 631 return; 627 632 628 // cookie == 'check' means the post was not saved properly, always show #local-storage-notice 629 if ( cookie != 'check' ) { 630 content = $('#content').val(); 631 check_data = $.extend( {}, post_data ); 633 content = $('#content').val() || ''; 634 post_title = $('#title').val() || ''; 635 excerpt = $('#excerpt').val() || ''; 632 636 633 if ( $('#wp-content-wrap').hasClass('tmce-active'))634 637 if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) 638 content = switchEditors.pre_wpautop( content ); 635 639 636 if ( this.compare( content, check_data.content ) && this.compare( $('#title').val(), check_data.post_title ) && this.compare( $('#excerpt').val(), check_data.excerpt ) ) 637 return; 640 // cookie == 'check' means the post was not saved properly, always show #local-storage-notice 641 if ( cookie != 'check' && this.compare( content, post_data.content ) && this.compare( post_title, post_data.post_title ) && this.compare( excerpt, post_data.excerpt ) ) { 642 return; 638 643 } 639 644 640 645 this.restore_post_data = post_data; 641 this.undo_post_data = wp.autosave.getPostData(); 646 this.undo_post_data = { 647 content: content, 648 post_title: post_title, 649 excerpt: excerpt 650 }; 642 651 643 652 notice = $('#local-storage-notice'); 644 653 $('.wrap h2').first().after( notice.addClass('updated').show() ); 645 654 646 655 notice.on( 'click', function(e) { 647 656 var target = $( e.target ); 648 657 649 658 if ( target.hasClass('restore-backup') ) { 650 659 self.restorePost( self.restore_post_data ); 651 660 target.parent().hide(); 652 661 $(this).find('p.undo-restore').show(); 653 662 } else if ( target.hasClass('undo-restore-backup') ) { 654 663 self.restorePost( self.undo_post_data ); 655 664 target.parent().hide(); 656 665 $(this).find('p.local-restore').show(); 657 666 } 658 667 659 668 e.preventDefault(); 660 669 }); 661 670 }, 662 671 663 672 // Restore the current title, content and excerpt from post_data. 664 673 restorePost: function( post_data ) { 665 674 var editor; 666 675 667 676 if ( post_data ) { 668 677 // Set the last saved data 669 this.lastsaveddata = post_data.post_title + ': ' + post_data.content;678 this.lastsaveddata = wp.autosave.getLastSavedCompareString( post_data ); 670 679 671 680 if ( $('#title').val() != post_data.post_title ) 672 681 $('#title').focus().val( post_data.post_title || '' ); 673 682 674 683 $('#excerpt').val( post_data.excerpt || '' ); 675 684 editor = typeof tinymce != 'undefined' && tinymce.get('content'); 676 685 677 if ( editor && ! editor.isHidden() ) {686 if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) { 678 687 // Make sure there's an undo level in the editor 679 688 editor.undoManager.add(); 680 689 editor.setContent( post_data.content ? switchEditors.wpautop( post_data.content ) : '' ); 681 690 } else { 682 691 // Make sure the Text editor is selected 683 692 $('#content-html').click(); 684 693 $('#content').val( post_data.content ); 685 694 } 686 695 687 696 return true; 688 697 } 689 698 690 699 return false; 691 700 } 692 } 701 }; 693 702 694 703 wp.autosave.local.init(); 695 704 696 705 }(jQuery));