var autosaveLast = ''; var autosavePeriodical; jQuery(function($) { autosaveLast = $('#post #title').val()+$('#post #content').val(); autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); //Disable autosave after the form has been submitted $("#post").submit(function() { $.cancel(autosavePeriodical); }); // Autosave early on for a new post. Why? Should this only be run once? $("#content").keypress(function() { if ( 1 === ( $(this).val().length % 15 ) && 1 > parseInt($("#post_ID").val(),10) ) setTimeout(autosave, 5000); }); }); // called when autosaving pre-existing post function autosave_saved(response) { var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response var message = ''; if ( res && 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 && 'disable' == res.responses[0].supplemental['disable_autosave'] ) { autosave = function() {}; res = { errors: true }; } // if no errors: add preview link and slug UI if ( !res.errors ) { var postID = parseInt( res.responses[0].id ); if ( !isNaN(postID) && postID > 0 ) { autosave_update_preview_link(postID); autosave_update_slug(postID); } } } if ( message ) { jQuery('#autosave').html(message); } // update autosave message autosave_enable_buttons(); // re-enable disabled form buttons return res; } // called when autosaving new post function autosave_update_post_ID(response) { var res = autosave_saved(response); // parse the ajax response do the above // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID if ( res && res.responses.length && !res.errors ) { var postID = parseInt( res.responses[0].id ); if ( !isNaN(postID) && postID > 0 ) { if ( postID == parseInt(jQuery('#post_ID').val()) ) { return; } // no need to do this more than once jQuery('#post_ID').attr({name: "post_ID"}); jQuery('#post_ID').val(postID); // We need new nonces jQuery.post(autosaveL10n.requestFile, { action: "autosave-generate-nonces", post_ID: postID, autosavenonce: jQuery('#autosavenonce').val(), post_type: jQuery('#post_type').val() }, function(html) { jQuery('#_wpnonce').val(html); }); jQuery('#hiddenaction').val('editpost'); } } } function autosave_update_preview_link(post_id) { // Add preview button if not already there if ( !jQuery('#previewview > *').size() ) { var post_type = jQuery('#post_type').val(); var previewText = 'page' == post_type ? autosaveL10n.previewPageText : autosaveL10n.previewPostText; jQuery.post(autosaveL10n.requestFile, { action: "get-permalink", post_id: post_id, getpermalinknonce: jQuery('#getpermalinknonce').val() }, function(permalink) { jQuery('#previewview').html(''+previewText+''); }); } } function autosave_update_slug(post_id) { // create slug area only if not already there if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) { jQuery.post( slugL10n.requestFile, { action: 'sample-permalink', post_id: post_id, samplepermalinknonce: jQuery('#samplepermalinknonce').val() }, function(data) { jQuery('#edit-slug-box').html(data); make_slugedit_clickable(); } ); } } function autosave_loading() { jQuery('#autosave').html('
' + autosaveL10n.savingText + '