WordPress.org

Make WordPress Core

Changeset 23693


Ignore:
Timestamp:
03/14/2013 03:06:07 AM (7 years ago)
Author:
azaozz
Message:

Local autosave: set a temp cookie on submitting the form and change it on redirecting after the post is saved/updated, then use it to determine if saving worked properly. Removes the chance for false positives after saving/updating a post. See #23220

Location:
trunk
Files:
2 edited

Legend:

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

    r23661 r23693  
    211211    $post_id = edit_post();
    212212
     213    // Session cookie flag that the post was saved
     214    if ( isset( $_COOKIE['wp-saving-post-' . $post_id] ) )
     215        setcookie( 'wp-saving-post-' . $post_id, 'saved' );
     216
    213217    redirect_post($post_id); // Send user on their way while we keep working
    214218
  • trunk/wp-includes/js/autosave.js

    r23683 r23693  
    476476        }
    477477
    478         // If the content and title are empty or did not change since the last save, don't save again
     478        // If the content and title did not change since the last save, don't save again
    479479        if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata )
    480480            return false;
     
    538538
    539539        $('form#post').on('submit.autosave-local', function() {
    540             var editor = typeof tinymce != 'undefined' && tinymce.get('content');
     540            var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0;
    541541
    542542            if ( editor && ! editor.isHidden() ) {
     
    556556                });
    557557            }
     558
     559            wpCookies.set( 'wp-saving-post-' + post_id, 'check' );
    558560        });
    559561    },
    560562
    561563    // Strip whitespace and compare two strings
    562     compare: function( str1, str2, strip_tags ) {
    563         function remove( string, strip_tags ) {
    564             string = string.toString();
    565 
    566             if ( strip_tags )
    567                 string = string.replace(/<[^<>]+>/g, '');
    568 
    569             return string.replace(/[\x20\t\r\n\f]+/g, '');
    570         }
    571 
    572         return ( remove( str1 || '', strip_tags ) == remove( str2 || '', strip_tags ) );
     564    compare: function( str1, str2 ) {
     565        function remove( string ) {
     566            return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
     567        }
     568
     569        return ( remove( str1 || '' ) == remove( str2 || '' ) );
    573570    },
    574571
     
    581578     */
    582579    checkPost: function() {
    583         var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice;
     580        var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice,
     581            post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
    584582
    585583        if ( ! post_data )
     
    590588            return;
    591589
    592         content = $('#content').val();
    593         check_data = $.extend( {}, post_data );
    594 
    595         if ( $('#wp-content-wrap').hasClass('tmce-active') )
    596             content = switchEditors.pre_wpautop( content );
    597 
    598         // The post has just been published, only compare text
    599         if ( $('#post_status').val() == 'publish' && check_data.status != 'publish' )
    600             strip_tags = true;
    601 
    602         if ( this.compare( content, check_data.content, strip_tags ) && this.compare( $('#title').val(), check_data.post_title, strip_tags ) && this.compare( $('#excerpt').val(), check_data.excerpt, strip_tags ) )
    603             return;
    604 
    605         // We have three choices here:
    606         // - Do an autosave and then show the standard notice "There is an autosave newer than...".
    607         // - Offer to load/restore the backed up post data.
    608         // - Restore the post_data without asking, then show a notice with an Undo link/button.
    609         // Doing an autosave will take few seconds and may take up to 30 and fail if network connectivity is bad
    610         // Restoring the post will leave the user with the proper content, but it won't be saved to the server until the next autosave.
     590        if ( cookie == 'saved' ) {
     591            return;
     592        } else if ( cookie != 'check' ) {
     593            content = $('#content').val();
     594            check_data = $.extend( {}, post_data );
     595
     596            if ( $('#wp-content-wrap').hasClass('tmce-active') )
     597                content = switchEditors.pre_wpautop( content );
     598
     599            if ( this.compare( content, check_data.content ) && this.compare( $('#title').val(), check_data.post_title ) && this.compare( $('#excerpt').val(), check_data.excerpt ) )
     600                return;
     601        }
    611602
    612603        this.restore_post_data = post_data;
    613604        this.undo_post_data = wp.autosave.getPostData();
    614 
    615         /*
    616         if ( $('#post_status').val() == 'publish' ) {
    617             // Different message when a post is published?
    618             // Comparing the current and saved post data may fail (false positive) when the post is published
    619             // as in some cases there are changes to post_content on publishing and updating before saving to the DB.
    620         }
    621         */
    622605
    623606        notice = $('#local-storage-notice');
Note: See TracChangeset for help on using the changeset viewer.