Make WordPress Core


Ignore:
Timestamp:
02/29/2008 09:51:36 AM (18 years ago)
Author:
ryan
Message:

Post Edit Collision Detection from mdawaffe. fixes #6043

File:
1 edited

Legend:

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

    r7098 r7103  
    471471case 'autosave' : // The name of this action is hardcoded in edit_post()
    472472    check_ajax_referer( 'autosave', 'autosavenonce' );
     473    global $current_user;
     474
    473475    $_POST['post_content'] = $_POST['content'];
    474476    $_POST['post_excerpt'] = $_POST['excerpt'];
     
    479481        unset($_POST['post_category']);
    480482
     483    $do_autosave = (bool) $_POST['autosave'];
     484    $do_lock = true;
     485
     486    $data = '<div class="updated"><p>' . sprintf( __('Saved at %s.'), date( __('g:i:s a'), current_time( 'timestamp', true ) ) ) . '</p></div>';
     487
     488    $supplemental = array();
     489
     490    $id = 0;
    481491    if($_POST['post_ID'] < 0) {
    482492        $_POST['temp_ID'] = $_POST['post_ID'];
    483         $id = wp_write_post();
    484         if( is_wp_error($id) )
    485             die($id->get_error_message());
    486         else
    487             die("$id");
     493        if ( $do_autosave )
     494            $id = wp_write_post();
    488495    } else {
    489496        $post_ID = (int) $_POST['post_ID'];
    490497        $_POST['ID'] = $post_ID;
    491498        $post = get_post($post_ID);
     499
     500        if ( $last = wp_check_post_lock( $post->ID ) ) {
     501            $do_autosave = $do_lock = false;
     502
     503            $last_user = get_userdata( $last );
     504            $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
     505            $data = new WP_Error( 'locked', sprintf(
     506                $_POST['post_type'] == 'page' ? __( 'Autosave disabled: %s is currently editing this page.' ) : __( 'Autosave disabled: %s is currently editing this post.' ),
     507                wp_specialchars( $last_user_name )
     508            ) );
     509
     510            $supplemental['disable_autosave'] = 'disable';
     511        }
     512
    492513        if ( 'page' == $post->post_type ) {
    493514            if ( !current_user_can('edit_page', $post_ID) )
     
    497518                die(__('You are not allowed to edit this post.'));
    498519        }
    499         wp_update_post($_POST);
    500     }
    501     die('0');
    502 break;
     520        if ( $do_autosave )
     521            $id = wp_update_post($_POST);
     522        else
     523            $id = $post->ID;
     524    }
     525
     526    if ( $do_lock && $id && is_numeric($id) )
     527        wp_set_post_lock( $id );
     528
     529    $x = new WP_Ajax_Response( array(
     530        'what' => 'autosave',
     531        'id' => $id,
     532        'data' => $id ? $data : '',
     533        'supplemental' => $supplemental
     534    ) );
     535    $x->send();
     536    break;
    503537case 'autosave-generate-nonces' :
    504538    check_ajax_referer( 'autosave', 'autosavenonce' );
Note: See TracChangeset for help on using the changeset viewer.