Make WordPress Core

Ticket #23220: Atosave-v3.patch

File Atosave-v3.patch, 10.8 KB (added by asannad, 13 years ago)
  • wp-admin/js/common.js

     
    425425});
    426426
    427427})(jQuery);
     428
     429jQuery(document).ready( function($){
     430
     431    autosave_local_storage.remove_stale_data();
     432
     433    autosave_local_storage.display_data();
     434
     435});
     436
     437autosave_local_storage = {
     438
     439    lastsaveddata:'',
     440    modified_data : {},
     441    html:'',
     442    content : new Array(),
     443
     444    /**
     445     * Prepares data for saving in local storage.
     446     * @return {Object}
     447     */
     448    prepare_data: function() {
     449
     450        var post_data = {
     451            action: "autosave",
     452            post_ID:  jQuery("#post_ID").val() || 0,
     453            post_type: jQuery('#post_type').val() || ""
     454        };
     455
     456        if ( fullscreen && fullscreen.settings.visible ) {
     457            post_data["post_title"] = jQuery('#wp-fullscreen-title').val() || '';
     458            post_data["content"] = jQuery("#wp_mce_fullscreen").val() || '';
     459        } else {
     460            post_data["post_title"] = jQuery("#title").val() || '';
     461            post_data["content"] = jQuery("#content").val() || '';
     462        }
     463
     464        if ( jQuery('#post_name').val() )
     465            post_data["post_name"] = jQuery('#post_name').val();
     466
     467        var cats = ([]);
     468
     469        jQuery("[name='post_category[]']:checked").each( function(i) {
     470            cats.push(this.value);
     471        } );
     472        post_data["catslist"] = cats.join(",");
     473
     474        if ( jQuery("#comment_status").prop("checked") )
     475            post_data["comment_status"] = 'open';
     476        if ( jQuery("#ping_status").prop("checked") )
     477            post_data["ping_status"] = 'open';
     478        if ( jQuery("#excerpt").size() )
     479            post_data["excerpt"] = jQuery("#excerpt").val();
     480        if ( jQuery("#post_author").size() )
     481            post_data["post_author"] = jQuery("#post_author").val();
     482        if ( jQuery("#parent_id").val() )
     483            post_data["parent_id"] = jQuery("#parent_id").val();
     484        post_data["user_ID"] = jQuery("#user-id").val();
     485        if ( jQuery('#auto_draft').val() == '1' )
     486            post_data["auto_draft"] = '1';
     487
     488        post_data["save_time"] = new Date().getTime();
     489
     490        post_data['storage_status'] = 'fresh';
     491
     492        return post_data;
     493
     494    },
     495
     496    /**
     497     * Returns post id from post edit page
     498     * @return {*|int}
     499     */
     500    get_post_id: function() {
     501        var post_id = jQuery("#post_ID").val() || 0;
     502
     503        if( 0 < post_id ) {
     504            return post_id;
     505        }
     506    },
     507
     508    /**
     509     * Checks if local storage is enabled or not
     510     * @return {*}
     511     */
     512    get_storage: function() {
     513
     514        if ( window.localStorage ) {
     515            //have local storage container per blogid
     516            var store = localStorage.getItem( 'wp_autosave_'+commonL10n.blog_id );
     517
     518            if( store ) {
     519                return JSON.parse( store );
     520            } else {
     521                return {};
     522            }
     523        }
     524    },
     525
     526    set_storage: function( storage_arr ) {
     527        if ( window.localStorage ) {
     528            localStorage.setItem( 'wp_autosave_'+commonL10n.blog_id, storage_arr );
     529        }
     530    },
     531
     532    /**
     533     * Misc utility function to save/delete/edit post data in local storage.
     534     * @param data
     535     * @return {*}
     536     */
     537    handle_post_data: function( data ) {
     538
     539        var storage_arr = this.get_storage();
     540
     541        if ( storage_arr ) {
     542
     543            var post_id = this.get_post_id();
     544
     545            if( post_id ) {
     546
     547                if( data ) {
     548
     549                    //we wanna get rid of particular revision if data ='del'
     550                    if( 'del' === data){
     551
     552                        if( typeof storage_arr[post_id] !== 'undefined' ) {
     553                            delete storage_arr[post_id];
     554                        }
     555
     556                    } else{
     557                        storage_arr[post_id] = data;
     558                    }
     559
     560                    storage_arr = JSON.stringify( storage_arr );
     561
     562                    this.set_storage( storage_arr );
     563                //else return data for particular post
     564                } else if( typeof storage_arr[post_id] !== 'undefined' ) {
     565
     566                     return storage_arr[post_id];
     567                }
     568            }
     569
     570        }
     571    },
     572
     573    /**
     574     * Set post data for particular post id
     575     */
     576    set_data: function() {
     577
     578        var post_data = this.prepare_data();
     579
     580        var storage_data = this.handle_post_data();
     581
     582        //if the content and title did not change from last server save,dont keep data in local storage
     583        if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 )
     584            || post_data["post_title"] + post_data["content"] == autosaveLast
     585            || post_data["post_title"] + post_data["content"] == this.lastsaveddata
     586            ) {
     587            //Save only 5 revisions if there are more remove
     588            if( storage_data && 5 < storage_data.length  ) {
     589                storage_data.pop();
     590                this.handle_post_data( storage_data )
     591            }
     592
     593        } else {
     594
     595            if( storage_data  ) {
     596
     597                //Save only 5 revisions if there are more remove
     598                if( 5 < storage_data.unshift( post_data ) ) {
     599                    storage_data.pop();
     600                }
     601
     602            } else {
     603                //post data was not available create data for our new post id
     604                var storage_data = new Array( post_data );
     605            }
     606
     607            //Make sre we know whats our last saved data, so we dont keep on saving same data and replace important info
     608            this.lastsaveddata = post_data["post_title"] + post_data["content"];
     609
     610            this.handle_post_data( storage_data );
     611
     612        }
     613    },
     614
     615    /**
     616     * Remove all data from local storage if its saved for more that 12 hours.
     617     */
     618    remove_stale_data: function() { 
     619
     620        var stored_data = this.get_storage(); 
     621
     622        if( stored_data ) { 
     623
     624            jQuery.each( stored_data, function( key, value ) { 
     625
     626                var current_time = new Date().getTime();
     627
     628                var new_val_arr = new Array();
     629
     630                for( var i=0; i < value.length; i++ ) {
     631
     632                    var post_store = value[i];
     633
     634                    var time_diff = current_time - post_store['save_time'];
     635
     636                    //check for 12 hr difference
     637                    if( 43200000 > time_diff  ) {
     638                        new_val_arr.unshift( post_store );
     639                    }
     640                }
     641                if( new_val_arr && 0 < new_val_arr.length ) {
     642                    autosave_local_storage.modified_data[key] = new_val_arr;
     643                }
     644            });
     645
     646            storage_arr = JSON.stringify( this.modified_data );
     647
     648            this.modified_data = '';
     649
     650            this.set_storage( storage_arr );
     651        }
     652    },
     653
     654    /**
     655     * Escape html so we can show in div
     656     * @param content
     657     * @return {*}
     658     */
     659    esc_html: function( content ) {
     660
     661            content = jQuery("<div/>").text(content).html();
     662
     663
     664            return content;
     665
     666    },
     667
     668    /**
     669     * Show data in admin menu that one has historical saved data available for recovery in case one wants.
     670     */
     671    display_data: function() {
     672
     673        stored_data = this.get_storage();
     674
     675        if( stored_data ) {
     676
     677            jQuery.each( stored_data, function( key, post_data ) {
     678
     679                for( var i= post_data.length-1; i > -1; i-- ) {
     680
     681                    var title = autosave_local_storage.esc_html(post_data[i]["post_title"]);
     682
     683                    if( jQuery.trim(title) == '' ) {
     684                        title = post_data[i]["post_ID"];
     685                    }
     686
     687
     688                    autosave_local_storage.content[key+i] = autosave_local_storage.esc_html(post_data[i]["content"]);
     689
     690
     691                   autosave_local_storage.html = autosave_local_storage.html + '<span style="padding: 0 10px;"><a class="autosave-recover" data-key="'+key+i+'" data-postid="'+key+'" data-title="'+title+'" href="javascript:void(0)">'+title+'</a> </span>'
     692
     693                }
     694
     695            });
     696
     697            if( this.html != '' ) {
     698
     699                var notice = "<div class='error' id='autosave_local_storage_wrap'><p><strong>Post History</strong> <a href='javascript:void(0)' id='autosave_local_storage_hide'>Hide This Box</a></p></p><p>"+this.html+"<div style='padding: 0 10px;' id='autosave_local_storage' ></div></p></div>";
     700
     701                jQuery("#wpbody .wrap h2:first").after( notice );
     702
     703                jQuery("#autosave_local_storage_hide").on("click", function(event){
     704
     705                    jQuery("#autosave_local_storage_wrap").hide();
     706                });
     707
     708                jQuery(".autosave-recover").on("click", function(event){
     709                    jQuery('#autosave_local_storage').html( autosave_local_storage.content[jQuery(this).data('key')] );
     710                });
     711            }
     712
     713        }
     714
     715    }
     716}
  • wp-includes/js/autosave.js

     
    55        autosaveLast = ( $('#post #title').val() || '' ) + ( $('#post #content').val() || '' );
    66        autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
    77
     8    autosavePeriodical_client = $.schedule({time: 10000, func: function() { autosave_local_storage.set_data(); }, repeat: true, protect: true});
     9
    810        //Disable autosave after the form has been submitted
    911        $("#post").submit(function() {
    1012                $.cancel(autosavePeriodical);
    11                 autosaveLockRelease = false;
     13        $.cancel(autosavePeriodical_client);
     14        autosaveLockRelease = false;
    1215        });
    1316
    1417        $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
     
    341344        }
    342345
    343346        autosaveOldMessage = jQuery('#autosave').html();
     347
     348    autosave_local_storage.set_data();
     349
    344350        jQuery.ajax({
    345351                data: post_data,
    346352                beforeSend: doAutoSave ? autosave_loading : null,
  • wp-includes/script-loader.php

     
    6868
    6969        $scripts->add( 'common', "/wp-admin/js/common$suffix.js", array('jquery', 'hoverIntent', 'utils'), false, 1 );
    7070        did_action( 'init' ) && $scripts->localize( 'common', 'commonL10n', array(
    71                 'warnDelete' => __("You are about to permanently delete the selected items.\n  'Cancel' to stop, 'OK' to delete.")
     71                'warnDelete' => __("You are about to permanently delete the selected items.\n  'Cancel' to stop, 'OK' to delete."),
     72                'blog_id' => get_current_blog_id()
    7273        ) );
    7374
    7475        $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", array(), '1.6.1', 1 );