Skip to content

Commit 4712210

Browse files
committed
Administration: Increase frequency of heartbeat API requests.
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
1 parent 1931906 commit 4712210

4 files changed

Lines changed: 38 additions & 36 deletions

File tree

src/js/_enqueues/admin/inline-edit-post.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ window.wp = window.wp || {};
250250
if ( ! $( this ).parent().find( 'input[name="indeterminate_post_category[]"]' ).length ) {
251251
// Get the term label text.
252252
var label = $( this ).parent().text();
253-
// Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
253+
// Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
254254
$( 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' ) );
255255
}
256256
}
@@ -603,9 +603,9 @@ $( function() { inlineEditPost.init(); } );
603603
// Show/hide locks on posts.
604604
$( function() {
605605

606-
// Set the heartbeat interval to 15 seconds.
606+
// Set the heartbeat interval to 10 seconds.
607607
if ( typeof wp !== 'undefined' && wp.heartbeat ) {
608-
wp.heartbeat.interval( 15 );
608+
wp.heartbeat.interval( 10 );
609609
}
610610
}).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
611611
var locked = data['wp-check-locked-posts'] || {};

src/js/_enqueues/admin/post.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ jQuery( function($) {
343343
}
344344
}).filter(':visible').find('.wp-tab-first').trigger( 'focus' );
345345

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

351351
// The form is being submitted by the user.

src/js/_enqueues/wp/heartbeat.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,17 @@
132132
}
133133

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

141-
if ( settings.mainInterval < 15 ) {
142-
settings.mainInterval = 15;
143-
} else if ( settings.mainInterval > 120 ) {
144-
settings.mainInterval = 120;
142+
if ( settings.mainInterval < 1 ) {
143+
settings.mainInterval = 1;
144+
} else if ( settings.mainInterval > 3600 ) {
145+
settings.mainInterval = 3600;
145146
}
146147
}
147148

@@ -721,10 +722,10 @@
721722
*
722723
* @memberOf wp.heartbeat.prototype
723724
*
724-
* @param {string|number} speed Interval: 'fast' or 5, 15, 30, 60, 120.
725+
* @param {string|number} speed Interval: 'fast' or integer between 1 and 3600 (seconds).
725726
* Fast equals 5.
726-
* @param {string} ticks Tells how many ticks before the interval reverts
727-
* back. Used with speed = 'fast' or 5.
727+
* @param {number} ticks Tells how many ticks before the interval reverts back.
728+
* Value must be between 1 and 30. Used with speed = 'fast' or 5.
728729
*
729730
* @return {number} Current interval in seconds.
730731
*/
@@ -733,35 +734,28 @@
733734
oldInterval = settings.tempInterval ? settings.tempInterval : settings.mainInterval;
734735

735736
if ( speed ) {
736-
switch ( speed ) {
737-
case 'fast':
738-
case 5:
739-
newInterval = 5000;
740-
break;
741-
case 15:
742-
newInterval = 15000;
743-
break;
744-
case 30:
745-
newInterval = 30000;
746-
break;
747-
case 60:
748-
newInterval = 60000;
749-
break;
750-
case 120:
751-
newInterval = 120000;
752-
break;
753-
case 'long-polling':
754-
// Allow long polling (experimental).
755-
settings.mainInterval = 0;
756-
return 0;
757-
default:
737+
if ( 'fast' === speed ) {
738+
// Special case, see below.
739+
newInterval = 5000;
740+
} else if ( 'long-polling' === speed ) {
741+
// Allow long polling (experimental).
742+
settings.mainInterval = 0;
743+
return 0;
744+
} else {
745+
speed = parseInt( speed, 10 );
746+
747+
if ( speed >= 1 && speed <= 3600 ) {
748+
newInterval = speed * 1000;
749+
} else {
758750
newInterval = settings.originalInterval;
751+
}
759752
}
760753

761754
if ( settings.minimalInterval && newInterval < settings.minimalInterval ) {
762755
newInterval = settings.minimalInterval;
763756
}
764757

758+
// Special case, runs for a number of ticks then reverts to the previous interval.
765759
if ( 5000 === newInterval ) {
766760
ticks = parseInt( ticks, 10 ) || 30;
767761
ticks = ticks < 1 || ticks > 30 ? 30 : ticks;

src/wp-admin/edit-form-blocks.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ static function ( $classes ) {
123123
'before'
124124
);
125125

126+
// Set Heartbeat interval to 10 seconds, used to refresh post locks.
127+
wp_add_inline_script(
128+
'heartbeat',
129+
'if ( window.wp && window.wp.heartbeat ) {
130+
window.wp.heartbeat.interval( 10 );
131+
}'
132+
);
133+
126134
/*
127135
* Get all available templates for the post/page attributes meta-box.
128136
* The "Default template" array element should only be added if the array is

0 commit comments

Comments
 (0)