Ticket #29779: 29779.patch
| File 29779.patch, 9.0 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/js/heartbeat.js
57 57 // Used when the interval is reset 58 58 originalInterval: 0, 59 59 60 // Used to limit the number of AJAX requests. 61 minimalInterval: 0, 62 60 63 // Used together with tempInterval 61 64 countdown: 0, 62 65 … … 81 84 // Flags whether events tracking user activity were set 82 85 userActivityEvents: false, 83 86 84 // References to various timeouts 85 beatTimer: 0, 86 winBlurTimer: 0, 87 frameBlurTimer: 0 87 beatTimer: 0 88 88 }; 89 89 90 90 /** … … 95 95 * @return void 96 96 */ 97 97 function initialize() { 98 var options, hidden, visibilityState, visibilitychange; 99 98 100 if ( typeof window.pagenow === 'string' ) { 99 101 settings.screenId = window.pagenow; 100 102 } … … 105 107 106 108 // Pull in options passed from PHP 107 109 if ( typeof window.heartbeatSettings === 'object' ) { 108 varoptions = window.heartbeatSettings;110 options = window.heartbeatSettings; 109 111 110 112 // The XHR URL can be passed as option when window.ajaxurl is not set 111 113 if ( ! settings.url && options.ajaxurl ) { … … 112 114 settings.url = options.ajaxurl; 113 115 } 114 116 115 // The interval can be from 15 to 60 sec. and can be set temporarily to 5 sec. 117 // The interval can be from 15 to 120 sec. and can be set temporarily to 5 sec. 118 // It can be set in the initial options or changed later from JS and/or from PHP. 116 119 if ( options.interval ) { 117 120 settings.mainInterval = options.interval; 118 121 119 122 if ( settings.mainInterval < 15 ) { 120 123 settings.mainInterval = 15; 121 } else if ( settings.mainInterval > 60 ) {122 settings.mainInterval = 60;124 } else if ( settings.mainInterval > 120 ) { 125 settings.mainInterval = 120; 123 126 } 124 127 } 125 128 129 // Used to limit the number of AJAX requests. Overrides all other intervals if they are shorter. 130 // Needed for some hosts that cannot handle frequent requests and the user may exceed the allocated server CPU time, etc. 131 // The minimal interval can be up to 600 sec. however setting it to longer than 120 sec. will limit or disable 132 // some of the functionality (like post locks). 133 // Once set at initialization, minimalInterval cannot be changed/overriden. 134 if ( options.minimalInterval ) { 135 options.minimalInterval = parseInt( options.minimalInterval, 10 ); 136 settings.minimalInterval = options.minimalInterval > 0 && options.minimalInterval <= 600 ? options.minimalInterval * 1000 : 0; 137 } 138 139 if ( settings.minimalInterval && settings.mainInterval < settings.minimalInterval ) { 140 settings.mainInterval = settings.minimalInterval; 141 } 142 126 143 // 'screenId' can be added from settings on the front-end where the JS global 'pagenow' is not set 127 144 if ( ! settings.screenId ) { 128 145 settings.screenId = options.screenId || 'front'; … … 137 154 settings.mainInterval = settings.mainInterval * 1000; 138 155 settings.originalInterval = settings.mainInterval; 139 156 140 // Set focus/blur events on the window 141 $(window).on( 'blur.wp-heartbeat-focus', function() { 142 setFrameFocusEvents(); 143 // We don't know why the 'blur' was fired. Either the user clicked in an iframe or outside the browser. 144 // Running blurred() after some timeout lets us cancel it if the user clicked in an iframe. 145 settings.winBlurTimer = window.setTimeout( function(){ blurred(); }, 500 ); 146 }).on( 'focus.wp-heartbeat-focus', function() { 147 removeFrameFocusEvents(); 148 focused(); 149 }).on( 'unload.wp-heartbeat', function() { 157 // Switch the interval to 120 sec. by using the Page Visibility API. 158 // If the browser doesn't support it (Safari < 7, Android < 4.4, IE < 10), the interval 159 // will be increased to 120 sec. after 5 min. of mouse and keyboard inactivity. 160 if ( typeof document.hidden !== 'undefined' ) { 161 hidden = 'hidden'; 162 visibilitychange = 'visibilitychange'; 163 visibilityState = 'visibilityState'; 164 } else if ( typeof document.msHidden !== 'undefined' ) { // IE10 165 hidden = 'msHidden'; 166 visibilitychange = 'msvisibilitychange'; 167 visibilityState = 'msVisibilityState'; 168 } else if ( typeof document.webkitHidden !== 'undefined' ) { // Android 169 hidden = 'webkitHidden'; 170 visibilitychange = 'webkitvisibilitychange'; 171 visibilityState = 'webkitVisibilityState'; 172 } 173 174 if ( hidden ) { 175 if ( document[hidden] ) { 176 settings.hasFocus = false; 177 } 178 179 $document.on( visibilitychange + '.wp-heartbeat', function() { 180 if ( document[visibilityState] === 'hidden' ) { 181 blurred(); 182 } else { 183 focused(); 184 } 185 }); 186 } 187 188 $(window).on( 'unload.wp-heartbeat', function() { 150 189 // Don't connect any more 151 190 settings.suspend = true; 152 191 … … 374 413 } 375 414 } 376 415 416 if ( settings.minimalInterval && interval < settings.minimalInterval ) { 417 interval = settings.minimalInterval; 418 } 419 377 420 window.clearTimeout( settings.beatTimer ); 378 421 379 422 if ( delta < interval ) { … … 389 432 } 390 433 391 434 /** 392 * Set the internal state when the browser window looses focus435 * Set the internal state when the browser window is hidden 393 436 * 394 437 * @access private 395 438 * … … 396 439 * @return void 397 440 */ 398 441 function blurred() { 399 clearFocusTimers();400 442 settings.hasFocus = false; 401 443 } 402 444 403 445 /** 404 * Set the internal state when the browser window is focused446 * Set the internal state when the browser window is visible 405 447 * 406 448 * @access private 407 449 * … … 408 450 * @return void 409 451 */ 410 452 function focused() { 411 clearFocusTimers();412 453 settings.userActivity = time(); 413 454 414 455 // Resume if suspended … … 421 462 } 422 463 423 464 /** 424 * Add focus/blur events to all local iframes425 *426 * Used to detect when focus is moved from the main window to an iframe427 *428 * @access private429 *430 * @return void431 */432 function setFrameFocusEvents() {433 $('iframe').each( function( i, frame ) {434 if ( ! isLocalFrame( frame ) ) {435 return;436 }437 438 if ( $.data( frame, 'wp-heartbeat-focus' ) ) {439 return;440 }441 442 $.data( frame, 'wp-heartbeat-focus', 1 );443 444 $( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function() {445 focused();446 }).on('blur.wp-heartbeat-focus', function() {447 setFrameFocusEvents();448 // We don't know why 'blur' was fired. Either the user clicked in the main window or outside the browser.449 // Running blurred() after some timeout lets us cancel it if the user clicked in the main window.450 settings.frameBlurTimer = window.setTimeout( function(){ blurred(); }, 500 );451 });452 });453 }454 455 /**456 * Remove the focus/blur events to all local iframes457 *458 * @access private459 *460 * @return void461 */462 function removeFrameFocusEvents() {463 $('iframe').each( function( i, frame ) {464 if ( ! isLocalFrame( frame ) ) {465 return;466 }467 468 $.removeData( frame, 'wp-heartbeat-focus' );469 $( frame.contentWindow ).off( '.wp-heartbeat-focus' );470 });471 }472 473 /**474 * Clear the reset timers for focus/blur events on the window and iframes475 *476 * @access private477 *478 * @return void479 */480 function clearFocusTimers() {481 window.clearTimeout( settings.winBlurTimer );482 window.clearTimeout( settings.frameBlurTimer );483 }484 485 /**486 465 * Runs when the user becomes active after a period of inactivity 487 466 * 488 467 * @access private … … 524 503 blurred(); 525 504 } 526 505 527 if ( settings.suspendEnabled && lastActive > 1200000 ) {528 // Suspend after 20 min. of inactivity506 if ( settings.suspendEnabled && lastActive > 600000 ) { 507 // Suspend after 10 min. of inactivity when suspending is enabled. 529 508 settings.suspend = true; 509 } else if ( lastActive > 3600000 ) { 510 // Always suspend after 60 min. of inactivity. This will release the post lock, etc. 511 settings.suspend = true; 530 512 } 531 513 532 514 if ( ! settings.userActivityEvents ) { … … 597 579 * In this case the number of 'ticks' can be passed as second argument. 598 580 * If the window doesn't have focus, the interval slows down to 2 min. 599 581 * 600 * @param mixed speed Interval: 'fast' or 5, 15, 30, 60 582 * @param mixed speed Interval: 'fast' or 5, 15, 30, 60, 120 601 583 * @param string ticks Used with speed = 'fast' or 5, how many ticks before the interval reverts back 602 584 * @return int Current interval in seconds 603 585 */ … … 620 602 case 60: 621 603 newInterval = 60000; 622 604 break; 605 case 120: 606 newInterval = 120000; 607 break; 623 608 case 'long-polling': 624 609 // Allow long polling, (experimental) 625 610 settings.mainInterval = 0; … … 628 613 newInterval = settings.originalInterval; 629 614 } 630 615 616 if ( settings.minimalInterval && newInterval < settings.minimalInterval ) { 617 newInterval = settings.minimalInterval; 618 } 619 631 620 if ( 5000 === newInterval ) { 632 621 ticks = parseInt( ticks, 10 ) || 30; 633 622 ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)