EnumSetting.php
3.98 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\ChoiceControlOption;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
/**
* Enum
*
* Note that if you want to allow NULL, it must be explicitly included as one
* of the possible values. Only setting the default value to NULL is not enough.
*/
class EnumSetting extends Setting {
/**
* @var array
*/
protected $enumValues = array();
protected $choiceDetails = array();
protected $valueEnabled = array();
protected $valueStateCallback = null;
public function __construct($id, StorageInterface $store, $enumValues, $params = array()) {
if ( empty($enumValues) ) {
throw new \InvalidArgumentException('Enum must have at least one possible value');
}
parent::__construct($id, $store, $params);
$this->enumValues = array_values($enumValues);
if ( !in_array($this->defaultValue, $this->enumValues) ) {
$this->defaultValue = reset($this->enumValues);
}
}
public function encodeForForm($value) {
return wp_json_encode($value);
}
public function decodeSubmittedValue($value) {
if ( is_string($value) ) {
return @json_decode($value, true);
}
return parent::decodeSubmittedValue($value);
}
public function validate($errors, $value, $stopOnFirstError = false) {
if ( $this->canTreatAsNull($value) ) {
return null;
}
if ( !in_array($value, $this->enumValues) ) {
$errors->add(
'invalid_value',
'Value must be one of: ' . implode(', ', $this->enumValues)
. '. Received: ' . wp_json_encode($value)
);
return $errors;
}
if ( !$this->isChoiceEnabled($value) ) {
$errors->add('disabled_value', 'That option is currently not allowed');
return $errors;
}
return $value;
}
public function isChoiceEnabled($value) {
if ( !in_array($value, $this->enumValues) ) {
return false;
}
$safeValue = $this->encodeForForm($value);
if ( isset($this->valueEnabled[$safeValue]) ) {
$decider = $this->valueEnabled[$safeValue];
if ( is_scalar($decider) ) {
return (bool)$decider;
} elseif ( is_callable($decider) ) {
return call_user_func($decider, $value);
}
}
if ( isset($this->valueStateCallback) ) {
return call_user_func($this->valueStateCallback, $value);
}
return true;
}
/**
* @param mixed $value
* @param string|null $label
* @param string|null $description
* @param bool|callable|null $state
* @param string|null $icon
* @return EnumSetting
*/
public function describeChoice($value, $label, $description = '', $state = null, $icon = null) {
$safeValue = $this->encodeForForm($value);
$this->choiceDetails[$safeValue] = array(
'label' => $label,
'description' => $description,
'icon' => $icon,
);
if ( $state !== null ) {
$this->valueEnabled[$safeValue] = $state;
}
return $this;
}
/**
* Automatically generate dropdown/radio/etc options from the setting's
* possible values.
*
* Will use custom labels/descriptions if available.
*
* @return \YahnisElsts\AdminMenuEditor\Customizable\Controls\ChoiceControlOption[]
*/
public function generateChoiceOptions() {
$results = array();
foreach ($this->enumValues as $value) {
$encodedValue = $this->encodeForForm($value);
if ( array_key_exists($encodedValue, $this->choiceDetails) ) {
$results[] = new ChoiceControlOption(
$value,
$this->choiceDetails[$encodedValue]['label'],
array(
'description' => $this->choiceDetails[$encodedValue]['description'],
'enabled' => $this->isChoiceEnabled($value),
'icon' => $this->choiceDetails[$encodedValue]['icon'],
)
);
} else {
if ( $value === null ) {
$label = 'Default';
} else {
$label = is_string($value) ? $value : wp_json_encode($value);
$label = ucwords(preg_replace('/[_-]+/', ' ', $label));
}
$results[] = new ChoiceControlOption($value, $label, array(
'enabled' => $this->isChoiceEnabled($value),
));
}
}
return $results;
}
}