| 1 |
<?php |
| 2 |
/** |
| 3 |
* Result value object for a health check. |
| 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 |
* Immutable outcome of one {@see HealthCheckInterface::run()}. |
| 19 |
* |
| 20 |
* The three status values mirror WordPress' own Site Health vocabulary |
| 21 |
* (`good` / `recommended` / `critical`) so the mapping into |
| 22 |
* `site_status_tests` stays a straight pass-through — no translation |
| 23 |
* table that could drift. |
| 24 |
*/ |
| 25 |
final class HealthCheckResult { |
| 26 |
|
| 27 |
public const STATUS_GOOD = 'good'; |
| 28 |
public const STATUS_RECOMMENDED = 'recommended'; |
| 29 |
public const STATUS_CRITICAL = 'critical'; |
| 30 |
|
| 31 |
/** |
| 32 |
* One of the STATUS_* constants. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $status; |
| 37 |
|
| 38 |
/** |
| 39 |
* Headline shown in the Site Health accordion row. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $label; |
| 44 |
|
| 45 |
/** |
| 46 |
* Plain-text explanation. Escaped by the renderer — pass text, not |
| 47 |
* HTML, so the same string can be reused in the admin notice and in |
| 48 |
* the Site Health → Info tab without double-escaping surprises. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
private $description; |
| 53 |
|
| 54 |
/** |
| 55 |
* Optional call to action. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
private $actionLabel; |
| 60 |
|
| 61 |
/** |
| 62 |
* Optional target for the call to action. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
private $actionUrl; |
| 67 |
|
| 68 |
/** |
| 69 |
* Compact machine value for the Site Health → Info export, e.g. |
| 70 |
* `missing` / `ok`. Kept separate from the description so support |
| 71 |
* can scan a report without reading prose. |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
private $debugValue; |
| 76 |
|
| 77 |
public function __construct( |
| 78 |
string $status, |
| 79 |
string $label, |
| 80 |
string $description = '', |
| 81 |
string $debugValue = '', |
| 82 |
string $actionLabel = '', |
| 83 |
string $actionUrl = '' |
| 84 |
) { |
| 85 |
$allowed = array( self::STATUS_GOOD, self::STATUS_RECOMMENDED, self::STATUS_CRITICAL ); |
| 86 |
$this->status = in_array( $status, $allowed, true ) ? $status : self::STATUS_RECOMMENDED; |
| 87 |
|
| 88 |
$this->label = $label; |
| 89 |
$this->description = $description; |
| 90 |
$this->debugValue = $debugValue !== '' ? $debugValue : $this->status; |
| 91 |
$this->actionLabel = $actionLabel; |
| 92 |
$this->actionUrl = $actionUrl; |
| 93 |
} |
| 94 |
|
| 95 |
public function getStatus(): string { |
| 96 |
return $this->status; |
| 97 |
} |
| 98 |
|
| 99 |
public function getLabel(): string { |
| 100 |
return $this->label; |
| 101 |
} |
| 102 |
|
| 103 |
public function getDescription(): string { |
| 104 |
return $this->description; |
| 105 |
} |
| 106 |
|
| 107 |
public function getDebugValue(): string { |
| 108 |
return $this->debugValue; |
| 109 |
} |
| 110 |
|
| 111 |
public function getActionLabel(): string { |
| 112 |
return $this->actionLabel; |
| 113 |
} |
| 114 |
|
| 115 |
public function getActionUrl(): string { |
| 116 |
return $this->actionUrl; |
| 117 |
} |
| 118 |
|
| 119 |
public function isCritical(): bool { |
| 120 |
return $this->status === self::STATUS_CRITICAL; |
| 121 |
} |
| 122 |
|
| 123 |
public function isGood(): bool { |
| 124 |
return $this->status === self::STATUS_GOOD; |
| 125 |
} |
| 126 |
} |
| 127 |
|