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
|
|
|
|
* the server every 15 seconds and triggers events (or callbacks) upon
|
|
|
|
* receiving data. Currently these 'ticks' handle transports for post locking,
|
|
|
|
* login-expiration warnings, and related tasks while a user is logged in.
|
|
|
|
*
|
|
|
|
* Available filters in ajax-actions.php:
|
|
|
|
* - heartbeat_received
|
|
|
|
* - heartbeat_send
|
|
|
|
* - heartbeat_tick
|
|
|
|
* - heartbeat_nopriv_received
|
|
|
|
* - heartbeat_nopriv_send
|
|
|
|
* - heartbeat_nopriv_tick
|
|
|
|
* @see wp_ajax_nopriv_heartbeat(), wp_ajax_heartbeat()
|
|
|
|
*
|
|
|
|
* @since 3.6.0
|
2013-01-29 01:15:25 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Ensure the global `wp` object exists.
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function($){
|
|
|
|
var Heartbeat = function() {
|
|
|
|
var self = this,
|
|
|
|
running,
|
2013-02-03 02:03:27 -05:00
|
|
|
beat,
|
2013-06-04 20:13:40 -04:00
|
|
|
screenId = typeof pagenow != 'undefined' ? pagenow : '',
|
2013-03-30 19:32:12 -04:00
|
|
|
url = typeof ajaxurl != 'undefined' ? ajaxurl : '',
|
2013-01-29 01:15:25 -05:00
|
|
|
settings,
|
|
|
|
tick = 0,
|
|
|
|
queue = {},
|
|
|
|
interval,
|
2013-02-03 02:03:27 -05:00
|
|
|
connecting,
|
2013-03-30 19:32:12 -04:00
|
|
|
countdown = 0,
|
|
|
|
errorcount = 0,
|
2013-02-03 02:03:27 -05:00
|
|
|
tempInterval,
|
|
|
|
hasFocus = true,
|
|
|
|
isUserActive,
|
|
|
|
userActiveEvents,
|
|
|
|
winBlurTimeout,
|
2013-07-18 21:39:48 -04:00
|
|
|
frameBlurTimeout = -1,
|
2013-10-24 18:53:14 -04:00
|
|
|
hasConnectionError = false;
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-07-18 21:39:48 -04:00
|
|
|
/**
|
|
|
|
* Returns a boolean that's indicative of whether or not there is a connection error
|
|
|
|
*
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
|
|
|
this.hasConnectionError = function() {
|
2013-10-24 18:53:14 -04:00
|
|
|
return hasConnectionError;
|
2013-07-18 21:39:48 -04:00
|
|
|
};
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-06-28 21:31:44 -04:00
|
|
|
if ( typeof( window.heartbeatSettings ) == 'object' ) {
|
|
|
|
settings = $.extend( {}, window.heartbeatSettings );
|
2013-01-29 01:15:25 -05:00
|
|
|
|
|
|
|
// Add private vars
|
2013-03-30 19:32:12 -04:00
|
|
|
url = settings.ajaxurl || url;
|
|
|
|
delete settings.ajaxurl;
|
2013-06-28 21:31:44 -04:00
|
|
|
delete settings.nonce;
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-02-03 02:03:27 -05:00
|
|
|
interval = settings.interval || 15; // default interval
|
2013-01-29 01:15:25 -05:00
|
|
|
delete settings.interval;
|
2013-06-04 20:13:40 -04:00
|
|
|
// The interval can be from 15 to 60 sec. and can be set temporarily to 5 sec.
|
|
|
|
if ( interval < 15 )
|
|
|
|
interval = 15;
|
2013-02-03 02:03:27 -05:00
|
|
|
else if ( interval > 60 )
|
|
|
|
interval = 60;
|
|
|
|
|
|
|
|
interval = interval * 1000;
|
|
|
|
|
2013-06-04 20:13:40 -04:00
|
|
|
// 'screenId' can be added from settings on the front-end where the JS global 'pagenow' is not set
|
|
|
|
screenId = screenId || settings.screenId || 'front';
|
|
|
|
delete settings.screenId;
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-02-03 02:03:27 -05:00
|
|
|
// Add or overwrite public vars
|
2013-01-29 01:15:25 -05:00
|
|
|
$.extend( this, settings );
|
|
|
|
}
|
|
|
|
|
|
|
|
function time(s) {
|
|
|
|
if ( s )
|
|
|
|
return parseInt( (new Date()).getTime() / 1000 );
|
|
|
|
|
|
|
|
return (new Date()).getTime();
|
|
|
|
}
|
|
|
|
|
2013-05-29 20:08:53 -04:00
|
|
|
function isLocalFrame( frame ) {
|
|
|
|
var origin, src = frame.src;
|
|
|
|
|
|
|
|
if ( src && /^https?:\/\//.test( src ) ) {
|
|
|
|
origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host;
|
|
|
|
|
|
|
|
if ( src.indexOf( origin ) !== 0 )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-06 02:10:04 -05:00
|
|
|
try {
|
|
|
|
if ( frame.contentWindow.document )
|
|
|
|
return true;
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:13:40 -04:00
|
|
|
// Set error state and fire an event on XHR errors or timeout
|
2013-10-24 18:53:14 -04:00
|
|
|
function errorstate( error ) {
|
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 'parsererror':
|
|
|
|
case 'error':
|
|
|
|
case 'empty':
|
|
|
|
case 'unknown':
|
|
|
|
errorcount++;
|
|
|
|
|
|
|
|
if ( errorcount > 2 )
|
|
|
|
trigger = true;
|
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-07-18 21:39:48 -04:00
|
|
|
if ( trigger && ! self.hasConnectionError() ) {
|
|
|
|
hasConnectionError = true;
|
2013-10-24 18:53:14 -04:00
|
|
|
$(document).trigger( 'heartbeat-connection-lost', [error] );
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
2013-07-18 21:39:48 -04:00
|
|
|
} else if ( self.hasConnectionError() ) {
|
2013-03-30 19:32:12 -04:00
|
|
|
errorcount = 0;
|
2013-07-18 21:39:48 -04:00
|
|
|
hasConnectionError = false;
|
2013-03-30 19:32:12 -04:00
|
|
|
$(document).trigger( 'heartbeat-connection-restored' );
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
2013-06-28 21:31:44 -04:00
|
|
|
var send = {}, data, i, empty = true,
|
|
|
|
nonce = typeof window.heartbeatSettings == 'object' ? window.heartbeatSettings.nonce : '';
|
2013-01-29 01:15:25 -05:00
|
|
|
tick = time();
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
data = $.extend( {}, queue );
|
2013-03-30 19:32:12 -04:00
|
|
|
// Clear the data queue, anything added after this point will be send on the next tick
|
|
|
|
queue = {};
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$(document).trigger( 'heartbeat-send', [data] );
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
for ( i in data ) {
|
|
|
|
if ( data.hasOwnProperty( i ) ) {
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing to send (nothing is expecting a response),
|
|
|
|
// schedule the next tick and bail
|
2013-07-18 21:39:48 -04:00
|
|
|
if ( empty && ! self.hasConnectionError() ) {
|
2013-05-15 19:17:51 -04:00
|
|
|
connecting = false;
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
send.data = data;
|
|
|
|
send.interval = interval / 1000;
|
|
|
|
send._nonce = nonce;
|
|
|
|
send.action = 'heartbeat';
|
2013-06-04 20:13:40 -04:00
|
|
|
send.screen_id = screenId;
|
2013-05-15 19:17:51 -04:00
|
|
|
send.has_focus = hasFocus;
|
2013-02-03 02:03:27 -05:00
|
|
|
|
|
|
|
connecting = true;
|
2013-03-30 19:32:12 -04:00
|
|
|
self.xhr = $.ajax({
|
|
|
|
url: url,
|
|
|
|
type: 'post',
|
2013-07-13 14:16:57 -04:00
|
|
|
timeout: 30000, // throw an error if not completed after 30 sec.
|
2013-05-15 19:17:51 -04:00
|
|
|
data: send,
|
2013-03-30 19:32:12 -04:00
|
|
|
dataType: 'json'
|
2013-05-15 19:17:51 -04:00
|
|
|
}).done( function( response, textStatus, jqXHR ) {
|
2013-05-29 20:08:53 -04:00
|
|
|
var new_interval;
|
2013-03-30 19:32:12 -04:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! response )
|
2013-03-30 19:32:12 -04:00
|
|
|
return errorstate( 'empty' );
|
2013-01-29 01:15:25 -05:00
|
|
|
|
|
|
|
// Clear error state
|
2013-07-18 21:39:48 -04:00
|
|
|
if ( self.hasConnectionError() )
|
2013-01-29 01:15:25 -05:00
|
|
|
errorstate();
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-06-28 21:31:44 -04:00
|
|
|
if ( response.nonces_expired ) {
|
|
|
|
$(document).trigger( 'heartbeat-nonces-expired' );
|
|
|
|
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 ) {
|
|
|
|
new_interval = response.heartbeat_interval;
|
|
|
|
delete response.heartbeat_interval;
|
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
self.tick( response, textStatus, jqXHR );
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-03-30 19:32:12 -04:00
|
|
|
// do this last, can trigger the next XHR if connection time > 5 sec. and new_interval == 'fast'
|
|
|
|
if ( new_interval )
|
|
|
|
self.interval.call( self, new_interval );
|
|
|
|
}).always( function() {
|
2013-02-03 02:03:27 -05:00
|
|
|
connecting = false;
|
2013-01-29 01:15:25 -05:00
|
|
|
next();
|
2013-03-30 19:32:12 -04:00
|
|
|
}).fail( function( jqXHR, textStatus, error ) {
|
2013-10-24 18:53:14 -04:00
|
|
|
errorstate( textStatus || 'unknown' );
|
2013-02-03 02:03:27 -05:00
|
|
|
self.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
|
|
|
|
|
|
|
function next() {
|
2013-02-03 02:03:27 -05:00
|
|
|
var delta = time() - tick, t = interval;
|
2013-01-29 01:15:25 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! running )
|
2013-01-29 01:15:25 -05:00
|
|
|
return;
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! hasFocus ) {
|
2013-07-30 23:23:22 -04:00
|
|
|
t = 100000; // 100 sec. Post locks expire after 120 sec.
|
2013-03-30 19:32:12 -04:00
|
|
|
} else if ( countdown > 0 && tempInterval ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
t = tempInterval;
|
|
|
|
countdown--;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.clearTimeout(beat);
|
|
|
|
|
|
|
|
if ( delta < t ) {
|
|
|
|
beat = window.setTimeout(
|
2013-01-29 01:15:25 -05:00
|
|
|
function(){
|
|
|
|
if ( running )
|
|
|
|
connect();
|
|
|
|
},
|
2013-02-03 02:03:27 -05:00
|
|
|
t - 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-02-03 02:03:27 -05:00
|
|
|
function blurred() {
|
|
|
|
window.clearTimeout(winBlurTimeout);
|
|
|
|
window.clearTimeout(frameBlurTimeout);
|
|
|
|
winBlurTimeout = frameBlurTimeout = 0;
|
|
|
|
|
|
|
|
hasFocus = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function focused() {
|
|
|
|
window.clearTimeout(winBlurTimeout);
|
|
|
|
window.clearTimeout(frameBlurTimeout);
|
|
|
|
winBlurTimeout = frameBlurTimeout = 0;
|
|
|
|
|
|
|
|
isUserActive = time();
|
|
|
|
|
|
|
|
if ( hasFocus )
|
|
|
|
return;
|
|
|
|
|
|
|
|
hasFocus = true;
|
|
|
|
window.clearTimeout(beat);
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! connecting )
|
2013-02-03 02:03:27 -05:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setFrameEvents() {
|
2013-05-15 19:17:51 -04:00
|
|
|
$('iframe').each( function( i, frame ){
|
|
|
|
if ( ! isLocalFrame( frame ) )
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( $.data( frame, 'wp-heartbeat-focus' ) )
|
2013-02-03 02:03:27 -05:00
|
|
|
return;
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$.data( frame, 'wp-heartbeat-focus', 1 );
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function(e) {
|
2013-02-03 02:03:27 -05:00
|
|
|
focused();
|
2013-05-15 19:17:51 -04:00
|
|
|
}).on('blur.wp-heartbeat-focus', function(e) {
|
2013-02-03 02:03:27 -05:00
|
|
|
setFrameEvents();
|
|
|
|
frameBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$(window).on( 'blur.wp-heartbeat-focus', function(e) {
|
2013-02-03 02:03:27 -05:00
|
|
|
setFrameEvents();
|
|
|
|
winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
|
2013-05-15 19:17:51 -04:00
|
|
|
}).on( 'focus.wp-heartbeat-focus', function() {
|
|
|
|
$('iframe').each( function( i, frame ) {
|
|
|
|
if ( !isLocalFrame( frame ) )
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
|
|
|
});
|
|
|
|
|
|
|
|
function userIsActive() {
|
|
|
|
userActiveEvents = false;
|
2013-05-15 19:17:51 -04:00
|
|
|
$(document).off( '.wp-heartbeat-active' );
|
|
|
|
$('iframe').each( function( i, frame ) {
|
|
|
|
if ( ! isLocalFrame( frame ) )
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
$( frame.contentWindow ).off( '.wp-heartbeat-active' );
|
2013-02-03 02:03:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
focused();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Throttle down when no mouse or keyboard activity for 5 min
|
|
|
|
if ( lastActive > 300000 && hasFocus )
|
|
|
|
blurred();
|
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! userActiveEvents ) {
|
|
|
|
$(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 ) {
|
|
|
|
if ( ! isLocalFrame( frame ) )
|
2013-02-06 02:10:04 -05:00
|
|
|
return;
|
|
|
|
|
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-02-03 02:03:27 -05:00
|
|
|
userActiveEvents = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for user activity every 30 seconds.
|
|
|
|
window.setInterval( function(){ checkUserActive(); }, 30000 );
|
2013-07-18 21:39:48 -04:00
|
|
|
$(document).ready( function() {
|
|
|
|
// Start one tick (15 sec) after DOM ready
|
|
|
|
running = true;
|
|
|
|
tick = time();
|
|
|
|
next();
|
|
|
|
});
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-03-30 19:32:12 -04:00
|
|
|
this.hasFocus = function() {
|
2013-02-03 02:03:27 -05:00
|
|
|
return hasFocus;
|
2013-07-18 21:39:48 -04:00
|
|
|
};
|
2013-02-03 02:03:27 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/Set the interval
|
|
|
|
*
|
2013-03-30 19:32:12 -04:00
|
|
|
* When setting to 'fast', the interval is 5 sec. for the next 30 ticks (for 2 min and 30 sec).
|
|
|
|
* If the window doesn't have focus, the interval slows down to 2 min.
|
2013-02-03 02:03:27 -05:00
|
|
|
*
|
|
|
|
* @param string speed Interval speed: 'fast' (5sec), 'standard' (15sec) default, 'slow' (60sec)
|
2013-06-28 21:31:44 -04:00
|
|
|
* @param string ticks Used with speed = 'fast', how many ticks before the speed reverts back
|
2013-02-03 02:03:27 -05:00
|
|
|
* @return int Current interval in seconds
|
|
|
|
*/
|
2013-06-28 21:31:44 -04:00
|
|
|
this.interval = function( speed, ticks ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
var reset, seconds;
|
2013-06-28 21:31:44 -04:00
|
|
|
ticks = parseInt( ticks, 10 ) || 30;
|
|
|
|
ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
|
2013-02-03 02:03:27 -05:00
|
|
|
|
|
|
|
if ( speed ) {
|
|
|
|
switch ( speed ) {
|
|
|
|
case 'fast':
|
|
|
|
seconds = 5;
|
2013-06-28 21:31:44 -04:00
|
|
|
countdown = ticks;
|
2013-02-03 02:03:27 -05:00
|
|
|
break;
|
|
|
|
case 'slow':
|
|
|
|
seconds = 60;
|
2013-03-30 19:32:12 -04:00
|
|
|
countdown = 0;
|
2013-02-03 02:03:27 -05:00
|
|
|
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;
|
|
|
|
|
2013-03-30 19:32:12 -04:00
|
|
|
if ( countdown > 0 ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
tempInterval = seconds * 1000;
|
|
|
|
} else {
|
|
|
|
interval = seconds * 1000;
|
|
|
|
tempInterval = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( reset )
|
|
|
|
next();
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
2013-02-03 02:03:27 -05:00
|
|
|
|
2013-05-15 19:17:51 -04:00
|
|
|
if ( ! hasFocus )
|
2013-02-03 02:03:27 -05:00
|
|
|
return 120;
|
|
|
|
|
|
|
|
return tempInterval ? tempInterval / 1000 : interval / 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
|
|
|
*
|
|
|
|
* 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:
|
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.
|
|
|
|
* $param bool dont_overwrite Whether to overwrite existing data in the queue.
|
|
|
|
* $return bool Whether the data was queued or not.
|
2013-02-03 02:03:27 -05:00
|
|
|
*/
|
2013-03-30 19:32:12 -04:00
|
|
|
this.enqueue = function( handle, data, dont_overwrite ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
if ( handle ) {
|
2013-08-21 18:55:09 -04:00
|
|
|
if ( dont_overwrite && this.isQueued( handle ) )
|
2013-02-03 02:03:27 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
queue[handle] = data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-07-18 21:39:48 -04: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-03-30 19:32:12 -04:00
|
|
|
this.isQueued = function( handle ) {
|
2013-08-21 18:55:09 -04:00
|
|
|
if ( handle )
|
|
|
|
return queue.hasOwnProperty( handle );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data with a particular handle from the queue
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
|
|
|
* $return void
|
|
|
|
*/
|
|
|
|
this.dequeue = function( handle ) {
|
|
|
|
if ( handle )
|
|
|
|
delete queue[handle];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data that was enqueued with a particular handle
|
|
|
|
*
|
|
|
|
* $param string handle The handle for the data
|
|
|
|
* $return mixed The data or undefined
|
|
|
|
*/
|
|
|
|
this.getQueuedItem = function( handle ) {
|
|
|
|
if ( handle )
|
|
|
|
return this.isQueued( handle ) ? queue[handle] : undefined;
|
2013-07-18 21:39:48 -04:00
|
|
|
};
|
|
|
|
};
|
2013-01-29 01:15:25 -05:00
|
|
|
|
|
|
|
$.extend( Heartbeat.prototype, {
|
2013-03-30 19:32:12 -04:00
|
|
|
tick: function( data, textStatus, jqXHR ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
$(document).trigger( 'heartbeat-tick', [data, textStatus, jqXHR] );
|
2013-01-29 01:15:25 -05:00
|
|
|
},
|
2013-03-30 19:32:12 -04:00
|
|
|
error: function( jqXHR, textStatus, error ) {
|
2013-02-03 02:03:27 -05:00
|
|
|
$(document).trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
|
2013-01-29 01:15:25 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
wp.heartbeat = new Heartbeat();
|
|
|
|
|
|
|
|
}(jQuery));
|