Make WordPress Core

Changeset 23735


Ignore:
Timestamp:
03/16/2013 09:15:43 PM (12 years ago)
Author:
azaozz
Message:

Create one autosave per user rather than a single autosave for all users. Remove unused code from autosave.js and wp_ajax_autosave(). See #23665.

Location:
trunk
Files:
4 edited

Legend:

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

    r23729 r23735  
    10441044
    10451045    $do_autosave = (bool) $_POST['autosave'];
    1046     $do_lock = true;
    10471046    $data = '';
    10481047    $supplemental = array();
    10491048    $id = $revision_id = 0;
    10501049
    1051     /* translators: draft saved date format, see http://php.net/date */
    1052     $draft_saved_date_format = __('g:i:s a');
    1053     /* translators: %s: date and time */
    1054     $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
     1050    if ( ! $user_id = get_current_user_id() )
     1051        wp_die('-1');
    10551052
    10561053    $post_id = (int) $_POST['post_id'];
     
    10591056    if ( 'auto-draft' == $post->post_status )
    10601057        $_POST['post_status'] = 'draft';
    1061 
    1062     if ( $last = wp_check_post_lock( $post->ID ) ) {
    1063         // This will change after we have per-user autosaves
    1064         $do_autosave = $do_lock = false;
    1065 
    1066         $last_user = get_userdata( $last );
    1067         $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
    1068         $data = __( 'Autosave disabled.' );
    1069 
    1070         $supplemental['disable_autosave'] = 'disable';
    1071     }
    10721058
    10731059    if ( 'page' == $post->post_type ) {
     
    10801066
    10811067    if ( $do_autosave ) {
    1082         // Drafts and auto-drafts are just overwritten by autosave
    1083         if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) {
     1068        // Drafts and auto-drafts are just overwritten by autosave for the same user
     1069        if ( $user_id == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
    10841070            $id = edit_post();
    1085         } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
     1071        } else { // Non drafts are not overwritten. The autosave is stored in a special post revision for each user.
    10861072            $revision_id = wp_create_post_autosave( $post->ID );
    10871073            if ( is_wp_error($revision_id) )
     
    10901076                $id = $post->ID;
    10911077        }
    1092         $data = $message;
     1078
     1079        if ( is_wp_error($id) ) {
     1080            // is_wp_error($id) overwrites $data in WP_Ajax_Response but no point in doing wp_create_nonce('update-post_' . $id) below
     1081            // todo: Needs review. The errors generated in WP_Ajax_Response and parsed with wpAjax.parseAjaxResponse() haven't been used for many years.
     1082            $data = $id;
     1083            $id = 0;
     1084        } else {
     1085            /* translators: draft saved date format, see http://php.net/date */
     1086            $draft_saved_date_format = __('g:i:s a');
     1087            /* translators: %s: date and time */
     1088            $data = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
     1089        }
    10931090    } else {
    10941091        if ( ! empty( $_POST['auto_draft'] ) )
     
    11041101        $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes');
    11051102        $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' );
    1106         if ( $id ) {
    1107             if ( $_POST['post_type'] == 'post' )
    1108                 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
    1109             elseif ( $_POST['post_type'] == 'page' )
    1110                 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id);
    1111         }
     1103        if ( $id )
     1104            $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id);
    11121105    }
    11131106
  • trunk/wp-admin/includes/post.php

    r23733 r23735  
    12811281        return $translated;
    12821282
    1283     // Only store one autosave. If there is already an autosave, overwrite it.
    1284     if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
     1283    $post_author = get_current_user_id();
     1284
     1285    // Store one autosave per author. If there is already an autosave, overwrite it.
     1286    if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
    12851287        $new_autosave = _wp_post_revision_fields( $_POST, true );
    12861288        $new_autosave['ID'] = $old_autosave->ID;
    1287         $new_autosave['post_author'] = get_current_user_id();
     1289        $new_autosave['post_author'] = $post_author;
    12881290        return wp_update_post( $new_autosave );
    12891291    }
     
    13401342    }
    13411343
    1342     if ( 'draft' == $post->post_status ) {
     1344    $user_id = get_current_user_id();
     1345    if ( 'draft' == $post->post_status && $user_id == $post->post_author ) {
    13431346        $id = edit_post();
    13441347    } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
     
    13511354        wp_die( $id->get_error_message() );
    13521355
    1353     if ( $_POST['post_status'] == 'draft' ) {
     1356    if ( $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
    13541357        $url = add_query_arg( 'preview', 'true', get_permalink($id) );
    13551358    } else {
  • trunk/wp-includes/js/autosave.js

    r23705 r23735  
    1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
     1var autosave, autosaveLast = '', autosavePeriodical, autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
    22
    33jQuery(document).ready( function($) {
     
    131131});
    132132
    133 function autosave_parse_response(response) {
    134     var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup;
     133function autosave_parse_response( response ) {
     134    var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup;
    135135
    136136    if ( res && res.responses && res.responses.length ) {
    137         message = res.responses[0].data; // The saved message or error.
    138         // someone else is editing: disable autosave, set errors
    139137        if ( res.responses[0].supplemental ) {
    140138            sup = res.responses[0].supplemental;
    141             if ( 'disable' == sup['disable_autosave'] ) {
    142                 autosave = function() {};
    143                 autosaveLockRelease = false;
    144                 res = { errors: true };
    145             }
    146 
    147             if ( sup['active-post-lock'] ) {
    148                 jQuery('#active_post_lock').val( sup['active-post-lock'] );
    149             }
    150 
    151             if ( sup['alert'] ) {
    152                 jQuery('#autosave-alert').remove();
    153                 jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>');
    154             }
    155 
    156             jQuery.each(sup, function(selector, value) {
    157                 if ( selector.match(/^replace-/) ) {
    158                     jQuery('#'+selector.replace('replace-', '')).val(value);
    159                 }
     139
     140            jQuery.each( sup, function( selector, value ) {
     141                if ( selector.match(/^replace-/) )
     142                    jQuery( '#' + selector.replace('replace-', '') ).val( value );
    160143            });
    161144        }
    162145
    163         // if no errors: add slug UI
     146        // if no errors: add slug UI and update autosave-message
    164147        if ( !res.errors ) {
    165             postID = parseInt( res.responses[0].id, 10 );
    166             if ( !isNaN(postID) && postID > 0 ) {
    167                 autosave_update_slug(postID);
    168             }
    169         }
    170     }
    171     if ( message ) { // update autosave message
    172         jQuery('.autosave-message').html(message);
    173     } else if ( autosaveOldMessage && res ) {
    174         jQuery('.autosave-message').html( autosaveOldMessage );
    175     }
     148            if ( post_id = parseInt( res.responses[0].id, 10 ) )
     149                autosave_update_slug( post_id );
     150
     151            if ( res.responses[0].data ) // update autosave message
     152                jQuery('.autosave-message').text( res.responses[0].data );
     153        }
     154    }
     155
    176156    return res;
    177157}
     
    187167function autosave_saved_new(response) {
    188168    blockSave = false;
    189     var res = autosave_parse_response(response), postID;
     169    var res = autosave_parse_response(response), post_id;
    190170
    191171    if ( res && res.responses.length && !res.errors ) {
    192172        // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
    193         postID = parseInt( res.responses[0].id, 10 );
    194         if ( !isNaN(postID) && postID > 0 ) {
     173        post_id = parseInt( res.responses[0].id, 10 );
     174
     175        if ( post_id ) {
    195176            notSaved = false;
    196177            jQuery('#auto_draft').val('0'); // No longer an auto-draft
    197178        }
     179
    198180        autosave_enable_buttons();
     181
    199182        if ( autosaveDelayPreview ) {
    200183            autosaveDelayPreview = false;
     
    287270    }
    288271
    289     autosaveOldMessage = jQuery('#autosave').html();
    290272    jQuery.ajax({
    291273        data: post_data,
  • trunk/wp-includes/revision.php

    r23594 r23735  
    136136 *
    137137 * Returns a post object containing the information that was autosaved for the
    138  * specified post.
    139  *
    140  * @package WordPress
    141  * @subpackage Post_Revisions
    142  * @since 2.6.0
    143  *
     138 * specified post. If the optional $user_id is passed, returns the autosave for that user
     139 * otherwise returns the latest autosave.
     140 *
     141 * @package WordPress
     142 * @subpackage Post_Revisions
     143 * @since 2.6.0
     144 * @uses wp_get_post_revisions()
     145 * 
    144146 * @param int $post_id The post ID.
     147 * @param int $user_id optional The post author ID.
    145148 * @return object|bool The autosaved data or false on failure or when no autosave exists.
    146149 */
    147 function wp_get_post_autosave( $post_id ) {
    148 
    149     if ( !$post = get_post( $post_id ) )
    150         return false;
    151 
    152     $q = array(
    153         'name' => "{$post->ID}-autosave",
    154         'post_parent' => $post->ID,
    155         'post_type' => 'revision',
    156         'post_status' => 'inherit'
    157     );
    158 
    159     // Use WP_Query so that the result gets cached
    160     $autosave_query = new WP_Query;
    161 
    162     add_action( 'parse_query', '_wp_get_post_autosave_hack' );
    163     $autosave = $autosave_query->query( $q );
    164     remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
    165 
    166     if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
    167         return $autosave[0];
     150function wp_get_post_autosave( $post_id, $user_id = 0 ) {
     151    $revisions = wp_get_post_revisions($post_id);
     152
     153    foreach ( $revisions as $revision ) {
     154        if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
     155            if ( $user_id && $user_id != $revision->post_author )
     156                continue;
     157
     158            return $revision;
     159            break;
     160        }
     161    }
    168162
    169163    return false;
    170 }
    171 
    172 /**
    173  * Internally used to hack WP_Query into submission.
    174  *
    175  * @package WordPress
    176  * @subpackage Post_Revisions
    177  * @since 2.6.0
    178  *
    179  * @param object $query WP_Query object
    180  */
    181 function _wp_get_post_autosave_hack( $query ) {
    182     $query->is_single = false;
    183164}
    184165
     
    196177    if ( !$post = wp_get_post_revision( $post ) )
    197178        return false;
     179
    198180    return (int) $post->post_parent;
    199181}
     
    212194    if ( !$post = wp_get_post_revision( $post ) )
    213195        return false;
    214     if ( "{$post->post_parent}-autosave" !== $post->post_name )
    215         return false;
    216     return (int) $post->post_parent;
     196
     197    if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) )
     198        return (int) $post->post_parent;
     199
     200    return false;
    217201}
    218202
     
    235219    elseif ( !is_array($post) )
    236220        $post = get_post($post, ARRAY_A);
     221
    237222    if ( !$post || empty($post['ID']) )
    238223        return;
     
    250235    if ( $revision_id )
    251236        do_action( '_wp_put_post_revision', $revision_id );
     237
    252238    return $revision_id;
    253239}
     
    313299
    314300    $update = array();
    315     foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
     301    foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) {
    316302        $update[$field] = $revision[$field];
     303    }
    317304
    318305    if ( !$update )
     
    375362 */
    376363function wp_get_post_revisions( $post_id = 0, $args = null ) {
    377     if ( ! WP_POST_REVISIONS )
    378         return array();
    379364    if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
    380365        return array();
     
    386371    if ( !$revisions = get_children( $args ) )
    387372        return array();
     373
    388374    return $revisions;
    389375}
Note: See TracChangeset for help on using the changeset viewer.