PluginProbe ʕ •ᴥ•ʔ
PublishPress Capabilities – User Role Editor, Access Permissions, User Capabilities, Admin Menus / 1.4.9
PublishPress Capabilities – User Role Editor, Access Permissions, User Capabilities, Admin Menus v1.4.9
2.45.0 2.44.0 trunk 1.10 1.10.1 1.4.1 1.4.10 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5 1.5.1 1.5.10 1.5.11 1.5.2 1.5.3 1.5.4 1.5.5 1.5.7 1.5.8 1.5.9 1.6 1.6.1 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.8.1 1.9 1.9.10 1.9.12 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.9 2.0 2.0.2 2.0.3 2.1 2.1.1 2.10.0 2.10.1 2.10.2 2.10.3 2.11.1 2.12.1 2.12.2 2.13.0 2.14.0 2.15.0 2.16.0 2.17.0 2.18.0 2.18.2 2.19.0 2.19.1 2.19.2 2.2 2.2.1 2.20.0 2.21.0 2.22.0 2.23.0 2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.30.0 2.31.0 2.32.0 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.41.0 2.42.0 2.43.0 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.7.0 2.7.1 2.8.0 2.8.1 2.9.0 2.9.1
capability-manager-enhanced / framework / classes / settings.php
capability-manager-enhanced / framework / classes Last commit date
abstract 12 years ago admin-notices.php 12 years ago settings.php 12 years ago template.php 12 years ago
settings.php
331 lines
1 <?php
2 /**
3 * Alkivia Settings Manager
4 *
5 * @version $Rev: 203758 $
6 * @author Jordi Canals
7 * @copyright Copyright (C) 2008, 2009, 2010 Jordi Canals
8 * @license GNU General Public License version 2
9 * @link http://alkivia.org
10 * @package Alkivia
11 * @subpackage Framework
12 *
13
14 Copyright 2008, 2009, 2010 Jordi Canals <devel@jcanals.cat>
15
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 version 2 as published by the Free Software Foundation.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 /**
30 * Class to manage settings.
31 * It is expected to receive all settings properly filtered.
32 *
33 * @author Jordi Canals
34 * @package Alkivia
35 * @subpackage Framework
36 * @link http://wiki.alkivia.org/framework/classes/settings
37 */
38 final class akSettings
39 {
40
41 /**
42 * Settings from alkivia.ini file
43 * This settings will be forced and cannot be set on the admin panel.
44 *
45 * @var array
46 */
47 private $forced = array();
48
49 /**
50 * Global settings for all Alkivia modules.
51 * This is a merge from the settings on db and settings on the ini file.
52 *
53 * @var array
54 */
55 private $settings = array();
56
57 /**
58 * Options retrieved from database and merged with defaults.
59 * This is what needs to be saved to database at end.
60 *
61 * @var array
62 */
63 private $options = array();
64
65 /**
66 * Default settings for all alkivia modules.
67 * This options are set at module startup and used if no other setting is found.
68 *
69 * @var array
70 */
71 private $defaults = array();
72
73 /**
74 * Flags to know if a setting has been updated or not.
75 * This is an array of flags, one item per module.
76 *
77 * @var array
78 */
79 private $updated = array();
80
81 /**
82 * Settings prefix and sufix for database entry.
83 */
84 const prefix = '';
85 const sufix = '_settings';
86
87 /**
88 * Class constructor.
89 * Loads settings from ini file.
90 *
91 * @return akSettings
92 */
93 public function __construct()
94 {
95 if ( defined('AK_INI_FILE') && file_exists(AK_INI_FILE) ) {
96 $this->forced = parse_ini_file(AK_INI_FILE, true);
97 }
98
99 add_action('shutdown', array($this, 'saveOptions'));
100 }
101
102 /**
103 * Populates module settings array.
104 * Merges into settings array, defaults and options retrieved from DB.
105 *
106 * @param string $module Module name to load.
107 * @return void
108 */
109 private function populateSettings ( $module )
110 {
111 if ( ! isset($this->defaults[$module]) ) {
112 $this->defaults[$module] = array();
113 }
114
115 if ( ! isset($this->options[$module]) ) {
116 $options = apply_filters('ak_' . $module . '_options', get_option(self::prefix . $module . self::sufix));
117
118 if ( is_array($options) ) {
119 $this->options[$module] = $options;
120 } else {
121 $this->options[$module] = array();
122 }
123 }
124
125 if ( ! isset($this->forced[$module]) ) {
126 $this->forced[$module] = array();
127 }
128
129 if ( ! isset($this->updated[$module]) ) {
130 $this->updated[$module] = false;
131 }
132
133 $this->options[$module] = array_merge(
134 $this->defaults[$module],
135 $this->options[$module]);
136
137 $this->settings[$module] = array_merge(
138 $this->options[$module],
139 $this->forced[$module]);
140 }
141
142 /**
143 * Sets default values for a module and fills missing settings with them.
144 *
145 * @uses apply_filters() Calls the 'ak_<module>_defaults' filter on new defaults.
146 * @param string $module Module name.
147 * @param array $options Default settings array.
148 * @return void
149 */
150 public function setDefaults ( $module, $options )
151 {
152 if ( ! is_array($options) ) { // Must be an array of options.
153 $options = array();
154 }
155
156 $this->defaults[$module] = apply_filters('ak_' . $module . '_defaults', $options);
157 $this->populateSettings($module);
158 }
159
160 /**
161 * Returns an array with default values for a module.
162 *
163 * @param $module Module name
164 * @return array Default values for this module.
165 */
166 public function getDefaults ( $module )
167 {
168 if ( isset($this->defaults[$module]) && is_array($this->defaults[$module]) ) {
169 return $this->defaults[$module];
170 } else {
171 return array();
172 }
173 }
174
175 /**
176 * Gets a setting for a module.
177 * If $option is empty, will return all settings for a module in an array.
178 *
179 * @param string $module Module internal name.
180 * @param string $option Setting name.
181 * @param mixed $default Default value if setting not found.
182 * @return mixed Returns the setting value or $default if not defined.
183 */
184 public function getSetting ( $module, $option = '', $default = false )
185 {
186 if ( ! isset($this->settings[$module]) ) {
187 $this->populateSettings($module);
188 }
189
190 if ( empty($option) ) {
191 return $this->settings[$module];
192 } elseif ( isset($this->settings[$module][$option]) ) {
193 return $this->settings[$module][$option];
194 } else {
195 return $default;
196 }
197 }
198
199 /**
200 * Checks if an option is forced in the ini file.
201 * It is forced if it was defined on the ini file.
202 *
203 * @param string $module Module internal name.
204 * @param string $option Setting name.
205 * @return boolean
206 */
207 public function isForced ( $module, $option )
208 {
209 if ( isset($this->forced[$module][$option]) ) {
210 return true;
211 } else {
212 return false;
213 }
214 }
215
216 /**
217 * Adds a new module setting only if it does not exists.
218 * Does not save them to database. After adding all new settings, must call akSettings::saveSettings()
219 *
220 * @param string $module Module name.
221 * @param string $option Setting name.
222 * @param mixed $value Setting value
223 * @return boolean Returns true if settings has been added, false otherwise.
224 */
225 public function addOption ( $module, $option, $value )
226 {
227 if ( ! isset($this->options[$module]) ) {
228 $this->populateSettings($module);
229 }
230
231 if ( isset($this->options[$module][$option]) ) {
232 return false;
233 } else {
234 $this->options[$module][$option] = $value;
235 }
236
237 $this->populateSettings($module);
238 $this->updated[$module] = true;
239
240 return true;
241 }
242
243 /**
244 * Updates a module setting. If the setting does not exists, it is created.
245 * Does not save them to database. After adding all new settings, must call akSettings::saveSettings()
246 *
247 * @param string $module Module name.
248 * @param string $option Setting name.
249 * @param mixed $value Setting value
250 * @return void
251 */
252 public function updateOption ( $module, $option, $value )
253 {
254 if ( ! isset($this->options[$module]) ) {
255 $this->populateSettings($module);
256 }
257
258 $this->options[$module][$option] = $value;
259 $this->populateSettings($module);
260 $this->updated[$module] = true;
261 }
262
263 /**
264 * Deletes an option from a module.
265 * Only if the option existed.
266 *
267 * @param string $module Module name.
268 * @param string $option Setting name.
269 * @return void
270 */
271 public function deleteOption ( $module, $option )
272 {
273 if ( ! isset($this->options[$module]) ) {
274 $this->populateSettings($module);
275 }
276
277 if ( isset($this->options[$module][$option]) ) {
278 unset($this->options[$module][$option]);
279 }
280 $this->populateSettings($module);
281 $this->updated[$module] = true;
282 }
283
284 /**
285 * Replaces all settings for a module and saves them to database.
286 * If settings does not exist, them are created.
287 *
288 * @uses apply_filters() Calls the 'ak_<module>_replace_options' filter on new options.
289 * @param string $module Module name.
290 * @param array $settings New module settings.
291 * @return boolean Returns true if settings have been replaced.
292 */
293 public function replaceOptions ( $module, $settings )
294 {
295 if ( ! is_array($settings) ) {
296 return false;
297 }
298
299 $this->options[$module] = apply_filters('ak_' . $module . '_replace_options', $settings);
300 $this->populateSettings($module);
301 $this->updated[$module] = true;
302
303 return $this->saveOptions($module);
304 }
305
306 /**
307 * Saves settings to database.
308 * If a module name is provided saves only this module settings, if not, saves all settings.
309 *
310 * @param string $module Module name to save.
311 * @return boolean Returns true is settings have been saved, false otherwise.
312 */
313 public function saveOptions ( $module = '' )
314 {
315 if ( empty($module) ) {
316 foreach ( $this->options as $module => $value ) {
317 if ( $this->updated[$module] ) {
318 update_option(self::prefix . $module . self::sufix, $value);
319 $this->updated[$module] = false;
320 }
321 }
322 return true;
323 } elseif ( isset($this->options[$module]) && $this->updated[$module]) {
324 update_option(self::prefix . $module . self::sufix, $this->options[$module]);
325 return true;
326 } else {
327 return false;
328 }
329 }
330 }
331