2011-09-27 16:52:07 -04:00
|
|
|
var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-05-27 07:04:00 -04:00
|
|
|
jQuery(document).ready( function($) {
|
|
|
|
var dotabkey = true;
|
2010-01-15 17:11:12 -05:00
|
|
|
|
2009-05-22 18:08:17 -04:00
|
|
|
autosaveLast = $('#post #title').val() + $('#post #content').val();
|
2009-01-02 10:08:58 -05:00
|
|
|
autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
|
|
|
|
|
|
|
|
//Disable autosave after the form has been submitted
|
2009-08-25 01:14:49 -04:00
|
|
|
$("#post").submit(function() {
|
|
|
|
$.cancel(autosavePeriodical);
|
2011-09-27 16:52:07 -04:00
|
|
|
autosaveLockRelease = false;
|
2009-08-25 01:14:49 -04:00
|
|
|
});
|
2009-05-23 04:41:00 -04:00
|
|
|
|
2009-08-25 01:14:49 -04:00
|
|
|
$('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
|
2009-08-26 00:11:16 -04:00
|
|
|
blockSave = true;
|
2009-08-25 01:14:49 -04:00
|
|
|
window.onbeforeunload = null;
|
|
|
|
$(':button, :submit', '#submitpost').each(function(){
|
|
|
|
var t = $(this);
|
|
|
|
if ( t.hasClass('button-primary') )
|
|
|
|
t.addClass('button-primary-disabled');
|
|
|
|
else
|
|
|
|
t.addClass('button-disabled');
|
|
|
|
});
|
2010-10-04 16:53:04 -04:00
|
|
|
if ( $(this).attr('id') == 'publish' )
|
|
|
|
$('#ajax-loading').css('visibility', 'visible');
|
|
|
|
else
|
|
|
|
$('#draft-ajax-loading').css('visibility', 'visible');
|
2009-08-25 01:14:49 -04:00
|
|
|
});
|
2009-05-23 04:41:00 -04:00
|
|
|
|
2009-05-22 18:08:17 -04:00
|
|
|
window.onbeforeunload = function(){
|
|
|
|
var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
|
|
|
|
|
|
|
|
if ( mce && !mce.isHidden() ) {
|
|
|
|
if ( mce.isDirty() )
|
|
|
|
return autosaveL10n.saveAlert;
|
|
|
|
} else {
|
2011-05-16 02:17:44 -04:00
|
|
|
if ( fullscreen && fullscreen.settings.visible ) {
|
2011-04-24 21:01:34 -04:00
|
|
|
title = $('#wp-fullscreen-title').val();
|
|
|
|
content = $("#wp_mce_fullscreen").val();
|
|
|
|
} else {
|
|
|
|
title = $('#post #title').val();
|
|
|
|
content = $('#post #content').val();
|
|
|
|
}
|
|
|
|
|
2009-05-22 18:08:17 -04:00
|
|
|
if ( ( title || content ) && title + content != autosaveLast )
|
|
|
|
return autosaveL10n.saveAlert;
|
|
|
|
}
|
|
|
|
};
|
2009-05-27 07:04:00 -04:00
|
|
|
|
2011-09-27 16:52:07 -04:00
|
|
|
$(window).unload( function() {
|
|
|
|
if ( ! autosaveLockRelease )
|
|
|
|
return;
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: ajaxurl,
|
|
|
|
async: false,
|
|
|
|
data: {
|
|
|
|
action: 'wp-remove-post-lock',
|
|
|
|
_wpnonce: $('#_wpnonce').val(),
|
|
|
|
post_ID: $('#post_ID').val(),
|
|
|
|
active_post_lock: $('#active_post_lock').val()
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} );
|
|
|
|
|
2009-05-27 07:04:00 -04:00
|
|
|
// preview
|
|
|
|
$('#post-preview').click(function(){
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( $('#auto_draft').val() == '1' && notSaved ) {
|
2009-05-27 07:04:00 -04:00
|
|
|
autosaveDelayPreview = true;
|
|
|
|
autosave();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
doPreview();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
doPreview = function() {
|
|
|
|
$('input#wp-preview').val('dopreview');
|
|
|
|
$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
|
|
|
|
$('input#wp-preview').val('');
|
|
|
|
}
|
|
|
|
|
|
|
|
// This code is meant to allow tabbing from Title to Post if tinyMCE is defined.
|
|
|
|
if ( typeof tinyMCE != 'undefined' ) {
|
|
|
|
$('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
|
|
|
|
if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); }
|
2009-05-27 07:04:00 -04:00
|
|
|
if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
|
|
|
|
e.preventDefault();
|
|
|
|
dotabkey = false;
|
|
|
|
tinyMCE.activeEditor.focus();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-08-26 00:11:16 -04:00
|
|
|
|
|
|
|
// autosave new posts after a title is typed but not if Publish or Save Draft is clicked
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( '1' == $('#auto_draft').val() ) {
|
2009-08-26 00:11:16 -04:00
|
|
|
$('#title').blur( function() {
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( !this.value || $('#auto_draft').val() != '1' )
|
2009-08-26 00:11:16 -04:00
|
|
|
return;
|
|
|
|
delayed_autosave();
|
|
|
|
});
|
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function autosave_parse_response(response) {
|
2011-04-15 22:37:44 -04:00
|
|
|
var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup;
|
2009-01-02 10:08:58 -05:00
|
|
|
|
|
|
|
if ( res && res.responses && res.responses.length ) {
|
|
|
|
message = res.responses[0].data; // The saved message or error.
|
|
|
|
// someone else is editing: disable autosave, set errors
|
|
|
|
if ( res.responses[0].supplemental ) {
|
2009-10-11 07:26:59 -04:00
|
|
|
sup = res.responses[0].supplemental;
|
|
|
|
if ( 'disable' == sup['disable_autosave'] ) {
|
2009-01-02 10:08:58 -05:00
|
|
|
autosave = function() {};
|
2011-09-27 16:52:07 -04:00
|
|
|
autosaveLockRelease = false;
|
2009-01-02 10:08:58 -05:00
|
|
|
res = { errors: true };
|
|
|
|
}
|
2011-04-15 22:37:44 -04:00
|
|
|
|
2011-09-27 16:52:07 -04:00
|
|
|
if ( sup['active-post-lock'] ) {
|
|
|
|
jQuery('#active_post_lock').val( sup['active-post-lock'] );
|
|
|
|
}
|
|
|
|
|
2011-04-15 22:37:44 -04:00
|
|
|
if ( sup['alert'] ) {
|
|
|
|
jQuery('#autosave-alert').remove();
|
|
|
|
jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>');
|
2009-10-11 07:26:59 -04:00
|
|
|
}
|
2011-04-15 22:37:44 -04:00
|
|
|
|
2009-10-11 07:26:59 -04:00
|
|
|
jQuery.each(sup, function(selector, value) {
|
2009-01-02 10:08:58 -05:00
|
|
|
if ( selector.match(/^replace-/) ) {
|
|
|
|
jQuery('#'+selector.replace('replace-', '')).val(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no errors: add slug UI
|
|
|
|
if ( !res.errors ) {
|
|
|
|
postID = parseInt( res.responses[0].id, 10 );
|
|
|
|
if ( !isNaN(postID) && postID > 0 ) {
|
|
|
|
autosave_update_slug(postID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-24 21:01:34 -04:00
|
|
|
if ( message ) { // update autosave message
|
|
|
|
jQuery('.autosave-message').html(message);
|
|
|
|
} else if ( autosaveOldMessage && res ) {
|
|
|
|
jQuery('.autosave-message').html( autosaveOldMessage );
|
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called when autosaving pre-existing post
|
|
|
|
function autosave_saved(response) {
|
2010-02-06 05:07:57 -05:00
|
|
|
blockSave = false;
|
2009-01-02 10:08:58 -05:00
|
|
|
autosave_parse_response(response); // parse the ajax response
|
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
|
|
|
}
|
|
|
|
|
|
|
|
// called when autosaving new post
|
|
|
|
function autosave_saved_new(response) {
|
2010-02-06 05:07:57 -05:00
|
|
|
blockSave = false;
|
2011-04-15 22:37:44 -04:00
|
|
|
var res = autosave_parse_response(response), postID;
|
2009-01-02 10:08:58 -05:00
|
|
|
if ( res && res.responses.length && !res.errors ) {
|
2010-02-06 05:07:57 -05:00
|
|
|
// An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
|
2009-01-02 10:08:58 -05:00
|
|
|
postID = parseInt( res.responses[0].id, 10 );
|
2010-05-15 10:03:46 -04:00
|
|
|
if ( !isNaN(postID) && postID > 0 ) {
|
|
|
|
notSaved = false;
|
|
|
|
jQuery('#auto_draft').val('0'); // No longer an auto-draft
|
|
|
|
}
|
|
|
|
autosave_enable_buttons();
|
2009-05-27 07:04:00 -04:00
|
|
|
if ( autosaveDelayPreview ) {
|
|
|
|
autosaveDelayPreview = false;
|
|
|
|
doPreview();
|
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
} else {
|
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_update_slug(post_id) {
|
|
|
|
// create slug area only if not already there
|
2009-10-13 06:02:42 -04:00
|
|
|
if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
|
2011-04-24 21:01:34 -04:00
|
|
|
jQuery.post( ajaxurl, {
|
2009-01-02 10:08:58 -05:00
|
|
|
action: 'sample-permalink',
|
|
|
|
post_id: post_id,
|
2011-05-16 02:17:44 -04:00
|
|
|
new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(),
|
2009-01-02 10:08:58 -05:00
|
|
|
samplepermalinknonce: jQuery('#samplepermalinknonce').val()
|
|
|
|
},
|
|
|
|
function(data) {
|
2011-03-27 16:09:09 -04:00
|
|
|
if ( data !== '-1' ) {
|
|
|
|
jQuery('#edit-slug-box').html(data);
|
|
|
|
makeSlugeditClickable();
|
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_loading() {
|
2011-04-24 21:01:34 -04:00
|
|
|
jQuery('.autosave-message').html(autosaveL10n.savingText);
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_enable_buttons() {
|
2009-09-13 19:36:09 -04:00
|
|
|
// delay that a bit to avoid some rare collisions while the DOM is being updated.
|
|
|
|
setTimeout(function(){
|
|
|
|
jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
|
2010-10-04 16:53:04 -04:00
|
|
|
jQuery('.ajax-loading').css('visibility', 'hidden');
|
2009-09-13 19:36:09 -04:00
|
|
|
}, 500);
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_disable_buttons() {
|
2011-05-24 21:04:12 -04:00
|
|
|
jQuery(':button, :submit', '#submitpost').prop('disabled', true);
|
2009-05-22 18:08:17 -04:00
|
|
|
// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
|
|
|
|
setTimeout(autosave_enable_buttons, 5000);
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
2009-08-26 00:11:16 -04:00
|
|
|
function delayed_autosave() {
|
|
|
|
setTimeout(function(){
|
|
|
|
if ( blockSave )
|
|
|
|
return;
|
|
|
|
autosave();
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
2009-05-22 18:08:17 -04:00
|
|
|
autosave = function() {
|
2009-01-02 10:08:58 -05:00
|
|
|
// (bool) is rich editor enabled and active
|
2010-02-06 05:07:57 -05:00
|
|
|
blockSave = true;
|
2011-04-24 21:01:34 -04:00
|
|
|
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(),
|
|
|
|
post_data, doAutoSave, ed, origStatus, successCallback;
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-08-25 01:14:49 -04:00
|
|
|
autosave_disable_buttons();
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
post_data = {
|
|
|
|
action: "autosave",
|
|
|
|
post_ID: jQuery("#post_ID").val() || 0,
|
|
|
|
autosavenonce: jQuery('#autosavenonce').val(),
|
|
|
|
post_type: jQuery('#post_type').val() || "",
|
|
|
|
autosave: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
jQuery('.tags-input').each( function() {
|
|
|
|
post_data[this.name] = this.value;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// 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 = true;
|
|
|
|
|
|
|
|
// No autosave while thickbox is open (media buttons)
|
|
|
|
if ( jQuery("#TB_window").css('display') == 'block' )
|
|
|
|
doAutoSave = false;
|
|
|
|
|
|
|
|
/* Gotta do this up here so we can check the length when tinyMCE is in use */
|
2009-08-08 05:03:56 -04:00
|
|
|
if ( rich && doAutoSave ) {
|
2009-01-02 10:08:58 -05:00
|
|
|
ed = tinyMCE.activeEditor;
|
2009-08-08 05:03:56 -04:00
|
|
|
// Don't run while the TinyMCE spellcheck is on. It resets all found words.
|
|
|
|
if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
|
|
|
|
doAutoSave = false;
|
|
|
|
} else {
|
2011-04-24 21:01:34 -04:00
|
|
|
if ( 'mce_fullscreen' == ed.id || 'wp_mce_fullscreen' == ed.id )
|
2009-08-08 05:03:56 -04:00
|
|
|
tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
|
2011-04-24 21:01:34 -04:00
|
|
|
tinyMCE.triggerSave();
|
2009-08-08 05:03:56 -04:00
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
2011-05-16 02:17:44 -04:00
|
|
|
if ( fullscreen && fullscreen.settings.visible ) {
|
2011-04-24 21:01:34 -04:00
|
|
|
post_data["post_title"] = jQuery('#wp-fullscreen-title').val();
|
|
|
|
post_data["content"] = jQuery("#wp_mce_fullscreen").val();
|
|
|
|
} else {
|
|
|
|
post_data["post_title"] = jQuery("#title").val()
|
|
|
|
post_data["content"] = jQuery("#content").val();
|
|
|
|
}
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if ( jQuery('#post_name').val() )
|
|
|
|
post_data["post_name"] = jQuery('#post_name').val();
|
|
|
|
|
|
|
|
// Nothing to save or no change.
|
2009-08-08 05:03:56 -04:00
|
|
|
if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
|
2009-05-27 07:04:00 -04:00
|
|
|
doAutoSave = false;
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
origStatus = jQuery('#original_post_status').val();
|
|
|
|
|
|
|
|
goodcats = ([]);
|
2009-01-07 00:02:40 -05:00
|
|
|
jQuery("[name='post_category[]']:checked").each( function(i) {
|
2009-01-02 10:08:58 -05:00
|
|
|
goodcats.push(this.value);
|
|
|
|
} );
|
|
|
|
post_data["catslist"] = goodcats.join(",");
|
|
|
|
|
2011-05-24 21:04:12 -04:00
|
|
|
if ( jQuery("#comment_status").prop("checked") )
|
2009-01-02 10:08:58 -05:00
|
|
|
post_data["comment_status"] = 'open';
|
2011-05-24 21:04:12 -04:00
|
|
|
if ( jQuery("#ping_status").prop("checked") )
|
2009-01-02 10:08:58 -05:00
|
|
|
post_data["ping_status"] = 'open';
|
|
|
|
if ( jQuery("#excerpt").size() )
|
|
|
|
post_data["excerpt"] = jQuery("#excerpt").val();
|
|
|
|
if ( jQuery("#post_author").size() )
|
|
|
|
post_data["post_author"] = jQuery("#post_author").val();
|
2010-10-21 15:55:28 -04:00
|
|
|
if ( jQuery("#parent_id").val() )
|
|
|
|
post_data["parent_id"] = jQuery("#parent_id").val();
|
2009-01-02 10:08:58 -05:00
|
|
|
post_data["user_ID"] = jQuery("#user-id").val();
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( jQuery('#auto_draft').val() == '1' )
|
|
|
|
post_data["auto_draft"] = '1';
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-08-08 05:03:56 -04:00
|
|
|
if ( doAutoSave ) {
|
2011-04-24 21:01:34 -04:00
|
|
|
autosaveLast = post_data["post_title"] + post_data["content"];
|
2011-05-16 02:17:44 -04:00
|
|
|
jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
|
2009-08-08 05:03:56 -04:00
|
|
|
} else {
|
|
|
|
post_data['autosave'] = 0;
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
2010-02-06 05:07:57 -05:00
|
|
|
if ( post_data["auto_draft"] == '1' ) {
|
2009-01-02 10:08:58 -05:00
|
|
|
successCallback = autosave_saved_new; // new post
|
|
|
|
} else {
|
|
|
|
successCallback = autosave_saved; // pre-existing post
|
|
|
|
}
|
|
|
|
|
|
|
|
autosaveOldMessage = jQuery('#autosave').html();
|
|
|
|
jQuery.ajax({
|
|
|
|
data: post_data,
|
|
|
|
beforeSend: doAutoSave ? autosave_loading : null,
|
|
|
|
type: "POST",
|
2011-05-16 02:17:44 -04:00
|
|
|
url: ajaxurl,
|
2009-01-02 10:08:58 -05:00
|
|
|
success: successCallback
|
|
|
|
});
|
|
|
|
}
|