| 1 |
<?php |
| 2 |
/** |
| 3 |
* Config.php |
| 4 |
* |
| 5 |
* The Config class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Config; |
| 19 |
|
| 20 |
use Exception; |
| 21 |
use UserAccessManager\Wrapper\Wordpress; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class Config |
| 25 |
* |
| 26 |
* @package UserAccessManager\Config |
| 27 |
*/ |
| 28 |
class Config |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var Wordpress |
| 32 |
*/ |
| 33 |
protected $wordpress; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $key; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $wpOptions = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var ConfigParameter[] |
| 47 |
*/ |
| 48 |
protected $defaultConfigParameters = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var null|ConfigParameter[] |
| 52 |
*/ |
| 53 |
protected $configParameters = null; |
| 54 |
|
| 55 |
/** |
| 56 |
* Config constructor. |
| 57 |
* @param Wordpress $wordpress |
| 58 |
* @param string $key |
| 59 |
*/ |
| 60 |
public function __construct( |
| 61 |
Wordpress $wordpress, |
| 62 |
string $key |
| 63 |
) { |
| 64 |
$this->wordpress = $wordpress; |
| 65 |
$this->key = $key; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the WordPress options. |
| 70 |
* @param string $option |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
public function getWpOption(string $option) |
| 74 |
{ |
| 75 |
if (!isset($this->wpOptions[$option]) === true) { |
| 76 |
$this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->wpOptions[$option]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the default parameters for the current config. |
| 84 |
* @return ConfigParameter[] |
| 85 |
*/ |
| 86 |
protected function getDefaultConfigParameters(): array |
| 87 |
{ |
| 88 |
return $this->defaultConfigParameters; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Sets the default config parameters |
| 93 |
* @param ConfigParameter[] $defaultConfigParameters |
| 94 |
*/ |
| 95 |
public function setDefaultConfigParameters(array $defaultConfigParameters) |
| 96 |
{ |
| 97 |
$this->defaultConfigParameters = $defaultConfigParameters; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the current settings |
| 102 |
* @return ConfigParameter[] |
| 103 |
*/ |
| 104 |
public function getConfigParameters(): array |
| 105 |
{ |
| 106 |
if ($this->configParameters === null) { |
| 107 |
$configParameters = $this->getDefaultConfigParameters(); |
| 108 |
$currentOptions = (array) $this->getWpOption($this->key); |
| 109 |
|
| 110 |
foreach ($currentOptions as $key => $option) { |
| 111 |
if (isset($configParameters[$key])) { |
| 112 |
$configParameters[$key]->setValue($option); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$this->configParameters = $configParameters; |
| 117 |
} |
| 118 |
|
| 119 |
return $this->configParameters; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Sets the new config parameters and saves them to the database. |
| 124 |
* @param array $rawParameters |
| 125 |
*/ |
| 126 |
public function setConfigParameters(array $rawParameters) |
| 127 |
{ |
| 128 |
$configParameters = $this->getConfigParameters(); |
| 129 |
|
| 130 |
foreach ($rawParameters as $key => $value) { |
| 131 |
if (isset($configParameters[$key]) === true) { |
| 132 |
$configParameters[$key]->setValue($value); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$this->configParameters = $configParameters; |
| 137 |
|
| 138 |
$simpleConfigParameters = []; |
| 139 |
|
| 140 |
foreach ($configParameters as $parameter) { |
| 141 |
$simpleConfigParameters[$parameter->getId()] = $parameter->getValue(); |
| 142 |
} |
| 143 |
|
| 144 |
$this->wordpress->updateOption($this->key, $simpleConfigParameters); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Flushes the config parameters. |
| 149 |
*/ |
| 150 |
public function flushConfigParameters() |
| 151 |
{ |
| 152 |
$this->defaultConfigParameters = []; |
| 153 |
$this->configParameters = null; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the requested parameter value |
| 158 |
* @param string $parameterName |
| 159 |
* @return mixed |
| 160 |
* @throws Exception |
| 161 |
*/ |
| 162 |
public function getParameterValueRaw(string $parameterName) |
| 163 |
{ |
| 164 |
$options = $this->getConfigParameters(); |
| 165 |
|
| 166 |
if (isset($options[$parameterName]) === false) { |
| 167 |
throw new Exception("Unknown config parameter '{$parameterName}'."); |
| 168 |
} |
| 169 |
|
| 170 |
return $options[$parameterName]->getValue(); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Returns the requested parameter value but suppresses exceptions. |
| 175 |
* @param string $parameterName |
| 176 |
* @return mixed |
| 177 |
*/ |
| 178 |
public function getParameterValue(string $parameterName) |
| 179 |
{ |
| 180 |
try { |
| 181 |
return $this->getParameterValueRaw($parameterName); |
| 182 |
} catch (Exception $exception) { |
| 183 |
return null; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|