| 1 |
<?php |
| 2 |
namespace WPIDE\App\Config; |
| 3 |
use const WPIDE\Constants\SLUG; |
| 4 |
|
| 5 |
class Config |
| 6 |
{ |
| 7 |
protected $config; |
| 8 |
protected $values; |
| 9 |
protected $defaults; |
| 10 |
protected $meta_key; |
| 11 |
|
| 12 |
public function __construct(array $config = []) |
| 13 |
{ |
| 14 |
|
| 15 |
$this->meta_key = SLUG.'_config'; |
| 16 |
$this->config = $config; |
| 17 |
|
| 18 |
$this->loadDefaults(); |
| 19 |
$this->loadValues(); |
| 20 |
} |
| 21 |
|
| 22 |
public function loadDefaults() |
| 23 |
{ |
| 24 |
|
| 25 |
$this->defaults = $this->getConfigDefaults($this->config); |
| 26 |
} |
| 27 |
|
| 28 |
public function loadValues() |
| 29 |
{ |
| 30 |
|
| 31 |
$this->values = $this->mergeValues($this->defaults, $this->getOption()); |
| 32 |
} |
| 33 |
|
| 34 |
public function getDefaults(): array |
| 35 |
{ |
| 36 |
|
| 37 |
return $this->defaults; |
| 38 |
} |
| 39 |
|
| 40 |
protected function getConfigDefaults($config): array |
| 41 |
{ |
| 42 |
|
| 43 |
$values = []; |
| 44 |
|
| 45 |
if(!is_array($config)){ |
| 46 |
return $values; |
| 47 |
} |
| 48 |
|
| 49 |
foreach($config as $key => $conf) { |
| 50 |
|
| 51 |
$values[$key] = isset($conf['default']) ? $conf['default'] : $this->getConfigDefaults($config[$key]); |
| 52 |
} |
| 53 |
|
| 54 |
return $values; |
| 55 |
} |
| 56 |
|
| 57 |
public function getOption() |
| 58 |
{ |
| 59 |
$config = get_option($this->meta_key); |
| 60 |
|
| 61 |
if(empty($config)) { |
| 62 |
|
| 63 |
$config = $this->defaults; |
| 64 |
$this->saveOption($config); |
| 65 |
} |
| 66 |
|
| 67 |
return $config; |
| 68 |
} |
| 69 |
|
| 70 |
public function saveOption($config) |
| 71 |
{ |
| 72 |
update_option($this->meta_key, $config); |
| 73 |
$this->loadValues(); |
| 74 |
} |
| 75 |
|
| 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; |
| 109 |
} |
| 110 |
|
| 111 |
public function getConfigFields(): array |
| 112 |
{ |
| 113 |
return $this->config; |
| 114 |
} |
| 115 |
|
| 116 |
public function get($key = null, $default = null) |
| 117 |
{ |
| 118 |
if (is_null($key)) { |
| 119 |
return $this->values; |
| 120 |
} |
| 121 |
|
| 122 |
$key = is_array($key) ? $key : explode('.', $key); |
| 123 |
|
| 124 |
$target = $this->values; |
| 125 |
|
| 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; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $target; |
| 135 |
} |
| 136 |
|
| 137 |
public function getField($key = null, $fieldKey = null) |
| 138 |
{ |
| 139 |
if (is_null($key)) { |
| 140 |
return $this->values; |
| 141 |
} |
| 142 |
|
| 143 |
$key = is_array($key) ? $key : explode('.', $key); |
| 144 |
|
| 145 |
$target = $this->config; |
| 146 |
|
| 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 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if(!empty($fieldKey)) { |
| 156 |
return $target[$fieldKey] ?? null; |
| 157 |
} |
| 158 |
|
| 159 |
return $target; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 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 |
| 174 |
{ |
| 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; |
| 188 |
} |
| 189 |
} |
| 190 |
|