2 var autosavePeriodical;
3 var autosaveOldMessage = '';
4 var autosaveDelayPreview = false;
5 var autosaveFirst = true;
8 autosaveLast = $('#post #title').val()+$('#post #content').val();
9 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
11 //Disable autosave after the form has been submitted
12 $("#post").submit(function() { $.cancel(autosavePeriodical); });
15 function autosave_parse_response(response) {
16 var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response
19 if ( res && res.responses && res.responses.length ) {
20 message = res.responses[0].data; // The saved message or error.
21 // someone else is editing: disable autosave, set errors
22 if ( res.responses[0].supplemental ) {
23 if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
24 autosave = function() {};
25 res = { errors: true };
27 jQuery.each(res.responses[0].supplemental, function(selector, value) {
28 if ( selector.match(/^replace-/) ) {
29 jQuery('#'+selector.replace('replace-', '')).val(value);
34 // if no errors: add slug UI
36 var postID = parseInt( res.responses[0].id );
37 if ( !isNaN(postID) && postID > 0 ) {
38 autosave_update_slug(postID);
42 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
43 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
47 // called when autosaving pre-existing post
48 function autosave_saved(response) {
49 autosave_parse_response(response); // parse the ajax response
50 autosave_enable_buttons(); // re-enable disabled form buttons
53 // called when autosaving new post
54 function autosave_saved_new(response) {
55 var res = autosave_parse_response(response); // parse the ajax response
56 // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
57 if ( res && res.responses.length && !res.errors ) {
58 var tempID = jQuery('#post_ID').val();
59 var postID = parseInt( res.responses[0].id );
60 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
61 if ( tempID < 0 && postID > 0 ) // update media buttons
62 jQuery('#media-buttons a').each(function(){
63 this.href = this.href.replace(tempID, postID);
66 autosaveFirst = false;
67 if ( autosaveDelayPreview )
68 jQuery('#post-preview').click();
70 autosave_enable_buttons(); // re-enable disabled form buttons
74 function autosave_update_post_ID( postID ) {
75 if ( !isNaN(postID) && postID > 0 ) {
76 if ( postID == parseInt(jQuery('#post_ID').val()) ) { return; } // no need to do this more than once
77 jQuery('#post_ID').attr({name: "post_ID"});
78 jQuery('#post_ID').val(postID);
80 jQuery.post(autosaveL10n.requestFile, {
81 action: "autosave-generate-nonces",
83 autosavenonce: jQuery('#autosavenonce').val(),
84 post_type: jQuery('#post_type').val()
86 jQuery('#_wpnonce').val(html);
87 autosave_enable_buttons(); // re-enable disabled form buttons
89 jQuery('#hiddenaction').val('editpost');
93 function autosave_update_slug(post_id) {
94 // create slug area only if not already there
95 if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
99 action: 'sample-permalink',
101 new_title: jQuery('#title').val(),
102 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
105 jQuery('#edit-slug-box').html(data);
106 make_slugedit_clickable();
112 function autosave_loading() {
113 jQuery('#autosave').html(autosaveL10n.savingText);
116 function autosave_enable_buttons() {
117 jQuery(".submitbox :button:disabled, .submitbox :submit:disabled").attr('disabled', '');
120 function autosave_disable_buttons() {
121 jQuery(".submitbox :button:enabled, .submitbox :submit:enabled").attr('disabled', 'disabled');
122 setTimeout(autosave_enable_buttons, 5000); // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
125 var autosave = function() {
126 // (bool) is rich editor enabled and active
127 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
130 post_ID: jQuery("#post_ID").val() || 0,
131 post_title: jQuery("#title").val() || "",
132 autosavenonce: jQuery('#autosavenonce').val(),
133 tags_input: jQuery("#tags-input").val() || "",
134 post_type: jQuery('#post_type').val() || "",
138 // We always send the ajax request in order to keep the post lock fresh.
139 // This (bool) tells whether or not to write the post to the DB during the ajax request.
140 var doAutoSave = true;
142 // No autosave while thickbox is open (media buttons)
143 if ( jQuery("#TB_window").css('display') == 'block' )
146 /* Gotta do this up here so we can check the length when tinyMCE is in use */
148 var ed = tinyMCE.activeEditor;
149 if ( 'mce_fullscreen' == ed.id )
150 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
151 tinyMCE.get('content').save();
154 post_data["content"] = jQuery("#content").val();
155 if ( jQuery('#post_name').val() )
156 post_data["post_name"] = jQuery('#post_name').val();
158 // Nothing to save or no change.
159 if( (post_data["post_title"].length==0 && post_data["content"].length==0) || post_data["post_title"] + post_data["content"] == autosaveLast) {
163 autosave_disable_buttons();
165 var origStatus = jQuery('#original_post_status').val();
167 autosaveLast = jQuery("#title").val()+jQuery("#content").val();
169 jQuery("[@name='post_category[]']:checked").each( function(i) {
170 goodcats.push(this.value);
172 post_data["catslist"] = goodcats.join(",");
174 if ( jQuery("#comment_status").attr("checked") )
175 post_data["comment_status"] = 'open';
176 if ( jQuery("#ping_status").attr("checked") )
177 post_data["ping_status"] = 'open';
178 if ( jQuery("#excerpt").size() )
179 post_data["excerpt"] = jQuery("#excerpt").val();
180 if ( jQuery("#post_author").size() )
181 post_data["post_author"] = jQuery("#post_author").val();
182 post_data["user_ID"] = jQuery("#user-id").val();
184 // Don't run while the TinyMCE spellcheck is on. Why? Who knows.
185 if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
189 if(parseInt(post_data["post_ID"]) < 1) {
190 post_data["temp_ID"] = post_data["post_ID"];
191 var successCallback = autosave_saved_new; // new post
193 var successCallback = autosave_saved; // pre-existing post
197 post_data['autosave'] = 0;
200 autosaveOldMessage = jQuery('#autosave').html();
204 beforeSend: doAutoSave ? autosave_loading : null,
206 url: autosaveL10n.requestFile,
207 success: successCallback