PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / src / Settings / SettingsRegistry.php

SettingsRegistry.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at src/Settings/SettingsRegistry.php

129 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace RenzoJohnson\Blocks\Settings;
6
7 use RenzoJohnson\Blocks\Settings\Tabs\AntiSpamTab;
8 use RenzoJohnson\Blocks\Settings\Tabs\AnalyticsTab;
9 use RenzoJohnson\Blocks\Settings\Tabs\AppearanceTab;
10 use RenzoJohnson\Blocks\Settings\Tabs\ContentTypesTab;
11 use RenzoJohnson\Blocks\Settings\Tabs\GeneralTab;
12 use RenzoJohnson\Blocks\Settings\Tabs\MiscTab;
13 use RenzoJohnson\Blocks\Settings\Tabs\PerformanceTab;
14 use RenzoJohnson\Blocks\Settings\Tabs\SecurityTab;
15 use RenzoJohnson\Blocks\Settings\Tabs\SupportTab;
16 use RenzoJohnson\Blocks\Settings\Tabs\WooCommerceTab;
17
18 \defined( 'ABSPATH' ) || exit;
19
20 final class SettingsRegistry {
21 /** @var list<TabDefinition> */
22 private array $tabs;
23
24 /** @var array<string, SettingDefinition> */
25 private array $settings = array();
26
27 /** @var array<string, FieldDefinition> */
28 private array $fields = array();
29
30 /**
31 * @param list<TabProvider> $providers Tab definition providers.
32 * @throws \InvalidArgumentException When identifiers are duplicated.
33 */
34 public function __construct( array $providers ) {
35 $this->tabs = array();
36 $tab_ids = array();
37 $section_ids = array();
38
39 foreach ( $providers as $provider ) {
40 $tab = $provider->definition();
41 $this->assert_unique( $tab->slug, $tab_ids );
42 $tab_ids[] = $tab->slug;
43 $this->tabs[] = $tab;
44
45 foreach ( $tab->sections as $section ) {
46 $this->assert_unique( $section->id, $section_ids );
47 $section_ids[] = $section->id;
48
49 foreach ( $section->fields as $field ) {
50 if ( isset( $this->fields[ $field->id ] ) ) {
51 throw new \InvalidArgumentException( 'Duplicate settings field definition.' );
52 }
53
54 $this->fields[ $field->id ] = $field;
55 foreach ( $field->settings as $setting ) {
56 if ( isset( $this->settings[ $setting->option_name ] ) ) {
57 throw new \InvalidArgumentException( 'Duplicate setting definition.' );
58 }
59
60 $this->settings[ $setting->option_name ] = $setting;
61 }
62 }
63 }
64 }
65 }
66
67 public static function create_default(): self {
68 return new self(
69 array(
70 new GeneralTab(),
71 new AnalyticsTab(),
72 new AppearanceTab(),
73 new SecurityTab(),
74 new AntiSpamTab(),
75 new PerformanceTab(),
76 new ContentTypesTab(),
77 new SupportTab(),
78 new WooCommerceTab(),
79 new MiscTab(),
80 )
81 );
82 }
83
84 /** @return list<TabDefinition> */
85 public function tabs(): array {
86 return $this->tabs;
87 }
88
89 /** @return list<TabDefinition> */
90 public function available_tabs(): array {
91 return \array_values(
92 \array_filter( $this->tabs, static fn ( TabDefinition $tab ): bool => $tab->requirement->satisfied() )
93 );
94 }
95
96 /** @return array<string, SettingDefinition> */
97 public function settings(): array {
98 return $this->settings;
99 }
100
101 /** @return array<string, FieldDefinition> */
102 public function fields(): array {
103 return $this->fields;
104 }
105
106 public function setting( string $option_name ): ?SettingDefinition {
107 return $this->settings[ $option_name ] ?? null;
108 }
109
110 public function field( string $field_id ): ?FieldDefinition {
111 return $this->fields[ $field_id ] ?? null;
112 }
113
114 /** @return array<string, string> */
115 public function public_tokens(): array {
116 return PublicTokenRegistry::all();
117 }
118
119 /**
120 * @param list<string> $ids Existing identifiers.
121 * @throws \InvalidArgumentException When the identifier already exists.
122 */
123 private function assert_unique( string $id, array $ids ): void {
124 if ( \in_array( $id, $ids, true ) ) {
125 throw new \InvalidArgumentException( 'Duplicate settings definition.' );
126 }
127 }
128 }
129