settings = Plugin::get_settings( 'performance' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
add_action( 'adminease_settings_saved', [ $this, 'adminease_settings_saved' ] );
add_action( 'adminease_after_field_render', [ $this, 'adminease_after_field_render' ] );
add_filter( 'adminease_dashboard_notices', [ $this, 'adminease_dashboard_notices' ] );
}
/**
* Adds the late scheduled tasks warning to the dashboard notices.
*
* @param array $notices The notices collected so far.
*
* @return array The notices, with the warning added when a task is more than an hour late.
*/
public function adminease_dashboard_notices( array $notices ): array {
if( $this->has_late_tasks() ) {
$notices[] = [
'id' => 'cron-late',
'severity' => 'warning',
'message' => __( 'WP-Cron is switched off and some scheduled tasks are more than an hour late. Make sure a cron job on your server is calling wp-cron.php.', 'adminease' ),
'field' => 'disable-wp-cron',
];
}
return $notices;
}
/**
* Adds and configures the settings fields for Adminease, including performance options.
*
* @param array $fields The array of existing settings fields to be updated.
*
* @return array The updated array of settings fields with additional configuration.
*/
public function adminease_settings_fields( array $fields ): array {
$fields['performance']['fields'][] = [
'type' => 'switch',
'id' => 'disable-wp-cron',
'name' => 'adminease[performance][disable_wp_cron]',
'value' => $this->settings['disable_wp_cron'] ?? '',
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input toggle-field',
'label' => __( 'Disable WP Cron', 'adminease' ),
'risk' => 'careful',
'breaks' => __( 'Scheduled tasks stop running unless the server runs them.', 'adminease' ),
'description' => __( "The WordPress Cron (WP-Cron) system is what handles scheduled tasks like publishing scheduled posts, checking for updates, or sending emails. By default, WP-Cron runs every time someone visits your site-which can lead to performance issues on busy or low-traffic websites. Disabling WP-Cron allows you to take control and replace it with a real server-side cron job that runs at set intervals. This improves reliability and efficiency, especially on larger or more dynamic sites.", 'adminease' ),
];
return $fields;
}
/**
* Handles the saving of Adminease settings by updating specific constants in the WordPress configuration.
*
* @param array $sanitized_settings The array of settings passed, including performance options.
*
* @return void
*/
public function adminease_settings_saved( array $sanitized_settings ): void {
FileHandler::get_instance()->stack_wp_config_constant( 'DISABLE_WP_CRON', ! empty( $sanitized_settings['performance']['disable_wp_cron'] ) );
}
/**
* Tells whether a scheduled task is more than an hour late. Only counts while the switch
* is saved as on, because with WP-Cron running a late task is not about this setting.
*
* @return bool True when a task is more than an hour overdue.
*/
private function has_late_tasks(): bool {
if( empty( $this->settings['disable_wp_cron'] ) ) {
return false;
}
$cron = _get_cron_array();
if( !is_array( $cron ) ) {
return false;
}
$limit = time() - HOUR_IN_SECONDS;
foreach( $cron as $timestamp => $hooks ) {
if( is_numeric( $timestamp ) && (int) $timestamp < $limit && !empty( $hooks ) ) {
return true;
}
}
return false;
}
/**
* Shows the server cron line and the late task warning under the Disable WP Cron switch.
*
* @param array $field The field being rendered.
*
* @return void
*/
public function adminease_after_field_render( array $field ): void {
if( 'disable-wp-cron' !== $field['id'] ) {
return;
}
if( !current_user_can( 'manage_options' ) ) {
return;
}
$adminease_cron_line = '*/5 * * * * wget -q -O - "' . site_url( 'wp-cron.php' ) . '?doing_wp_cron" >/dev/null 2>&1';
$adminease_cron_late = $this->has_late_tasks();
include ADMINEASE_DIR . 'partials/wp-cron-help.php';
}
}