PluginProbe
User Access Manager / 2.1.12
User Access Manager v2.1.12
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
user-access-manager / src / Config / Config.php

Config.php in User Access Manager 2.1.12, at src/Config/Config.php

195 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 namespace UserAccessManager\Config;
16
17 use UserAccessManager\Wrapper\Wordpress;
18
19 /**
20 * Class Config
21 *
22 * @package UserAccessManager\Config
23 */
24 class Config
25 {
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 /**
42 * @var ConfigParameter[]
43 */
44 protected $defaultConfigParameters = [];
45
46 /**
47 * @var null|ConfigParameter[]
48 */
49 protected $configParameters = null;
50
51 /**
52 * Config constructor.
53 *
54 * @param Wordpress $wordpress
55 * @param string $key
56 */
57 public function __construct(
58 Wordpress $wordpress,
59 $key
60 ) {
61 $this->wordpress = $wordpress;
62 $this->key = $key;
63 }
64
65 /**
66 * Returns the WordPress options.
67 *
68 * @param string $option
69 *
70 * @return mixed
71 */
72 public function getWpOption($option)
73 {
74 if (!isset($this->wpOptions[$option]) === true) {
75 $this->wpOptions[$option] = $this->wordpress->getOption($option);
76 }
77
78 return $this->wpOptions[$option];
79 }
80
81 /**
82 * Returns the default parameters for the current config.
83 *
84 * @return ConfigParameter[]
85 */
86 protected function getDefaultConfigParameters()
87 {
88 return $this->defaultConfigParameters;
89 }
90
91 /**
92 * Sets the default config parameters
93 *
94 * @param ConfigParameter[] $defaultConfigParameters
95 */
96 public function setDefaultConfigParameters(array $defaultConfigParameters)
97 {
98 $this->defaultConfigParameters = $defaultConfigParameters;
99 }
100
101 /**
102 * Returns the current settings
103 *
104 * @return ConfigParameter[]
105 */
106 public function getConfigParameters()
107 {
108 if ($this->configParameters === null) {
109 $configParameters = $this->getDefaultConfigParameters();
110 $currentOptions = (array)$this->getWpOption($this->key);
111
112 foreach ($currentOptions as $key => $option) {
113 if (isset($configParameters[$key])) {
114 $configParameters[$key]->setValue($option);
115 }
116 }
117
118 $this->configParameters = $configParameters;
119 }
120
121 return $this->configParameters;
122 }
123
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)
130 {
131 $configParameters = $this->getConfigParameters();
132
133 foreach ($rawParameters as $key => $value) {
134 if (isset($configParameters[$key]) === true) {
135 $configParameters[$key]->setValue($value);
136 }
137 }
138
139 $this->configParameters = $configParameters;
140
141 $simpleConfigParameters = [];
142
143 foreach ($configParameters as $parameter) {
144 $simpleConfigParameters[$parameter->getId()] = $parameter->getValue();
145 }
146
147 $this->wordpress->updateOption($this->key, $simpleConfigParameters);
148 }
149
150 /**
151 * Flushes the config parameters.
152 */
153 public function flushConfigParameters()
154 {
155 $this->defaultConfigParameters = [];
156 $this->configParameters = null;
157 }
158
159 /**
160 * Returns the requested parameter value
161 *
162 * @param string $parameterName
163 *
164 * @return mixed
165 *
166 * @throws \Exception
167 */
168 public function getParameterValueRaw($parameterName)
169 {
170 $options = $this->getConfigParameters();
171
172 if (isset($options[$parameterName]) === false) {
173 throw new \Exception("Unknown config parameter '{$parameterName}'.");
174 }
175
176 return $options[$parameterName]->getValue();
177 }
178
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)
187 {
188 try {
189 return $this->getParameterValueRaw($parameterName);
190 } catch (\Exception $exception) {
191 return null;
192 }
193 }
194 }
195