2013-01-29 01:15:25 -05:00
|
|
|
/**
|
|
|
|
* Heartbeat API
|
2013-07-10 20:35:52 -04:00
|
|
|
*
|
2013-07-28 16:54:48 -04:00
|
|
|
* Note: this API is "experimental" meaning it will likely change a lot
|
|
|
|
* in the next few releases based on feedback from 3.6.0. If you intend
|
|
|
|
* to use it, please follow the development closely.
|
|
|
|
*
|
2013-07-10 20:35:52 -04:00
|
|
|
* Heartbeat is a simple server polling API that sends XHR requests to
|
2013-11-14 13:41:10 -05:00
|
|
|
* the server every 15 - 60 seconds and triggers events (or callbacks) upon
|
2013-07-10 20:35:52 -04:00
|
|
|
* receiving data. Currently these 'ticks' handle transports for post locking,
|
|
|
|
* login-expiration warnings, and related tasks while a user is logged in.
|
|
|
|
*
|
2013-11-14 13:41:10 -05:00
|
|
|
* Available PHP filters (in ajax-actions.php):
|
2013-07-10 20:35:52 -04: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 13:41:10 -05:00
|
|
|
* Custom jQuery events:
|
|
|
|
* - heartbeat-send
|
|
|
|
* - heartbeat-tick
|
|
|
|
* - heartbeat-error
|
|
|
|
* - heartbeat-connection-lost
|
|
|
|
* - heartbeat-connection-restored
|
|
|
|
* - heartbeat-nonces-expired
|
|
|
|
*
|
2013-07-10 20:35:52 -04:00
|
|
|
* @since 3.6.0
|
2013-01-29 01:15:25 -05:00
|
|
|
*/
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
( function( $, window, undefined ) {
|
2013-01-29 01:15:25 -05:00
|
|
|
var Heartbeat = function() {
|
2013-11-14 13:41:10 -05:00
|
|
|
var $document = $(document),
|
|
|
|
settings = {
|
2013-11-26 20:56:10 -05:00
|
|
|
// Suspend/resume
|
|
|
|
suspend: false,
|
|
|
|
|
|
|
|
// Whether suspending is enabled
|
|
|
|
suspendEnabled: true,
|
2013-11-14 13:41:10 -05: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,
|
|
|
|
|
|
|
|
// Used together with tempInterval
|
|
|
|
countdown: 0,
|
|
|
|
|
|
|
|
// Whether a connection is currently in progress
|
|
|
|
connecting: false,
|
|
|
|
|
|
|
|
// Whether a connection error occured
|
|
|
|
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,
|
|
|
|
|
|
|
|
// References to various timeouts
|
|
|
|
beatTimer: 0,
|
|
|
|
winBlurTimer: 0,
|
|
|
|
frameBlurTimer: 0
|
|
|
|
};
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-07-18 21:39:48 -04:00
|
|
|
/**
|
2013-11-14 13:41:10 -05:00
|
|
|
* Set local vars and events, then start
|
|
|
|
*
|
|
|
|
* @access private
|
2013-07-18 21:39:48 -04:00
|
|
|
*
|
2013-11-14 13:41:10 -05:00
|
|
|
* @return void
|
2013-07-18 21:39:48 -04:00
|
|
|
*/
|
2013-11-14 13:41:10 -05:00
|
|
|
function initialize() {
|
|
|
|
if ( typeof window.pagenow === 'string' ) {
|
|
|
|
settings.screenId = window.pagenow;
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( typeof window.ajaxurl === 'string' ) {
|
|
|
|
settings.url = window.ajaxurl;
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// Pull in options passed from PHP
|
|
|
|
if ( typeof window.heartbeatSettings === 'object' ) {
|
|
|
|
var options = window.heartbeatSettings;
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-11-14 13:41:10 -05: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 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// The interval can be from 15 to 60 sec. and can be set temporarily to 5 sec.
|
|
|
|
if ( options.interval ) {
|
|
|
|
settings.mainInterval = options.interval;
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( settings.mainInterval < 15 ) {
|
|
|
|
settings.mainInterval = 15;
|
|
|
|
} else if ( settings.mainInterval > 60 ) {
|
|
|
|
settings.mainInterval = 60;
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05: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-26 20:56:10 -05:00
|
|
|
|
2013-12-02 19:45:10 -05:00
|
|
|
if ( options.suspension === 'disable' ) {
|
2013-11-26 20:56:10 -05:00
|
|
|
settings.suspendEnabled = false;
|
|
|
|
}
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// Convert to milliseconds
|
|
|
|
settings.mainInterval = settings.mainInterval * 1000;
|
|
|
|
settings.originalInterval = settings.mainInterval;
|
|
|
|
|
|
|
|
// Set focus/blur events on the window
|
|
|
|
$(window).on( 'blur.wp-heartbeat-focus', function() {
|
|
|
|
setFrameFocusEvents();
|
|
|
|
// We don't know why the 'blur' was fired. Either the user clicked in an iframe or outside the browser.
|
|
|
|
// Running blurred() after some timeout lets us cancel it if the user clicked in an iframe.
|
|
|
|
settings.winBlurTimer = window.setTimeout( function(){ blurred(); }, 500 );
|
|
|
|
}).on( 'focus.wp-heartbeat-focus', function() {
|
|
|
|
removeFrameFocusEvents();
|
|
|
|
focused();
|
|
|
|
}).on( 'unload.wp-heartbeat', function() {
|
|
|
|
// Don't connect any more
|
2013-11-26 20:56:10 -05:00
|
|
|
settings.suspend = true;
|
|
|
|
|
|
|
|
// Abort the last request if not completed
|
|
|
|
if ( settings.xhr && settings.xhr.readyState !== 4 ) {
|
|
|
|
settings.xhr.abort();
|
|
|
|
}
|
2013-11-14 13:41:10 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Check for user activity every 30 seconds.
|
|
|
|
window.setInterval( function(){ checkUserActivity(); }, 30000 );
|
|
|
|
|
|
|
|
// Start one tick after DOM ready
|
|
|
|
$document.ready( function() {
|
|
|
|
settings.lastTick = time();
|
|
|
|
scheduleNextTick();
|
|
|
|
});
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Return the current time according to the browser
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function time() {
|
2013-01-29 01:15:25 -05:00
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Check if the iframe is from the same origin
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-05-29 20:08:53 -04:00
|
|
|
function isLocalFrame( frame ) {
|
|
|
|
var origin, src = frame.src;
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// Need to compare strings as WebKit doesn't throw JS errors when iframes have different origin.
|
|
|
|
// It throws uncatchable exceptions.
|
2013-05-29 20:08:53 -04:00
|
|
|
if ( src && /^https?:\/\//.test( src ) ) {
|
|
|
|
origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( src.indexOf( origin ) !== 0 ) {
|
2013-05-29 20:08:53 -04:00
|
|
|
return false;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-05-29 20:08:53 -04:00
|
|
|
}
|
|
|
|
|
2013-02-06 02:10:04 -05:00
|
|
|
try {
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( frame.contentWindow.document ) {
|
2013-02-06 02:10:04 -05:00
|
|
|
return true;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-06 02:10:04 -05:00
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05: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-30 19:32:12 -04: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 13:41:10 -05:00
|
|
|
if ( 503 === status && settings.hasConnected ) {
|
|
|
|
trigger = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-15 00:00:10 -05:00
|
|
|
/* falls through */
|
2013-11-14 13:41:10 -05:00
|
|
|
case 'parsererror':
|
2013-03-30 19:32:12 -04:00
|
|
|
case 'empty':
|
|
|
|
case 'unknown':
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.errorcount++;
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( settings.errorcount > 2 && settings.hasConnected ) {
|
2013-03-30 19:32:12 -04:00
|
|
|
trigger = true;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-03-30 19:32:12 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( trigger && ! hasConnectionError() ) {
|
|
|
|
settings.connectionError = true;
|
2013-11-19 18:52:10 -05:00
|
|
|
$document.trigger( 'heartbeat-connection-lost', [error, status] );
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05: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-15 19:17:51 -04:00
|
|
|
}
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather the data and connect to the server
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function connect() {
|
|
|
|
var ajaxData, heartbeatData;
|
2013-05-15 19:17:51 -04:00
|
|
|
|
2013-11-14 13:41:10 -05: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-26 20:56:10 -05:00
|
|
|
if ( settings.connecting || settings.suspend ) {
|
2013-05-15 19:17:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.lastTick = time();
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05: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-30 19:32:12 -04:00
|
|
|
type: 'post',
|
2013-07-13 14:16:57 -04:00
|
|
|
timeout: 30000, // throw an error if not completed after 30 sec.
|
2013-11-14 13:41:10 -05:00
|
|
|
data: ajaxData,
|
2013-03-30 19:32:12 -04:00
|
|
|
dataType: 'json'
|
2013-11-14 13:41:10 -05:00
|
|
|
}).always( function() {
|
|
|
|
settings.connecting = false;
|
|
|
|
scheduleNextTick();
|
2013-05-15 19:17:51 -04:00
|
|
|
}).done( function( response, textStatus, jqXHR ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
var newInterval;
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! response ) {
|
|
|
|
setErrorState( 'empty' );
|
|
|
|
return;
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
clearErrorState();
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-06-28 21:31:44 -04:00
|
|
|
if ( response.nonces_expired ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
$document.trigger( 'heartbeat-nonces-expired' );
|
2013-06-28 21:31:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-03 02:03:27 -05:00
|
|
|
// Change the interval from PHP
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( response.heartbeat_interval ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
newInterval = response.heartbeat_interval;
|
2013-05-15 19:17:51 -04:00
|
|
|
delete response.heartbeat_interval;
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
$document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] );
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// Do this last, can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'
|
|
|
|
if ( newInterval ) {
|
2013-12-02 19:45:10 -05:00
|
|
|
interval( newInterval );
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-03-30 19:32:12 -04:00
|
|
|
}).fail( function( jqXHR, textStatus, error ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
setErrorState( textStatus || 'unknown', jqXHR.status );
|
|
|
|
$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
|
2013-01-29 01:15:25 -05:00
|
|
|
});
|
2013-07-18 21:39:48 -04:00
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05: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 01:15:25 -05:00
|
|
|
|
2013-11-26 20:56:10 -05:00
|
|
|
if ( settings.suspend ) {
|
2013-01-29 01:15:25 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05: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 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( settings.countdown < 1 ) {
|
|
|
|
settings.tempInterval = 0;
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
window.clearTimeout( settings.beatTimer );
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( delta < interval ) {
|
|
|
|
settings.beatTimer = window.setTimeout(
|
|
|
|
function() {
|
2013-01-29 01:15:25 -05:00
|
|
|
connect();
|
|
|
|
},
|
2013-11-14 13:41:10 -05:00
|
|
|
interval - delta
|
2013-01-29 01:15:25 -05:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
connect();
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Set the internal state when the browser window looses focus
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 02:03:27 -05:00
|
|
|
function blurred() {
|
2013-11-14 13:41:10 -05:00
|
|
|
clearFocusTimers();
|
|
|
|
settings.hasFocus = false;
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Set the internal state when the browser window is focused
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 02:03:27 -05:00
|
|
|
function focused() {
|
2013-11-14 13:41:10 -05:00
|
|
|
clearFocusTimers();
|
|
|
|
settings.userActivity = time();
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-26 20:56:10 -05:00
|
|
|
// Resume if suspended
|
|
|
|
settings.suspend = false;
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! settings.hasFocus ) {
|
|
|
|
settings.hasFocus = true;
|
|
|
|
scheduleNextTick();
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Add focus/blur events to all local iframes
|
|
|
|
*
|
|
|
|
* Used to detect when focus is moved from the main window to an iframe
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setFrameFocusEvents() {
|
|
|
|
$('iframe').each( function( i, frame ) {
|
|
|
|
if ( ! isLocalFrame( frame ) ) {
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-06 02:10:04 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( $.data( frame, 'wp-heartbeat-focus' ) ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$.data( frame, 'wp-heartbeat-focus', 1 );
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
$( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function() {
|
2013-02-03 02:03:27 -05:00
|
|
|
focused();
|
2013-11-14 13:41:10 -05:00
|
|
|
}).on('blur.wp-heartbeat-focus', function() {
|
|
|
|
setFrameFocusEvents();
|
|
|
|
// We don't know why 'blur' was fired. Either the user clicked in the main window or outside the browser.
|
|
|
|
// Running blurred() after some timeout lets us cancel it if the user clicked in the main window.
|
|
|
|
settings.frameBlurTimer = window.setTimeout( function(){ blurred(); }, 500 );
|
2013-02-03 02:03:27 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Remove the focus/blur events to all local iframes
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function removeFrameFocusEvents() {
|
2013-05-15 19:17:51 -04:00
|
|
|
$('iframe').each( function( i, frame ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! isLocalFrame( frame ) ) {
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-06 02:10:04 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$.removeData( frame, 'wp-heartbeat-focus' );
|
|
|
|
$( frame.contentWindow ).off( '.wp-heartbeat-focus' );
|
2013-02-03 02:03:27 -05:00
|
|
|
});
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Clear the reset timers for focus/blur events on the window and iframes
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function clearFocusTimers() {
|
|
|
|
window.clearTimeout( settings.winBlurTimer );
|
|
|
|
window.clearTimeout( settings.frameBlurTimer );
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
/**
|
|
|
|
* Runs when the user becomes active after a period of inactivity
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-02-03 02:03:27 -05:00
|
|
|
function userIsActive() {
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.userActivityEvents = false;
|
|
|
|
$document.off( '.wp-heartbeat-active' );
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$('iframe').each( function( i, frame ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! isLocalFrame( frame ) ) {
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-06 02:10:04 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$( frame.contentWindow ).off( '.wp-heartbeat-active' );
|
2013-02-03 02:03:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05: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 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( lastActive > 300000 && settings.hasFocus ) {
|
|
|
|
// Throttle down when no mouse or keyboard activity for 5 min
|
|
|
|
blurred();
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-26 20:56:10 -05:00
|
|
|
if ( settings.suspendEnabled && lastActive > 1200000 ) {
|
|
|
|
// Suspend after 20 min. of inactivity
|
|
|
|
settings.suspend = true;
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! settings.userActivityEvents ) {
|
|
|
|
$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$('iframe').each( function( i, frame ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( ! isLocalFrame( frame ) ) {
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-06 02:10:04 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
|
2013-02-03 02:03:27 -05:00
|
|
|
});
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.userActivityEvents = true;
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05: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 02:03:27 -05:00
|
|
|
|
2013-11-26 20:56:10 -05: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 02:03:27 -05:00
|
|
|
/**
|
|
|
|
* Get/Set the interval
|
|
|
|
*
|
2013-11-14 13:41:10 -05: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-30 19:32:12 -04:00
|
|
|
* If the window doesn't have focus, the interval slows down to 2 min.
|
2013-02-03 02:03:27 -05:00
|
|
|
*
|
2013-11-14 13:41:10 -05:00
|
|
|
* @param mixed speed Interval: 'fast' or 5, 15, 30, 60
|
|
|
|
* @param string ticks Used with speed = 'fast' or 5, how many ticks before the interval reverts back
|
2013-02-03 02:03:27 -05:00
|
|
|
* @return int Current interval in seconds
|
|
|
|
*/
|
2013-12-02 19:45:10 -05:00
|
|
|
function interval( speed, ticks ) {
|
2013-12-04 14:24:09 -05:00
|
|
|
var newInterval,
|
|
|
|
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;
|
2013-02-03 02:03:27 -05:00
|
|
|
|
|
|
|
if ( speed ) {
|
|
|
|
switch ( speed ) {
|
|
|
|
case 'fast':
|
2013-11-14 13:41:10 -05:00
|
|
|
case 5:
|
2013-12-04 14:24:09 -05:00
|
|
|
newInterval = 5000;
|
2013-11-14 13:41:10 -05:00
|
|
|
break;
|
|
|
|
case 15:
|
2013-12-04 14:24:09 -05:00
|
|
|
newInterval = 15000;
|
2013-11-14 13:41:10 -05:00
|
|
|
break;
|
|
|
|
case 30:
|
2013-12-04 14:24:09 -05:00
|
|
|
newInterval = 30000;
|
2013-02-03 02:03:27 -05:00
|
|
|
break;
|
2013-11-14 13:41:10 -05:00
|
|
|
case 60:
|
2013-12-04 14:24:09 -05:00
|
|
|
newInterval = 60000;
|
2013-02-03 02:03:27 -05:00
|
|
|
break;
|
|
|
|
case 'long-polling':
|
|
|
|
// Allow long polling, (experimental)
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.mainInterval = 0;
|
2013-02-03 02:03:27 -05:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-12-04 14:24:09 -05:00
|
|
|
newInterval = settings.originalInterval;
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
|
2013-12-04 14:24:09 -05:00
|
|
|
if ( 5000 === newInterval ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
ticks = parseInt( ticks, 10 ) || 30;
|
|
|
|
ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.countdown = ticks;
|
2013-12-04 14:24:09 -05:00
|
|
|
settings.tempInterval = newInterval;
|
2013-02-03 02:03:27 -05:00
|
|
|
} else {
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.countdown = 0;
|
|
|
|
settings.tempInterval = 0;
|
2013-12-04 14:24:09 -05:00
|
|
|
settings.mainInterval = newInterval;
|
2013-02-03 02:03:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-14 13:41:10 -05: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 14:24:09 -05:00
|
|
|
if ( newInterval !== oldInterval ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
scheduleNextTick();
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000;
|
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-02-03 02:03:27 -05:00
|
|
|
/**
|
2013-03-30 19:32:12 -04:00
|
|
|
* Enqueue data to send with the next XHR
|
2013-02-03 02:03:27 -05:00
|
|
|
*
|
2013-11-14 13:41:10 -05:00
|
|
|
* As the data is send asynchronously, this function doesn't return the XHR response.
|
2013-02-03 02:03:27 -05:00
|
|
|
* To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
|
2013-05-15 19:17:51 -04:00
|
|
|
* $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
* // code
|
|
|
|
* });
|
2013-05-15 19:17:51 -04:00
|
|
|
* If the same 'handle' is used more than once, the data is not overwritten when the third argument is 'true'.
|
2013-02-03 02:03:27 -05:00
|
|
|
* Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
|
|
|
|
*
|
2013-03-30 19:32:12 -04: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 13:41:10 -05:00
|
|
|
* $param bool noOverwrite Whether to overwrite existing data in the queue.
|
2013-03-30 19:32:12 -04:00
|
|
|
* $return bool Whether the data was queued or not.
|
2013-02-03 02:03:27 -05:00
|
|
|
*/
|
2013-11-14 13:41:10 -05:00
|
|
|
function enqueue( handle, data, noOverwrite ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
if ( handle ) {
|
2013-11-14 13:41:10 -05:00
|
|
|
if ( noOverwrite && this.isQueued( handle ) ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
return false;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
settings.queue[handle] = data;
|
2013-02-03 02:03:27 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-11-14 13:41:10 -05:00
|
|
|
}
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-02-03 02:03:27 -05:00
|
|
|
/**
|
|
|
|
* Check if data with a particular handle is queued
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
2013-08-21 18:55:09 -04:00
|
|
|
* $return bool Whether some data is queued with this handle
|
2013-02-03 02:03:27 -05:00
|
|
|
*/
|
2013-11-14 13:41:10 -05:00
|
|
|
function isQueued( handle ) {
|
|
|
|
if ( handle ) {
|
|
|
|
return settings.queue.hasOwnProperty( handle );
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 18:55:09 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data with a particular handle from the queue
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
|
|
|
* $return void
|
|
|
|
*/
|
2013-11-14 13:41:10 -05:00
|
|
|
function dequeue( handle ) {
|
|
|
|
if ( handle ) {
|
|
|
|
delete settings.queue[handle];
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 18:55:09 -04: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 13:41:10 -05: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-26 20:56:10 -05:00
|
|
|
disableSuspend: disableSuspend,
|
2013-12-02 19:45:10 -05:00
|
|
|
interval: interval,
|
2013-11-14 13:41:10 -05:00
|
|
|
hasConnectionError: hasConnectionError,
|
|
|
|
enqueue: enqueue,
|
|
|
|
dequeue: dequeue,
|
|
|
|
isQueued: isQueued,
|
|
|
|
getQueuedItem: getQueuedItem
|
2013-07-18 21:39:48 -04:00
|
|
|
};
|
|
|
|
};
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
// Ensure the global `wp` object exists.
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.heartbeat = new Heartbeat();
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-11-14 13:41:10 -05:00
|
|
|
}( jQuery, window ));
|