settings = Plugin::get_settings( 'performance' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['heartbeat_control_enabled'] ) ) { add_action( 'init', [ $this, 'maybe_deregister_heartbeat' ], 1 ); add_filter( 'heartbeat_settings', [ $this, 'heartbeat_settings' ] ); } } /** * 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 { $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-control-location', 'name' => 'adminease[performance][heartbeat_control_location]', 'value' => $this->settings['heartbeat_control_location'] ?? 'allow_post_editor', 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Where to Disable Heartbeat', 'adminease' ), 'description' => __( 'Keeping it in the post editor preserves autosave and post locking, which is the safe choice for most sites.', 'adminease' ), 'options' => [ 'allow_post_editor' => __( 'Everywhere except the post editor', 'adminease' ), 'disable_frontend' => __( 'Front end only', 'adminease' ), 'disable_everywhere' => __( 'Everywhere', 'adminease' ), ], 'attributes' => [ 'data-parent' => 'heartbeat-control-enabled', ], ], [ 'type' => 'select', 'id' => 'heartbeat-control-frequency', 'name' => 'adminease[performance][heartbeat_control_frequency]', 'value' => $this->settings['heartbeat_control_frequency'] ?? '60', '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 where it is still active. 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; } /** * Removes the heartbeat script on the screens the chosen location covers. * @return void */ public function maybe_deregister_heartbeat(): void { global $pagenow; $location = $this->settings['heartbeat_control_location'] ?? 'allow_post_editor'; if( 'disable_frontend' === $location ) { if( !is_admin() ) { wp_deregister_script( 'heartbeat' ); } return; } if( 'disable_everywhere' === $location ) { wp_deregister_script( 'heartbeat' ); return; } if( !is_admin() || !in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) { wp_deregister_script( 'heartbeat' ); } } /** * Applies the chosen Heartbeat interval where the API is still running. * * @param array $settings The heartbeat settings handed to the browser. * * @return array The modified heartbeat settings. */ public function heartbeat_settings( array $settings ): array { $frequency = (int) ( $this->settings['heartbeat_control_frequency'] ?? 60 ); if( !in_array( $frequency, [ 15, 30, 60, 120 ], true ) ) { $frequency = 60; } $settings['interval'] = $frequency; return $settings; } }