diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index d8c097c93d..a42732d277 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -11,14 +11,17 @@ class WP_Site_Health { private $mysql_min_version_check; private $mysql_rec_version_check; - public $is_mariadb = false; + public $is_mariadb = false; private $mysql_server_version = ''; private $health_check_mysql_required_version = '5.5'; private $health_check_mysql_rec_version = ''; public $schedules; public $crons; - public $last_missed_cron = null; + public $last_missed_cron = null; + public $last_late_cron = null; + private $timeout_missed_cron = null; + private $timeout_late_cron = null; /** * WP_Site_Health constructor. @@ -28,6 +31,14 @@ class WP_Site_Health { public function __construct() { $this->prepare_sql_data(); + $this->timeout_late_cron = 0; + $this->timeout_missed_cron = - 5 * MINUTE_IN_SECONDS; + + if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { + $this->timeout_late_cron = - 15 * MINUTE_IN_SECONDS; + $this->timeout_missed_cron = - 1 * HOUR_IN_SECONDS; + } + add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); @@ -1416,21 +1427,32 @@ class WP_Site_Health { $this->has_missed_cron()->get_error_message() ) ); - } else { - if ( $this->has_missed_cron() ) { - $result['status'] = 'recommended'; + } elseif ( $this->has_missed_cron() ) { + $result['status'] = 'recommended'; - $result['label'] = __( 'A scheduled event has failed' ); + $result['label'] = __( 'A scheduled event has failed' ); - $result['description'] = sprintf( - '
%s
', - 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 - ) - ); - } + $result['description'] = sprintf( + '%s
', + 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( + '%s
', + 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; @@ -1926,7 +1948,7 @@ class WP_Site_Health { } foreach ( $this->crons as $id => $cron ) { - if ( ( $cron->time - time() ) < 0 ) { + if ( ( $cron->time - time() ) < $this->timeout_missed_cron ) { $this->last_missed_cron = $cron->hook; return true; } @@ -1935,6 +1957,33 @@ class WP_Site_Health { return false; } + /** + * Check if any scheduled tasks are late. + * + * Returns a boolean value of `true` if a scheduled task is late and ends processing. If the list of + * crons is an instance of WP_Error, return the instance instead of a boolean value. + * + * @return bool|WP_Error true if a cron is late, false if it wasn't. WP_Error if the cron is set to that. + */ + public function has_late_cron() { + if ( is_wp_error( $this->crons ) ) { + return $this->crons; + } + + foreach ( $this->crons as $id => $cron ) { + $cron_offset = $cron->time - time(); + if ( + $cron_offset >= $this->timeout_missed_cron && + $cron_offset < $this->timeout_late_cron + ) { + $this->last_late_cron = $cron->hook; + return true; + } + } + + return false; + } + /** * Run a loopback test on our site. *