PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / seo / class-email-report-section-registry.php

class-email-report-section-registry.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.7.0, at includes/seo/class-email-report-section-registry.php

112 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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