PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-option.php

class-option.php in Sessions 2.3.1, at includes/system/class-option.php

313 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Options handling
4 *
5 * Handles all options operations for the plugin.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\System;
13
14 use POSessions\System\Role;
15
16 /**
17 * Define the options functionality.
18 *
19 * Handles all options operations for the plugin.
20 *
21 * @package System
22 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
23 * @since 1.0.0
24 */
25 class Option {
26
27 /**
28 * The list of defaults options.
29 *
30 * @since 1.0.0
31 * @access private
32 * @var array $defaults The defaults list.
33 */
34 private static $defaults = [];
35
36 /**
37 * The list of network-wide options.
38 *
39 * @since 1.0.0
40 * @access private
41 * @var array $network The network-wide list.
42 */
43 private static $network = [];
44
45 /**
46 * The list of site options.
47 *
48 * @since 1.0.0
49 * @access private
50 * @var array $site The site list.
51 */
52 public static $specific = [];
53
54 /**
55 * The list of private options.
56 *
57 * @since 1.0.0
58 * @access private
59 * @var array $private The private options list.
60 */
61 private static $private = [];
62
63 /**
64 * Set the defaults options.
65 *
66 * @since 1.0.0
67 */
68 public static function init() {
69 // Options for whole site(s).
70 self::$defaults['use_cdn'] = false;
71 self::$defaults['script_in_footer'] = false;
72 self::$defaults['download_favicons'] = false;
73 self::$defaults['display_nag'] = false;
74 self::$defaults['nags'] = [];
75 self::$defaults['version'] = '0.0.0';
76 self::$defaults['history'] = 30;
77 self::$defaults['analytics'] = true;
78 self::$defaults['metrics'] = true;
79 self::$defaults['forceip'] = true;
80 self::$defaults['followip'] = true;
81 self::$defaults['rolemode'] = 0;
82 self::$defaults['zk_semaphore'] = 300;
83 self::$defaults['zk_cycle'] = 90;
84 self::$defaults['zk_tsize'] = 20;
85 self::$defaults['buffer_limit'] = 4000;
86 self::$defaults['bad_ip_message'] = '';
87 self::$defaults['blocked_message'] = '';
88 self::$network = [ 'version', 'use_cdn', 'download_favicons', 'script_in_footer', 'display_nag', 'analytics', 'forceip', 'followip', 'history', 'metrics', 'rolemode', 'zk_cycle', 'zk_tsize', 'zk_semaphore', 'buffer_limit', 'bad_ip_message', 'blocked_message' ];
89 // Specific options.
90 self::$defaults['limit'] = 'none';
91 self::$defaults['method'] = 'block';
92 self::$defaults['idle'] = 0;
93 self::$defaults['maxip'] = 0;
94 self::$defaults['block'] = 'none';
95 self::$defaults['cookie-ttl'] = 48;
96 self::$defaults['cookie-rttl'] = 336;
97 self::$specific = [ 'limit', 'maxip', 'method', 'idle', 'block', 'cookie-ttl', 'cookie-rttl' ];
98 }
99
100 /**
101 * Get the options infos for Site Health "info" tab.
102 *
103 * @since 1.0.0
104 */
105 public static function debug_info() {
106 $result = [];
107 $si = '[Site Option] ';
108 $nt = $si;
109 if ( Environment::is_wordpress_multisite() ) {
110 $nt = '[Network Option] ';
111 }
112 foreach ( self::$network as $opt ) {
113 $val = self::network_get( $opt );
114 $result[ $opt ] = [
115 'label' => $nt . $opt,
116 'value' => is_bool( $val ) ? $val ? 1 : 0 : $val,
117 'private' => in_array( $opt, self::$private, true ),
118 ];
119 }
120 foreach ( self::roles_get() as $role => $opt ) {
121 foreach ( $opt as $k => $s ) {
122 $result[ $role . $k ] = [
123 'label' => $nt . $role . ':' . $k,
124 'value' => $s,
125 'private' => false,
126 ];
127 }
128 }
129 return $result;
130 }
131
132 /**
133 * Get an option value for a site.
134 *
135 * @param string $option Option name. Expected to not be SQL-escaped.
136 * @param boolean $default Optional. The default value if option doesn't exists.
137 * @return mixed The value of the option.
138 * @since 1.0.0
139 */
140 public static function site_get( $option, $default = null ) {
141 if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) {
142 $default = self::$defaults[ $option ];
143 }
144 $val = get_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, $default );
145 if ( is_bool( $default ) ) {
146 return (bool) $val;
147 }
148 return $val;
149 }
150
151 /**
152 * Get an option value for a network.
153 *
154 * @param string $option Option name. Expected to not be SQL-escaped.
155 * @param boolean $default Optional. The default value if option doesn't exists.
156 * @return mixed The value of the option.
157 * @since 1.0.0
158 */
159 public static function network_get( $option, $default = null ) {
160 if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) {
161 $default = self::$defaults[ $option ];
162 }
163 $val = get_site_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, $default );
164 if ( is_bool( $default ) ) {
165 return (bool) $val;
166 }
167 return $val;
168 }
169
170 /**
171 * Verify if an option exists.
172 *
173 * @param string $option Option name. Expected to not be SQL-escaped.
174 * @return boolean True if the option exists, false otherwise.
175 * @since 1.0.0
176 */
177 public static function site_exists( $option ) {
178 return 'non_existent_option' !== get_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' );
179 }
180
181 /**
182 * Verify if an option exists.
183 *
184 * @param string $option Option name. Expected to not be SQL-escaped.
185 * @return boolean True if the option exists, false otherwise.
186 * @since 1.0.0
187 */
188 public static function network_exists( $option ) {
189 return 'non_existent_option' !== get_site_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' );
190 }
191
192 /**
193 * Set an option value for a site.
194 *
195 * @param string $option Option name. Expected to not be SQL-escaped.
196 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
197 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
198 * `$autoload` can only be updated using `update_option()` if `$value` is also changed.
199 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
200 * the default value is 'yes'. Default null.
201 * @return boolean False if value was not updated and true if value was updated.
202 * @since 1.0.0
203 */
204 public static function site_set( $option, $value, $autoload = null ) {
205 if ( false === $value ) {
206 $value = 0;
207 }
208 return update_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, $value, $autoload );
209 }
210
211 /**
212 * Set an option value for a network.
213 *
214 * @param string $option Option name. Expected to not be SQL-escaped.
215 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
216 * @return boolean False if value was not updated and true if value was updated.
217 * @since 1.0.0
218 */
219 public static function network_set( $option, $value ) {
220 if ( false === $value ) {
221 $value = 0;
222 }
223 return update_site_option( POSE_PRODUCT_ABBREVIATION . '_' . $option, $value );
224 }
225
226 /**
227 * Delete all options for a site.
228 *
229 * @return integer Number of deleted items.
230 * @since 1.0.0
231 */
232 public static function site_delete_all() {
233 global $wpdb;
234 $result = 0;
235 // phpcs:ignore
236 $delete = $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '" . POSE_PRODUCT_ABBREVIATION . '_%' . "';" );
237 foreach ( $delete as $option ) {
238 if ( delete_option( $option ) ) {
239 ++$result;
240 }
241 }
242 return $result;
243 }
244
245 /**
246 * Reset some options to their defaults.
247 *
248 * @since 1.0.0
249 */
250 public static function reset_to_defaults() {
251 foreach ( self::$network as $key ) {
252 if ( 'version' !== $key ) {
253 self::network_set( $key, self::$defaults[ $key ] );
254 }
255 }
256 }
257
258 /**
259 * Normalize the role settings.
260 *
261 * @param array $settings The settings to normalize;
262 * @return array The normalized settings.
263 * @since 1.0.0
264 */
265 public static function roles_normalize( $settings ) {
266 foreach ( Role::get_all() as $role => $detail ) {
267 if ( array_key_exists( $role, $settings ) ) {
268 foreach ( self::$specific as $spec ) {
269 if ( ! array_key_exists( $spec, $settings[ $role ] ) ) {
270 $settings[ $role ][ $spec ] = self::$defaults[ $spec ];
271 }
272 }
273 } else {
274 foreach ( self::$specific as $spec ) {
275 $settings[ $role ][ $spec ] = self::$defaults[ $spec ];
276 }
277 }
278 }
279 return $settings;
280 }
281
282 /**
283 * Get the role settings.
284 *
285 * @return array The value of role settings.
286 * @since 1.0.0
287 */
288 public static function roles_get() {
289 return self::roles_normalize( self::network_get( 'roles', [] ) );
290 }
291
292 /**
293 * Set the role settings.
294 *
295 * @param array $settings The settings to normalize;
296 * @return boolean False if value was not updated and true if value was updated.
297 * @since 1.0.0
298 */
299 public static function roles_set( $settings ) {
300 return self::network_set( 'roles', self::roles_normalize( $settings ) );
301 }
302
303 /**
304 * Initializes the class and set its properties.
305 *
306 * @since 1.0.0
307 */
308 public function __construct() {
309 }
310 }
311
312 Option::init();
313