| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email Report Section Registry |
| 4 |
* |
| 5 |
* Holds the ordered list of section objects that make up the email body. |
| 6 |
* Free code registers the six built-in sections at boot. Other plugins add |
| 7 |
* sections by hooking `thinkrank_email_report_register_sections` and calling |
| 8 |
* register(). |
| 9 |
* |
| 10 |
* @package ThinkRank |
| 11 |
* @subpackage SEO |
| 12 |
* @since 1.9.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
declare(strict_types=1); |
| 16 |
|
| 17 |
namespace ThinkRank\SEO; |
| 18 |
|
| 19 |
use ThinkRank\SEO\Email_Report_Sections\Email_Report_Section_Interface; |
| 20 |
|
| 21 |
if (!defined('ABSPATH')) { |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Email_Report_Section_Registry |
| 27 |
* |
| 28 |
* @since 1.9.0 |
| 29 |
*/ |
| 30 |
final class Email_Report_Section_Registry { |
| 31 |
|
| 32 |
/** |
| 33 |
* @var array<string,Email_Report_Section_Interface> |
| 34 |
*/ |
| 35 |
private array $sections = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register a section. Last writer wins on key collision so an extension |
| 39 |
* can intentionally override a built-in section if it ever needs to. |
| 40 |
*/ |
| 41 |
public function register(Email_Report_Section_Interface $section): void { |
| 42 |
$this->sections[$section->key()] = $section; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Drop a section by key. Free code shouldn't call this — it's here |
| 47 |
* for tests and extensions. |
| 48 |
*/ |
| 49 |
public function unregister(string $key): void { |
| 50 |
unset($this->sections[$key]); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return array<string,Email_Report_Section_Interface> |
| 55 |
*/ |
| 56 |
public function all(): array { |
| 57 |
return $this->sections; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Resolve the ordered, enabled section list for a given config. Returned |
| 62 |
* in the order they should render. |
| 63 |
* |
| 64 |
* @param array $config Resolved Email Report config. |
| 65 |
* @return array<int,Email_Report_Section_Interface> |
| 66 |
*/ |
| 67 |
public function resolve_for(array $config): array { |
| 68 |
$enabled = $config['sections_enabled'] ?? []; |
| 69 |
|
| 70 |
$resolved = []; |
| 71 |
foreach ($this->sections as $key => $section) { |
| 72 |
if (!in_array($key, $enabled, true)) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
$resolved[$key] = $section; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Filter the ordered section list for a single report. |
| 80 |
* |
| 81 |
* @since 1.9.0 |
| 82 |
* |
| 83 |
* @param array<string,Email_Report_Section_Interface> $resolved Ordered sections, key => section. |
| 84 |
* @param array $config Resolved config. |
| 85 |
*/ |
| 86 |
$resolved = apply_filters('thinkrank_email_report_sections', $resolved, $config); |
| 87 |
|
| 88 |
// Re-validate after filter: drop anything that doesn't implement the contract. |
| 89 |
return array_values(array_filter( |
| 90 |
$resolved, |
| 91 |
static fn ($s) => $s instanceof Email_Report_Section_Interface |
| 92 |
)); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Section catalog for the admin UI, in registry order. |
| 97 |
* |
| 98 |
* @return array<int,array{key:string,label:string,default_enabled:bool}> |
| 99 |
*/ |
| 100 |
public function describe_for_ui(): array { |
| 101 |
$out = []; |
| 102 |
foreach ($this->sections as $key => $section) { |
| 103 |
$out[] = [ |
| 104 |
'key' => $key, |
| 105 |
'label' => $section->label(), |
| 106 |
'default_enabled' => $section->default_enabled(), |
| 107 |
]; |
| 108 |
} |
| 109 |
return $out; |
| 110 |
} |
| 111 |
} |
| 112 |
|