| 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 Decalog\System; |
| 13 |
|
| 14 |
use Decalog\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['download_favicons'] = false; |
| 72 |
self::$defaults['script_in_footer'] = false; |
| 73 |
self::$defaults['display_nag'] = false; // In plugin settings. |
| 74 |
self::$defaults['privileges'] = 0; |
| 75 |
self::$defaults['nags'] = []; |
| 76 |
self::$defaults['version'] = '0.0.0'; |
| 77 |
self::$defaults['loggers'] = []; |
| 78 |
self::$defaults['livelog'] = true; |
| 79 |
self::$defaults['respect_wp_debug'] = false; // In plugin settings. |
| 80 |
self::$defaults['logger_autostart'] = true; // In plugin settings. |
| 81 |
self::$defaults['autolisteners'] = true; // In plugin settings. |
| 82 |
self::$defaults['listeners'] = []; // In plugin settings. |
| 83 |
self::$defaults['pseudonymization'] = false; // In plugin settings. |
| 84 |
self::$defaults['earlyloading'] = false; // In plugin settings. |
| 85 |
self::$defaults['metrics_authent'] = false; // In plugin settings. |
| 86 |
self::$defaults['adminbar'] = true; |
| 87 |
self::$defaults['slow_query_ms'] = 50; |
| 88 |
self::$defaults['medium_query_ms'] = 10; |
| 89 |
self::$defaults['trace_query'] = false; |
| 90 |
self::$defaults['slow_query_warn'] = true; |
| 91 |
self::$defaults['unbuffered_cli'] = true; |
| 92 |
self::$defaults['buffer_size'] = 100; |
| 93 |
self::$defaults['unknown_metrics_warn'] = true; |
| 94 |
self::$defaults['env_substitution'] = false; |
| 95 |
self::$network = [ 'version', 'earlyloading', 'use_cdn', 'download_favicons', 'script_in_footer', 'display_nag', 'respect_wp_debug', 'livelog', 'logger_autostart', 'autolisteners', 'pseudonymization', 'privileges', 'metrics_authent', 'adminbar', 'slow_query_ms', 'medium_query_ms', 'trace_query', 'slow_query_warn', 'unknown_metrics_warn', 'env_substitution', 'unbuffered_cli', 'buffer_size' ]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the options infos for Site Health "info" tab. |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
*/ |
| 103 |
public static function debug_info() { |
| 104 |
$result = []; |
| 105 |
$si = '[Site Option] '; |
| 106 |
$nt = $si; |
| 107 |
if ( Environment::is_wordpress_multisite() ) { |
| 108 |
$nt = '[Network Option] '; |
| 109 |
} |
| 110 |
foreach ( self::$network as $opt ) { |
| 111 |
$val = self::network_get( $opt ); |
| 112 |
$result[ $opt ] = [ |
| 113 |
'label' => $nt . $opt, |
| 114 |
'value' => is_bool( $val ) ? $val ? 1 : 0 : $val, |
| 115 |
'private' => in_array( $opt, self::$private, true ), |
| 116 |
]; |
| 117 |
} |
| 118 |
if ( ! (bool) self::site_get( 'autolisteners' ) ) { |
| 119 |
$result['listeners'] = [ |
| 120 |
'label' => $nt . 'listeners', |
| 121 |
'value' => implode( ', ', self::network_get( 'listeners' ) ), |
| 122 |
]; |
| 123 |
} |
| 124 |
foreach ( self::$site as $opt ) { |
| 125 |
$val = self::site_get( $opt ); |
| 126 |
$result[ $opt ] = [ |
| 127 |
'label' => $si . $opt, |
| 128 |
'value' => is_bool( $val ) ? $val ? 1 : 0 : $val, |
| 129 |
'private' => in_array( $opt, self::$private, true ), |
| 130 |
]; |
| 131 |
} |
| 132 |
if ( (bool) self::site_get( 'earlyloading' ) ) { |
| 133 |
if ( defined( 'DECALOG_EARLY_INIT' ) ) { |
| 134 |
$result['muplugin'] = [ |
| 135 |
'label' => '[MU-Plugin diagnostic]', |
| 136 |
'value' => 'OK: loaded and operational', |
| 137 |
]; |
| 138 |
} else { |
| 139 |
$result['muplugin'] = [ |
| 140 |
'label' => '[MU-Plugin diagnostic]', |
| 141 |
'value' => 'Error: not loaded', |
| 142 |
]; |
| 143 |
$a = []; |
| 144 |
foreach ( [ 'DECALOG_EARLY_INIT_WPMUDIR_ERROR', 'DECALOG_EARLY_INIT_COPY_ERROR', 'DECALOG_EARLY_INIT_ERROR' ] as $err ) { |
| 145 |
if ( defined( $err ) ) { |
| 146 |
$a[] = $err; |
| 147 |
} |
| 148 |
} |
| 149 |
if ( 0 < count( $a ) ) { |
| 150 |
$result['muplugin']['value'] .= ' ' . implode( ' ', $a ); |
| 151 |
} |
| 152 |
} |
| 153 |
if ( defined( 'DECALOG_BOOTSTRAPPED' ) ) { |
| 154 |
$result['dropin'] = [ |
| 155 |
'label' => '[Drop-In diagnostic]', |
| 156 |
'value' => 'OK: loaded and operational', |
| 157 |
]; |
| 158 |
if ( defined( 'DECALOG_TRACEID' ) ) { |
| 159 |
$result['traceid'] = [ |
| 160 |
'label' => '[traceID diagnostic]', |
| 161 |
'value' => 'OK: ' . DECALOG_TRACEID, |
| 162 |
]; |
| 163 |
} else { |
| 164 |
$result['traceid'] = [ |
| 165 |
'label' => '[traceID diagnostic]', |
| 166 |
'value' => 'Error: no value', |
| 167 |
]; |
| 168 |
} |
| 169 |
} else { |
| 170 |
$result['dropin'] = [ |
| 171 |
'label' => '[Drop-In diagnostic]', |
| 172 |
'value' => 'Error: not loaded', |
| 173 |
]; |
| 174 |
$a = []; |
| 175 |
foreach ( [ 'DECALOG_BOOTSTRAP_COPY_ERROR', 'DECALOG_BOOTSTRAP_ALREADY_EXISTS_ERROR' ] as $err ) { |
| 176 |
if ( defined( $err ) ) { |
| 177 |
$a[] = $err; |
| 178 |
} |
| 179 |
} |
| 180 |
if ( 0 < count( $a ) ) { |
| 181 |
$result['dropin']['value'] .= ' ' . implode( ' ', $a ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
return $result; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Get an option value for a site. |
| 192 |
* |
| 193 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 194 |
* @param boolean $default Optional. The default value if option doesn't exists. |
| 195 |
* @return mixed The value of the option. |
| 196 |
* @since 1.0.0 |
| 197 |
*/ |
| 198 |
public static function site_get( $option, $default = null ) { |
| 199 |
if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) { |
| 200 |
$default = self::$defaults[ $option ]; |
| 201 |
} |
| 202 |
$val = get_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, $default ); |
| 203 |
if ( is_bool( $default ) ) { |
| 204 |
return (bool) $val; |
| 205 |
} |
| 206 |
return $val; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get an option value for a network. |
| 211 |
* |
| 212 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 213 |
* @param boolean $default Optional. The default value if option doesn't exists. |
| 214 |
* @return mixed The value of the option. |
| 215 |
* @since 1.0.0 |
| 216 |
*/ |
| 217 |
public static function network_get( $option, $default = null ) { |
| 218 |
if ( array_key_exists( $option, self::$defaults ) && ! isset( $default ) ) { |
| 219 |
$default = self::$defaults[ $option ]; |
| 220 |
} |
| 221 |
$val = get_site_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, $default ); |
| 222 |
if ( is_bool( $default ) ) { |
| 223 |
return (bool) $val; |
| 224 |
} |
| 225 |
return $val; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Verify if an option exists. |
| 230 |
* |
| 231 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 232 |
* @return boolean True if the option exists, false otherwise. |
| 233 |
* @since 1.0.0 |
| 234 |
*/ |
| 235 |
public static function site_exists( $option ) { |
| 236 |
return 'non_existent_option' !== get_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Verify if an option exists. |
| 241 |
* |
| 242 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 243 |
* @return boolean True if the option exists, false otherwise. |
| 244 |
* @since 1.0.0 |
| 245 |
*/ |
| 246 |
public static function network_exists( $option ) { |
| 247 |
return 'non_existent_option' !== get_site_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, 'non_existent_option' ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Set an option value for a site. |
| 252 |
* |
| 253 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 254 |
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
| 255 |
* @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, |
| 256 |
* `$autoload` can only be updated using `update_option()` if `$value` is also changed. |
| 257 |
* Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, |
| 258 |
* the default value is 'yes'. Default null. |
| 259 |
* @return boolean False if value was not updated and true if value was updated. |
| 260 |
* @since 1.0.0 |
| 261 |
*/ |
| 262 |
public static function site_set( $option, $value, $autoload = null ) { |
| 263 |
if ( false === $value ) { |
| 264 |
$value = 0; |
| 265 |
} |
| 266 |
return update_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, $value, $autoload ); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Set an option value for a network. |
| 271 |
* |
| 272 |
* @param string $option Option name. Expected to not be SQL-escaped. |
| 273 |
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
| 274 |
* @return boolean False if value was not updated and true if value was updated. |
| 275 |
* @since 1.0.0 |
| 276 |
*/ |
| 277 |
public static function network_set( $option, $value ) { |
| 278 |
if ( false === $value ) { |
| 279 |
$value = 0; |
| 280 |
} |
| 281 |
if ( false === $value ) { |
| 282 |
$value = 0; |
| 283 |
} |
| 284 |
return update_site_option( DECALOG_PRODUCT_ABBREVIATION . '_' . $option, $value ); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Delete all options for a site. |
| 289 |
* |
| 290 |
* @return integer Number of deleted items. |
| 291 |
* @since 1.0.0 |
| 292 |
*/ |
| 293 |
public static function site_delete_all() { |
| 294 |
global $wpdb; |
| 295 |
$result = 0; |
| 296 |
// phpcs:ignore |
| 297 |
$delete = $wpdb->get_col( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '" . DECALOG_PRODUCT_ABBREVIATION . '_%' . "';" ); |
| 298 |
foreach ( $delete as $option ) { |
| 299 |
if ( delete_option( $option ) ) { |
| 300 |
++$result; |
| 301 |
} |
| 302 |
} |
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Reset some options to their defaults. |
| 308 |
* |
| 309 |
* @since 1.0.0 |
| 310 |
*/ |
| 311 |
public static function reset_to_defaults() { |
| 312 |
foreach ( self::$network as $key ) { |
| 313 |
if ( 'version' !== $key ) { |
| 314 |
self::network_set( $key, self::$defaults[ $key ] ); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Initializes the class and set its properties. |
| 321 |
* |
| 322 |
* @since 1.0.0 |
| 323 |
*/ |
| 324 |
public function __construct() { |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
Option::init(); |
| 329 |
|