Make WordPress Core

Ticket #18515: 18515.diff

File 18515.diff, 5.7 KB (added by nacin, 12 years ago)
  • wp-includes/js/autosave.dev.js

     
    1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen;
     1var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
    22
    33jQuery(document).ready( function($) {
    44        var dotabkey = true;
     
    99        //Disable autosave after the form has been submitted
    1010        $("#post").submit(function() {
    1111                $.cancel(autosavePeriodical);
     12                autosaveLockRelease = false;
    1213        });
    1314
    1415        $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
     
    4748                }
    4849        };
    4950
     51        $(window).unload( function() {
     52                if ( ! autosaveLockRelease )
     53                        return;
     54                $.ajax({
     55                        type: 'POST',
     56                        url: ajaxurl,
     57                        async: false,
     58                        data: {
     59                                action: 'wp-remove-post-lock',
     60                                _wpnonce: $('#_wpnonce').val(),
     61                                post_ID: $('#post_ID').val(),
     62                                active_post_lock: $('#active_post_lock').val()
     63                        },
     64                });
     65        } );
     66
    5067        // preview
    5168        $('#post-preview').click(function(){
    5269                if ( $('#auto_draft').val() == '1' && notSaved ) {
     
    99116                        sup = res.responses[0].supplemental;
    100117                        if ( 'disable' == sup['disable_autosave'] ) {
    101118                                autosave = function() {};
     119                                autosaveLockRelease = false;
    102120                                res = { errors: true };
    103121                        }
    104122
     123                        if ( sup['active-post-lock'] ) {
     124                                jQuery('#active_post_lock').val( sup['active-post-lock'] );
     125                        }
     126
    105127                        if ( sup['alert'] ) {
    106128                                jQuery('#autosave-alert').remove();
    107129                                jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>');
  • wp-admin/admin-ajax.php

     
    988988                        $id = $post->ID;
    989989        }
    990990
    991         if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) )
    992                 wp_set_post_lock( $id );
     991        if ( $do_lock && empty( $_POST['auto_draft'] ) && $id && is_numeric( $id ) ) {
     992                $lock_result = wp_set_post_lock( $id );
     993                $supplemental['active-post-lock'] = implode( ':', $lock_result );
     994        }
    993995
    994996        if ( $nonce_age == 2 ) {
    995997                $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
     
    15511553        echo json_encode( array( 'message' => $message, 'last_edited' => $last_edited ) );
    15521554        die();
    15531555        break;
     1556case 'wp-remove-post-lock' :
     1557        if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) )
     1558                die( '0' );
     1559        $post_id = (int) $_POST['post_ID'];
     1560        if ( ! $post = get_post( $post_id ) )
     1561                die( '0' );
     1562
     1563        check_ajax_referer( 'update-' . $post->post_type . '_' . $post_id );
     1564
     1565        if ( ! current_user_can( 'edit_post', $post_id ) )
     1566                die( '-1' );
     1567
     1568        $active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) );
     1569        if ( $active_lock[1] != get_current_user_id() )
     1570                die( '0' );
     1571
     1572        $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ) + 5 ) . ':' . $active_lock[1];
     1573        update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) );
     1574        die( '1' );
     1575
    15541576default :
    15551577        do_action( 'wp_ajax_' . $_POST['action'] );
    15561578        die('0');
  • wp-admin/includes/post.php

     
    12271227        $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
    12281228
    12291229        $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
    1230 
    12311230        if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
    12321231                return $user;
    12331232        return false;
     
    12391238 * @since 2.5.0
    12401239 *
    12411240 * @param int $post_id ID of the post to being edited
    1242  * @return bool Returns false if the post doesn't exist of there is no current user
     1241 * @return bool|array Returns false if the post doesn't exist of there is no current user, or
     1242 *      an array of the lock time and the user ID.
    12431243 */
    12441244function wp_set_post_lock( $post_id ) {
    12451245        if ( !$post = get_post( $post_id ) )
     
    12511251        $lock = "$now:$user_id";
    12521252
    12531253        update_post_meta( $post->ID, '_edit_lock', $lock );
     1254        return array( $now, $user_id );
    12541255}
    12551256
    12561257/**
  • wp-admin/post.php

     
    174174        if ( $last = wp_check_post_lock( $post->ID ) ) {
    175175                add_action('admin_notices', '_admin_notice_post_locked' );
    176176        } else {
    177                 wp_set_post_lock( $post->ID );
     177                $active_post_lock = wp_set_post_lock( $post->ID );
    178178                wp_enqueue_script('autosave');
    179179        }
    180180
  • wp-admin/edit-form-advanced.php

     
    231231<input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post_type ) ?>" />
    232232<input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status) ?>" />
    233233<input type="hidden" id="referredby" name="referredby" value="<?php echo esc_url(stripslashes(wp_get_referer())); ?>" />
     234<?php if ( ! empty( $active_post_lock ) ) { ?>
     235<input type="hidden" id="active_post_lock" value="<?php echo esc_attr( implode( ':', $active_post_lock ) ); ?>" />
    234236<?php
     237}
    235238if ( 'draft' != $post->post_status )
    236239        wp_original_referer_field(true, 'previous');
    237240