-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-settings-store.php
More file actions
executable file
·303 lines (261 loc) · 6.64 KB
/
class-settings-store.php
File metadata and controls
executable file
·303 lines (261 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* Class Settings_Store
*
* @package wpct-plugin
*/
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
namespace WPCT_PLUGIN;
use ArgumentCountError;
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
require_once 'class-singleton.php';
require_once 'class-setting.php';
require_once 'class-rest-settings-controller.php';
require_once 'class-undefined.php';
require_once 'json-schema-utils.php';
/**
* Settings store base class.
*/
class Settings_Store extends Singleton {
/**
* Handle plugin settings rest controller class name.
*
* @var string
*/
const REST_CONTROLLER = '\WPCT_PLUGIN\REST_Settings_Controller';
/**
* Handle settings' group name.
*
* @var string
*/
private $group;
/**
* Handle settings instanes store.
*
* @var array
*/
private $store = array();
/**
* Add a new setting to the store.
*
* @param string $setting_name Setting name.
* @param Setting $setting Setting instance.
*/
private static function store_setting( $setting_name, $setting ) {
static::get_instance()->store[ $setting_name ] = $setting;
}
/**
* Proxy to the store settings `use_setter` hooks.
*
* @param string $name Setting name.
* @param callable $setter Callback function.
* @param integer $priority Priority of the callabck in the setters chain.
*/
final public static function use_setter( $name, $setter, $priority = 10 ) {
$setting = static::setting( $name );
if ( $setting ) {
$setting->use_setter( $setter, $priority );
}
}
/**
* Proxy to the store settings `use_getter` hooks.
*
* @param string $name Setting name.
* @param callable $getter Callback function.
* @param integer $priority Priority of the callback in the getters chain.
*/
final public static function use_getter( $name, $getter, $priority = 10 ) {
$setting = static::setting( $name );
if ( $setting ) {
$setting->use_getter( $getter, $priority );
}
}
/**
* Proxy to the store settings `use_cleaner` hooks.
*
* @param string $name Setting name.
* @param callable $cleaner Callback function.
* @param integer $priority Priority of the cleaner in the cleaners chain.
*/
final public static function use_cleaner(
$name,
$cleaner,
$priority = 10
) {
$setting = static::setting( $name );
if ( $setting ) {
$setting->use_cleaner( $cleaner, $priority );
}
}
/**
* Register a setting in the store.
*
* @param array|callable $setting Setting's schema or a filter function. The function will be
* called with the array of registered settings and should return
* a new array of settings as its output.
*/
final public static function register_setting( $setting ) {
add_filter(
'wpct_plugin_register_settings',
static function ( $settings, $group ) use ( $setting ) {
if ( static::group() === $group ) {
if ( is_array( $setting ) ) {
$settings[] = $setting;
} elseif ( is_callable( $setting ) ) {
$filter = $setting;
$settings = $filter( $settings );
}
}
return $settings;
},
10,
2
);
}
/**
* Hook to execute callbacks once the settings are registered
* and the store is ready.
*
* @param callable $callback Callback to be executed.
*/
final public static function ready( $callback ) {
if ( ! is_callable( $callback ) ) {
return;
}
add_filter(
'wpct_plugin_registered_settings',
static function ( $settings, $group, $store ) use ( $callback ) {
if ( static::group() === $group ) {
$callback( $store, $group );
}
},
10,
3
);
}
/**
* Store the store group and set up init hook callbacks.
*
* @param aryay{0: string} ...$args Array with setting's group name in its first position.
*
* @throws ArgumentCountError If no group argument.
*/
protected function construct( ...$args ) {
if ( empty( $args ) ) {
throw new ArgumentCountError( 'Too few arguments to Settigs Store constructor' );
}
$this->group = $args[0];
$rest_controller_class = static::REST_CONTROLLER;
$rest_controller_class::setup( $this->group );
add_action(
'init',
function () {
$settings = static::register_settings();
do_action(
'wpct_plugin_registered_settings',
$settings,
$this->group,
$this
);
},
10,
5
);
}
/**
* Group name getter.
*
* @return string $group_name settings group name
*/
final public static function group() {
return static::get_instance()->group;
}
/**
* Store data getter.
*
* @return array<string, Setting> Array with store's settings instances.
*/
final public static function store() {
return static::get_instance()->store ?: array();
}
/**
* Instance's store settings collection getter.
*
* @return Setting[]
*/
final public static function settings() {
return array_values( static::store() );
}
/**
* Store settings getter.
*
* @param string $name Setting name.
*
* @return Setting|null
*/
final public static function setting( $name ) {
$store = static::store();
if ( empty( $store ) ) {
return;
}
return $store[ $name ] ?? null;
}
/**
* Registers a setting and its fields.
*
* @return array list with setting instances
*/
private static function register_settings() {
$group = static::group();
$schemas = apply_filters(
'wpct_plugin_register_settings',
array(),
$group
);
$settings = array();
foreach ( $schemas as $schema ) {
if ( ! is_array( $schema ) || ! is_string( $schema['name'] ?? null ) ) {
continue;
}
$name = $schema['name'];
$setting = static::setting( $name );
if ( $setting ) {
$settings[] = $setting;
continue;
}
if (
isset( $schema['properties'] ) &&
is_array( $schema['properties'] )
) {
$default_required = array_keys( $schema['properties'] );
}
$schema = array_merge(
array(
'$id' => $group . '_' . $name,
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => "Setting {$name} of {$group}",
'type' => 'object',
'properties' => array(),
'required' => $default_required ?? array(),
'additionalProperties' => false,
'default' => array(),
),
$schema
);
$default = is_array( $schema['default'] )
? $schema['default']
: array();
foreach ( $default as $prop => $value ) {
if ( isset( $schema['properties'][ $prop ] ) ) {
$schema['properties'][ $prop ]['default'] = $value;
}
}
$setting = new Setting( $group, $name, $default, $schema );
static::store_setting( $name, $setting );
$settings[] = $setting;
}
return $settings;
}
}