Skip to content

Commit

Permalink
Administration: Increase frequency of heartbeat API requests.
Browse files Browse the repository at this point in the history
Increases the frequency of heartbeat API requests from once every 15 seconds to once every 10 seconds.

The purpose of this change is to reduce the length of time before a post becomes unlocked as a user navigates around the WordPress Dashboard and ceases editing a post.

`wp.heartbeat.interval()` has been modified to allow theme and plugin authors to set the heartbeat interval to any value between one second and one hour rather than limiting them to a fixed set of values.

Props azaozz, annezazu, jorbin, kirasong.
Fixes #61960.


git-svn-id: https://develop.svn.wordpress.org/trunk@59016 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Sep 11, 2024
1 parent 1931906 commit 4712210
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/js/_enqueues/admin/inline-edit-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ window.wp = window.wp || {};
if ( ! $( this ).parent().find( 'input[name="indeterminate_post_category[]"]' ).length ) {
// Get the term label text.
var label = $( this ).parent().text();
// Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
// Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
$( this ).after( '<input type="hidden" name="indeterminate_post_category[]" value="' + $( this ).val() + '">' ).attr( 'aria-label', label.trim() + ': ' + wp.i18n.__( 'Some selected posts have this category' ) );
}
}
Expand Down Expand Up @@ -603,9 +603,9 @@ $( function() { inlineEditPost.init(); } );
// Show/hide locks on posts.
$( function() {

// Set the heartbeat interval to 15 seconds.
// Set the heartbeat interval to 10 seconds.
if ( typeof wp !== 'undefined' && wp.heartbeat ) {
wp.heartbeat.interval( 15 );
wp.heartbeat.interval( 10 );
}
}).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
var locked = data['wp-check-locked-posts'] || {};
Expand Down
4 changes: 2 additions & 2 deletions src/js/_enqueues/admin/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ jQuery( function($) {
}
}).filter(':visible').find('.wp-tab-first').trigger( 'focus' );

// Set the heartbeat interval to 15 seconds if post lock dialogs are enabled.
// Set the heartbeat interval to 10 seconds if post lock dialogs are enabled.
if ( wp.heartbeat && $('#post-lock-dialog').length ) {
wp.heartbeat.interval( 15 );
wp.heartbeat.interval( 10 );
}

// The form is being submitted by the user.
Expand Down
56 changes: 25 additions & 31 deletions src/js/_enqueues/wp/heartbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,17 @@
}

/*
* The interval can be from 15 to 120 seconds and can be set temporarily to 5 seconds.
* It can be set in the initial options or changed later through JS and/or through PHP.
* Logic check: the interval can be from 1 to 3600 seconds and can be set temporarily
* to 5 seconds. It can be set in the initial options or changed later from JS
* or from PHP through the AJAX responses.
*/
if ( options.interval ) {
settings.mainInterval = options.interval;

if ( settings.mainInterval < 15 ) {
settings.mainInterval = 15;
} else if ( settings.mainInterval > 120 ) {
settings.mainInterval = 120;
if ( settings.mainInterval < 1 ) {
settings.mainInterval = 1;
} else if ( settings.mainInterval > 3600 ) {
settings.mainInterval = 3600;
}
}

Expand Down Expand Up @@ -721,10 +722,10 @@
*
* @memberOf wp.heartbeat.prototype
*
* @param {string|number} speed Interval: 'fast' or 5, 15, 30, 60, 120.
* @param {string|number} speed Interval: 'fast' or integer between 1 and 3600 (seconds).
* Fast equals 5.
* @param {string} ticks Tells how many ticks before the interval reverts
* back. Used with speed = 'fast' or 5.
* @param {number} ticks Tells how many ticks before the interval reverts back.
* Value must be between 1 and 30. Used with speed = 'fast' or 5.
*
* @return {number} Current interval in seconds.
*/
Expand All @@ -733,35 +734,28 @@
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;

if ( speed ) {
switch ( speed ) {
case 'fast':
case 5:
newInterval = 5000;
break;
case 15:
newInterval = 15000;
break;
case 30:
newInterval = 30000;
break;
case 60:
newInterval = 60000;
break;
case 120:
newInterval = 120000;
break;
case 'long-polling':
// Allow long polling (experimental).
settings.mainInterval = 0;
return 0;
default:
if ( 'fast' === speed ) {
// Special case, see below.
newInterval = 5000;
} else if ( 'long-polling' === speed ) {
// Allow long polling (experimental).
settings.mainInterval = 0;
return 0;
} else {
speed = parseInt( speed, 10 );

if ( speed >= 1 && speed <= 3600 ) {
newInterval = speed * 1000;
} else {
newInterval = settings.originalInterval;
}
}

if ( settings.minimalInterval && newInterval < settings.minimalInterval ) {
newInterval = settings.minimalInterval;
}

// Special case, runs for a number of ticks then reverts to the previous interval.
if ( 5000 === newInterval ) {
ticks = parseInt( ticks, 10 ) || 30;
ticks = ticks < 1 || ticks > 30 ? 30 : ticks;
Expand Down
8 changes: 8 additions & 0 deletions src/wp-admin/edit-form-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ static function ( $classes ) {
'before'
);

// Set Heartbeat interval to 10 seconds, used to refresh post locks.
wp_add_inline_script(
'heartbeat',
'if ( window.wp && window.wp.heartbeat ) {
window.wp.heartbeat.interval( 10 );
}'
);

/*
* Get all available templates for the post/page attributes meta-box.
* The "Default template" array element should only be added if the array is
Expand Down

0 comments on commit 4712210

Please sign in to comment.