settings = Plugin::get_settings( 'performance' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
if( !empty( $this->settings['heartbeat_control_enabled'] ) ) {
add_action( 'admin_enqueue_scripts', [ $this, 'maybe_remove_heartbeat' ], 9999 );
add_action( 'customize_controls_enqueue_scripts', [ $this, 'maybe_remove_heartbeat' ], 9999 );
add_action( 'wp_print_scripts', [ $this, 'maybe_remove_heartbeat' ], 9999 );
add_filter( 'heartbeat_settings', [ $this, 'heartbeat_settings' ], 20 );
add_filter( 'wp_check_post_lock_window', [ $this, 'post_lock_window' ] );
}
}
/**
* Adds the Heartbeat Control fields to the Performance tab.
*
* @param array $fields The existing settings fields array.
*
* @return array The modified settings fields array.
*/
public function adminease_settings_fields( array $fields ): array {
$mode_options = [
'default' => __( 'Normal (WordPress default)', 'adminease' ),
'off' => __( 'Turn off', 'adminease' ),
];
foreach( [ 15, 30, 60, 120, 180, 240, 300 ] as $seconds ) {
/* translators: %d: number of seconds */
$mode_options[ (string) $seconds ] = sprintf( __( 'Slow down: every %d seconds', 'adminease' ), $seconds );
}
$fields['performance']['fields'][] = [
'type' => 'switch',
'id' => 'heartbeat-control-enabled',
'name' => 'adminease[performance][heartbeat_control_enabled]',
'value' => $this->settings['heartbeat_control_enabled'] ?? '',
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input toggle-field',
'label' => __( 'Heartbeat Control', 'adminease' ),
'description' => __( 'The Heartbeat API is what WordPress uses to autosave, lock posts that someone else is editing, and keep the dashboard up to date. It does that by calling admin-ajax.php every 15 to 60 seconds for every logged-in user, which on shared hosting is often the single biggest source of admin CPU usage. Switch it off where you do not need it and slow it down where you do.', 'adminease' ),
'child_fields' => [
[
'type' => 'select',
'id' => 'heartbeat-dashboard',
'name' => 'adminease[performance][heartbeat_dashboard]',
'value' => $this->get_choice( 'dashboard' ),
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Dashboard and other admin screens', 'adminease' ),
'description' => __( 'Every admin screen except the post editor. WordPress runs it every 60 seconds by default.', 'adminease' ),
'options' => $mode_options,
'attributes' => [
'data-parent' => 'heartbeat-control-enabled',
],
],
[
'type' => 'select',
'id' => 'heartbeat-editor',
'name' => 'adminease[performance][heartbeat_editor]',
'value' => $this->get_choice( 'editor' ),
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Post editor', 'adminease' ),
'description' => __( 'WordPress runs it every 15 seconds by default. Turning it off here also turns off post locking, so two people can edit the same post at the same time without a warning.', 'adminease' ),
'options' => $mode_options,
'attributes' => [
'data-parent' => 'heartbeat-control-enabled',
],
],
[
'type' => 'select',
'id' => 'heartbeat-front',
'name' => 'adminease[performance][heartbeat_front]',
'value' => $this->get_choice( 'front' ),
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Front end', 'adminease' ),
'description' => __( 'The public side of your site. WordPress runs it every 60 seconds by default.', 'adminease' ),
'options' => $mode_options,
'attributes' => [
'data-parent' => 'heartbeat-control-enabled',
],
],
],
];
return $fields;
}
/**
* Tells which of the three places the current request belongs to.
*
* @return string One of dashboard, editor or front.
*/
private function get_location(): string {
global $pagenow;
if( !is_admin() ) {
return 'front';
}
if( in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) {
return 'editor';
}
return 'dashboard';
}
/**
* Gets the mode for one place. A site that has not saved the new settings yet
* keeps the behaviour of the old single location choice.
*
* @param string $location One of dashboard, editor or front.
*
* @return string One of default, slow or off.
*/
private function get_mode( string $location ): string {
$mode = $this->settings[ 'heartbeat_' . $location ] ?? '';
if( in_array( $mode, [ 'default', 'slow', 'off' ], true ) ) {
return $mode;
}
if( is_scalar( $mode ) && 1 === preg_match( '/^\d+$/', (string) $mode ) ) {
return 'slow';
}
$old_location = $this->settings['heartbeat_control_location'] ?? 'allow_post_editor';
if( 'disable_everywhere' === $old_location ) {
return 'off';
}
if( 'front' === $location ) {
return 'off';
}
if( 'disable_frontend' === $old_location || 'editor' === $location ) {
return 'slow';
}
return 'off';
}
/**
* Gets what the dropdown of one place shows: default, off, or the slowed-down interval in seconds.
*
* @param string $location One of dashboard, editor or front.
*
* @return string The choice.
*/
private function get_choice( string $location ): string {
$mode = $this->get_mode( $location );
return 'slow' === $mode ? (string) $this->get_interval( $location ) : $mode;
}
/**
* Gets the slowed-down interval in seconds for one place, between 15 and 300. A place saved as
* a number uses it. A place still saved as plain "slow" by earlier versions uses the shared
* interval of those versions, then the old frequency, so existing sites keep their speed.
*
* @param string $location One of dashboard, editor or front.
*
* @return int The interval.
*/
private function get_interval( string $location ): int {
$interval = $this->settings[ 'heartbeat_' . $location ] ?? '';
if( !is_scalar( $interval ) || 1 !== preg_match( '/^\d+$/', (string) $interval ) ) {
$interval = $this->settings['heartbeat_interval'] ?? ( $this->settings['heartbeat_control_frequency'] ?? 60 );
}
if( !is_numeric( $interval ) ) {
return 60;
}
return max( 15, min( 300, (int) $interval ) );
}
/**
* Removes the heartbeat script, and every script that needs it, where the place is set to off.
* Scripts that need heartbeat go too, so none of them is left waiting for a script that is gone.
*
* @return void
*/
public function maybe_remove_heartbeat(): void {
if( 'off' !== $this->get_mode( $this->get_location() ) ) {
return;
}
$scripts = wp_scripts();
$handles = [ 'heartbeat' ];
do {
$found = false;
foreach( $scripts->registered as $handle => $script ) {
if( in_array( $handle, $handles, true ) ) {
continue;
}
if( array_intersect( $handles, (array) $script->deps ) ) {
$handles[] = $handle;
$found = true;
}
}
} while( $found );
foreach( $handles as $handle ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
}
}
/**
* Applies the chosen interval where the place is set to slow. Both values are set,
* because the block editor ignores the interval alone.
*
* @param mixed $settings The heartbeat settings handed to the browser.
*
* @return mixed The modified heartbeat settings.
*/
public function heartbeat_settings( $settings ) {
if( !is_array( $settings ) || 'slow' !== $this->get_mode( $this->get_location() ) ) {
return $settings;
}
$interval = $this->get_interval( $this->get_location() );
$settings['interval'] = $interval;
$settings['minimalInterval'] = $interval;
return $settings;
}
/**
* Keeps a post lock alive between two slow editor heartbeats. WordPress treats a lock as
* expired after 150 seconds, and the lock is only renewed when the heartbeat runs.
*
* @param mixed $window The lock window in seconds.
*
* @return mixed The window, widened when the editor is slowed down.
*/
public function post_lock_window( $window ) {
if( 'slow' !== $this->get_mode( 'editor' ) || !is_numeric( $window ) ) {
return $window;
}
return max( (int) $window, $this->get_interval( 'editor' ) + 30 );
}
}