2013-01-29 07:15:25 +01:00
|
|
|
/**
|
|
|
|
* Heartbeat API
|
2013-07-11 02:35:52 +02:00
|
|
|
*
|
|
|
|
* Heartbeat is a simple server polling API that sends XHR requests to
|
2013-11-14 19:41:10 +01:00
|
|
|
* the server every 15 - 60 seconds and triggers events (or callbacks) upon
|
2013-07-11 02:35:52 +02:00
|
|
|
* receiving data. Currently these 'ticks' handle transports for post locking,
|
2014-04-07 08:33:16 +02:00
|
|
|
* login-expiration warnings, autosave, and related tasks while a user is logged in.
|
2013-07-11 02:35:52 +02:00
|
|
|
*
|
2013-11-14 19:41:10 +01:00
|
|
|
* Available PHP filters (in ajax-actions.php):
|
2013-07-11 02:35:52 +02:00
|
|
|
* - heartbeat_received
|
|
|
|
* - heartbeat_send
|
|
|
|
* - heartbeat_tick
|
|
|
|
* - heartbeat_nopriv_received
|
|
|
|
* - heartbeat_nopriv_send
|
|
|
|
* - heartbeat_nopriv_tick
|
|
|
|
* @see wp_ajax_nopriv_heartbeat(), wp_ajax_heartbeat()
|
|
|
|
*
|
2013-11-14 19:41:10 +01:00
|
|
|
* Custom jQuery events:
|
|
|
|
* - heartbeat-send
|
|
|
|
* - heartbeat-tick
|
|
|
|
* - heartbeat-error
|
|
|
|
* - heartbeat-connection-lost
|
|
|
|
* - heartbeat-connection-restored
|
|
|
|
* - heartbeat-nonces-expired
|
|
|
|
*
|
2013-07-11 02:35:52 +02:00
|
|
|
* @since 3.6.0
|
2013-01-29 07:15:25 +01:00
|
|
|
*/
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
( function( $, window, undefined ) {
|
2013-01-29 07:15:25 +01:00
|
|
|
var Heartbeat = function() {
|
2013-11-14 19:41:10 +01:00
|
|
|
var $document = $(document),
|
|
|
|
settings = {
|
2013-11-27 02:56:10 +01:00
|
|
|
// Suspend/resume
|
|
|
|
suspend: false,
|
|
|
|
|
|
|
|
// Whether suspending is enabled
|
|
|
|
suspendEnabled: true,
|
2013-11-14 19:41:10 +01:00
|
|
|
|
|
|
|
// Current screen id, defaults to the JS global 'pagenow' when present (in the admin) or 'front'
|
|
|
|
screenId: '',
|
|
|
|
|
|
|
|
// XHR request URL, defaults to the JS global 'ajaxurl' when present
|
|
|
|
url: '',
|
|
|
|
|
|
|
|
// Timestamp, start of the last connection request
|
|
|
|
lastTick: 0,
|
|
|
|
|
|
|
|
// Container for the enqueued items
|
|
|
|
queue: {},
|
|
|
|
|
|
|
|
// Connect interval (in seconds)
|
|
|
|
mainInterval: 60,
|
|
|
|
|
|
|
|
// Used when the interval is set to 5 sec. temporarily
|
|
|
|
tempInterval: 0,
|
|
|
|
|
|
|
|
// Used when the interval is reset
|
|
|
|
originalInterval: 0,
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// Used to limit the number of AJAX requests.
|
|
|
|
minimalInterval: 0,
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Used together with tempInterval
|
|
|
|
countdown: 0,
|
|
|
|
|
|
|
|
// Whether a connection is currently in progress
|
|
|
|
connecting: false,
|
|
|
|
|
2014-08-13 05:56:17 +02:00
|
|
|
// Whether a connection error occurred
|
2013-11-14 19:41:10 +01:00
|
|
|
connectionError: false,
|
|
|
|
|
|
|
|
// Used to track non-critical errors
|
|
|
|
errorcount: 0,
|
|
|
|
|
|
|
|
// Whether at least one connection has completed successfully
|
|
|
|
hasConnected: false,
|
|
|
|
|
|
|
|
// Whether the current browser window is in focus and the user is active
|
|
|
|
hasFocus: true,
|
|
|
|
|
|
|
|
// Timestamp, last time the user was active. Checked every 30 sec.
|
|
|
|
userActivity: 0,
|
|
|
|
|
|
|
|
// Flags whether events tracking user activity were set
|
|
|
|
userActivityEvents: false,
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
checkFocusTimer: 0,
|
|
|
|
beatTimer: 0
|
2013-11-14 19:41:10 +01:00
|
|
|
};
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-07-19 03:39:48 +02:00
|
|
|
/**
|
2013-11-14 19:41:10 +01:00
|
|
|
* Set local vars and events, then start
|
|
|
|
*
|
|
|
|
* @access private
|
2013-07-19 03:39:48 +02:00
|
|
|
*
|
2013-11-14 19:41:10 +01:00
|
|
|
* @return void
|
2013-07-19 03:39:48 +02:00
|
|
|
*/
|
2013-11-14 19:41:10 +01:00
|
|
|
function initialize() {
|
2014-11-10 02:47:24 +01:00
|
|
|
var options, hidden, visibilityState, visibilitychange;
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( typeof window.pagenow === 'string' ) {
|
|
|
|
settings.screenId = window.pagenow;
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( typeof window.ajaxurl === 'string' ) {
|
|
|
|
settings.url = window.ajaxurl;
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Pull in options passed from PHP
|
|
|
|
if ( typeof window.heartbeatSettings === 'object' ) {
|
2014-11-10 02:47:24 +01:00
|
|
|
options = window.heartbeatSettings;
|
2013-03-31 00:32:12 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// The XHR URL can be passed as option when window.ajaxurl is not set
|
|
|
|
if ( ! settings.url && options.ajaxurl ) {
|
|
|
|
settings.url = options.ajaxurl;
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec.
|
|
|
|
// It can be set in the initial options or changed later from JS and/or from PHP.
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( options.interval ) {
|
|
|
|
settings.mainInterval = options.interval;
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( settings.mainInterval < 15 ) {
|
|
|
|
settings.mainInterval = 15;
|
2014-11-10 02:47:24 +01:00
|
|
|
} else if ( settings.mainInterval > 120 ) {
|
|
|
|
settings.mainInterval = 120;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// Used to limit the number of AJAX requests. Overrides all other intervals if they are shorter.
|
|
|
|
// Needed for some hosts that cannot handle frequent requests and the user may exceed the allocated server CPU time, etc.
|
|
|
|
// The minimal interval can be up to 600 sec. however setting it to longer than 120 sec. will limit or disable
|
|
|
|
// some of the functionality (like post locks).
|
|
|
|
// Once set at initialization, minimalInterval cannot be changed/overriden.
|
|
|
|
if ( options.minimalInterval ) {
|
|
|
|
options.minimalInterval = parseInt( options.minimalInterval, 10 );
|
|
|
|
settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( settings.minimalInterval && settings.mainInterval < settings.minimalInterval ) {
|
|
|
|
settings.mainInterval = settings.minimalInterval;
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// 'screenId' can be added from settings on the front-end where the JS global 'pagenow' is not set
|
|
|
|
if ( ! settings.screenId ) {
|
|
|
|
settings.screenId = options.screenId || 'front';
|
|
|
|
}
|
2013-11-27 02:56:10 +01:00
|
|
|
|
2013-12-03 01:45:10 +01:00
|
|
|
if ( options.suspension === 'disable' ) {
|
2013-11-27 02:56:10 +01:00
|
|
|
settings.suspendEnabled = false;
|
|
|
|
}
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Convert to milliseconds
|
|
|
|
settings.mainInterval = settings.mainInterval * 1000;
|
|
|
|
settings.originalInterval = settings.mainInterval;
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// Switch the interval to 120 sec. by using the Page Visibility API.
|
|
|
|
// If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the interval
|
|
|
|
// will be increased to 120 sec. after 5 min. of mouse and keyboard inactivity.
|
|
|
|
if ( typeof document.hidden !== 'undefined' ) {
|
|
|
|
hidden = 'hidden';
|
|
|
|
visibilitychange = 'visibilitychange';
|
|
|
|
visibilityState = 'visibilityState';
|
|
|
|
} else if ( typeof document.msHidden !== 'undefined' ) { // IE10
|
|
|
|
hidden = 'msHidden';
|
|
|
|
visibilitychange = 'msvisibilitychange';
|
|
|
|
visibilityState = 'msVisibilityState';
|
|
|
|
} else if ( typeof document.webkitHidden !== 'undefined' ) { // Android
|
|
|
|
hidden = 'webkitHidden';
|
|
|
|
visibilitychange = 'webkitvisibilitychange';
|
|
|
|
visibilityState = 'webkitVisibilityState';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( hidden ) {
|
|
|
|
if ( document[hidden] ) {
|
|
|
|
settings.hasFocus = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$document.on( visibilitychange + '.wp-heartbeat', function() {
|
|
|
|
if ( document[visibilityState] === 'hidden' ) {
|
|
|
|
blurred();
|
|
|
|
window.clearInterval( settings.checkFocusTimer );
|
|
|
|
} else {
|
|
|
|
focused();
|
|
|
|
if ( document.hasFocus ) {
|
|
|
|
settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use document.hasFocus() if available.
|
|
|
|
if ( document.hasFocus ) {
|
|
|
|
settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$(window).on( 'unload.wp-heartbeat', function() {
|
2013-11-14 19:41:10 +01:00
|
|
|
// Don't connect any more
|
2013-11-27 02:56:10 +01:00
|
|
|
settings.suspend = true;
|
|
|
|
|
|
|
|
// Abort the last request if not completed
|
|
|
|
if ( settings.xhr && settings.xhr.readyState !== 4 ) {
|
|
|
|
settings.xhr.abort();
|
|
|
|
}
|
2013-11-14 19:41:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Check for user activity every 30 seconds.
|
2014-11-10 02:47:24 +01:00
|
|
|
window.setInterval( checkUserActivity, 30000 );
|
2013-11-14 19:41:10 +01:00
|
|
|
|
|
|
|
// Start one tick after DOM ready
|
|
|
|
$document.ready( function() {
|
|
|
|
settings.lastTick = time();
|
|
|
|
scheduleNextTick();
|
|
|
|
});
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Return the current time according to the browser
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function time() {
|
2013-01-29 07:15:25 +01:00
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Check if the iframe is from the same origin
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-05-30 02:08:53 +02:00
|
|
|
function isLocalFrame( frame ) {
|
|
|
|
var origin, src = frame.src;
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
|
|
|
|
// It throws uncatchable exceptions.
|
2013-05-30 02:08:53 +02:00
|
|
|
if ( src && /^https?:\/\//.test( src ) ) {
|
|
|
|
origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( src.indexOf( origin ) !== 0 ) {
|
2013-05-30 02:08:53 +02:00
|
|
|
return false;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-05-30 02:08:53 +02:00
|
|
|
}
|
|
|
|
|
2013-02-06 08:10:04 +01:00
|
|
|
try {
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( frame.contentWindow.document ) {
|
2013-02-06 08:10:04 +01:00
|
|
|
return true;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-02-06 08:10:04 +01:00
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
/**
|
|
|
|
* Check if the document's focus has changed
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function checkFocus() {
|
|
|
|
if ( settings.hasFocus && ! document.hasFocus() ) {
|
|
|
|
blurred();
|
|
|
|
} else if ( ! settings.hasFocus && document.hasFocus() ) {
|
|
|
|
focused();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Set error state and fire an event on XHR errors or timeout
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param string error The error type passed from the XHR
|
|
|
|
* @param int status The HTTP status code passed from jqXHR (200, 404, 500, etc.)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setErrorState( error, status ) {
|
2013-03-31 00:32:12 +01:00
|
|
|
var trigger;
|
|
|
|
|
|
|
|
if ( error ) {
|
|
|
|
switch ( error ) {
|
|
|
|
case 'abort':
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
case 'timeout':
|
|
|
|
// no response for 30 sec.
|
|
|
|
trigger = true;
|
|
|
|
break;
|
|
|
|
case 'error':
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( 503 === status && settings.hasConnected ) {
|
|
|
|
trigger = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-15 06:00:10 +01:00
|
|
|
/* falls through */
|
2013-11-14 19:41:10 +01:00
|
|
|
case 'parsererror':
|
2013-03-31 00:32:12 +01:00
|
|
|
case 'empty':
|
|
|
|
case 'unknown':
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.errorcount++;
|
2013-03-31 00:32:12 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( settings.errorcount > 2 && settings.hasConnected ) {
|
2013-03-31 00:32:12 +01:00
|
|
|
trigger = true;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-03-31 00:32:12 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( trigger && ! hasConnectionError() ) {
|
|
|
|
settings.connectionError = true;
|
2013-11-20 00:52:10 +01:00
|
|
|
$document.trigger( 'heartbeat-connection-lost', [error, status] );
|
2013-01-29 07:15:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Clear the error state and fire an event
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function clearErrorState() {
|
|
|
|
// Has connected successfully
|
|
|
|
settings.hasConnected = true;
|
|
|
|
|
|
|
|
if ( hasConnectionError() ) {
|
|
|
|
settings.errorcount = 0;
|
|
|
|
settings.connectionError = false;
|
|
|
|
$document.trigger( 'heartbeat-connection-restored' );
|
2013-05-16 01:17:51 +02:00
|
|
|
}
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather the data and connect to the server
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function connect() {
|
|
|
|
var ajaxData, heartbeatData;
|
2013-05-16 01:17:51 +02:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// If the connection to the server is slower than the interval,
|
|
|
|
// heartbeat connects as soon as the previous connection's response is received.
|
2013-11-27 02:56:10 +01:00
|
|
|
if ( settings.connecting || settings.suspend ) {
|
2013-05-16 01:17:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.lastTick = time();
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
heartbeatData = $.extend( {}, settings.queue );
|
|
|
|
// Clear the data queue, anything added after this point will be send on the next tick
|
|
|
|
settings.queue = {};
|
|
|
|
|
|
|
|
$document.trigger( 'heartbeat-send', [ heartbeatData ] );
|
|
|
|
|
|
|
|
ajaxData = {
|
|
|
|
data: heartbeatData,
|
|
|
|
interval: settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000,
|
|
|
|
_nonce: typeof window.heartbeatSettings === 'object' ? window.heartbeatSettings.nonce : '',
|
|
|
|
action: 'heartbeat',
|
|
|
|
screen_id: settings.screenId,
|
|
|
|
has_focus: settings.hasFocus
|
|
|
|
};
|
|
|
|
|
|
|
|
settings.connecting = true;
|
|
|
|
settings.xhr = $.ajax({
|
|
|
|
url: settings.url,
|
2013-03-31 00:32:12 +01:00
|
|
|
type: 'post',
|
2013-07-13 20:16:57 +02:00
|
|
|
timeout: 30000, // throw an error if not completed after 30 sec.
|
2013-11-14 19:41:10 +01:00
|
|
|
data: ajaxData,
|
2013-03-31 00:32:12 +01:00
|
|
|
dataType: 'json'
|
2013-11-14 19:41:10 +01:00
|
|
|
}).always( function() {
|
|
|
|
settings.connecting = false;
|
|
|
|
scheduleNextTick();
|
2013-05-16 01:17:51 +02:00
|
|
|
}).done( function( response, textStatus, jqXHR ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
var newInterval;
|
2013-03-31 00:32:12 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( ! response ) {
|
|
|
|
setErrorState( 'empty' );
|
|
|
|
return;
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
clearErrorState();
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-06-29 03:31:44 +02:00
|
|
|
if ( response.nonces_expired ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
$document.trigger( 'heartbeat-nonces-expired' );
|
2013-06-29 03:31:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// Change the interval from PHP
|
2013-05-16 01:17:51 +02:00
|
|
|
if ( response.heartbeat_interval ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
newInterval = response.heartbeat_interval;
|
2013-05-16 01:17:51 +02:00
|
|
|
delete response.heartbeat_interval;
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
$document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
|
|
|
|
if ( newInterval ) {
|
2013-12-03 01:45:10 +01:00
|
|
|
interval( newInterval );
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-03-31 00:32:12 +01:00
|
|
|
}).fail( function( jqXHR, textStatus, error ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
setErrorState( textStatus || 'unknown', jqXHR.status );
|
|
|
|
$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
|
2013-01-29 07:15:25 +01:00
|
|
|
});
|
2013-07-19 03:39:48 +02:00
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Schedule the next connection
|
|
|
|
*
|
|
|
|
* Fires immediately if the connection time is longer than the interval.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function scheduleNextTick() {
|
|
|
|
var delta = time() - settings.lastTick,
|
|
|
|
interval = settings.mainInterval;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-27 02:56:10 +01:00
|
|
|
if ( settings.suspend ) {
|
2013-01-29 07:15:25 +01:00
|
|
|
return;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! settings.hasFocus ) {
|
|
|
|
interval = 120000; // 120 sec. Post locks expire after 150 sec.
|
|
|
|
} else if ( settings.countdown > 0 && settings.tempInterval ) {
|
|
|
|
interval = settings.tempInterval;
|
|
|
|
settings.countdown--;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( settings.countdown < 1 ) {
|
|
|
|
settings.tempInterval = 0;
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
if ( settings.minimalInterval && interval < settings.minimalInterval ) {
|
|
|
|
interval = settings.minimalInterval;
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
window.clearTimeout( settings.beatTimer );
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( delta < interval ) {
|
|
|
|
settings.beatTimer = window.setTimeout(
|
|
|
|
function() {
|
2014-11-10 02:47:24 +01:00
|
|
|
connect();
|
2013-01-29 07:15:25 +01:00
|
|
|
},
|
2013-11-14 19:41:10 +01:00
|
|
|
interval - delta
|
2013-01-29 07:15:25 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
connect();
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
2014-11-10 02:47:24 +01:00
|
|
|
* Set the internal state when the browser window becomes hidden or loses focus
|
2013-11-14 19:41:10 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 08:03:27 +01:00
|
|
|
function blurred() {
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.hasFocus = false;
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
2014-11-10 02:47:24 +01:00
|
|
|
* Set the internal state when the browser window becomes visible or is in focus
|
2013-11-14 19:41:10 +01:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 08:03:27 +01:00
|
|
|
function focused() {
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.userActivity = time();
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-27 02:56:10 +01:00
|
|
|
// Resume if suspended
|
|
|
|
settings.suspend = false;
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( ! settings.hasFocus ) {
|
|
|
|
settings.hasFocus = true;
|
|
|
|
scheduleNextTick();
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Runs when the user becomes active after a period of inactivity
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 08:03:27 +01:00
|
|
|
function userIsActive() {
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.userActivityEvents = false;
|
|
|
|
$document.off( '.wp-heartbeat-active' );
|
|
|
|
|
2013-05-16 01:17:51 +02:00
|
|
|
$('iframe').each( function( i, frame ) {
|
2014-11-10 02:47:24 +01:00
|
|
|
if ( isLocalFrame( frame ) ) {
|
|
|
|
$( frame.contentWindow ).off( '.wp-heartbeat-active' );
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
/**
|
|
|
|
* Check for user activity
|
|
|
|
*
|
|
|
|
* Runs every 30 sec.
|
|
|
|
* Sets 'hasFocus = true' if user is active and the window is in the background.
|
|
|
|
* Set 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity)
|
|
|
|
* for 5 min. even when the window has focus.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function checkUserActivity() {
|
|
|
|
var lastActive = settings.userActivity ? time() - settings.userActivity : 0;
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// Throttle down when no mouse or keyboard activity for 5 min.
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( lastActive > 300000 && settings.hasFocus ) {
|
|
|
|
blurred();
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
// Suspend after 10 min. of inactivity when suspending is enabled.
|
|
|
|
// Always suspend after 60 min. of inactivity. This will release the post lock, etc.
|
|
|
|
if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
|
2013-11-27 02:56:10 +01:00
|
|
|
settings.suspend = true;
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( ! settings.userActivityEvents ) {
|
2014-11-10 02:47:24 +01:00
|
|
|
$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
|
|
|
|
userIsActive();
|
|
|
|
});
|
2013-03-31 00:32:12 +01:00
|
|
|
|
2013-05-16 01:17:51 +02:00
|
|
|
$('iframe').each( function( i, frame ) {
|
2014-11-10 02:47:24 +01:00
|
|
|
if ( isLocalFrame( frame ) ) {
|
|
|
|
$( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() {
|
|
|
|
userIsActive();
|
|
|
|
});
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
});
|
2013-03-31 00:32:12 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.userActivityEvents = true;
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Public methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the window (or any local iframe in it) has focus, or the user is active
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function hasFocus() {
|
|
|
|
return settings.hasFocus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether there is a connection error
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function hasConnectionError() {
|
|
|
|
return settings.connectionError;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect asap regardless of 'hasFocus'
|
|
|
|
*
|
|
|
|
* Will not open two concurrent connections. If a connection is in progress,
|
|
|
|
* will connect again immediately after the current connection completes.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function connectNow() {
|
|
|
|
settings.lastTick = 0;
|
|
|
|
scheduleNextTick();
|
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-27 02:56:10 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
/**
|
|
|
|
* Get/Set the interval
|
|
|
|
*
|
2013-11-14 19:41:10 +01:00
|
|
|
* When setting to 'fast' or 5, by default interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
|
|
|
|
* In this case the number of 'ticks' can be passed as second argument.
|
2013-03-31 00:32:12 +01:00
|
|
|
* If the window doesn't have focus, the interval slows down to 2 min.
|
2013-02-03 08:03:27 +01:00
|
|
|
*
|
2014-11-10 02:47:24 +01:00
|
|
|
* @param mixed speed Interval: 'fast' or 5, 15, 30, 60, 120
|
2013-11-14 19:41:10 +01:00
|
|
|
* @param string ticks Used with speed = 'fast' or 5, how many ticks before the interval reverts back
|
2013-02-03 08:03:27 +01:00
|
|
|
* @return int Current interval in seconds
|
|
|
|
*/
|
2013-12-03 01:45:10 +01:00
|
|
|
function interval( speed, ticks ) {
|
2013-12-04 20:24:09 +01:00
|
|
|
var newInterval,
|
|
|
|
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;
|
2013-02-03 08:03:27 +01:00
|
|
|
|
|
|
|
if ( speed ) {
|
|
|
|
switch ( speed ) {
|
|
|
|
case 'fast':
|
2013-11-14 19:41:10 +01:00
|
|
|
case 5:
|
2013-12-04 20:24:09 +01:00
|
|
|
newInterval = 5000;
|
2013-11-14 19:41:10 +01:00
|
|
|
break;
|
|
|
|
case 15:
|
2013-12-04 20:24:09 +01:00
|
|
|
newInterval = 15000;
|
2013-11-14 19:41:10 +01:00
|
|
|
break;
|
|
|
|
case 30:
|
2013-12-04 20:24:09 +01:00
|
|
|
newInterval = 30000;
|
2013-02-03 08:03:27 +01:00
|
|
|
break;
|
2013-11-14 19:41:10 +01:00
|
|
|
case 60:
|
2013-12-04 20:24:09 +01:00
|
|
|
newInterval = 60000;
|
2013-02-03 08:03:27 +01:00
|
|
|
break;
|
2014-11-10 02:47:24 +01:00
|
|
|
case 120:
|
|
|
|
newInterval = 120000;
|
|
|
|
break;
|
2013-02-03 08:03:27 +01:00
|
|
|
case 'long-polling':
|
|
|
|
// Allow long polling, (experimental)
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.mainInterval = 0;
|
2013-02-03 08:03:27 +01:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-12-04 20:24:09 +01:00
|
|
|
newInterval = settings.originalInterval;
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 02:47:24 +01:00
|
|
|
if ( settings.minimalInterval && newInterval < settings.minimalInterval ) {
|
|
|
|
newInterval = settings.minimalInterval;
|
|
|
|
}
|
|
|
|
|
2013-12-04 20:24:09 +01:00
|
|
|
if ( 5000 === newInterval ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
ticks = parseInt( ticks, 10 ) || 30;
|
|
|
|
ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.countdown = ticks;
|
2013-12-04 20:24:09 +01:00
|
|
|
settings.tempInterval = newInterval;
|
2013-02-03 08:03:27 +01:00
|
|
|
} else {
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.countdown = 0;
|
|
|
|
settings.tempInterval = 0;
|
2013-12-04 20:24:09 +01:00
|
|
|
settings.mainInterval = newInterval;
|
2013-02-03 08:03:27 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Change the next connection time if new interval has been set.
|
|
|
|
// Will connect immediately if the time since the last connection
|
|
|
|
// is greater than the new interval.
|
2013-12-04 20:24:09 +01:00
|
|
|
if ( newInterval !== oldInterval ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
scheduleNextTick();
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000;
|
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
/**
|
2013-03-31 00:32:12 +01:00
|
|
|
* Enqueue data to send with the next XHR
|
2013-02-03 08:03:27 +01:00
|
|
|
*
|
2013-11-14 19:41:10 +01:00
|
|
|
* As the data is send asynchronously, this function doesn't return the XHR response.
|
2013-02-03 08:03:27 +01:00
|
|
|
* To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
|
2013-05-16 01:17:51 +02:00
|
|
|
* $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
|
2013-02-03 08:03:27 +01:00
|
|
|
* // code
|
|
|
|
* });
|
2013-05-16 01:17:51 +02:00
|
|
|
* If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
|
2013-02-03 08:03:27 +01:00
|
|
|
* Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
|
|
|
|
*
|
2013-03-31 00:32:12 +01:00
|
|
|
* $param string handle Unique handle for the data. The handle is used in PHP to receive the data.
|
|
|
|
* $param mixed data The data to send.
|
2013-11-14 19:41:10 +01:00
|
|
|
* $param bool noOverwrite Whether to overwrite existing data in the queue.
|
2013-03-31 00:32:12 +01:00
|
|
|
* $return bool Whether the data was queued or not.
|
2013-02-03 08:03:27 +01:00
|
|
|
*/
|
2013-11-14 19:41:10 +01:00
|
|
|
function enqueue( handle, data, noOverwrite ) {
|
2013-02-03 08:03:27 +01:00
|
|
|
if ( handle ) {
|
2013-11-14 19:41:10 +01:00
|
|
|
if ( noOverwrite && this.isQueued( handle ) ) {
|
2013-02-03 08:03:27 +01:00
|
|
|
return false;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
settings.queue[handle] = data;
|
2013-02-03 08:03:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-11-14 19:41:10 +01:00
|
|
|
}
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
/**
|
|
|
|
* Check if data with a particular handle is queued
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
2013-08-22 00:55:09 +02:00
|
|
|
* $return bool Whether some data is queued with this handle
|
2013-02-03 08:03:27 +01:00
|
|
|
*/
|
2013-11-14 19:41:10 +01:00
|
|
|
function isQueued( handle ) {
|
|
|
|
if ( handle ) {
|
|
|
|
return settings.queue.hasOwnProperty( handle );
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 00:55:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data with a particular handle from the queue
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
|
|
|
* $return void
|
|
|
|
*/
|
2013-11-14 19:41:10 +01:00
|
|
|
function dequeue( handle ) {
|
|
|
|
if ( handle ) {
|
|
|
|
delete settings.queue[handle];
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 00:55:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data that was enqueued with a particular handle
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
|
|
|
* $return mixed The data or undefined
|
|
|
|
*/
|
2013-11-14 19:41:10 +01:00
|
|
|
function getQueuedItem( handle ) {
|
|
|
|
if ( handle ) {
|
|
|
|
return this.isQueued( handle ) ? settings.queue[handle] : undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
// Expose public methods
|
|
|
|
return {
|
|
|
|
hasFocus: hasFocus,
|
|
|
|
connectNow: connectNow,
|
2013-11-27 02:56:10 +01:00
|
|
|
disableSuspend: disableSuspend,
|
2013-12-03 01:45:10 +01:00
|
|
|
interval: interval,
|
2013-11-14 19:41:10 +01:00
|
|
|
hasConnectionError: hasConnectionError,
|
|
|
|
enqueue: enqueue,
|
|
|
|
dequeue: dequeue,
|
|
|
|
isQueued: isQueued,
|
|
|
|
getQueuedItem: getQueuedItem
|
2013-07-19 03:39:48 +02:00
|
|
|
};
|
|
|
|
};
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
// Ensure the global `wp` object exists.
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.heartbeat = new Heartbeat();
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-11-14 19:41:10 +01:00
|
|
|
}( jQuery, window ));
|