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 );
}
}
/**
* 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' ),
'slow' => __( 'Slow down', 'adminease' ),
'off' => __( 'Turn off', 'adminease' ),
];
$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_mode( '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.', 'adminease' ),
'options' => $mode_options,
'attributes' => [
'data-parent' => 'heartbeat-control-enabled',
],
],
[
'type' => 'select',
'id' => 'heartbeat-editor',
'name' => 'adminease[performance][heartbeat_editor]',
'value' => $this->get_mode( 'editor' ),
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Post editor', 'adminease' ),
'description' => __( '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_mode( '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.', 'adminease' ),
'options' => $mode_options,
'attributes' => [
'data-parent' => 'heartbeat-control-enabled',
],
],
[
'type' => 'select',
'id' => 'heartbeat-interval',
'name' => 'adminease[performance][heartbeat_interval]',
'value' => (string) $this->get_interval(),
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Heartbeat Frequency', 'adminease' ),
'description' => __( 'How often the Heartbeat API runs in the places set to "Slow down". WordPress uses 15 seconds in the post editor and 60 seconds elsewhere by default.', 'adminease' ),
'options' => [
'15' => __( 'Every 15 seconds', 'adminease' ),
'30' => __( 'Every 30 seconds', 'adminease' ),
'60' => __( 'Every 60 seconds', 'adminease' ),
'120' => __( 'Every 120 seconds', 'adminease' ),
],
'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;
}
$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 the slowed-down interval in seconds, between 15 and 120. A site that has not
* saved the new settings yet keeps the old frequency.
*
* @return int The interval.
*/
private function get_interval(): int {
$interval = $this->settings['heartbeat_interval'] ?? ( $this->settings['heartbeat_control_frequency'] ?? 60 );
if( !is_numeric( $interval ) ) {
return 60;
}
return max( 15, min( 120, (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();
$settings['interval'] = $interval;
$settings['minimalInterval'] = $interval;
return $settings;
}
}