Make WordPress Core


Ignore:
Timestamp:
01/18/2007 03:32:54 AM (20 years ago)
Author:
ryan
Message:

Autosave fixes from mdawaffe. fixes #3601

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.