2013-01-29 07:15:25 +01:00
|
|
|
/**
|
|
|
|
* Heartbeat API
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Ensure the global `wp` object exists.
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function($){
|
|
|
|
var Heartbeat = function() {
|
|
|
|
var self = this,
|
|
|
|
running,
|
2013-02-03 08:03:27 +01:00
|
|
|
beat,
|
2013-01-29 07:15:25 +01:00
|
|
|
nonce,
|
2013-02-03 08:03:27 +01:00
|
|
|
screenid = typeof pagenow != 'undefined' ? pagenow : '',
|
2013-01-29 07:15:25 +01:00
|
|
|
settings,
|
|
|
|
tick = 0,
|
|
|
|
queue = {},
|
|
|
|
interval,
|
2013-02-03 08:03:27 +01:00
|
|
|
lastconnect = 0,
|
|
|
|
connecting,
|
|
|
|
countdown,
|
|
|
|
tempInterval,
|
|
|
|
hasFocus = true,
|
|
|
|
isUserActive,
|
|
|
|
userActiveEvents,
|
|
|
|
winBlurTimeout,
|
|
|
|
frameBlurTimeout = -1;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
|
|
|
this.url = typeof ajaxurl != 'undefined' ? ajaxurl : 'wp-admin/admin-ajax.php';
|
|
|
|
this.autostart = true;
|
|
|
|
|
|
|
|
if ( typeof( window.heartbeatSettings != 'undefined' ) ) {
|
|
|
|
settings = $.extend( {}, window.heartbeatSettings );
|
2013-02-06 08:10:04 +01:00
|
|
|
window.heartbeatSettings = null;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
|
|
|
// Add private vars
|
|
|
|
nonce = settings.nonce || '';
|
|
|
|
delete settings.nonce;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
interval = settings.interval || 15; // default interval
|
2013-01-29 07:15:25 +01:00
|
|
|
delete settings.interval;
|
2013-02-03 08:03:27 +01:00
|
|
|
// The interval can be from 5 to 60 sec.
|
|
|
|
if ( interval < 5 )
|
|
|
|
interval = 5;
|
|
|
|
else if ( interval > 60 )
|
|
|
|
interval = 60;
|
|
|
|
|
|
|
|
interval = interval * 1000;
|
|
|
|
|
2013-01-29 07:15:25 +01:00
|
|
|
// todo: needed?
|
2013-02-03 08:03:27 +01:00
|
|
|
// 'screenid' can be added from settings on the front-end where the JS global 'pagenow' is not set
|
|
|
|
screenid = screenid || settings.screenid || 'site';
|
|
|
|
delete settings.screenid;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// Add or overwrite public vars
|
2013-01-29 07:15:25 +01:00
|
|
|
$.extend( this, settings );
|
|
|
|
}
|
|
|
|
|
|
|
|
function time(s) {
|
|
|
|
if ( s )
|
|
|
|
return parseInt( (new Date()).getTime() / 1000 );
|
|
|
|
|
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
2013-02-06 08:10:04 +01:00
|
|
|
function isLocalFrame(frame) {
|
|
|
|
try {
|
|
|
|
if ( frame.contentWindow.document )
|
|
|
|
return true;
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// Set error state and fire an event if errors persist for over 2 min when the window has focus
|
|
|
|
// or 6 min when the window is in the background
|
2013-01-29 07:15:25 +01:00
|
|
|
function errorstate() {
|
|
|
|
var since;
|
|
|
|
|
|
|
|
if ( lastconnect ) {
|
2013-02-03 08:03:27 +01:00
|
|
|
since = time() - lastconnect, duration = hasFocus ? 120000 : 360000;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
if ( since > duration ) {
|
2013-01-29 07:15:25 +01:00
|
|
|
self.connectionLost = true;
|
|
|
|
$(document).trigger( 'heartbeat-connection-lost', parseInt(since / 1000) );
|
|
|
|
} else if ( self.connectionLost ) {
|
|
|
|
self.connectionLost = false;
|
|
|
|
$(document).trigger( 'heartbeat-connection-restored' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
|
|
|
var data = {};
|
|
|
|
tick = time();
|
|
|
|
|
|
|
|
data.data = $.extend( {}, queue );
|
2013-02-25 03:32:22 +01:00
|
|
|
$(document).trigger( 'heartbeat-send', [data.data] );
|
2013-01-29 07:15:25 +01:00
|
|
|
|
|
|
|
data.interval = interval / 1000;
|
|
|
|
data._nonce = nonce;
|
|
|
|
data.action = 'heartbeat';
|
2013-02-03 08:03:27 +01:00
|
|
|
data.screenid = screenid;
|
|
|
|
data.has_focus = hasFocus;
|
|
|
|
|
|
|
|
connecting = true;
|
|
|
|
self.xhr = $.post( self.url, data, 'json' )
|
|
|
|
.done( function( data, textStatus, jqXHR ) {
|
|
|
|
var interval;
|
|
|
|
|
|
|
|
// Clear the data queue
|
|
|
|
queue = {};
|
2013-01-29 07:15:25 +01:00
|
|
|
|
|
|
|
// Clear error state
|
2013-02-03 08:03:27 +01:00
|
|
|
lastconnect = time();
|
2013-01-29 07:15:25 +01:00
|
|
|
if ( self.connectionLost )
|
|
|
|
errorstate();
|
2013-02-03 08:03:27 +01:00
|
|
|
|
|
|
|
// Change the interval from PHP
|
|
|
|
interval = data.heartbeat_interval;
|
|
|
|
delete data.heartbeat_interval;
|
|
|
|
|
|
|
|
self.tick( data, textStatus, jqXHR );
|
|
|
|
|
|
|
|
// do this last, can trigger the next XHR
|
|
|
|
if ( interval )
|
|
|
|
self.interval.apply( self, data.heartbeat_interval );
|
|
|
|
}).always( function(){
|
|
|
|
connecting = false;
|
2013-01-29 07:15:25 +01:00
|
|
|
next();
|
2013-02-03 08:03:27 +01:00
|
|
|
}).fail( function( jqXHR, textStatus, error ){
|
2013-01-29 07:15:25 +01:00
|
|
|
errorstate();
|
2013-02-03 08:03:27 +01:00
|
|
|
self.error( jqXHR, textStatus, error );
|
2013-01-29 07:15:25 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function next() {
|
2013-02-03 08:03:27 +01:00
|
|
|
var delta = time() - tick, t = interval;
|
2013-01-29 07:15:25 +01:00
|
|
|
|
|
|
|
if ( !running )
|
|
|
|
return;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
if ( !hasFocus ) {
|
|
|
|
t = 120000; // 2 min
|
|
|
|
} else if ( countdown && tempInterval ) {
|
|
|
|
t = tempInterval;
|
|
|
|
countdown--;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.clearTimeout(beat);
|
|
|
|
|
|
|
|
if ( delta < t ) {
|
|
|
|
beat = window.setTimeout(
|
2013-01-29 07:15:25 +01:00
|
|
|
function(){
|
|
|
|
if ( running )
|
|
|
|
connect();
|
|
|
|
},
|
2013-02-03 08:03:27 +01:00
|
|
|
t - 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-02-03 08:03:27 +01:00
|
|
|
function blurred() {
|
|
|
|
window.clearTimeout(winBlurTimeout);
|
|
|
|
window.clearTimeout(frameBlurTimeout);
|
|
|
|
winBlurTimeout = frameBlurTimeout = 0;
|
|
|
|
|
|
|
|
hasFocus = false;
|
|
|
|
|
|
|
|
// temp debug
|
|
|
|
if ( self.debug )
|
|
|
|
console.log('### blurred(), slow down...')
|
|
|
|
}
|
|
|
|
|
|
|
|
function focused() {
|
|
|
|
window.clearTimeout(winBlurTimeout);
|
|
|
|
window.clearTimeout(frameBlurTimeout);
|
|
|
|
winBlurTimeout = frameBlurTimeout = 0;
|
|
|
|
|
|
|
|
isUserActive = time();
|
|
|
|
|
|
|
|
if ( hasFocus )
|
|
|
|
return;
|
|
|
|
|
|
|
|
hasFocus = true;
|
|
|
|
window.clearTimeout(beat);
|
|
|
|
|
|
|
|
if ( !connecting )
|
|
|
|
next();
|
|
|
|
|
|
|
|
// temp debug
|
|
|
|
if ( self.debug )
|
|
|
|
console.log('### focused(), speed up... ')
|
|
|
|
}
|
|
|
|
|
|
|
|
function setFrameEvents() {
|
|
|
|
$('iframe').each( function(i, frame){
|
2013-02-06 08:10:04 +01:00
|
|
|
if ( !isLocalFrame(frame) )
|
|
|
|
return;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
if ( $.data(frame, 'wp-heartbeat-focus') )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$.data(frame, 'wp-heartbeat-focus', 1);
|
|
|
|
|
|
|
|
$(frame.contentWindow).on('focus.wp-heartbeat-focus', function(e){
|
|
|
|
focused();
|
|
|
|
}).on('blur.wp-heartbeat-focus', function(e){
|
|
|
|
setFrameEvents();
|
|
|
|
frameBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(window).on('blur.wp-heartbeat-focus', function(e){
|
|
|
|
setFrameEvents();
|
|
|
|
winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
|
|
|
|
}).on('focus.wp-heartbeat-focus', function(){
|
|
|
|
$('iframe').each( function(i, frame){
|
2013-02-06 08:10:04 +01:00
|
|
|
if ( !isLocalFrame(frame) )
|
|
|
|
return;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
$.removeData(frame, 'wp-heartbeat-focus');
|
|
|
|
$(frame.contentWindow).off('.wp-heartbeat-focus');
|
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
|
|
|
});
|
|
|
|
|
|
|
|
function userIsActive() {
|
|
|
|
userActiveEvents = false;
|
|
|
|
$(document).off('.wp-heartbeat-active');
|
|
|
|
$('iframe').each( function(i, frame){
|
2013-02-06 08:10:04 +01:00
|
|
|
if ( !isLocalFrame(frame) )
|
|
|
|
return;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
$(frame.contentWindow).off('.wp-heartbeat-active');
|
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
2013-01-29 07:15:25 +01:00
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// temp debug
|
|
|
|
if ( self.debug )
|
|
|
|
console.log( 'userIsActive()' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set '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.
|
|
|
|
function checkUserActive() {
|
|
|
|
var lastActive = isUserActive ? time() - isUserActive : 0;
|
|
|
|
|
|
|
|
// temp debug
|
|
|
|
if ( self.debug )
|
|
|
|
console.log( 'checkUserActive(), lastActive = %s seconds ago', parseInt(lastActive / 1000) || 'null' );
|
|
|
|
|
|
|
|
// Throttle down when no mouse or keyboard activity for 5 min
|
|
|
|
if ( lastActive > 300000 && hasFocus )
|
|
|
|
blurred();
|
|
|
|
|
|
|
|
if ( !userActiveEvents ) {
|
|
|
|
$(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
|
|
|
|
$('iframe').each( function(i, frame){
|
2013-02-06 08:10:04 +01:00
|
|
|
if ( !isLocalFrame(frame) )
|
|
|
|
return;
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
$(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
|
|
|
|
});
|
|
|
|
userActiveEvents = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for user activity every 30 seconds.
|
|
|
|
window.setInterval( function(){ checkUserActive(); }, 30000 );
|
|
|
|
|
|
|
|
if ( this.autostart ) {
|
|
|
|
$(document).ready( function(){
|
|
|
|
// Start one tick (15 sec) after DOM ready
|
|
|
|
running = true;
|
|
|
|
tick = time();
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.winHasFocus = function() {
|
|
|
|
return hasFocus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/Set the interval
|
|
|
|
*
|
|
|
|
* When setting the interval to 'fast', the number of ticks is specified wiht the second argument, default 30.
|
|
|
|
* If the window doesn't have focus, the interval is overridden to 2 min. In this case setting the 'ticks'
|
|
|
|
* will start counting after the window gets focus.
|
|
|
|
*
|
|
|
|
* @param string speed Interval speed: 'fast' (5sec), 'standard' (15sec) default, 'slow' (60sec)
|
|
|
|
* @param int ticks Number of ticks for the changed interval, optional when setting 'standard' or 'slow'
|
|
|
|
* @return int Current interval in seconds
|
|
|
|
*/
|
|
|
|
this.interval = function(speed, ticks) {
|
|
|
|
var reset, seconds;
|
|
|
|
|
|
|
|
if ( speed ) {
|
|
|
|
switch ( speed ) {
|
|
|
|
case 'fast':
|
|
|
|
seconds = 5;
|
|
|
|
countdown = parseInt(ticks) || 30;
|
|
|
|
break;
|
|
|
|
case 'slow':
|
|
|
|
seconds = 60;
|
|
|
|
countdown = parseInt(ticks) || 0;
|
|
|
|
break;
|
|
|
|
case 'long-polling':
|
|
|
|
// Allow long polling, (experimental)
|
|
|
|
interval = 0;
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
seconds = 15;
|
|
|
|
countdown = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset when the new interval value is lower than the current one
|
|
|
|
reset = seconds * 1000 < interval;
|
|
|
|
|
|
|
|
if ( countdown ) {
|
|
|
|
tempInterval = seconds * 1000;
|
|
|
|
} else {
|
|
|
|
interval = seconds * 1000;
|
|
|
|
tempInterval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( reset )
|
|
|
|
next();
|
2013-01-29 07:15:25 +01:00
|
|
|
}
|
2013-02-03 08:03:27 +01:00
|
|
|
|
|
|
|
if ( !hasFocus )
|
|
|
|
return 120;
|
|
|
|
|
|
|
|
return tempInterval ? tempInterval / 1000 : interval / 1000;
|
2013-01-29 07:15:25 +01:00
|
|
|
};
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// Start. Has no effect if heartbeat is already running
|
2013-01-29 07:15:25 +01:00
|
|
|
this.start = function() {
|
|
|
|
if ( running )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
running = true;
|
|
|
|
connect();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
// Stop. If a XHR is in progress, abort it
|
2013-01-29 07:15:25 +01:00
|
|
|
this.stop = function() {
|
2013-02-03 08:03:27 +01:00
|
|
|
if ( self.xhr && self.xhr.readyState != 4 )
|
2013-01-29 07:15:25 +01:00
|
|
|
self.xhr.abort();
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-03 08:03:27 +01:00
|
|
|
/**
|
|
|
|
* Send data with the next XHR
|
|
|
|
*
|
|
|
|
* As the data is sent later, this function doesn't return the XHR response.
|
|
|
|
* To see the response, use the custom jQuery event 'heartbeat-tick' on the document, example:
|
|
|
|
* $(document).on('heartbeat-tick.myname', function(data, textStatus, jqXHR) {
|
|
|
|
* // code
|
|
|
|
* });
|
|
|
|
* If the same 'handle' is used more than once, the data is overwritten when the third argument is 'true'.
|
|
|
|
* Use wp.heartbeat.isQueued('handle') to see if any data is already queued for that handle.
|
|
|
|
*
|
|
|
|
* $param string handle Unique handle for the data. The handle is used in PHP to receive the data
|
|
|
|
* $param mixed data The data to be sent
|
|
|
|
* $param bool overwrite Whether to overwrite existing data in the queue
|
|
|
|
* $return bool Whether the data was queued or not
|
|
|
|
*/
|
|
|
|
this.send = function(handle, data, overwrite) {
|
|
|
|
if ( handle ) {
|
|
|
|
if ( queue.hasOwnProperty(handle) && !overwrite )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
queue[handle] = data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
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
|
|
|
|
* $return mixed The data queued with that handle or null
|
|
|
|
*/
|
|
|
|
this.isQueued = function(handle) {
|
|
|
|
return queue[handle];
|
2013-01-29 07:15:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( Heartbeat.prototype, {
|
2013-02-03 08:03:27 +01:00
|
|
|
tick: function(data, textStatus, jqXHR) {
|
|
|
|
$(document).trigger( 'heartbeat-tick', [data, textStatus, jqXHR] );
|
2013-01-29 07:15:25 +01:00
|
|
|
},
|
2013-02-03 08:03:27 +01:00
|
|
|
error: function(jqXHR, textStatus, error) {
|
|
|
|
$(document).trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
|
2013-01-29 07:15:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
wp.heartbeat = new Heartbeat();
|
|
|
|
|
|
|
|
}(jQuery));
|