PluginProbe
WPIDE – File Manager & Code Editor / trunk
WPIDE – File Manager & Code Editor vtrunk
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 +57 -92 3.1trunk 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,54 +69,29 @@
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);
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 + }
65 83 }
66 84
67 85 return $config;
68 86 }
69 87
70 - public function saveOption($config)
88 + public function saveOption($config): bool
71 89 {
72 - update_option($this->meta_key, $config);
90 + $updated = update_option($this->meta_key, $config);
73 91 $this->loadValues();
74 - }
75 92
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;
93 + return $updated;
109 94 }
110 95
111 96 public function getConfigFields(): array
112 97 {
@@ -115,75 +100,55 @@
115 100
116 101 public function get($key = null, $default = null)
117 102 {
118 103 if (is_null($key)) {
119 - return $this->values;
104 + return $this->values->all();
120 105 }
121 106
122 - $key = is_array($key) ? $key : explode('.', $key);
107 + return $this->values->get($key, $default);
123 108
124 - $target = $this->values;
109 + }
125 110
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;
111 + public function update($key, $value)
112 + {
113 +
114 + if($this->fieldExists($key)) {
115 +
116 + if(is_object($value)) {
117 + $value = (array) $value;
131 118 }
132 - }
133 119
134 - return $target;
135 - }
120 + $this->values->set($key, $value);
136 121
137 - public function getField($key = null, $fieldKey = null)
138 - {
139 - if (is_null($key)) {
140 - return $this->values;
122 + $this->saveOption($this->values->all());
123 +
124 + return $this->values->all();
125 +
141 126 }
142 127
143 - $key = is_array($key) ? $key : explode('.', $key);
128 + return false;
129 + }
144 130
145 - $target = $this->config;
131 + public function delete($key) {
146 132
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 - }
133 + if($this->values->has($key)) {
134 + $this->values->delete($key);
135 + $this->saveOption($this->values->all());
153 136 }
137 + }
154 138
155 - if(!empty($fieldKey)) {
156 - return $target[$fieldKey] ?? null;
157 - }
139 + public function getField($key)
140 + {
158 141
159 - return $target;
142 + $config = new Dot($this->config);
160 143
144 + return $config->get($key);
161 145 }
162 146
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
147 + public function fieldExists($key): bool
174 148 {
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;
149 +
150 + $config = new Dot($this->config);
151 +
152 + return $config->has($key);
188 153 }
189 154 }