*/ private $checks = array(); /** * Memoised results, keyed by check id. * * @var array|null */ private $results = null; /** * Whether the filter pass has already run. Guarded so a second * call doesn't re-append the same objects. * * @var bool */ private $filtered = false; public function register( HealthCheckInterface $check ): void { $this->checks[ $check->getId() ] = $check; $this->results = null; } /** * Every registered check, addon contributions included. * * @return array */ public function all(): array { if ( ! $this->filtered ) { $this->filtered = true; /** * Filter the list of Double Opt-In health checks. * * Addons append their own {@see HealthCheckInterface} * instances here. Entries that are not health checks are * dropped silently — a broken addon must not take the * Site Health page down with it. * * @since 5.3.0 * * @param HealthCheckInterface[] $checks */ $contributed = apply_filters( 'f12_doi_health_checks', array() ); if ( is_array( $contributed ) ) { foreach ( $contributed as $check ) { if ( $check instanceof HealthCheckInterface ) { $this->checks[ $check->getId() ] = $check; } } } $this->results = null; } return $this->checks; } /** * Run every check once per request. * * A check that throws is reported as `recommended` rather than * being allowed to bubble — Site Health is a diagnostic screen and * must stay reachable exactly when something is broken. * * @return array */ public function runAll(): array { if ( $this->results !== null ) { return $this->results; } $out = array(); foreach ( $this->all() as $id => $check ) { try { $out[ $id ] = $check->run(); } catch ( \Throwable $e ) { $out[ $id ] = new HealthCheckResult( HealthCheckResult::STATUS_RECOMMENDED, $check->getLabel(), sprintf( /* translators: %s: the error message thrown by the check. */ __( 'This check could not be completed: %s', 'double-opt-in' ), $e->getMessage() ), 'error' ); } } $this->results = $out; return $out; } /** * Only the results that need the operator's attention. * * @return array */ public function criticals(): array { return array_filter( $this->runAll(), static function ( HealthCheckResult $result ): bool { return $result->isCritical(); } ); } }