PluginProbe
WPIDE – File Manager & Code Editor / 3.5.1
WPIDE – File Manager & Code Editor v3.5.1
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
wpide / App / Config / Config.php

Config.php in WPIDE – File Manager & Code Editor 3.5.1, at App/Config/Config.php

147 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPIDE\App\Config;
3
4 use Adbar\Dot;
5 use const WPIDE\Constants\SLUG;
6
7 class Config
8 {
9 /* @var $config array */
10 protected $config;
11
12 /* @var $values Dot */
13 protected $values;
14
15 /* @var $defaults Dot */
16 protected $defaults;
17
18 /* @var $meta_key string */
19 protected $meta_key;
20
21 public function __construct(array $config = [])
22 {
23
24 $this->meta_key = SLUG.'_config';
25 $this->config = $config;
26
27 $this->loadDefaults();
28 $this->loadValues();
29 }
30
31 public function loadDefaults()
32 {
33
34 $this->defaults = new Dot($this->getConfigDefaults($this->config));
35 }
36
37 public function loadValues()
38 {
39
40 $options = $this->getOption();
41 $this->values = new Dot($options);
42 }
43
44 public function getDefaults(): array
45 {
46
47 return $this->defaults->all();
48 }
49
50 protected function getConfigDefaults($config): array
51 {
52
53 $values = [];
54
55 if(!is_array($config)){
56 return $values;
57 }
58
59 foreach($config as $key => $conf) {
60
61 $values[$key] = $conf['default'] ?? $this->getConfigDefaults($conf);
62 }
63
64 return $values;
65 }
66
67 public function getOption()
68 {
69 $config = get_option($this->meta_key);
70
71 if(empty($config)) {
72
73 $config = $this->defaults->all();
74 $this->saveOption($config);
75 }
76
77 return $config;
78 }
79
80 public function saveOption($config): bool
81 {
82 $updated = update_option($this->meta_key, $config);
83 $this->loadValues();
84
85 return $updated;
86 }
87
88 public function getConfigFields(): array
89 {
90 return $this->config;
91 }
92
93 public function get($key = null, $default = null)
94 {
95 if (is_null($key)) {
96 return $this->values->all();
97 }
98
99 return $this->values->get($key, $default);
100
101 }
102
103 public function update($key, $value)
104 {
105
106 if($this->fieldExists($key)) {
107
108 if(is_object($value)) {
109 $value = (array) $value;
110 }
111
112 $this->values->set($key, $value);
113
114 $this->saveOption($this->values->all());
115
116 return $this->values->all();
117
118 }
119
120 return false;
121 }
122
123 public function delete($key) {
124
125 if($this->values->has($key)) {
126 $this->values->delete($key);
127 $this->saveOption($this->values->all());
128 }
129 }
130
131 public function getField($key)
132 {
133
134 $config = new Dot($this->config);
135
136 return $config->get($key);
137 }
138
139 public function fieldExists($key): bool
140 {
141
142 $config = new Dot($this->config);
143
144 return $config->has($key);
145 }
146 }
147