diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php
index 0d94e6b46a..ece9539e1f 100644
--- a/wp-admin/admin-ajax.php
+++ b/wp-admin/admin-ajax.php
@@ -220,7 +220,7 @@ case 'add-user' :
) );
$x->send();
break;
-case 'autosave' :
+case 'autosave' : // The name of this action is hardcoded in edit_post()
$_POST['post_content'] = $_POST['content'];
$_POST['post_excerpt'] = $_POST['excerpt'];
$_POST['post_status'] = 'draft';
diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php
index fe8ca5dec1..f0b61ce9b2 100644
--- a/wp-admin/admin-functions.php
+++ b/wp-admin/admin-functions.php
@@ -20,6 +20,25 @@ function wp_write_post() {
return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) );
}
+
+ // Check for autosave collisions
+ if ( isset($_POST['temp_ID']) ) {
+ $temp_id = (int) $_POST['temp_ID'];
+ if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
+ $draft_ids = array();
+ foreach ( $draft_ids as $temp => $real )
+ if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
+ unset($draft_ids[$temp]);
+
+ if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
+ $_POST['post_ID'] = $draft_ids[$temp_id];
+ unset($_POST['temp_ID']);
+ relocate_children( $temp_id, $_POST['post_ID'] );
+ update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
+ return edit_post();
+ }
+ }
+
// Rename.
$_POST['post_content'] = $_POST['content'];
$_POST['post_excerpt'] = $_POST['excerpt'];
@@ -88,12 +107,17 @@ function wp_write_post() {
}
// Create the post.
- $post_ID = wp_insert_post( $_POST);
+ $post_ID = wp_insert_post( $_POST );
+
add_meta( $post_ID );
// Reunite any orphaned attachments with their parent
- if ( $_POST['temp_ID'] )
- relocate_children( $_POST['temp_ID'], $post_ID );
+ // Update autosave collision detection
+ if ( $temp_id ) {
+ relocate_children( $temp_id, $post_ID );
+ $draft_ids[$temp_id] = $post_ID;
+ update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
+ }
// Now that we have an ID we can fix any attachment anchor hrefs
fix_attachment_links( $post_ID );
@@ -165,6 +189,17 @@ function edit_post() {
wp_die( __('You are not allowed to edit this post.' ));
}
+ // Autosave shouldn't save too soon after a real save
+ if ( 'autosave' == $_POST['action'] ) {
+ $post =& get_post( $post_ID );
+ $now = time();
+ $then = strtotime($post->post_date_gmt . ' +0000');
+ // Keep autosave_interval in sync with autosave-js.php.
+ $delta = apply_filters( 'autosave_interval', 120 ) / 2;
+ if ( ($now - $then) < $delta )
+ return $post_ID;
+ }
+
// Rename.
$_POST['ID'] = (int) $_POST['post_ID'];
$_POST['post_content'] = $_POST['content'];
diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php
index 133e7dd32e..c07ae4cf47 100644
--- a/wp-admin/edit-form-advanced.php
+++ b/wp-admin/edit-form-advanced.php
@@ -17,7 +17,7 @@ $messages[3] = __('Custom field deleted.');
if (0 == $post_ID) {
$form_action = 'post';
- $temp_ID = -1 * time();
+ $temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post()
$form_extra = "";
wp_nonce_field('add-post');
} else {
diff --git a/wp-admin/edit-page-form.php b/wp-admin/edit-page-form.php
index 5247f17932..6f2cbd17f8 100644
--- a/wp-admin/edit-page-form.php
+++ b/wp-admin/edit-page-form.php
@@ -5,7 +5,7 @@
if (0 == $post_ID) {
$form_action = 'post';
$nonce_action = 'add-page';
- $temp_ID = -1 * time();
+ $temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post()
$form_extra = "";
} else {
$form_action = 'editpost';
diff --git a/wp-includes/js/autosave-js.php b/wp-includes/js/autosave-js.php
index 67fcde760c..e30134d0e6 100644
--- a/wp-includes/js/autosave-js.php
+++ b/wp-includes/js/autosave-js.php
@@ -7,6 +7,7 @@ var autosavePeriodical;
function autosave_start_timer() {
var form = $('post');
autosaveLast = form.post_title.value+form.content.value;
+ // Keep autosave_interval in sync with edit_post().
autosavePeriodical = new PeriodicalExecuter(autosave, );
//Disable autosave after the form has been submitted
if(form.addEventListener) {
@@ -85,6 +86,7 @@ function autosave_disable_buttons() {
form.submit ? form.submit.disabled = 'disabled' : null;
form.publish ? form.publish.disabled = 'disabled' : null;
form.deletepost ? form.deletepost.disabled = 'disabled' : null;
+ setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later. Just gives autosave a head start to avoid collisions.
}
function autosave_enable_buttons() {
diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
index bcc8bc5fc1..63af673b3c 100644
--- a/wp-includes/script-loader.php
+++ b/wp-includes/script-loader.php
@@ -19,7 +19,7 @@ class WP_Scripts {
$mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php');
$this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20061113' );
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0');
- $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '4508');
+ $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '20070116');
$this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '4459');
$this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), '4583');
$this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/wp-scriptaculous.js', array('prototype'), '1.6.1');