| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registry of runtime health checks. |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Health |
| 6 |
* @since 5.3.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\Health; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Collects every {@see HealthCheckInterface} the Core and its addons |
| 19 |
* contribute, and runs them on demand. |
| 20 |
* |
| 21 |
* Two ways in: |
| 22 |
* |
| 23 |
* - `register()` — used by Core itself and by anything holding the |
| 24 |
* container instance. |
| 25 |
* - the `f12_doi_health_checks` filter — the addon-facing path. An |
| 26 |
* addon appends its check objects to the array; if it runs against |
| 27 |
* an older Core that has no Health API, the filter simply never |
| 28 |
* fires and the addon keeps working. |
| 29 |
* |
| 30 |
* Results are memoised per request: the Site Health page and the admin |
| 31 |
* notice both ask for them, and a `SHOW TABLES` per asker would be |
| 32 |
* wasteful for no gain. |
| 33 |
*/ |
| 34 |
final class HealthCheckRegistry { |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array<string,HealthCheckInterface> |
| 38 |
*/ |
| 39 |
private $checks = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Memoised results, keyed by check id. |
| 43 |
* |
| 44 |
* @var array<string,HealthCheckResult>|null |
| 45 |
*/ |
| 46 |
private $results = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether the filter pass has already run. Guarded so a second |
| 50 |
* call doesn't re-append the same objects. |
| 51 |
* |
| 52 |
* @var bool |
| 53 |
*/ |
| 54 |
private $filtered = false; |
| 55 |
|
| 56 |
public function register( HealthCheckInterface $check ): void { |
| 57 |
$this->checks[ $check->getId() ] = $check; |
| 58 |
$this->results = null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Every registered check, addon contributions included. |
| 63 |
* |
| 64 |
* @return array<string,HealthCheckInterface> |
| 65 |
*/ |
| 66 |
public function all(): array { |
| 67 |
if ( ! $this->filtered ) { |
| 68 |
$this->filtered = true; |
| 69 |
|
| 70 |
/** |
| 71 |
* Filter the list of Double Opt-In health checks. |
| 72 |
* |
| 73 |
* Addons append their own {@see HealthCheckInterface} |
| 74 |
* instances here. Entries that are not health checks are |
| 75 |
* dropped silently — a broken addon must not take the |
| 76 |
* Site Health page down with it. |
| 77 |
* |
| 78 |
* @since 5.3.0 |
| 79 |
* |
| 80 |
* @param HealthCheckInterface[] $checks |
| 81 |
*/ |
| 82 |
$contributed = apply_filters( 'f12_doi_health_checks', array() ); |
| 83 |
|
| 84 |
if ( is_array( $contributed ) ) { |
| 85 |
foreach ( $contributed as $check ) { |
| 86 |
if ( $check instanceof HealthCheckInterface ) { |
| 87 |
$this->checks[ $check->getId() ] = $check; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$this->results = null; |
| 93 |
} |
| 94 |
|
| 95 |
return $this->checks; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Run every check once per request. |
| 100 |
* |
| 101 |
* A check that throws is reported as `recommended` rather than |
| 102 |
* being allowed to bubble — Site Health is a diagnostic screen and |
| 103 |
* must stay reachable exactly when something is broken. |
| 104 |
* |
| 105 |
* @return array<string,HealthCheckResult> |
| 106 |
*/ |
| 107 |
public function runAll(): array { |
| 108 |
if ( $this->results !== null ) { |
| 109 |
return $this->results; |
| 110 |
} |
| 111 |
|
| 112 |
$out = array(); |
| 113 |
foreach ( $this->all() as $id => $check ) { |
| 114 |
try { |
| 115 |
$out[ $id ] = $check->run(); |
| 116 |
} catch ( \Throwable $e ) { |
| 117 |
$out[ $id ] = new HealthCheckResult( |
| 118 |
HealthCheckResult::STATUS_RECOMMENDED, |
| 119 |
$check->getLabel(), |
| 120 |
sprintf( |
| 121 |
/* translators: %s: the error message thrown by the check. */ |
| 122 |
__( 'This check could not be completed: %s', 'double-opt-in' ), |
| 123 |
$e->getMessage() |
| 124 |
), |
| 125 |
'error' |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$this->results = $out; |
| 131 |
|
| 132 |
return $out; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Only the results that need the operator's attention. |
| 137 |
* |
| 138 |
* @return array<string,HealthCheckResult> |
| 139 |
*/ |
| 140 |
public function criticals(): array { |
| 141 |
return array_filter( |
| 142 |
$this->runAll(), |
| 143 |
static function ( HealthCheckResult $result ): bool { |
| 144 |
return $result->isCritical(); |
| 145 |
} |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
|