PluginProbe
WPIDE – File Manager & Code Editor / 3.4.1
WPIDE – File Manager & Code Editor v3.4.1
3.5.9 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 All 55 releases
← All changes | App/Config/Config.php +92 -57 3.5.93.4.1 View file →
@@ -1,22 +1,13 @@
1 1 <?php
2 2 namespace WPIDE\App\Config;
3 -
4 -use Adbar\Dot;
5 3 use const WPIDE\Constants\SLUG;
6 4
7 5 class Config
8 6 {
9 - /* @var $config array */
10 7 protected $config;
11 -
12 - /* @var $values Dot */
13 8 protected $values;
14 -
15 - /* @var $defaults Dot */
16 9 protected $defaults;
17 -
18 - /* @var $meta_key string */
19 10 protected $meta_key;
20 11
21 12 public function __construct(array $config = [])
22 13 {
@@ -30,22 +21,21 @@
30 21
31 22 public function loadDefaults()
32 23 {
33 24
34 - $this->defaults = new Dot($this->getConfigDefaults($this->config));
25 + $this->defaults = $this->getConfigDefaults($this->config);
35 26 }
36 27
37 28 public function loadValues()
38 29 {
39 30
40 - $options = $this->getOption();
41 - $this->values = new Dot($options);
31 + $this->values = $this->mergeValues($this->defaults, $this->getOption());
42 32 }
43 33
44 34 public function getDefaults(): array
45 35 {
46 36
47 - return $this->defaults->all();
37 + return $this->defaults;
48 38 }
49 39
50 40 protected function getConfigDefaults($config): array
51 41 {
@@ -57,9 +47,9 @@
57 47 }
58 48
59 49 foreach($config as $key => $conf) {
60 50
61 - $values[$key] = $conf['default'] ?? $this->getConfigDefaults($conf);
51 + $values[$key] = isset($conf['default']) ? $conf['default'] : $this->getConfigDefaults($config[$key]);
62 52 }
63 53
64 54 return $values;
65 55 }
@@ -69,29 +59,54 @@
69 59 $config = get_option($this->meta_key);
70 60
71 61 if(empty($config)) {
72 62
73 - $config = $this->defaults->all();
63 + $config = $this->defaults;
74 64 $this->saveOption($config);
75 - } else {
76 - $defaults = $this->defaults->all();
77 - $merged = array_replace_recursive($defaults, $config);
78 -
79 - if ($merged !== $config) {
80 - $config = $merged;
81 - $this->saveOption($config);
82 - }
83 65 }
84 66
85 67 return $config;
86 68 }
87 69
88 - public function saveOption($config): bool
70 + public function saveOption($config)
89 71 {
90 - $updated = update_option($this->meta_key, $config);
72 + update_option($this->meta_key, $config);
91 73 $this->loadValues();
74 + }
92 75
93 - return $updated;
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;
94 109 }
95 110
96 111 public function getConfigFields(): array
97 112 {
@@ -100,55 +115,75 @@
100 115
101 116 public function get($key = null, $default = null)
102 117 {
103 118 if (is_null($key)) {
104 - return $this->values->all();
119 + return $this->values;
105 120 }
106 121
107 - return $this->values->get($key, $default);
122 + $key = is_array($key) ? $key : explode('.', $key);
108 123
109 - }
124 + $target = $this->values;
110 125
111 - public function update($key, $value)
112 - {
113 -
114 - if($this->fieldExists($key)) {
115 -
116 - if(is_object($value)) {
117 - $value = (array) $value;
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;
118 131 }
132 + }
119 133
120 - $this->values->set($key, $value);
134 + return $target;
135 + }
121 136
122 - $this->saveOption($this->values->all());
123 -
124 - return $this->values->all();
125 -
137 + public function getField($key = null, $fieldKey = null)
138 + {
139 + if (is_null($key)) {
140 + return $this->values;
126 141 }
127 142
128 - return false;
129 - }
143 + $key = is_array($key) ? $key : explode('.', $key);
130 144
131 - public function delete($key) {
145 + $target = $this->config;
132 146
133 - if($this->values->has($key)) {
134 - $this->values->delete($key);
135 - $this->saveOption($this->values->all());
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 + }
136 153 }
137 - }
138 154
139 - public function getField($key)
140 - {
155 + if(!empty($fieldKey)) {
156 + return $target[$fieldKey] ?? null;
157 + }
141 158
142 - $config = new Dot($this->config);
159 + return $target;
143 160
144 - return $config->get($key);
145 161 }
146 162
147 - public function fieldExists($key): bool
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
148 174 {
149 -
150 - $config = new Dot($this->config);
151 -
152 - return $config->has($key);
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;
153 188 }
154 189 }