CompositeSetting.php
1.54 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
class CompositeSetting extends AbstractStructSetting {
public function update($validValue) {
$validValue = $this->filterNewValues($validValue);
return parent::update($validValue);
}
/**
* This should be called after validation and sanitization but before the underlying
* settings are updated or saved.
*
* @param array $values
* @return array
*/
protected function filterNewValues($values) {
return $values;
}
public function preview($unsafeValue, $errors = null) {
if ( $errors === null ) {
$errors = new \WP_Error();
}
//Unlike a general struct, composite settings are all-or-nothing: if any
//children fail validation, all preview values are disregarded.
$validationResult = $this->validate($errors, $unsafeValue, true);
if ( is_wp_error($validationResult) ) {
$previewValues = [];
} else {
$previewValues = $validationResult;
}
$previewValues = $this->filterNewValues($previewValues);
foreach ($this->settings as $key => $setting) {
//Note: In this implementation, child settings might get validated twice.
//because preview() will typically validate the value again.
if ( array_key_exists($key, $previewValues) ) {
$setting->preview($previewValues[$key], $errors);
} else {
$setting->preview(null, $errors);
}
}
}
protected function shouldEnablePostMessageForChildren() {
//A composite settings should be updated or previewed as a whole,
//so we don't need to enable postMessage for its children.
return false;
}
}