Autosave fixes from mdawaffe. fixes #3601

git-svn-id: http://svn.automattic.com/wordpress/trunk@4760 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-01-18 03:32:54 +00:00
parent 0409c8671d
commit 8c6162e883
6 changed files with 44 additions and 7 deletions

View File

@ -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';

View File

@ -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'];
@ -89,11 +108,16 @@ function wp_write_post() {
// Create the 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'];

View File

@ -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 = "<input type='hidden' id='post_ID' name='temp_ID' value='$temp_ID' />";
wp_nonce_field('add-post');
} else {

View File

@ -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 = "<input type='hidden' id='post_ID' name='temp_ID' value='$temp_ID' />";
} else {
$form_action = 'editpost';

View File

@ -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, <?php echo apply_filters('autosave_interval', '120'); ?>);
//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() {

View File

@ -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');