AbstractStructSetting.php
7.24 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use YahnisElsts\AdminMenuEditor\Customizable\Storage;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
abstract class AbstractStructSetting extends AbstractSetting implements \ArrayAccess, \IteratorAggregate {
protected $dataType = 'map';
/**
* @var array<string,AbstractSetting>
*/
protected $settings = [];
/**
* @var callable
*/
protected $childUpdateCallback;
protected $childSubscriptionsAdded = false;
protected $isInUpdateLoop = false;
public function __construct($id, StorageInterface $store = null, $params = []) {
//Minor optimization: Create the callback array once and reuse it for every child.
$this->childUpdateCallback = [$this, 'notifyChildWasUpdated'];
if ( $store === null ) {
$store = new Storage\NullStorage();
}
parent::__construct($id, $store, $params);
}
/**
* @param \WP_Error $errors
* @param array<string,mixed> $value
* @return \WP_Error|array<string,mixed>
*/
public function validate($errors, $value, $stopOnFirstError = false) {
if ( !is_array($value) ) {
$errors->add('struct_value_invalid', 'Struct value must be an associative array');
return $errors;
}
$validatedValues = [];
$foundErrors = false;
foreach ($this->settings as $key => $setting) {
if ( array_key_exists($key, $value) ) {
$validity = $setting->validate($errors, $value[$key], $stopOnFirstError);
if ( is_wp_error($validity) ) {
$foundErrors = true;
if ( $stopOnFirstError ) {
break;
}
} else {
$validatedValues[$key] = $validity;
}
}
}
if ( $foundErrors ) {
return $errors;
} else {
return $validatedValues;
}
}
public function update($validValue) {
$this->isInUpdateLoop = true;
$isSuccess = true;
foreach ($this->settings as $key => $setting) {
if ( array_key_exists($key, $validValue) ) {
$isSuccess = $isSuccess && $setting->update($validValue[$key]);
}
}
$this->isInUpdateLoop = false;
$this->notifyUpdated();
return $isSuccess;
}
public function getValue($customDefault = []) {
$result = is_array($customDefault) ? $customDefault : [];
foreach ($this->settings as $key => $setting) {
$result[$key] = $setting->getValue();
}
return $result;
}
public function preview($unsafeValue, $errors = null) {
if ( !is_array($unsafeValue) ) {
return;
}
if ( $errors === null ) {
$errors = new \WP_Error();
}
foreach ($this->settings as $key => $setting) {
if ( array_key_exists($key, $unsafeValue) ) {
$setting->preview($unsafeValue[$key], $errors);
}
}
}
/**
* @return array
*/
public function getDefaultValue() {
return [];
}
public function canBeDeleted() {
if ( $this->deleteWhenBlank ) {
//In addition to other "blank" states, a struct can also be deleted
//if it has no children or if all of its children can be deleted.
$isDeletable = true;
foreach ($this->settings as $setting) {
if ( !$setting->canBeDeleted() ) {
$isDeletable = false;
break;
}
}
if ( $isDeletable ) {
return true;
}
}
return parent::canBeDeleted();
}
public function subscribe($callback) {
parent::subscribe($callback);
//Optimization: Listen for child updates only if we have subscribers.
if ( !empty($this->updateSubscribers) && !$this->childSubscriptionsAdded ) {
$this->childSubscriptionsAdded = true;
foreach ($this->settings as $setting) {
$setting->subscribe($this->childUpdateCallback);
}
}
}
/**
* @internal This method needs to be public because it's used as a callback,
* but you should not call it directly.
*
* @noinspection PhpUnusedParameterInspection In the current implementation,
* we don't care which specific child was updated, only that one was.
*
* @param AbstractSetting $childSetting
* @return void
*/
public function notifyChildWasUpdated(AbstractSetting $childSetting) {
//If we're inside the foreach loop in update(), we don't need to notify
//our subscribers here - update() will do it after the loop.
//Also, we currently don't support recursive notifications, so if a child
//is updated while we're sending notifications, we'll just ignore it.
if ( $this->isInUpdateLoop || $this->isNotifyingSubscribers ) {
return;
}
//Optimization: Multiple children can be updated at the same time, and
//it would be inefficient to notify the parent's subscribers every time.
//Instead, we'll put the parent in a queue. The code performing the update
//should send pending notifications when it's done.
static::getNotificationQueue()->enqueue($this);
}
/**
* @param string $key
* @return AbstractSetting|null
*/
public function getChild($key) {
if ( array_key_exists($key, $this->settings) ) {
return $this->settings[$key];
}
return null;
}
public function getChildValue($childSettingKey, $defaultValue = null) {
if ( array_key_exists($childSettingKey, $this->settings) ) {
return $this->settings[$childSettingKey]->getValue($defaultValue);
}
return $defaultValue;
}
public function makeChildId($childKey) {
return $this->id . '.' . $childKey;
}
/**
* Create a child setting of the specified type.
*
* @param string $childKey
* @param class-string<AbstractSetting> $className
* @param ...$constructorParams
* @return AbstractSetting
*/
protected function createChild($childKey, $className, ...$constructorParams) {
$child = new $className(
$this->makeChildId($childKey),
$this->store->buildSlot($childKey),
...$constructorParams
);
if ( $this->shouldEnablePostMessageForChildren() ) {
$child->enablePostMessageSupport();
}
//Children inherit the parent's tags.
$child->addTags(...$this->tags);
$this->registerChild($childKey, $child);
return $child;
}
protected function registerChild($childKey, AbstractSetting $child) {
$this->settings[$childKey] = $child;
if ( $this->childSubscriptionsAdded ) {
$child->subscribe($this->childUpdateCallback);
}
}
public function enablePostMessageSupport() {
parent::enablePostMessageSupport();
if ( $this->shouldEnablePostMessageForChildren() ) {
foreach ($this->settings as $setting) {
$setting->enablePostMessageSupport();
}
}
}
protected function shouldEnablePostMessageForChildren() {
return $this->supportsPostMessage;
}
/** @noinspection PhpLanguageLevelInspection */
#[\ReturnTypeWillChange]
public function offsetExists($offset) {
return array_key_exists($offset, $this->settings);
}
/**
* @param string $offset
* @return AbstractSetting|null
* @noinspection PhpLanguageLevelInspection
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
return $this->getChild($offset);
}
/** @noinspection PhpLanguageLevelInspection */
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value) {
throw new \LogicException(
'Cannot add or replace a child of a struct. The setting list is read-only.'
);
}
/** @noinspection PhpLanguageLevelInspection */
#[\ReturnTypeWillChange]
public function offsetUnset($offset) {
throw new \LogicException(
'Cannot remove a child of a struct. The setting list is read-only.'
);
}
/** @noinspection PhpLanguageLevelInspection */
#[\ReturnTypeWillChange]
public function getIterator() {
return new \ArrayIterator($this->settings);
}
}