PluginProbe
OPcache Manager / 3.3.0
OPcache Manager v3.3.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / system / class-option.php

class-option.php in OPcache Manager 3.3.0, at includes/system/class-option.php

254 lines 7.2 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 OPcacheManager\System;
13
14 use OPcacheManager\System\Environment;
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 private static $site = [];
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 self::$defaults['use_apcu'] = true;
70 self::$defaults['use_cdn'] = false;
71 self::$defaults['script_in_footer'] = false;
72 self::$defaults['display_nag'] = false;
73 self::$defaults['nags'] = [];
74 self::$defaults['version'] = '0.0.0';
75 self::$defaults['reset_frequency'] = 'never';
76 self::$defaults['last_check'] = [];
77 self::$defaults['history'] = 21;
78 self::$defaults['analytics'] = true;
79 self::$defaults['metrics'] = true;
80 self::$defaults['warmup'] = false;
81 self::$defaults['flash_invalidate'] = false;
82 self::$defaults['flash_warmup'] = false;
83 self::$defaults['adminbar'] = true;
84 self::$network = [ 'version', 'use_cdn', 'use_apcu', 'script_in_footer', 'display_nag', 'reset_frequency', 'analytics', 'history', 'metrics', 'warmup', 'flash_invalidate', 'flash_warmup', 'adminbar' ];
85 }
86 /**
87 * Get the options infos for Site Health "info" tab.
88 *
89 * @since 1.0.0
90 */
91 public static function debug_info() {
92 $result = [];
93 $si = '[Site Option] ';
94 $nt = $si;
95 if ( Environment::is_wordpress_multisite() ) {
96 $nt = '[Network Option] ';
97 }
98 foreach ( self::$network as $opt ) {
99 $val = self::network_get( $opt );
100 $result[ $opt ] = [
101 'label' => $nt . $opt,
102 'value' => is_bool( $val ) ? $val ? 1 : 0 : $val,
103 'private' => in_array( $opt, self::$private, true ),
104 ];
105 }
106 foreach ( self::$site as $opt ) {
107 $val = self::site_get( $opt );
108 $result[ $opt ] = [
109 'label' => $si . $opt,
110 'value' => is_bool( $val ) ? $val ? 1 : 0 : $val,
111 'private' => in_array( $opt, self::$private, true ),
112 ];
113 }
114 return $result;
115 }
116
117
118 /**
119 * Get an option value for a site.
120 *
121 * @param string $option Option name. Expected to not be SQL-escaped.
122 * @param boolean $default Optional. The default value if option doesn't exists.
123 * @return mixed The value of the option.
124 * @since 1.0.0
125 */
126 public static function site_get( $option, $default = null ) {
127 if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) {
128 $default = self::$defaults[ $option ];
129 }
130 $val = get_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, $default );
131 if ( is_bool( $default ) ) {
132 return (bool) $val;
133 }
134 return $val;
135 }
136
137 /**
138 * Get an option value for a network.
139 *
140 * @param string $option Option name. Expected to not be SQL-escaped.
141 * @param boolean $default Optional. The default value if option doesn't exists.
142 * @return mixed The value of the option.
143 * @since 1.0.0
144 */
145 public static function network_get( $option, $default = null ) {
146 if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) {
147 $default = self::$defaults[ $option ];
148 }
149 $val = get_site_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, $default );
150 if ( is_bool( $default ) ) {
151 return (bool) $val;
152 }
153 return $val;
154 }
155
156 /**
157 * Verify if an option exists.
158 *
159 * @param string $option Option name. Expected to not be SQL-escaped.
160 * @return boolean True if the option exists, false otherwise.
161 * @since 1.0.0
162 */
163 public static function site_exists( $option ) {
164 return 'non_existent_option' !== get_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' );
165 }
166
167 /**
168 * Verify if an option exists.
169 *
170 * @param string $option Option name. Expected to not be SQL-escaped.
171 * @return boolean True if the option exists, false otherwise.
172 * @since 1.0.0
173 */
174 public static function network_exists( $option ) {
175 return 'non_existent_option' !== get_site_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' );
176 }
177
178 /**
179 * Set an option value for a site.
180 *
181 * @param string $option Option name. Expected to not be SQL-escaped.
182 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
183 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
184 * `$autoload` can only be updated using `update_option()` if `$value` is also changed.
185 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
186 * the default value is 'yes'. Default null.
187 * @return boolean False if value was not updated and true if value was updated.
188 * @since 1.0.0
189 */
190 public static function site_set( $option, $value, $autoload = null ) {
191 if ( false === $value ) {
192 $value = 0;
193 }
194 return update_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, $value, $autoload );
195 }
196
197 /**
198 * Set an option value for a network.
199 *
200 * @param string $option Option name. Expected to not be SQL-escaped.
201 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
202 * @return boolean False if value was not updated and true if value was updated.
203 * @since 1.0.0
204 */
205 public static function network_set( $option, $value ) {
206 if ( false === $value ) {
207 $value = 0;
208 }
209 return update_site_option( OPCM_PRODUCT_ABBREVIATION . '_' . $option, $value );
210 }
211
212 /**
213 * Delete all options for a site.
214 *
215 * @return integer Number of deleted items.
216 * @since 1.0.0
217 */
218 public static function site_delete_all() {
219 global $wpdb;
220 $result = 0;
221 // phpcs:ignore
222 $delete = $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '" . OPCM_PRODUCT_ABBREVIATION . '_%' . "';" );
223 foreach ( $delete as $option ) {
224 if ( delete_option( $option ) ) {
225 ++$result;
226 }
227 }
228 return $result;
229 }
230
231 /**
232 * Reset some options to their defaults.
233 *
234 * @since 1.0.0
235 */
236 public static function reset_to_defaults() {
237 foreach ( self::$network as $key ) {
238 if ( 'version' !== $key ) {
239 self::network_set( $key, self::$defaults[ $key ] );
240 }
241 }
242 }
243
244 /**
245 * Initializes the class and set its properties.
246 *
247 * @since 1.0.0
248 */
249 public function __construct() {
250 }
251 }
252
253 Option::init();
254