PluginProbe
WPIDE – File Manager & Code Editor / 3.5.5
WPIDE – File Manager & Code Editor v3.5.5
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
← All changes | App/Config/Config.php +49 -92 3.23.5.5 View file →
@@ -1,13 +1,22 @@
1 1 <?php
2 2 namespace WPIDE\App\Config;
3 +
4 +use Adbar\Dot;
3 5 use const WPIDE\Constants\SLUG;
4 6
5 7 class Config
6 8 {
9 + /* @var $config array */
7 10 protected $config;
11 +
12 + /* @var $values Dot */
8 13 protected $values;
14 +
15 + /* @var $defaults Dot */
9 16 protected $defaults;
17 +
18 + /* @var $meta_key string */
10 19 protected $meta_key;
11 20
12 21 public function __construct(array $config = [])
13 22 {
@@ -21,21 +30,22 @@
21 30
22 31 public function loadDefaults()
23 32 {
24 33
25 - $this->defaults = $this->getConfigDefaults($this->config);
34 + $this->defaults = new Dot($this->getConfigDefaults($this->config));
26 35 }
27 36
28 37 public function loadValues()
29 38 {
30 39
31 - $this->values = $this->mergeValues($this->defaults, $this->getOption());
40 + $options = $this->getOption();
41 + $this->values = new Dot($options);
32 42 }
33 43
34 44 public function getDefaults(): array
35 45 {
36 46
37 - return $this->defaults;
47 + return $this->defaults->all();
38 48 }
39 49
40 50 protected function getConfigDefaults($config): array
41 51 {
@@ -47,9 +57,9 @@
47 57 }
48 58
49 59 foreach($config as $key => $conf) {
50 60
51 - $values[$key] = isset($conf['default']) ? $conf['default'] : $this->getConfigDefaults($config[$key]);
61 + $values[$key] = $conf['default'] ?? $this->getConfigDefaults($conf);
52 62 }
53 63
54 64 return $values;
55 65 }
@@ -59,9 +69,9 @@
59 69 $config = get_option($this->meta_key);
60 70
61 71 if(empty($config)) {
62 72
63 - $config = $this->defaults;
73 + $config = $this->defaults->all();
64 74 $this->saveOption($config);
65 75 }
66 76
67 77 return $config;
@@ -66,47 +76,14 @@
66 76
67 77 return $config;
68 78 }
69 79
70 - public function saveOption($config)
80 + public function saveOption($config): bool
71 81 {
72 - update_option($this->meta_key, $config);
82 + $updated = update_option($this->meta_key, $config);
73 83 $this->loadValues();
74 - }
75 84
76 - public function updateConfig(object $config): bool
77 - {
78 - $_config = $this->getOption();
79 -
80 - if(empty($config)) {
81 - return false;
82 - }
83 -
84 - foreach($config as $k => $value) {
85 -
86 - // Check that key exists
87 - if(!isset($this->values[$k])) {
88 - return false;
89 - }
90 -
91 - if(is_object($value)) {
92 - $value = (array)$value;
93 - }
94 -
95 - // Check that old and new value are the same type
96 - $_type = gettype($this->values[$k]);
97 - $type = gettype($value);
98 -
99 - if($_type !== $type) {
100 - return false;
101 - }
102 -
103 - $_config[$k] = $value;
104 - }
105 -
106 - $this->saveOption($_config);
107 -
108 - return true;
85 + return $updated;
109 86 }
110 87
111 88 public function getConfigFields(): array
112 89 {
@@ -115,75 +92,55 @@
115 92
116 93 public function get($key = null, $default = null)
117 94 {
118 95 if (is_null($key)) {
119 - return $this->values;
96 + return $this->values->all();
120 97 }
121 98
122 - $key = is_array($key) ? $key : explode('.', $key);
99 + return $this->values->get($key, $default);
123 100
124 - $target = $this->values;
101 + }
125 102
126 - while (! is_null($segment = array_shift($key))) {
127 - if (is_array($target) && array_key_exists($segment, $target)) {
128 - $target = $target[$segment];
129 - } else {
130 - return $default;
103 + public function update($key, $value)
104 + {
105 +
106 + if($this->fieldExists($key)) {
107 +
108 + if(is_object($value)) {
109 + $value = (array) $value;
131 110 }
132 - }
133 111
134 - return $target;
135 - }
112 + $this->values->set($key, $value);
136 113
137 - public function getField($key = null, $fieldKey = null)
138 - {
139 - if (is_null($key)) {
140 - return $this->values;
114 + $this->saveOption($this->values->all());
115 +
116 + return $this->values->all();
117 +
141 118 }
142 119
143 - $key = is_array($key) ? $key : explode('.', $key);
120 + return false;
121 + }
144 122
145 - $target = $this->config;
123 + public function delete($key) {
146 124
147 - while (! is_null($segment = array_shift($key))) {
148 - if (is_array($target) && array_key_exists($segment, $target)) {
149 - $target = $target[$segment];
150 - } else {
151 - return null;
152 - }
125 + if($this->values->has($key)) {
126 + $this->values->delete($key);
127 + $this->saveOption($this->values->all());
153 128 }
129 + }
154 130
155 - if(!empty($fieldKey)) {
156 - return $target[$fieldKey] ?? null;
157 - }
131 + public function getField($key)
132 + {
158 133
159 - return $target;
134 + $config = new Dot($this->config);
160 135
136 + return $config->get($key);
161 137 }
162 138
163 - /**
164 - * Same as array_merge_recursive, however, without duplicate keys
165 - *
166 - * mergeValues(array('key' => 'org value'), array('key' => 'new value'));
167 - * => array('key' => array('new value'));
168 - *
169 - * @param array $array1
170 - * @param array $array2
171 - * @return array
172 - */
173 - protected function mergeValues(array $array1, array $array2): array
139 + public function fieldExists($key): bool
174 140 {
175 - $merged = $array1;
176 - foreach ($array2 as $key => $value)
177 - {
178 - if (is_array($value) && isset($merged[$key]) && is_array($merged[$key]))
179 - {
180 - $merged[$key] = $this->mergeValues($merged[$key], $value);
181 - }
182 - else
183 - {
184 - $merged[$key] = $value;
185 - }
186 - }
187 - return $merged;
141 +
142 + $config = new Dot($this->config);
143 +
144 + return $config->has($key);
188 145 }
189 146 }