Make WordPress Core

Changeset 26428


Ignore:
Timestamp:
11/27/2013 01:55:59 AM (11 years ago)
Author:
azaozz
Message:

Heartbeat: introduce "suspend" functionality and enable it after 20 min. of inactivity, see #25073.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r26406 r26428  
    740740}
    741741add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
     742
     743/**
     744 * Disable suspending of Heartbeat on the Add/Edit Post screens
     745 *
     746 * @since 3.8
     747 */
     748function wp_disable_heartbeat_suspend( $settings ) {
     749    global $pagenow;
     750
     751    if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
     752        $settings['suspend'] = 'disable';
     753    }
     754
     755    return $settings;
     756}
     757add_filter( 'heartbeat_settings', 'wp_disable_heartbeat_suspend' );
  • trunk/src/wp-includes/js/heartbeat.js

    r26276 r26428  
    3535        var $document = $(document),
    3636            settings = {
    37                 // Used to stop the "beat"
    38                 isRunning: true,
     37                // Suspend/resume
     38                suspend: false,
     39
     40                // Whether suspending is enabled
     41                suspendEnabled: true,
    3942
    4043                // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
     
    128131                if ( ! settings.screenId ) {
    129132                    settings.screenId = options.screenId || 'front';
     133                }
     134
     135                if ( options.suspend === 'disable' ) {
     136                    settings.suspendEnabled = false;
    130137                }
    131138            }
     
    146153            }).on( 'unload.wp-heartbeat', function() {
    147154                // Don't connect any more
    148                 settings.isRunning = false;
     155                settings.suspend = true;
     156
     157                // Abort the last request if not completed
     158                if ( settings.xhr && settings.xhr.readyState !== 4 ) {
     159                    settings.xhr.abort();
     160                }
    149161            });
    150162
     
    275287            // If the connection to the server is slower than the interval,
    276288            // heartbeat connects as soon as the previous connection's response is received.
    277             if ( settings.connecting ) {
     289            if ( settings.connecting || settings.suspend ) {
    278290                return;
    279291            }
     
    352364                interval = settings.mainInterval;
    353365
    354             if ( ! settings.isRunning ) {
     366            if ( settings.suspend ) {
    355367                return;
    356368            }
     
    404416            settings.userActivity = time();
    405417
     418            // Resume if suspended
     419            settings.suspend = false;
     420
    406421            if ( ! settings.hasFocus ) {
    407422                settings.hasFocus = true;
     
    514529            }
    515530
     531            if ( settings.suspendEnabled && lastActive > 1200000 ) {
     532                // Suspend after 20 min. of inactivity
     533                settings.suspend = true;
     534            }
     535
    516536            if ( ! settings.userActivityEvents ) {
    517537                $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
     
    560580            settings.lastTick = 0;
    561581            scheduleNextTick();
     582        }
     583
     584        /**
     585         * Disable suspending
     586         *
     587         * Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
     588         * Using this on many screens may overload the user's hosting account if several
     589         * browser windows/tabs are left open for a long time.
     590         *
     591         * @return void
     592         */
     593        function disableSuspend() {
     594            settings.suspendEnabled = false;
    562595        }
    563596
     
    692725            hasFocus: hasFocus,
    693726            connectNow: connectNow,
     727            disableSuspend: disableSuspend,
    694728            setInterval: setInterval,
    695729            hasConnectionError: hasConnectionError,
Note: See TracChangeset for help on using the changeset viewer.