WP_Site_Health::get_test_scheduled_events()publicWP 5.2.0

Tests if scheduled events run as intended.

If scheduled events are not running, this may indicate something with WP_Cron is not working as intended, or that there are orphaned events hanging around from older code.

Method of the class: WP_Site_Health{}

No Hooks.

Return

Array. The test results.

Usage

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->get_test_scheduled_events();

Changelog

Since 5.2.0 Introduced.

WP_Site_Health::get_test_scheduled_events() code WP 6.6.2

public function get_test_scheduled_events() {
	$result = array(
		'label'       => __( 'Scheduled events are running' ),
		'status'      => 'good',
		'badge'       => array(
			'label' => __( 'Performance' ),
			'color' => 'blue',
		),
		'description' => sprintf(
			'<p>%s</p>',
			__( 'Scheduled events are what periodically looks for updates to plugins, themes and WordPress itself. It is also what makes sure scheduled posts are published on time. It may also be used by various plugins to make sure that planned actions are executed.' )
		),
		'actions'     => '',
		'test'        => 'scheduled_events',
	);

	$this->wp_schedule_test_init();

	if ( is_wp_error( $this->has_missed_cron() ) ) {
		$result['status'] = 'critical';

		$result['label'] = __( 'It was not possible to check your scheduled events' );

		$result['description'] = sprintf(
			'<p>%s</p>',
			sprintf(
				/* translators: %s: The error message returned while from the cron scheduler. */
				__( 'While trying to test your site&#8217;s scheduled events, the following error was returned: %s' ),
				$this->has_missed_cron()->get_error_message()
			)
		);
	} elseif ( $this->has_missed_cron() ) {
		$result['status'] = 'recommended';

		$result['label'] = __( 'A scheduled event has failed' );

		$result['description'] = sprintf(
			'<p>%s</p>',
			sprintf(
				/* translators: %s: The name of the failed cron event. */
				__( 'The scheduled event, %s, failed to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
				$this->last_missed_cron
			)
		);
	} elseif ( $this->has_late_cron() ) {
		$result['status'] = 'recommended';

		$result['label'] = __( 'A scheduled event is late' );

		$result['description'] = sprintf(
			'<p>%s</p>',
			sprintf(
				/* translators: %s: The name of the late cron event. */
				__( 'The scheduled event, %s, is late to run. Your site still works, but this may indicate that scheduling posts or automated updates may not work as intended.' ),
				$this->last_late_cron
			)
		);
	}

	return $result;
}