WP_Site_Health::wp_cron_scheduled_check() │ public │ WP 5.4.0
Runs the scheduled event to check and update the latest site health status for the website.
Method of the class: WP_Site_Health{}
No Hooks.
Return
null
. Nothing (null).
Usage
$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->wp_cron_scheduled_check();
Changelog
WP_Site_Health::wp_cron_scheduled_check() WP Site Health::wp cron scheduled check code
WP 6.6.2
public function wp_cron_scheduled_check() {
// Bootstrap wp-admin, as WP_Cron doesn't do this for us.
require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/admin.php';
$tests = WP_Site_Health::get_tests();
$results = array();
$site_status = array(
'good' => 0,
'recommended' => 0,
'critical' => 0,
);
// Don't run https test on development environments.
if ( $this->is_development_environment() ) {
unset( $tests['async']['https_status'] );
}
foreach ( $tests['direct'] as $test ) {
if ( ! empty( $test['skip_cron'] ) ) {
continue;
}
if ( is_string( $test['test'] ) ) {
$test_function = sprintf(
'get_test_%s',
$test['test']
);
if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
$results[] = $this->perform_test( array( $this, $test_function ) );
continue;
}
}
if ( is_callable( $test['test'] ) ) {
$results[] = $this->perform_test( $test['test'] );
}
}
foreach ( $tests['async'] as $test ) {
if ( ! empty( $test['skip_cron'] ) ) {
continue;
}
// Local endpoints may require authentication, so asynchronous tests can pass a direct test runner as well.
if ( ! empty( $test['async_direct_test'] ) && is_callable( $test['async_direct_test'] ) ) {
// This test is callable, do so and continue to the next asynchronous check.
$results[] = $this->perform_test( $test['async_direct_test'] );
continue;
}
if ( is_string( $test['test'] ) ) {
// Check if this test has a REST API endpoint.
if ( isset( $test['has_rest'] ) && $test['has_rest'] ) {
$result_fetch = wp_remote_get(
$test['test'],
array(
'body' => array(
'_wpnonce' => wp_create_nonce( 'wp_rest' ),
),
)
);
} else {
$result_fetch = wp_remote_post(
admin_url( 'admin-ajax.php' ),
array(
'body' => array(
'action' => $test['test'],
'_wpnonce' => wp_create_nonce( 'health-check-site-status' ),
),
)
);
}
if ( ! is_wp_error( $result_fetch ) && 200 === wp_remote_retrieve_response_code( $result_fetch ) ) {
$result = json_decode( wp_remote_retrieve_body( $result_fetch ), true );
} else {
$result = false;
}
if ( is_array( $result ) ) {
$results[] = $result;
} else {
$results[] = array(
'status' => 'recommended',
'label' => __( 'A test is unavailable' ),
);
}
}
}
foreach ( $results as $result ) {
if ( 'critical' === $result['status'] ) {
++$site_status['critical'];
} elseif ( 'recommended' === $result['status'] ) {
++$site_status['recommended'];
} else {
++$site_status['good'];
}
}
set_transient( 'health-check-site-status-result', wp_json_encode( $site_status ) );
}