author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Heartbeat API |
|
3 |
* |
|
5 | 4 |
* Heartbeat is a simple server polling API that sends XHR requests to |
5 |
* the server every 15 - 60 seconds and triggers events (or callbacks) upon |
|
6 |
* receiving data. Currently these 'ticks' handle transports for post locking, |
|
7 |
* login-expiration warnings, autosave, and related tasks while a user is logged in. |
|
0 | 8 |
* |
5 | 9 |
* Available PHP filters (in ajax-actions.php): |
0 | 10 |
* - heartbeat_received |
11 |
* - heartbeat_send |
|
12 |
* - heartbeat_tick |
|
13 |
* - heartbeat_nopriv_received |
|
14 |
* - heartbeat_nopriv_send |
|
15 |
* - heartbeat_nopriv_tick |
|
16 |
* @see wp_ajax_nopriv_heartbeat(), wp_ajax_heartbeat() |
|
17 |
* |
|
5 | 18 |
* Custom jQuery events: |
19 |
* - heartbeat-send |
|
20 |
* - heartbeat-tick |
|
21 |
* - heartbeat-error |
|
22 |
* - heartbeat-connection-lost |
|
23 |
* - heartbeat-connection-restored |
|
24 |
* - heartbeat-nonces-expired |
|
25 |
* |
|
0 | 26 |
* @since 3.6.0 |
9 | 27 |
* @output wp-includes/js/heartbeat.js |
0 | 28 |
*/ |
29 |
||
5 | 30 |
( function( $, window, undefined ) { |
9 | 31 |
|
32 |
/** |
|
33 |
* Constructs the Heartbeat API. |
|
34 |
* |
|
35 |
* @since 3.6.0 |
|
36 |
* |
|
37 |
* @returns {Object} An instance of the Heartbeat class. |
|
38 |
* @constructor |
|
39 |
*/ |
|
0 | 40 |
var Heartbeat = function() { |
5 | 41 |
var $document = $(document), |
42 |
settings = { |
|
9 | 43 |
// Suspend/resume. |
5 | 44 |
suspend: false, |
45 |
||
9 | 46 |
// Whether suspending is enabled. |
5 | 47 |
suspendEnabled: true, |
48 |
||
9 | 49 |
// Current screen id, defaults to the JS global 'pagenow' when present |
50 |
// (in the admin) or 'front'. |
|
5 | 51 |
screenId: '', |
52 |
||
9 | 53 |
// XHR request URL, defaults to the JS global 'ajaxurl' when present. |
5 | 54 |
url: '', |
55 |
||
9 | 56 |
// Timestamp, start of the last connection request. |
5 | 57 |
lastTick: 0, |
58 |
||
9 | 59 |
// Container for the enqueued items. |
5 | 60 |
queue: {}, |
61 |
||
9 | 62 |
// Connect interval (in seconds). |
5 | 63 |
mainInterval: 60, |
64 |
||
9 | 65 |
// Used when the interval is set to 5 sec. temporarily. |
5 | 66 |
tempInterval: 0, |
67 |
||
9 | 68 |
// Used when the interval is reset. |
5 | 69 |
originalInterval: 0, |
70 |
||
71 |
// Used to limit the number of AJAX requests. |
|
72 |
minimalInterval: 0, |
|
73 |
||
9 | 74 |
// Used together with tempInterval. |
5 | 75 |
countdown: 0, |
76 |
||
9 | 77 |
// Whether a connection is currently in progress. |
5 | 78 |
connecting: false, |
79 |
||
9 | 80 |
// Whether a connection error occurred. |
5 | 81 |
connectionError: false, |
82 |
||
9 | 83 |
// Used to track non-critical errors. |
5 | 84 |
errorcount: 0, |
85 |
||
9 | 86 |
// Whether at least one connection has been completed successfully. |
5 | 87 |
hasConnected: false, |
88 |
||
9 | 89 |
// Whether the current browser window is in focus and the user is active. |
5 | 90 |
hasFocus: true, |
91 |
||
92 |
// Timestamp, last time the user was active. Checked every 30 sec. |
|
93 |
userActivity: 0, |
|
94 |
||
9 | 95 |
// Flag whether events tracking user activity were set. |
5 | 96 |
userActivityEvents: false, |
97 |
||
9 | 98 |
// Timer that keeps track of how long a user has focus. |
5 | 99 |
checkFocusTimer: 0, |
9 | 100 |
|
101 |
// Timer that keeps track of how long needs to be waited before connecting to |
|
102 |
// the server again. |
|
5 | 103 |
beatTimer: 0 |
104 |
}; |
|
0 | 105 |
|
106 |
/** |
|
9 | 107 |
* Sets local variables and events, then starts the heartbeat. |
0 | 108 |
* |
5 | 109 |
* @access private |
110 |
* |
|
9 | 111 |
* @since 3.8.0 |
112 |
* |
|
113 |
* @returns {void} |
|
0 | 114 |
*/ |
5 | 115 |
function initialize() { |
116 |
var options, hidden, visibilityState, visibilitychange; |
|
117 |
||
118 |
if ( typeof window.pagenow === 'string' ) { |
|
119 |
settings.screenId = window.pagenow; |
|
120 |
} |
|
121 |
||
122 |
if ( typeof window.ajaxurl === 'string' ) { |
|
123 |
settings.url = window.ajaxurl; |
|
124 |
} |
|
125 |
||
9 | 126 |
// Pull in options passed from PHP. |
5 | 127 |
if ( typeof window.heartbeatSettings === 'object' ) { |
128 |
options = window.heartbeatSettings; |
|
129 |
||
9 | 130 |
// The XHR URL can be passed as option when window.ajaxurl is not set. |
5 | 131 |
if ( ! settings.url && options.ajaxurl ) { |
132 |
settings.url = options.ajaxurl; |
|
133 |
} |
|
134 |
||
9 | 135 |
/* |
136 |
* The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec. |
|
137 |
* It can be set in the initial options or changed later through JS and/or |
|
138 |
* through PHP. |
|
139 |
*/ |
|
5 | 140 |
if ( options.interval ) { |
141 |
settings.mainInterval = options.interval; |
|
0 | 142 |
|
5 | 143 |
if ( settings.mainInterval < 15 ) { |
144 |
settings.mainInterval = 15; |
|
145 |
} else if ( settings.mainInterval > 120 ) { |
|
146 |
settings.mainInterval = 120; |
|
147 |
} |
|
148 |
} |
|
0 | 149 |
|
9 | 150 |
/* |
151 |
* Used to limit the number of AJAX requests. Overrides all other intervals if |
|
152 |
* they are shorter. Needed for some hosts that cannot handle frequent requests |
|
153 |
* and the user may exceed the allocated server CPU time, etc. The minimal |
|
154 |
* interval can be up to 600 sec. however setting it to longer than 120 sec. |
|
155 |
* will limit or disable some of the functionality (like post locks). Once set |
|
156 |
* at initialization, minimalInterval cannot be changed/overridden. |
|
157 |
*/ |
|
5 | 158 |
if ( options.minimalInterval ) { |
159 |
options.minimalInterval = parseInt( options.minimalInterval, 10 ); |
|
160 |
settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0; |
|
161 |
} |
|
162 |
||
163 |
if ( settings.minimalInterval && settings.mainInterval < settings.minimalInterval ) { |
|
164 |
settings.mainInterval = settings.minimalInterval; |
|
165 |
} |
|
166 |
||
9 | 167 |
// 'screenId' can be added from settings on the front end where the JS global |
168 |
// 'pagenow' is not set. |
|
5 | 169 |
if ( ! settings.screenId ) { |
170 |
settings.screenId = options.screenId || 'front'; |
|
171 |
} |
|
172 |
||
173 |
if ( options.suspension === 'disable' ) { |
|
174 |
settings.suspendEnabled = false; |
|
175 |
} |
|
176 |
} |
|
0 | 177 |
|
9 | 178 |
// Convert to milliseconds. |
5 | 179 |
settings.mainInterval = settings.mainInterval * 1000; |
180 |
settings.originalInterval = settings.mainInterval; |
|
181 |
||
9 | 182 |
/* |
183 |
* Switch the interval to 120 seconds by using the Page Visibility API. |
|
184 |
* If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the |
|
185 |
* interval will be increased to 120 seconds after 5 minutes of mouse and keyboard |
|
186 |
* inactivity. |
|
187 |
*/ |
|
5 | 188 |
if ( typeof document.hidden !== 'undefined' ) { |
189 |
hidden = 'hidden'; |
|
190 |
visibilitychange = 'visibilitychange'; |
|
191 |
visibilityState = 'visibilityState'; |
|
192 |
} else if ( typeof document.msHidden !== 'undefined' ) { // IE10 |
|
193 |
hidden = 'msHidden'; |
|
194 |
visibilitychange = 'msvisibilitychange'; |
|
195 |
visibilityState = 'msVisibilityState'; |
|
196 |
} else if ( typeof document.webkitHidden !== 'undefined' ) { // Android |
|
197 |
hidden = 'webkitHidden'; |
|
198 |
visibilitychange = 'webkitvisibilitychange'; |
|
199 |
visibilityState = 'webkitVisibilityState'; |
|
200 |
} |
|
201 |
||
202 |
if ( hidden ) { |
|
203 |
if ( document[hidden] ) { |
|
204 |
settings.hasFocus = false; |
|
205 |
} |
|
0 | 206 |
|
5 | 207 |
$document.on( visibilitychange + '.wp-heartbeat', function() { |
208 |
if ( document[visibilityState] === 'hidden' ) { |
|
209 |
blurred(); |
|
210 |
window.clearInterval( settings.checkFocusTimer ); |
|
211 |
} else { |
|
212 |
focused(); |
|
213 |
if ( document.hasFocus ) { |
|
214 |
settings.checkFocusTimer = window.setInterval( checkFocus, 10000 ); |
|
215 |
} |
|
216 |
} |
|
217 |
}); |
|
218 |
} |
|
219 |
||
220 |
// Use document.hasFocus() if available. |
|
221 |
if ( document.hasFocus ) { |
|
222 |
settings.checkFocusTimer = window.setInterval( checkFocus, 10000 ); |
|
223 |
} |
|
0 | 224 |
|
5 | 225 |
$(window).on( 'unload.wp-heartbeat', function() { |
9 | 226 |
// Don't connect anymore. |
5 | 227 |
settings.suspend = true; |
0 | 228 |
|
9 | 229 |
// Abort the last request if not completed. |
5 | 230 |
if ( settings.xhr && settings.xhr.readyState !== 4 ) { |
231 |
settings.xhr.abort(); |
|
232 |
} |
|
233 |
}); |
|
234 |
||
235 |
// Check for user activity every 30 seconds. |
|
236 |
window.setInterval( checkUserActivity, 30000 ); |
|
237 |
||
9 | 238 |
// Start one tick after DOM ready. |
5 | 239 |
$document.ready( function() { |
240 |
settings.lastTick = time(); |
|
241 |
scheduleNextTick(); |
|
242 |
}); |
|
0 | 243 |
} |
244 |
||
5 | 245 |
/** |
9 | 246 |
* Returns the current time according to the browser. |
5 | 247 |
* |
248 |
* @access private |
|
249 |
* |
|
9 | 250 |
* @since 3.6.0 |
251 |
* |
|
252 |
* @returns {number} Returns the current time. |
|
5 | 253 |
*/ |
254 |
function time() { |
|
0 | 255 |
return (new Date()).getTime(); |
256 |
} |
|
257 |
||
5 | 258 |
/** |
9 | 259 |
* Checks if the iframe is from the same origin. |
5 | 260 |
* |
261 |
* @access private |
|
262 |
* |
|
9 | 263 |
* @since 3.6.0 |
264 |
* |
|
265 |
* @returns {boolean} Returns whether or not the iframe is from the same origin. |
|
5 | 266 |
*/ |
0 | 267 |
function isLocalFrame( frame ) { |
268 |
var origin, src = frame.src; |
|
269 |
||
9 | 270 |
/* |
271 |
* Need to compare strings as WebKit doesn't throw JS errors when iframes have |
|
272 |
* different origin. It throws uncatchable exceptions. |
|
273 |
*/ |
|
0 | 274 |
if ( src && /^https?:\/\//.test( src ) ) { |
275 |
origin = window.location.origin ? window.location.origin : window.location.protocol + '//' + window.location.host; |
|
276 |
||
5 | 277 |
if ( src.indexOf( origin ) !== 0 ) { |
0 | 278 |
return false; |
5 | 279 |
} |
0 | 280 |
} |
281 |
||
282 |
try { |
|
5 | 283 |
if ( frame.contentWindow.document ) { |
0 | 284 |
return true; |
5 | 285 |
} |
0 | 286 |
} catch(e) {} |
287 |
||
288 |
return false; |
|
289 |
} |
|
290 |
||
5 | 291 |
/** |
9 | 292 |
* Checks if the document's focus has changed. |
5 | 293 |
* |
294 |
* @access private |
|
295 |
* |
|
9 | 296 |
* @since 4.1.0 |
297 |
* |
|
298 |
* @returns {void} |
|
5 | 299 |
*/ |
300 |
function checkFocus() { |
|
301 |
if ( settings.hasFocus && ! document.hasFocus() ) { |
|
302 |
blurred(); |
|
303 |
} else if ( ! settings.hasFocus && document.hasFocus() ) { |
|
304 |
focused(); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
/** |
|
9 | 309 |
* Sets error state and fires an event on XHR errors or timeout. |
5 | 310 |
* |
311 |
* @access private |
|
312 |
* |
|
9 | 313 |
* @since 3.8.0 |
314 |
* |
|
315 |
* @param {string} error The error type passed from the XHR. |
|
316 |
* @param {number} status The HTTP status code passed from jqXHR |
|
317 |
* (200, 404, 500, etc.). |
|
318 |
* |
|
319 |
* @returns {void} |
|
5 | 320 |
*/ |
321 |
function setErrorState( error, status ) { |
|
0 | 322 |
var trigger; |
323 |
||
324 |
if ( error ) { |
|
325 |
switch ( error ) { |
|
326 |
case 'abort': |
|
9 | 327 |
// Do nothing. |
0 | 328 |
break; |
329 |
case 'timeout': |
|
9 | 330 |
// No response for 30 sec. |
0 | 331 |
trigger = true; |
332 |
break; |
|
5 | 333 |
case 'error': |
334 |
if ( 503 === status && settings.hasConnected ) { |
|
335 |
trigger = true; |
|
336 |
break; |
|
337 |
} |
|
338 |
/* falls through */ |
|
0 | 339 |
case 'parsererror': |
340 |
case 'empty': |
|
341 |
case 'unknown': |
|
5 | 342 |
settings.errorcount++; |
0 | 343 |
|
5 | 344 |
if ( settings.errorcount > 2 && settings.hasConnected ) { |
0 | 345 |
trigger = true; |
5 | 346 |
} |
0 | 347 |
|
348 |
break; |
|
349 |
} |
|
350 |
||
5 | 351 |
if ( trigger && ! hasConnectionError() ) { |
352 |
settings.connectionError = true; |
|
353 |
$document.trigger( 'heartbeat-connection-lost', [error, status] ); |
|
9 | 354 |
wp.hooks.doAction( 'heartbeat.connection-lost', error, status ); |
0 | 355 |
} |
5 | 356 |
} |
357 |
} |
|
0 | 358 |
|
5 | 359 |
/** |
9 | 360 |
* Clears the error state and fires an event if there is a connection error. |
5 | 361 |
* |
362 |
* @access private |
|
363 |
* |
|
9 | 364 |
* @since 3.8.0 |
365 |
* |
|
366 |
* @returns {void} |
|
5 | 367 |
*/ |
368 |
function clearErrorState() { |
|
9 | 369 |
// Has connected successfully. |
5 | 370 |
settings.hasConnected = true; |
371 |
||
372 |
if ( hasConnectionError() ) { |
|
373 |
settings.errorcount = 0; |
|
374 |
settings.connectionError = false; |
|
375 |
$document.trigger( 'heartbeat-connection-restored' ); |
|
9 | 376 |
wp.hooks.doAction( 'heartbeat.connection-restored' ); |
0 | 377 |
} |
378 |
} |
|
379 |
||
5 | 380 |
/** |
9 | 381 |
* Gathers the data and connects to the server. |
5 | 382 |
* |
383 |
* @access private |
|
384 |
* |
|
9 | 385 |
* @since 3.6.0 |
386 |
* |
|
387 |
* @returns {void} |
|
5 | 388 |
*/ |
0 | 389 |
function connect() { |
5 | 390 |
var ajaxData, heartbeatData; |
0 | 391 |
|
5 | 392 |
// If the connection to the server is slower than the interval, |
393 |
// heartbeat connects as soon as the previous connection's response is received. |
|
394 |
if ( settings.connecting || settings.suspend ) { |
|
0 | 395 |
return; |
396 |
} |
|
397 |
||
5 | 398 |
settings.lastTick = time(); |
399 |
||
400 |
heartbeatData = $.extend( {}, settings.queue ); |
|
9 | 401 |
// Clear the data queue. Anything added after this point will be sent on the next tick. |
5 | 402 |
settings.queue = {}; |
403 |
||
404 |
$document.trigger( 'heartbeat-send', [ heartbeatData ] ); |
|
9 | 405 |
wp.hooks.doAction( 'heartbeat.send', heartbeatData ); |
0 | 406 |
|
5 | 407 |
ajaxData = { |
408 |
data: heartbeatData, |
|
409 |
interval: settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000, |
|
410 |
_nonce: typeof window.heartbeatSettings === 'object' ? window.heartbeatSettings.nonce : '', |
|
411 |
action: 'heartbeat', |
|
412 |
screen_id: settings.screenId, |
|
413 |
has_focus: settings.hasFocus |
|
414 |
}; |
|
415 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
if ( 'customize' === settings.screenId ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
ajaxData.wp_customize = 'on'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
|
5 | 420 |
settings.connecting = true; |
421 |
settings.xhr = $.ajax({ |
|
422 |
url: settings.url, |
|
0 | 423 |
type: 'post', |
424 |
timeout: 30000, // throw an error if not completed after 30 sec. |
|
5 | 425 |
data: ajaxData, |
0 | 426 |
dataType: 'json' |
5 | 427 |
}).always( function() { |
428 |
settings.connecting = false; |
|
429 |
scheduleNextTick(); |
|
0 | 430 |
}).done( function( response, textStatus, jqXHR ) { |
5 | 431 |
var newInterval; |
0 | 432 |
|
5 | 433 |
if ( ! response ) { |
434 |
setErrorState( 'empty' ); |
|
435 |
return; |
|
436 |
} |
|
0 | 437 |
|
5 | 438 |
clearErrorState(); |
0 | 439 |
|
440 |
if ( response.nonces_expired ) { |
|
5 | 441 |
$document.trigger( 'heartbeat-nonces-expired' ); |
9 | 442 |
wp.hooks.doAction( 'heartbeat.nonces-expired' ); |
0 | 443 |
} |
444 |
||
445 |
// Change the interval from PHP |
|
446 |
if ( response.heartbeat_interval ) { |
|
5 | 447 |
newInterval = response.heartbeat_interval; |
0 | 448 |
delete response.heartbeat_interval; |
449 |
} |
|
450 |
||
9 | 451 |
// Update the heartbeat nonce if set. |
452 |
if ( response.heartbeat_nonce && typeof window.heartbeatSettings === 'object' ) { |
|
453 |
window.heartbeatSettings.nonce = response.heartbeat_nonce; |
|
454 |
delete response.heartbeat_nonce; |
|
455 |
} |
|
0 | 456 |
|
9 | 457 |
// Update the Rest API nonce if set and wp-api loaded. |
458 |
if ( response.rest_nonce && typeof window.wpApiSettings === 'object' ) { |
|
459 |
window.wpApiSettings.nonce = response.rest_nonce; |
|
460 |
// This nonce is required for api-fetch through heartbeat.tick. |
|
461 |
// delete response.rest_nonce; |
|
462 |
} |
|
463 |
||
464 |
$document.trigger( 'heartbeat-tick', [response, textStatus, jqXHR] ); |
|
465 |
wp.hooks.doAction( 'heartbeat.tick', response, textStatus, jqXHR ); |
|
466 |
||
467 |
// Do this last. Can trigger the next XHR if connection time > 5 sec. and newInterval == 'fast'. |
|
5 | 468 |
if ( newInterval ) { |
469 |
interval( newInterval ); |
|
470 |
} |
|
0 | 471 |
}).fail( function( jqXHR, textStatus, error ) { |
5 | 472 |
setErrorState( textStatus || 'unknown', jqXHR.status ); |
473 |
$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] ); |
|
9 | 474 |
wp.hooks.doAction( 'heartbeat.error', jqXHR, textStatus, error ); |
0 | 475 |
}); |
476 |
} |
|
477 |
||
5 | 478 |
/** |
9 | 479 |
* Schedules the next connection. |
5 | 480 |
* |
481 |
* Fires immediately if the connection time is longer than the interval. |
|
482 |
* |
|
483 |
* @access private |
|
484 |
* |
|
9 | 485 |
* @since 3.8.0 |
486 |
* |
|
487 |
* @returns {void} |
|
5 | 488 |
*/ |
489 |
function scheduleNextTick() { |
|
490 |
var delta = time() - settings.lastTick, |
|
491 |
interval = settings.mainInterval; |
|
0 | 492 |
|
5 | 493 |
if ( settings.suspend ) { |
494 |
return; |
|
0 | 495 |
} |
496 |
||
5 | 497 |
if ( ! settings.hasFocus ) { |
498 |
interval = 120000; // 120 sec. Post locks expire after 150 sec. |
|
499 |
} else if ( settings.countdown > 0 && settings.tempInterval ) { |
|
500 |
interval = settings.tempInterval; |
|
501 |
settings.countdown--; |
|
502 |
||
503 |
if ( settings.countdown < 1 ) { |
|
504 |
settings.tempInterval = 0; |
|
505 |
} |
|
506 |
} |
|
0 | 507 |
|
5 | 508 |
if ( settings.minimalInterval && interval < settings.minimalInterval ) { |
509 |
interval = settings.minimalInterval; |
|
510 |
} |
|
511 |
||
512 |
window.clearTimeout( settings.beatTimer ); |
|
513 |
||
514 |
if ( delta < interval ) { |
|
515 |
settings.beatTimer = window.setTimeout( |
|
516 |
function() { |
|
517 |
connect(); |
|
0 | 518 |
}, |
5 | 519 |
interval - delta |
0 | 520 |
); |
521 |
} else { |
|
522 |
connect(); |
|
523 |
} |
|
524 |
} |
|
525 |
||
5 | 526 |
/** |
9 | 527 |
* Sets the internal state when the browser window becomes hidden or loses focus. |
5 | 528 |
* |
529 |
* @access private |
|
530 |
* |
|
9 | 531 |
* @since 3.6.0 |
532 |
* |
|
533 |
* @returns {void} |
|
5 | 534 |
*/ |
0 | 535 |
function blurred() { |
5 | 536 |
settings.hasFocus = false; |
0 | 537 |
} |
538 |
||
5 | 539 |
/** |
9 | 540 |
* Sets the internal state when the browser window becomes visible or is in focus. |
5 | 541 |
* |
542 |
* @access private |
|
543 |
* |
|
9 | 544 |
* @since 3.6.0 |
545 |
* |
|
546 |
* @returns {void} |
|
5 | 547 |
*/ |
548 |
function focused() { |
|
549 |
settings.userActivity = time(); |
|
0 | 550 |
|
5 | 551 |
// Resume if suspended |
552 |
settings.suspend = false; |
|
0 | 553 |
|
5 | 554 |
if ( ! settings.hasFocus ) { |
555 |
settings.hasFocus = true; |
|
556 |
scheduleNextTick(); |
|
557 |
} |
|
0 | 558 |
} |
559 |
||
5 | 560 |
/** |
9 | 561 |
* Runs when the user becomes active after a period of inactivity. |
5 | 562 |
* |
563 |
* @access private |
|
564 |
* |
|
9 | 565 |
* @since 3.6.0 |
566 |
* |
|
567 |
* @returns {void} |
|
5 | 568 |
*/ |
569 |
function userIsActive() { |
|
570 |
settings.userActivityEvents = false; |
|
571 |
$document.off( '.wp-heartbeat-active' ); |
|
0 | 572 |
|
573 |
$('iframe').each( function( i, frame ) { |
|
5 | 574 |
if ( isLocalFrame( frame ) ) { |
575 |
$( frame.contentWindow ).off( '.wp-heartbeat-active' ); |
|
576 |
} |
|
0 | 577 |
}); |
578 |
||
579 |
focused(); |
|
580 |
} |
|
581 |
||
5 | 582 |
/** |
9 | 583 |
* Checks for user activity. |
5 | 584 |
* |
9 | 585 |
* Runs every 30 sec. Sets 'hasFocus = true' if user is active and the window is |
586 |
* in the background. Sets 'hasFocus = false' if the user has been inactive |
|
587 |
* (no mouse or keyboard activity) for 5 min. even when the window has focus. |
|
5 | 588 |
* |
589 |
* @access private |
|
590 |
* |
|
9 | 591 |
* @since 3.8.0 |
592 |
* |
|
593 |
* @returns {void} |
|
5 | 594 |
*/ |
595 |
function checkUserActivity() { |
|
596 |
var lastActive = settings.userActivity ? time() - settings.userActivity : 0; |
|
0 | 597 |
|
5 | 598 |
// Throttle down when no mouse or keyboard activity for 5 min. |
599 |
if ( lastActive > 300000 && settings.hasFocus ) { |
|
600 |
blurred(); |
|
601 |
} |
|
0 | 602 |
|
5 | 603 |
// Suspend after 10 min. of inactivity when suspending is enabled. |
604 |
// Always suspend after 60 min. of inactivity. This will release the post lock, etc. |
|
605 |
if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) { |
|
606 |
settings.suspend = true; |
|
607 |
} |
|
608 |
||
609 |
if ( ! settings.userActivityEvents ) { |
|
610 |
$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() { |
|
611 |
userIsActive(); |
|
612 |
}); |
|
0 | 613 |
|
614 |
$('iframe').each( function( i, frame ) { |
|
5 | 615 |
if ( isLocalFrame( frame ) ) { |
616 |
$( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active', function() { |
|
617 |
userIsActive(); |
|
618 |
}); |
|
619 |
} |
|
0 | 620 |
}); |
621 |
||
5 | 622 |
settings.userActivityEvents = true; |
0 | 623 |
} |
624 |
} |
|
625 |
||
9 | 626 |
// Public methods. |
5 | 627 |
|
628 |
/** |
|
9 | 629 |
* Checks whether the window (or any local iframe in it) has focus, or the user |
630 |
* is active. |
|
5 | 631 |
* |
9 | 632 |
* @since 3.6.0 |
633 |
* @memberOf wp.heartbeat.prototype |
|
634 |
* |
|
635 |
* @returns {boolean} True if the window or the user is active. |
|
5 | 636 |
*/ |
637 |
function hasFocus() { |
|
638 |
return settings.hasFocus; |
|
639 |
} |
|
640 |
||
641 |
/** |
|
9 | 642 |
* Checks whether there is a connection error. |
643 |
* |
|
644 |
* @since 3.6.0 |
|
5 | 645 |
* |
9 | 646 |
* @memberOf wp.heartbeat.prototype |
647 |
* |
|
648 |
* @returns {boolean} True if a connection error was found. |
|
5 | 649 |
*/ |
650 |
function hasConnectionError() { |
|
651 |
return settings.connectionError; |
|
652 |
} |
|
0 | 653 |
|
5 | 654 |
/** |
9 | 655 |
* Connects as soon as possible regardless of 'hasFocus' state. |
5 | 656 |
* |
657 |
* Will not open two concurrent connections. If a connection is in progress, |
|
658 |
* will connect again immediately after the current connection completes. |
|
659 |
* |
|
9 | 660 |
* @since 3.8.0 |
661 |
* |
|
662 |
* @memberOf wp.heartbeat.prototype |
|
663 |
* |
|
664 |
* @returns {void} |
|
5 | 665 |
*/ |
666 |
function connectNow() { |
|
667 |
settings.lastTick = 0; |
|
668 |
scheduleNextTick(); |
|
669 |
} |
|
670 |
||
671 |
/** |
|
9 | 672 |
* Disables suspending. |
5 | 673 |
* |
9 | 674 |
* Should be used only when Heartbeat is performing critical tasks like |
675 |
* autosave, post-locking, etc. Using this on many screens may overload the |
|
676 |
* user's hosting account if several browser windows/tabs are left open for a |
|
677 |
* long time. |
|
5 | 678 |
* |
9 | 679 |
* @since 3.8.0 |
680 |
* |
|
681 |
* @memberOf wp.heartbeat.prototype |
|
682 |
* |
|
683 |
* @returns {void} |
|
5 | 684 |
*/ |
685 |
function disableSuspend() { |
|
686 |
settings.suspendEnabled = false; |
|
687 |
} |
|
0 | 688 |
|
689 |
/** |
|
9 | 690 |
* Gets/Sets the interval. |
691 |
* |
|
692 |
* When setting to 'fast' or 5, the interval is 5 seconds for the next 30 ticks |
|
693 |
* (for 2 minutes and 30 seconds) by default. In this case the number of 'ticks' |
|
694 |
* can be passed as second argument. If the window doesn't have focus, the |
|
695 |
* interval slows down to 2 min. |
|
696 |
* |
|
697 |
* @since 3.6.0 |
|
0 | 698 |
* |
9 | 699 |
* @memberOf wp.heartbeat.prototype |
0 | 700 |
* |
9 | 701 |
* @param {string|number} speed Interval: 'fast' or 5, 15, 30, 60, 120. Fast |
702 |
* equals 5. |
|
703 |
* @param {string} ticks Tells how many ticks before the interval reverts |
|
704 |
* back. Used with speed = 'fast' or 5. |
|
705 |
* |
|
706 |
* @returns {number} Current interval in seconds. |
|
0 | 707 |
*/ |
5 | 708 |
function interval( speed, ticks ) { |
709 |
var newInterval, |
|
710 |
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval; |
|
0 | 711 |
|
712 |
if ( speed ) { |
|
713 |
switch ( speed ) { |
|
714 |
case 'fast': |
|
5 | 715 |
case 5: |
716 |
newInterval = 5000; |
|
717 |
break; |
|
718 |
case 15: |
|
719 |
newInterval = 15000; |
|
0 | 720 |
break; |
5 | 721 |
case 30: |
722 |
newInterval = 30000; |
|
723 |
break; |
|
724 |
case 60: |
|
725 |
newInterval = 60000; |
|
726 |
break; |
|
727 |
case 120: |
|
728 |
newInterval = 120000; |
|
0 | 729 |
break; |
730 |
case 'long-polling': |
|
731 |
// Allow long polling, (experimental) |
|
5 | 732 |
settings.mainInterval = 0; |
0 | 733 |
return 0; |
734 |
default: |
|
5 | 735 |
newInterval = settings.originalInterval; |
736 |
} |
|
737 |
||
738 |
if ( settings.minimalInterval && newInterval < settings.minimalInterval ) { |
|
739 |
newInterval = settings.minimalInterval; |
|
0 | 740 |
} |
741 |
||
5 | 742 |
if ( 5000 === newInterval ) { |
743 |
ticks = parseInt( ticks, 10 ) || 30; |
|
744 |
ticks = ticks < 1 || ticks > 30 ? 30 : ticks; |
|
0 | 745 |
|
5 | 746 |
settings.countdown = ticks; |
747 |
settings.tempInterval = newInterval; |
|
0 | 748 |
} else { |
5 | 749 |
settings.countdown = 0; |
750 |
settings.tempInterval = 0; |
|
751 |
settings.mainInterval = newInterval; |
|
0 | 752 |
} |
753 |
||
5 | 754 |
// Change the next connection time if new interval has been set. |
755 |
// Will connect immediately if the time since the last connection |
|
756 |
// is greater than the new interval. |
|
757 |
if ( newInterval !== oldInterval ) { |
|
758 |
scheduleNextTick(); |
|
759 |
} |
|
0 | 760 |
} |
761 |
||
5 | 762 |
return settings.tempInterval ? settings.tempInterval / 1000 : settings.mainInterval / 1000; |
763 |
} |
|
0 | 764 |
|
765 |
/** |
|
9 | 766 |
* Enqueues data to send with the next XHR. |
0 | 767 |
* |
9 | 768 |
* As the data is send asynchronously, this function doesn't return the XHR |
769 |
* response. To see the response, use the custom jQuery event 'heartbeat-tick' |
|
770 |
* on the document, example: |
|
0 | 771 |
* $(document).on( 'heartbeat-tick.myname', function( event, data, textStatus, jqXHR ) { |
772 |
* // code |
|
773 |
* }); |
|
9 | 774 |
* If the same 'handle' is used more than once, the data is not overwritten when |
775 |
* the third argument is 'true'. Use `wp.heartbeat.isQueued('handle')` to see if |
|
776 |
* any data is already queued for that handle. |
|
777 |
* |
|
778 |
* @since 3.6.0 |
|
0 | 779 |
* |
9 | 780 |
* @memberOf wp.heartbeat.prototype |
781 |
* |
|
782 |
* @param {string} handle Unique handle for the data, used in PHP to |
|
783 |
* receive the data. |
|
784 |
* @param {*} data The data to send. |
|
785 |
* @param {boolean} noOverwrite Whether to overwrite existing data in the queue. |
|
786 |
* |
|
787 |
* @returns {boolean} True if the data was queued. |
|
0 | 788 |
*/ |
5 | 789 |
function enqueue( handle, data, noOverwrite ) { |
0 | 790 |
if ( handle ) { |
5 | 791 |
if ( noOverwrite && this.isQueued( handle ) ) { |
0 | 792 |
return false; |
5 | 793 |
} |
0 | 794 |
|
5 | 795 |
settings.queue[handle] = data; |
0 | 796 |
return true; |
797 |
} |
|
798 |
return false; |
|
5 | 799 |
} |
0 | 800 |
|
801 |
/** |
|
9 | 802 |
* Checks if data with a particular handle is queued. |
803 |
* |
|
804 |
* @since 3.6.0 |
|
0 | 805 |
* |
9 | 806 |
* @param {string} handle The handle for the data. |
807 |
* |
|
808 |
* @returns {boolean} True if the data is queued with this handle. |
|
0 | 809 |
*/ |
5 | 810 |
function isQueued( handle ) { |
811 |
if ( handle ) { |
|
812 |
return settings.queue.hasOwnProperty( handle ); |
|
813 |
} |
|
814 |
} |
|
0 | 815 |
|
816 |
/** |
|
9 | 817 |
* Removes data with a particular handle from the queue. |
818 |
* |
|
819 |
* @since 3.7.0 |
|
0 | 820 |
* |
9 | 821 |
* @memberOf wp.heartbeat.prototype |
822 |
* |
|
823 |
* @param {string} handle The handle for the data. |
|
824 |
* |
|
825 |
* @returns {void} |
|
0 | 826 |
*/ |
5 | 827 |
function dequeue( handle ) { |
828 |
if ( handle ) { |
|
829 |
delete settings.queue[handle]; |
|
830 |
} |
|
831 |
} |
|
0 | 832 |
|
833 |
/** |
|
9 | 834 |
* Gets data that was enqueued with a particular handle. |
835 |
* |
|
836 |
* @since 3.7.0 |
|
0 | 837 |
* |
9 | 838 |
* @memberOf wp.heartbeat.prototype |
839 |
* |
|
840 |
* @param {string} handle The handle for the data. |
|
841 |
* |
|
842 |
* @returns {*} The data or undefined. |
|
0 | 843 |
*/ |
5 | 844 |
function getQueuedItem( handle ) { |
845 |
if ( handle ) { |
|
846 |
return this.isQueued( handle ) ? settings.queue[handle] : undefined; |
|
847 |
} |
|
848 |
} |
|
849 |
||
850 |
initialize(); |
|
851 |
||
9 | 852 |
// Expose public methods. |
5 | 853 |
return { |
854 |
hasFocus: hasFocus, |
|
855 |
connectNow: connectNow, |
|
856 |
disableSuspend: disableSuspend, |
|
857 |
interval: interval, |
|
858 |
hasConnectionError: hasConnectionError, |
|
859 |
enqueue: enqueue, |
|
860 |
dequeue: dequeue, |
|
861 |
isQueued: isQueued, |
|
862 |
getQueuedItem: getQueuedItem |
|
0 | 863 |
}; |
864 |
}; |
|
865 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* Ensure the global `wp` object exists. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
* @namespace wp |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
*/ |
5 | 871 |
window.wp = window.wp || {}; |
9 | 872 |
|
873 |
/** |
|
874 |
* Contains the Heartbeat API. |
|
875 |
* |
|
876 |
* @namespace wp.heartbeat |
|
877 |
* @type {Heartbeat} |
|
878 |
*/ |
|
5 | 879 |
window.wp.heartbeat = new Heartbeat(); |
0 | 880 |
|
5 | 881 |
}( jQuery, window )); |