settings = new Heartbeat_Settings();
add_action( 'wp_enqueue_scripts', [ $this, 'disable_heartbeat' ], 99 );
add_action( 'admin_enqueue_scripts', [ $this, 'disable_heartbeat' ], 99 );
add_filter( 'heartbeat_settings', [ $this, 'modify_heartbeat_frequency' ], 99 );
add_filter(
'adminoptim_notification_message_' . self::MODULE_SLUG,
static function ( string $message, string $display ): string {
if ( 'banner' === $display ) {
return sprintf(
/* translators: 1: Opening strong tag, 2: Closing strong tag. */
__(
'%1$sReducing unnecessary Heartbeat requests?%2$s Leave a ★★★★★ review on WordPress.org and help other WordPress users discover Admin Optimizer.',
'admin-optimizer'
),
'',
''
);
}
return $message;
},
10,
2
);
}
/**
* Return the module's submenu definition.
*
* @return array
*/
protected function get_submenu_config(): array {
return [
'page_title' => __( 'Heartbeat Control', 'admin-optimizer' ),
'menu_title' => __( 'Heartbeat Control', 'admin-optimizer' ),
'menu_slug' => self::MENU_SLUG,
'callback' => [ $this->settings, 'render_settings_page' ],
];
}
/**
* Disable heartbeat
*
* @return void
*/
public function disable_heartbeat() {
global $pagenow;
if ( ( isset( $this->options['admin'] ) && 'disable' === $this->options['admin'] && is_admin() ) ||
( isset( $this->options['frontend'] ) && 'disable' === $this->options['frontend'] && ! is_admin() ) ||
( isset( $this->options['editor'] ) && 'disable' === $this->options['editor'] && ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) ) ) {
wp_deregister_script( 'heartbeat' );
}
}
/**
* Modify heartbeat frequency
*
* @param array $settings List of user defined options.
*
* @return array
*/
public function modify_heartbeat_frequency( $settings ) {
global $pagenow;
if ( wp_doing_cron() ) {
return $settings;
}
$default = [
'admin' => '',
'frontend' => '',
'editor' => '',
];
$options = wp_parse_args( $this->options, $default );
// Disable heartbeat autostart.
$settings['autostart'] = false;
if ( is_admin() ) {
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
if ( 'modify' === $options['editor'] ) {
$settings['minimalInterval'] = absint( $options['editor_freq'] );
} elseif ( 'disable' === $options['editor'] ) {
$settings['disabled'] = true;
}
} elseif ( 'modify' === $options['admin'] ) {
$settings['interval'] = absint( $options['admin_freq'] );
} elseif ( 'disable' === $options['admin'] ) {
$settings['disabled'] = true;
}
} elseif ( 'modify' === $options['frontend'] ) {
$settings['interval'] = absint( $options['frontend'] );
} elseif ( 'disable' === $options['frontend'] ) {
$settings['disabled'] = true;
}
return $settings;
}
}