Setting.php
2.56 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
class Setting extends AbstractSetting {
protected $defaultValue = null;
public function __construct($id, StorageInterface $store = null, $params = array()) {
parent::__construct($id, $store, $params);
if ( array_key_exists('default', $params) ) {
$this->defaultValue = $params['default'];
}
$this->dataType = isset($params['type']) ? $params['type'] : $this->dataType;
}
public function getValue($customDefault = null) {
$currentDefault = ($customDefault !== null) ? $customDefault : $this->defaultValue;
if ( $this->store ) {
return $this->store->getValue($currentDefault);
}
return $currentDefault;
}
/**
* Update the value of this setting.
*
* @param $validValue
* @return boolean
*/
public function update($validValue) {
if ( $this->store ) {
$isSuccess = $this->store->setValue($validValue);
$this->notifyUpdated();
return $isSuccess;
}
return false;
}
/**
* Convert a setting value to a string usable in HTML forms.
*
* This does NOT encode special HTML characters. It is only intended to convert
* non-string values - like booleans and NULLs - to a format suitable for form field.
*
* @param $value
* @return string
*/
public function encodeForForm($value) {
//Subclasses that require more advanced encoding should override this method.
return (string)$value;
}
/**
* Convert submitted form data to a type suitable for validation.
* This is not necessarily the same as the setting data type.
*
* @param string $value
*/
public function decodeSubmittedValue($value) {
//The default implementation is a no-op. Subclasses can override this.
return $value;
}
/**
* @param \WP_Error $errors
* @param $value
* @param bool $stopOnFirstError
* @return \WP_Error|mixed
*/
public function validate($errors, $value, $stopOnFirstError = false) {
//Should be overridden by subclasses.
return $value;
}
public function validateFormValue($errors, $value, $stopOnFirstError = false) {
$decodedValue = $this->decodeSubmittedValue($value);
return $this->validate($errors, $decodedValue, $stopOnFirstError);
}
protected function canTreatAsNull($inputValue) {
if ( $this->isNullable() && (($inputValue === null) || ($inputValue === '')) ) {
return true;
}
return false;
}
public function isNullable() {
return ($this->defaultValue === null);
}
/**
* @return mixed|null
*/
public function getDefaultValue() {
return $this->defaultValue;
}
}