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

Built from https://develop.svn.wordpress.org/trunk@26428


git-svn-id: http://core.svn.wordpress.org/trunk@26328 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2013-11-27 01:56:10 +00:00
parent 29af7b55f3
commit 162037903d
3 changed files with 56 additions and 6 deletions

View File

@ -739,3 +739,19 @@ function wp_refresh_post_nonces( $response, $data, $screen_id ) {
return $response; return $response;
} }
add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 ); add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
/**
* Disable suspending of Heartbeat on the Add/Edit Post screens
*
* @since 3.8
*/
function wp_disable_heartbeat_suspend( $settings ) {
global $pagenow;
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
$settings['suspend'] = 'disable';
}
return $settings;
}
add_filter( 'heartbeat_settings', 'wp_disable_heartbeat_suspend' );

View File

@ -34,8 +34,11 @@
var Heartbeat = function() { var Heartbeat = function() {
var $document = $(document), var $document = $(document),
settings = { settings = {
// Used to stop the "beat" // Suspend/resume
isRunning: true, suspend: false,
// Whether suspending is enabled
suspendEnabled: true,
// Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front' // Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
screenId: '', screenId: '',
@ -128,6 +131,10 @@
if ( ! settings.screenId ) { if ( ! settings.screenId ) {
settings.screenId = options.screenId || 'front'; settings.screenId = options.screenId || 'front';
} }
if ( options.suspend === 'disable' ) {
settings.suspendEnabled = false;
}
} }
// Convert to milliseconds // Convert to milliseconds
@ -145,7 +152,12 @@
focused(); focused();
}).on( 'unload.wp-heartbeat', function() { }).on( 'unload.wp-heartbeat', function() {
// Don't connect any more // Don't connect any more
settings.isRunning = false; settings.suspend = true;
// Abort the last request if not completed
if ( settings.xhr && settings.xhr.readyState !== 4 ) {
settings.xhr.abort();
}
}); });
// Check for user activity every 30 seconds. // Check for user activity every 30 seconds.
@ -274,7 +286,7 @@
// If the connection to the server is slower than the interval, // If the connection to the server is slower than the interval,
// heartbeat connects as soon as the previous connection's response is received. // heartbeat connects as soon as the previous connection's response is received.
if ( settings.connecting ) { if ( settings.connecting || settings.suspend ) {
return; return;
} }
@ -351,7 +363,7 @@
var delta = time() - settings.lastTick, var delta = time() - settings.lastTick,
interval = settings.mainInterval; interval = settings.mainInterval;
if ( ! settings.isRunning ) { if ( settings.suspend ) {
return; return;
} }
@ -403,6 +415,9 @@
clearFocusTimers(); clearFocusTimers();
settings.userActivity = time(); settings.userActivity = time();
// Resume if suspended
settings.suspend = false;
if ( ! settings.hasFocus ) { if ( ! settings.hasFocus ) {
settings.hasFocus = true; settings.hasFocus = true;
scheduleNextTick(); scheduleNextTick();
@ -513,6 +528,11 @@
blurred(); blurred();
} }
if ( settings.suspendEnabled && lastActive > 1200000 ) {
// Suspend after 20 min. of inactivity
settings.suspend = true;
}
if ( ! settings.userActivityEvents ) { if ( ! settings.userActivityEvents ) {
$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } ); $document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
@ -561,6 +581,19 @@
scheduleNextTick(); scheduleNextTick();
} }
/**
* Disable suspending
*
* Should be used only when Heartbeat is performing critical tasks like autosave, post-locking, etc.
* Using this on many screens may overload the user's hosting account if several
* browser windows/tabs are left open for a long time.
*
* @return void
*/
function disableSuspend() {
settings.suspendEnabled = false;
}
/** /**
* Get/Set the interval * Get/Set the interval
* *
@ -691,6 +724,7 @@
return { return {
hasFocus: hasFocus, hasFocus: hasFocus,
connectNow: connectNow, connectNow: connectNow,
disableSuspend: disableSuspend,
setInterval: setInterval, setInterval: setInterval,
hasConnectionError: hasConnectionError, hasConnectionError: hasConnectionError,
enqueue: enqueue, enqueue: enqueue,

File diff suppressed because one or more lines are too long