Changeset 4760

Show
Ignore:
Timestamp:
01/18/07 03:32:54 (2 years ago)
Author:
ryan
Message:

Autosave fixes from mdawaffe. fixes #3601

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/admin-ajax.php

    r4700 r4760  
    221221    $x->send(); 
    222222    break; 
    223 case 'autosave' : 
     223case 'autosave' : // The name of this action is hardcoded in edit_post() 
    224224    $_POST['post_content'] = $_POST['content']; 
    225225    $_POST['post_excerpt'] = $_POST['excerpt']; 
  • trunk/wp-admin/admin-functions.php

    r4759 r4760  
    1919        if ( !current_user_can( 'edit_posts' ) ) 
    2020            return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) ); 
     21    } 
     22 
     23 
     24    // Check for autosave collisions 
     25    if ( isset($_POST['temp_ID']) ) { 
     26        $temp_id = (int) $_POST['temp_ID']; 
     27        if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) ) 
     28            $draft_ids = array(); 
     29        foreach ( $draft_ids as $temp => $real ) 
     30            if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then ) 
     31                unset($draft_ids[$temp]); 
     32 
     33        if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write 
     34            $_POST['post_ID'] = $draft_ids[$temp_id]; 
     35            unset($_POST['temp_ID']); 
     36            relocate_children( $temp_id, $_POST['post_ID'] ); 
     37            update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids ); 
     38            return edit_post(); 
     39        } 
    2140    } 
    2241 
     
    89108 
    90109    // Create the post. 
    91     $post_ID = wp_insert_post( $_POST); 
     110    $post_ID = wp_insert_post( $_POST ); 
     111 
    92112    add_meta( $post_ID ); 
    93113 
    94114    // Reunite any orphaned attachments with their parent 
    95     if ( $_POST['temp_ID'] ) 
    96         relocate_children( $_POST['temp_ID'], $post_ID ); 
     115    // Update autosave collision detection 
     116    if ( $temp_id ) { 
     117        relocate_children( $temp_id, $post_ID ); 
     118        $draft_ids[$temp_id] = $post_ID; 
     119        update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids ); 
     120    } 
    97121 
    98122    // Now that we have an ID we can fix any attachment anchor hrefs 
     
    164188        if ( !current_user_can( 'edit_post', $post_ID ) ) 
    165189            wp_die( __('You are not allowed to edit this post.' )); 
     190    } 
     191 
     192    // Autosave shouldn't save too soon after a real save 
     193    if ( 'autosave' == $_POST['action'] ) { 
     194        $post =& get_post( $post_ID ); 
     195        $now = time(); 
     196        $then = strtotime($post->post_date_gmt . ' +0000'); 
     197        // Keep autosave_interval in sync with autosave-js.php. 
     198        $delta = apply_filters( 'autosave_interval', 120 ) / 2; 
     199        if ( ($now - $then) < $delta ) 
     200            return $post_ID; 
    166201    } 
    167202 
  • trunk/wp-admin/edit-form-advanced.php

    r4658 r4760  
    1818if (0 == $post_ID) { 
    1919    $form_action = 'post'; 
    20     $temp_ID = -1 * time(); 
     20    $temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post() 
    2121    $form_extra = "<input type='hidden' id='post_ID' name='temp_ID' value='$temp_ID' />"; 
    2222    wp_nonce_field('add-post'); 
  • trunk/wp-admin/edit-page-form.php

    r4658 r4760  
    66    $form_action = 'post'; 
    77    $nonce_action = 'add-page'; 
    8     $temp_ID = -1 * time(); 
     8    $temp_ID = -1 * time(); // don't change this formula without looking at wp_write_post() 
    99    $form_extra = "<input type='hidden' id='post_ID' name='temp_ID' value='$temp_ID' />"; 
    1010} else { 
  • trunk/wp-includes/js/autosave-js.php

    r4689 r4760  
    88    var form = $('post'); 
    99    autosaveLast = form.post_title.value+form.content.value; 
     10    // Keep autosave_interval in sync with edit_post(). 
    1011    autosavePeriodical = new PeriodicalExecuter(autosave, <?php echo apply_filters('autosave_interval', '120'); ?>); 
    1112    //Disable autosave after the form has been submitted 
     
    8687    form.publish ? form.publish.disabled = 'disabled' : null; 
    8788    form.deletepost ? form.deletepost.disabled = 'disabled' : null; 
     89    setTimeout('autosave_enable_buttons();', 1000); // Re-enable 1 sec later.  Just gives autosave a head start to avoid collisions. 
    8890} 
    8991 
  • trunk/wp-includes/script-loader.php

    r4748 r4760  
    2020        $this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20061113' ); 
    2121        $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0'); 
    22         $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '4508'); 
     22        $this->add( 'autosave', '/wp-includes/js/autosave-js.php', array('prototype', 'sack'), '20070116'); 
    2323        $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '4459'); 
    2424        $this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), '4583');