PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Config/Config.php +107 -34 2.3.19 → 2.1.11 View file →
@@ -1,35 +1,78 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * Config.php
4 + *
5 + * The Config class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
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 + */
5 15 namespace UserAccessManager\Config;
6 16
7 -use Exception;
8 -use UserAccessManager\Config\Parameter\ConfigParameter;
9 17 use UserAccessManager\Wrapper\Wordpress;
10 18
19 +/**
20 + * Class Config
21 + *
22 + * @package UserAccessManager\Config
23 + */
11 24 class Config
12 25 {
13 - protected array $wpOptions = [];
14 26 /**
27 + * @var Wordpress
28 + */
29 + protected $wordpress;
30 +
31 + /**
32 + * @var string
33 + */
34 + protected $key;
35 +
36 + /**
37 + * @var array
38 + */
39 + protected $wpOptions = [];
40 +
41 + /**
15 42 * @var ConfigParameter[]
16 43 */
17 - protected array $defaultConfigParameters = [];
44 + protected $defaultConfigParameters = [];
45 +
18 46 /**
19 47 * @var null|ConfigParameter[]
20 48 */
21 - protected ?array $configParameters = null;
49 + protected $configParameters = null;
22 50
51 + /**
52 + * Config constructor.
53 + *
54 + * @param Wordpress $wordpress
55 + * @param string $key
56 + */
23 57 public function __construct(
24 - private Wordpress $wordpress,
25 - protected string $key
58 + Wordpress $wordpress,
59 + $key
26 60 ) {
61 + $this->wordpress = $wordpress;
62 + $this->key = $key;
27 63 }
28 64
29 - public function getWpOption(string $option): mixed
65 + /**
66 + * Returns the WordPress options.
67 + *
68 + * @param string $option
69 + *
70 + * @return mixed
71 + */
72 + public function getWpOption($option)
30 73 {
31 - if (isset($this->wpOptions[$option]) === false) {
74 + if (!isset($this->wpOptions[$option]) === true) {
32 75 $this->wpOptions[$option] = $this->wordpress->getOption($option);
33 76 }
34 77
35 78 return $this->wpOptions[$option];
@@ -35,44 +78,56 @@
35 78 return $this->wpOptions[$option];
36 79 }
37 80
38 81 /**
82 + * Returns the default parameters for the current config.
83 + *
39 84 * @return ConfigParameter[]
40 85 */
41 - protected function getDefaultConfigParameters(): array
86 + protected function getDefaultConfigParameters()
42 87 {
43 88 return $this->defaultConfigParameters;
44 89 }
45 90
46 91 /**
92 + * Sets the default config parameters
93 + *
47 94 * @param ConfigParameter[] $defaultConfigParameters
48 95 */
49 - public function setDefaultConfigParameters(array $defaultConfigParameters): void
96 + public function setDefaultConfigParameters(array $defaultConfigParameters)
50 97 {
51 98 $this->defaultConfigParameters = $defaultConfigParameters;
52 99 }
53 100
54 101 /**
102 + * Returns the current settings
103 + *
55 104 * @return ConfigParameter[]
56 105 */
57 - public function getConfigParameters(): array
106 + public function getConfigParameters()
58 107 {
59 - if ($this->configParameters !== null) {
60 - return $this->configParameters;
61 - }
108 + if ($this->configParameters === null) {
109 + $configParameters = $this->getDefaultConfigParameters();
110 + $currentOptions = (array)$this->getWpOption($this->key);
62 111
63 - $configParameters = $this->getDefaultConfigParameters();
112 + foreach ($currentOptions as $key => $option) {
113 + if (isset($configParameters[$key])) {
114 + $configParameters[$key]->setValue($option);
115 + }
116 + }
64 117
65 - foreach ((array) $this->getWpOption($this->key) as $key => $option) {
66 - if (isset($configParameters[$key]) === true) {
67 - $configParameters[$key]->setValue($option);
68 - }
118 + $this->configParameters = $configParameters;
69 119 }
70 120
71 - return $this->configParameters = $configParameters;
121 + return $this->configParameters;
72 122 }
73 123
74 - public function setConfigParameters(array $rawParameters): void
124 + /**
125 + * Sets the new config parameters and saves them to the database.
126 + *
127 + * @param array $rawParameters
128 + */
129 + public function setConfigParameters(array $rawParameters)
75 130 {
76 131 $configParameters = $this->getConfigParameters();
77 132
78 133 foreach ($rawParameters as $key => $value) {
@@ -80,18 +135,23 @@
80 135 $configParameters[$key]->setValue($value);
81 136 }
82 137 }
83 138
84 - $persistableValues = [];
139 + $this->configParameters = $configParameters;
85 140
141 + $simpleConfigParameters = [];
142 +
86 143 foreach ($configParameters as $parameter) {
87 - $persistableValues[$parameter->getId()] = $parameter->getValue();
144 + $simpleConfigParameters[$parameter->getId()] = $parameter->getValue();
88 145 }
89 146
90 - $this->wordpress->updateOption($this->key, $persistableValues);
147 + $this->wordpress->updateOption($this->key, $simpleConfigParameters);
91 148 }
92 149
93 - public function flushConfigParameters(): void
150 + /**
151 + * Flushes the config parameters.
152 + */
153 + public function flushConfigParameters()
94 154 {
95 155 $this->defaultConfigParameters = [];
96 156 $this->configParameters = null;
97 157 }
@@ -96,26 +156,39 @@
96 156 $this->configParameters = null;
97 157 }
98 158
99 159 /**
100 - * @throws Exception
160 + * Returns the requested parameter value
161 + *
162 + * @param string $parameterName
163 + *
164 + * @return mixed
165 + *
166 + * @throws \Exception
101 167 */
102 - public function getParameterValueRaw(string $parameterName): mixed
168 + public function getParameterValueRaw($parameterName)
103 169 {
104 170 $options = $this->getConfigParameters();
105 171
106 172 if (isset($options[$parameterName]) === false) {
107 - throw new Exception("Unknown config parameter '$parameterName'.");
173 + throw new \Exception("Unknown config parameter '{$parameterName}'.");
108 174 }
109 175
110 176 return $options[$parameterName]->getValue();
111 177 }
112 178
113 - public function getParameterValue(string $parameterName): mixed
179 + /**
180 + * Returns the requested parameter value but suppresses exceptions.
181 + *
182 + * @param string $parameterName
183 + *
184 + * @return mixed
185 + */
186 + public function getParameterValue($parameterName)
114 187 {
115 188 try {
116 189 return $this->getParameterValueRaw($parameterName);
117 - } catch (Exception) {
190 + } catch (\Exception $exception) {
118 191 return null;
119 192 }
120 193 }
121 194 }