AbstractSetting.php
15.7 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use WP_Error;
use YahnisElsts\AdminMenuEditor\Customizable\Customizable;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
abstract class AbstractSetting extends Customizable implements UpdateNotificationSender {
const SERIALIZE_INCLUDE_VALUE = 1;
const SERIALIZE_INCLUDE_DEFAULT = 2;
const SERIALIZE_INCLUDE_ID = 4;
const SERIALIZE_INCLUDE_GROUP_TITLE = 8;
const SERIALIZE_INCLUDE_POST_MESSAGE_SUPPORT = 16;
const SERIALIZE_INCLUDE_TAGS = 32;
const SERIALIZE_INCLUDE_VALIDATION = 64;
const SERIALIZE_INCLUDE_ALL = (self::SERIALIZE_INCLUDE_ID | self::SERIALIZE_INCLUDE_VALUE
| self::SERIALIZE_INCLUDE_DEFAULT | self::SERIALIZE_INCLUDE_GROUP_TITLE
| self::SERIALIZE_INCLUDE_POST_MESSAGE_SUPPORT
| self::SERIALIZE_INCLUDE_TAGS
| self::SERIALIZE_INCLUDE_VALIDATION
);
const SERIALIZE_LEAVES_ONLY = 128;
const TAG_ADMIN_THEME = 'admin_theme';
/**
* The data type could be useful for automatically choosing an appropriate
* control, if applicable.
*
* @var string
*/
protected $dataType = 'string';
/**
* @var array
*/
protected $recommendedControls = [];
/**
* @var null|callable
*/
protected $isEditableCallback = null;
/**
* Whether to delete the setting from storage when its value is blank.
*
* Blank values are: NULL, '', empty array. Note that this is intentionally
* different from the behaviour of the empty() language construct which also
* considers 0.0, '0', and false to be empty.
*
* @var bool
*/
protected $deleteWhenBlank = false;
/**
* @var callable[]
*/
protected $updateSubscribers = [];
protected $isNotifyingSubscribers = false;
/**
* The queue should be shared by all settings.
*
* @var null|UniqueSettingQueue
*/
protected static $updateNotificationQueue = null;
/**
* @var bool Whether the setting supports updating its preview via postMessage.
*/
protected $supportsPostMessage = false;
/**
* @var array<string> List of tags that can be used to group settings.
*/
protected $tags = [];
public function __construct($id, StorageInterface $store = null, $params = []) {
parent::__construct($id, $store, $params);
if ( isset($params['isEditable']) && is_callable($params['isEditable']) ) {
$this->isEditableCallback = $params['isEditable'];
}
if ( isset($params['deleteWhenBlank']) ) {
$this->deleteWhenBlank = (bool)$params['deleteWhenBlank'];
}
if ( isset($params['supportsPostMessage']) ) {
$this->supportsPostMessage = (bool)$params['supportsPostMessage'];
}
if ( isset($params['tags']) ) {
$this->tags = $params['tags'];
}
}
/**
* Validate and sanitize a setting value.
*
* On success, this method returns the sanitized value. If there is
* a validation error, it returns a WP_Error instance instead.
*
* @param \WP_Error $errors
* @param array<string,mixed>|mixed $value
* @param bool $stopOnFirstError Only applies to settings that have children. Other settings may ignore this parameter.
* @return \WP_Error|mixed
*/
abstract public function validate($errors, $value, $stopOnFirstError = false);
/**
* Update the value of this setting.
*
* This method assumes that the value has already been validated and sanitized,
* and that any applicable permissions have been checked.
*
* This may not immediately write the new value to the database. Call the save()
* method on the underlying storage to ensure that the value is saved.
*
* @param $validValue
* @return boolean
*/
abstract public function update($validValue);
/**
* @param mixed $customDefault
* @return mixed
*/
abstract public function getValue($customDefault = null);
/**
* @return mixed
*/
abstract public function getDefaultValue();
/**
* Enable preview mode for the current request. This will make the setting
* return the specified value instead of its actual value.
*
* Note: Usually, the preview value will be validated before it's passed
* to this method. However, in some cases, a value can be sent to the preview
* frame before it's saved in the changeset, so it will only have undergone
* JS-based validation, not full server-side validation. This means it's
* a good idea to validate the preview value even if that will sometimes
* duplicate work that has already been done.
*
* Also, even a value that has already been validated and saved in a changeset
* can occasionally become invalid later. For example, an image could be deleted
* from the media library. In exceptional cases, validation rules themselves
* could change as part of a plugin update.
*
* @param $unsafeValue
* @param \WP_Error|null $errors Optional. To avoid the creation of temporary
* WP_Error instances during value validation, you can provide an existing error
* object to this method.
*
* @return void
*/
public function preview($unsafeValue, $errors = null) {
if ( $errors === null ) {
$errors = new WP_Error();
}
$validationResult = $this->validate($errors, $unsafeValue, true);
if ( is_wp_error($validationResult) ) {
$previewValue = $this->getDefaultValue();
} else {
$previewValue = $validationResult;
}
if ( $this->store ) {
$this->store->setPreviewValue($previewValue);
}
}
/**
* Validate a value that has been submitted via an HTML form.
*
* For most settings, this is simply an alias for the `validate()` method.
* However, some settings may contain values that can't be directly represented
* in an HTML form, like `null` or objects. Values like that will need to be
* encoded/decoded when used in HTML. This method provides a way to decode
* and validate submitted form values.
*
* When the data comes from a form, you should use this method instead
* of `validate()` to ensure that the data is properly decoded.
*
* @param \WP_Error $errors
* @param array<string,mixed>|mixed $value
* @param bool $stopOnFirstError
* @return \WP_Error|mixed
*/
public function validateFormValue($errors, $value, $stopOnFirstError = false) {
return $this->validate($errors, $value, $stopOnFirstError);
}
public function getDataType() {
return $this->dataType;
}
public function getRecommendedControls() {
return $this->recommendedControls;
}
/**
* Is the current user allowed to change this setting?
*
* @return bool
*/
public function isEditableByUser() {
if ( isset($this->isEditableCallback) ) {
return call_user_func($this->isEditableCallback);
}
return true;
}
/**
* Is it currently OK to delete this setting from storage?
*
* For example, some settings may choose to be removed when their value
* is empty, NULL, or equal to the default value.
*
* @return bool
*/
public function canBeDeleted() {
if ( $this->deleteWhenBlank ) {
$value = $this->getValue();
if ( ($value === null) || ($value === '') ) {
return true;
} else if ( is_array($value) && empty($value) ) {
return true;
}
}
return false;
}
/**
* Apply one or more validators to a setting value.
*
* Each validator should be a callable that takes two arguments:
* - The value to validate
* - A WP_Error object to which validation errors should be added.
*
* The callable should return the validated value, or a WP_Error instance
* if there was a validation error.
*
* @param callable[] $validators
* @param mixed $value
* @param \WP_Error $errors
* @param boolean $stopOnFirstError
* @return mixed|\WP_Error
*/
protected static function applyValidators($validators, $value, $errors, $stopOnFirstError = false) {
$convertedValue = $value;
$hasErrors = false;
foreach ($validators as $validator) {
$result = call_user_func($validator, $convertedValue, $errors);
if ( is_wp_error($result) ) {
$hasErrors = true;
if ( $stopOnFirstError ) {
return $result;
}
} else {
$convertedValue = $result;
}
}
return $hasErrors ? $errors : $convertedValue;
}
/**
* Notify the setting that its value has been updated.
*
* Usually, the setting itself will call this method from its update() method.
* If you change the value without calling the update() method, such as by
* directly updating a child of a struct, you should call this method manually.
*
* @return void
*/
public function notifyUpdated() {
if ( $this->store && $this->canBeDeleted() ) {
$this->store->deleteValue();
}
//Disallow recursive notifications as it could lead to infinite loops.
//If necessary, we can change this later.
if ( !$this->isNotifyingSubscribers ) {
$this->isNotifyingSubscribers = true;
//We'll notify subscribers now, so remove the setting from the notification
//queue. This can be redundant if this method gets called while processing
//the queue, but it's probably not a big deal.
if ( static::$updateNotificationQueue ) {
static::$updateNotificationQueue->remove($this);
}
foreach ($this->updateSubscribers as $callback) {
call_user_func($callback, $this);
}
$this->isNotifyingSubscribers = false;
}
}
/**
* @param callable $callback
* @return void
*/
public function subscribe($callback) {
$this->updateSubscribers[] = $callback;
}
/**
* @return bool
*/
public function supportsPostMessage() {
return $this->supportsPostMessage;
}
public function enablePostMessageSupport() {
$this->supportsPostMessage = true;
return $this;
}
//region Tags
/**
* @return string[]
*/
public function getTags() {
return $this->tags;
}
/**
* @param string[] $tags
* @return $this
*/
public function addTags(...$tags) {
$this->tags = array_unique(array_merge($this->tags, $tags));
return $this;
}
/**
* @param string $tag
* @return bool
*/
public function hasTag($tag) {
return in_array($tag, $this->tags);
}
//endregion
/**
* @return array|null
*/
public function serializeValidationRules() {
return null; //It's up to subclasses to implement this.
}
protected static function getNotificationQueue() {
if ( !static::$updateNotificationQueue ) {
static::$updateNotificationQueue = new UniqueSettingQueue();
}
return static::$updateNotificationQueue;
}
public static function sendPendingNotifications() {
if ( !static::$updateNotificationQueue ) {
return;
}
$queue = static::$updateNotificationQueue;
$queue->processAll();
}
/**
* @param AbstractSetting[] $settings
* @return void
*/
public static function saveAll($settings) {
static::sendPendingNotifications();
//Find and deduplicate the stores that contain these settings.
$stores = new \SplObjectStorage();
foreach ($settings as $setting) {
if ( $setting->store ) {
$stores->attach($setting->store->getSmallestSavable());
}
}
//Tell each store to save its data.
foreach ($stores as $store) {
/** @var StorageInterface $store */
$store->save();
}
}
/**
* @param AbstractSetting[] $settingsToWatch
* @param callable $callback Expected signature: function($updatedSettings) => void
*/
public static function subscribeDeferred($settingsToWatch, $callback) {
if ( empty($settingsToWatch) ) {
return;
}
$queue = static::getNotificationQueue();
new DeferredUpdateSubscriber($queue, $settingsToWatch, $callback);
//The subscriber object doesn't need to be stored anywhere because it will
//automatically add itself as a subscriber to each setting.
}
/**
* Recursively iterate over a collection of settings.
*
* This method will not recurse into composite settings, but it will return
* the children of regular structs.
*
* @param iterable $settings
* @param bool $leavesOnly When encountering a struct setting, only return its
* children and not the struct itself. Does not apply
* to composite settings.
* @param string|int|null $parentKey The key of the parent setting. Used to generate
* an iterator key for each setting.
* @return \Generator
*/
public static function recursivelyIterateSettings($settings, $leavesOnly = false, $parentKey = null) {
foreach ($settings as $key => $setting) {
if ( $parentKey !== null ) {
$effectiveKey = ((string)$parentKey) . '.' . $key;
} else {
$effectiveKey = $key;
}
//Descend into structs and arrays, except composite settings.
//WP 4.9.6+ includes a polyfill for is_iterable().
$isContainer = is_iterable($setting) && !($setting instanceof CompositeSetting);
if ( ($setting instanceof AbstractSetting) && (!$isContainer || !$leavesOnly) ) {
yield $effectiveKey => $setting;
}
if ( $isContainer ) {
/** @var iterable $setting */
yield from self::recursivelyIterateSettings($setting, $leavesOnly, $effectiveKey);
}
}
}
/**
* Recursively serialize a collection of settings for use in JavaScript.
*
* Optionally, you can provide a callback that will be called for each setting.
* It can be used to modify the serialized data. The callback will be called
* with two arguments:
* - The serialized data as an associative array.
* - The setting object.
*
* The callback should return an associative array. Alternatively, it can return
* `null` to exclude the setting from the result.
*
* @param AbstractSetting[] $settings
* @param int|null $flags
* @param callable $customizer Optional. A callback that can be used to modify each
* setting's serialized data.
* @return array<string,array> A map of setting IDs to serialized settings.
*/
public static function serializeSettingsForJs(
$settings,
$flags = self::SERIALIZE_INCLUDE_ALL,
$customizer = null
) {
//Right now, the serialization process is fairly straightforward, so we
//just do it here. If it becomes more complex, we could add a serializeForJs()
//method to individual settings, or add a separate serializer class.
if ( $flags === null ) {
$flags = self::SERIALIZE_INCLUDE_ALL;
}
$leavesOnly = (bool)($flags & self::SERIALIZE_LEAVES_ONLY);
$serialized = [];
foreach (self::recursivelyIterateSettings($settings, $leavesOnly) as $setting) {
$emptyArraysAsObjects = ($setting->getDataType() === 'map');
$data = [];
if ( $flags & self::SERIALIZE_INCLUDE_ID ) {
$data['id'] = $setting->id;
}
if ( $flags & self::SERIALIZE_INCLUDE_DEFAULT ) {
$data['default'] = $setting->getDefaultValue();
if ( $emptyArraysAsObjects && is_array($data['default']) && empty($data['default']) ) {
$data['default'] = (object)$data['default'];
}
}
if ( $flags & self::SERIALIZE_INCLUDE_VALUE ) {
//Note: This will use the previewed value if one is available.
$data['value'] = $setting->getValue();
if ( $emptyArraysAsObjects && is_array($data['value']) && empty($data['value']) ) {
$data['value'] = (object)$data['value'];
}
}
if ( $flags & self::SERIALIZE_INCLUDE_GROUP_TITLE ) {
$groupTitle = $setting->getCustomGroupTitle();
if ( ($groupTitle !== null) && ($groupTitle !== '') ) {
$data['groupTitle'] = $groupTitle;
}
}
if ( $flags & self::SERIALIZE_INCLUDE_POST_MESSAGE_SUPPORT ) {
if ( $setting->supportsPostMessage() ) {
$data['supportsPostMessage'] = true;
}
}
if ( $flags & self::SERIALIZE_INCLUDE_TAGS ) {
$tags = $setting->getTags();
if ( !empty($tags) ) {
$data['tags'] = $tags;
}
}
if ( $flags & self::SERIALIZE_INCLUDE_VALIDATION ) {
$validationRules = $setting->serializeValidationRules();
if ( !empty($validationRules) ) {
$data['validation'] = $validationRules;
}
}
if ( $customizer ) {
$data = call_user_func($customizer, $data, $setting);
//Skip settings excluded by the callback.
if ( $data === null ) {
continue;
}
}
$serialized[$setting->id] = $data;
}
return $serialized;
}
}