| 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 |
} 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 |
} |
| 84 |
|
| 85 |
return $config; |
| 86 |
} |
| 87 |
|
| 88 |
public function saveOption($config): bool |
| 89 |
{ |
| 90 |
$updated = update_option($this->meta_key, $config); |
| 91 |
$this->loadValues(); |
| 92 |
|
| 93 |
return $updated; |
| 94 |
} |
| 95 |
|
| 96 |
public function getConfigFields(): array |
| 97 |
{ |
| 98 |
return $this->config; |
| 99 |
} |
| 100 |
|
| 101 |
public function get($key = null, $default = null) |
| 102 |
{ |
| 103 |
if (is_null($key)) { |
| 104 |
return $this->values->all(); |
| 105 |
} |
| 106 |
|
| 107 |
return $this->values->get($key, $default); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public function update($key, $value) |
| 112 |
{ |
| 113 |
|
| 114 |
if($this->fieldExists($key)) { |
| 115 |
|
| 116 |
if(is_object($value)) { |
| 117 |
$value = (array) $value; |
| 118 |
} |
| 119 |
|
| 120 |
$this->values->set($key, $value); |
| 121 |
|
| 122 |
$this->saveOption($this->values->all()); |
| 123 |
|
| 124 |
return $this->values->all(); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
public function delete($key) { |
| 132 |
|
| 133 |
if($this->values->has($key)) { |
| 134 |
$this->values->delete($key); |
| 135 |
$this->saveOption($this->values->all()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public function getField($key) |
| 140 |
{ |
| 141 |
|
| 142 |
$config = new Dot($this->config); |
| 143 |
|
| 144 |
return $config->get($key); |
| 145 |
} |
| 146 |
|
| 147 |
public function fieldExists($key): bool |
| 148 |
{ |
| 149 |
|
| 150 |
$config = new Dot($this->config); |
| 151 |
|
| 152 |
return $config->has($key); |
| 153 |
} |
| 154 |
} |
| 155 |
|