| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (!defined('WPO_VERSION')) die('No direct access allowed'); | |
| 3 | +if (!defined('ABSPATH')) die('No direct access allowed'); | |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * The proper way to obtain access to the instance is via WP_Optimize()->get_options(). |
| 7 | 7 | */ |
| @@ -6,8 +6,21 @@ | ||
| 6 | 6 | * The proper way to obtain access to the instance is via WP_Optimize()->get_options(). |
| 7 | 7 | */ |
| 8 | 8 | class WP_Optimize_Options { |
| 9 | 9 | |
| 10 | + /** | |
| 11 | + * Returns singleton instance object | |
| 12 | + * | |
| 13 | + * @return WP_Optimize_Options Returns `WP_Optimize_Options` object | |
| 14 | + */ | |
| 15 | + public static function instance() { | |
| 16 | + static $_instance = null; | |
| 17 | + if (null === $_instance) { | |
| 18 | + $_instance = new self(); | |
| 19 | + } | |
| 20 | + return $_instance; | |
| 21 | + } | |
| 22 | + | |
| 10 | 23 | public $default_settings = array( |
| 11 | 24 | 'settings' => '', |
| 12 | 25 | 'schedule' => 'false', |
| 13 | 26 | 'schedule-type' => 'wpo_weekly', |
| @@ -19,13 +32,15 @@ | ||
| 19 | 32 | 'comments_action' => array(), |
| 20 | 33 | 'auto' => '', |
| 21 | 34 | 'logging' => '', |
| 22 | 35 | 'logging-additional' => '', |
| 36 | + '404_detector' => 0, | |
| 23 | 37 | ); |
| 24 | 38 | |
| 25 | 39 | /** |
| 26 | 40 | * Returns url to WP-Optimize admin dashboard. |
| 27 | 41 | * |
| 42 | + * @param string $page | |
| 28 | 43 | * @return string |
| 29 | 44 | */ |
| 30 | 45 | public function admin_page_url($page = 'WP-Optimize') { |
| 31 | 46 | if (is_multisite()) { |
| @@ -38,9 +53,9 @@ | ||
| 38 | 53 | /** |
| 39 | 54 | * Returns WP-Optimize option value. |
| 40 | 55 | * |
| 41 | 56 | * @param string $option Option name. |
| 42 | - * @param bool $default | |
| 57 | + * @param mixed $default | |
| 43 | 58 | * @return mixed|void |
| 44 | 59 | */ |
| 45 | 60 | public function get_option($option, $default = false) { |
| 46 | 61 | if (is_multisite()) { |
| @@ -62,9 +77,9 @@ | ||
| 62 | 77 | // if blog was changed. |
| 63 | 78 | if ($blog_changed) restore_current_blog(); |
| 64 | 79 | // check option value for new plugin versions. |
| 65 | 80 | $new_version_option_value = get_site_option('wp-optimize-mu-'.$option, null); |
| 66 | - // if it is exists old version value and doesn't exists new version option then return value. | |
| 81 | + // if it exists old version value and doesn't exist new version option then return value. | |
| 67 | 82 | if (null !== $old_version_option_value && null === $new_version_option_value) return $old_version_option_value; |
| 68 | 83 | |
| 69 | 84 | return get_site_option('wp-optimize-mu-'.$option, $default); |
| 70 | 85 | } else { |
| @@ -74,17 +89,18 @@ | ||
| 74 | 89 | |
| 75 | 90 | /** |
| 76 | 91 | * Update WP-Optimize option value. |
| 77 | 92 | * |
| 78 | - * @param string $option Option name. | |
| 79 | - * @param mixed $value Option value. | |
| 93 | + * @param string $option Option name. | |
| 94 | + * @param mixed $value Option value. | |
| 95 | + * @param bool|string|null $autoload Whether to autoload the option. Ignored for multisite network options. | |
| 80 | 96 | * @return bool |
| 81 | 97 | */ |
| 82 | - public function update_option($option, $value) { | |
| 98 | + public function update_option($option, $value, $autoload = null) { | |
| 83 | 99 | if (is_multisite()) { |
| 84 | 100 | return update_site_option('wp-optimize-mu-'.$option, $value); |
| 85 | 101 | } else { |
| 86 | - return update_option('wp-optimize-'.$option, $value); | |
| 102 | + return update_option('wp-optimize-'.$option, $value, $autoload); | |
| 87 | 103 | } |
| 88 | 104 | } |
| 89 | 105 | |
| 90 | 106 | /** |
| @@ -99,22 +115,29 @@ | ||
| 99 | 115 | delete_option('wp-optimize-'.$option); |
| 100 | 116 | } |
| 101 | 117 | } |
| 102 | 118 | |
| 119 | + /** | |
| 120 | + * Get option keys | |
| 121 | + * | |
| 122 | + * @return array | |
| 123 | + */ | |
| 103 | 124 | public function get_option_keys() { |
| 104 | - | |
| 105 | - return apply_filters( | |
| 125 | + $keys = array('defaults', 'weekly-schedule', 'schedule', 'retention-enabled', 'retention-period', 'last-optimized', 'enable-admin-menu', 'schedule-type', 'total-cleaned', 'current-cleaned', 'email-address', 'email', 'auto', 'settings', 'dismiss_page_notice_until', 'dismiss_dash_notice_until', 'enable_cache_in_admin_bar', 'total-cleaned-current-month', 'total-cleaned-previous-month'); | |
| 126 | + $filtered_option_keys = apply_filters( | |
| 106 | 127 | 'wp_optimize_option_keys', |
| 107 | - array('defaults', 'weekly-schedule', 'schedule', 'retention-enabled', 'retention-period', 'last-optimized', 'enable-admin-menu', 'schedule-type', 'total-cleaned', 'current-cleaned', 'email-address', 'email', 'auto', 'settings', 'dismiss_page_notice_until', 'dismiss_dash_notice_until', 'enable_cache_in_admin_bar') | |
| 128 | + $keys | |
| 108 | 129 | ); |
| 130 | + | |
| 131 | + return is_array($filtered_option_keys) ? $filtered_option_keys : $keys; | |
| 109 | 132 | } |
| 110 | 133 | |
| 111 | 134 | /** |
| 112 | - * This particular option has its own functions abstracted to make it easier to change the format in future. | |
| 135 | + * This particular option has its own functions abstracted to make it easier to change the format in the future. | |
| 113 | 136 | * To allow callers to always assume the latest format (because get_main_settings() will convert, if needed). |
| 114 | 137 | * |
| 115 | 138 | * @param array $settings Array of optimization settings. |
| 116 | - * @return array | |
| 139 | + * @return bool | |
| 117 | 140 | */ |
| 118 | 141 | private function save_manual_run_optimizations_settings($settings) { |
| 119 | 142 | $settings['last_saved_in'] = WPO_VERSION; |
| 120 | 143 | return $this->update_option('settings', $settings); |
| @@ -167,8 +190,9 @@ | ||
| 167 | 190 | |
| 168 | 191 | /** |
| 169 | 192 | * Save options |
| 170 | 193 | * |
| 194 | + * @param array $settings | |
| 171 | 195 | * @return array |
| 172 | 196 | */ |
| 173 | 197 | public function save_settings($settings) { |
| 174 | 198 | $output = array('messages' => array(), 'errors' => array()); |
| @@ -209,9 +233,9 @@ | ||
| 209 | 233 | |
| 210 | 234 | // Get saved admin menu value before check. |
| 211 | 235 | $saved_admin_bar = $this->get_option('enable-admin-menu', 'false'); |
| 212 | 236 | |
| 213 | - // Set refresh of default false so it doesnt refresh after save. | |
| 237 | + // Set refresh of default false so it doesn't refresh after save. | |
| 214 | 238 | $output['refresh'] = false; |
| 215 | 239 | |
| 216 | 240 | if (!empty($settings['enable-admin-bar'])) { |
| 217 | 241 | $this->update_option('enable-admin-menu', 'true'); |
| @@ -222,16 +246,16 @@ | ||
| 222 | 246 | // Make sure inbound input is a string. |
| 223 | 247 | $updated_admin_bar = (isset($settings['enable-admin-bar']) && $settings['enable-admin-bar']) ? 'true' : 'false'; |
| 224 | 248 | |
| 225 | 249 | // Check if the value is refreshed . |
| 226 | - if ($saved_admin_bar != $updated_admin_bar) { | |
| 250 | + if ($saved_admin_bar !== $updated_admin_bar) { | |
| 227 | 251 | // Set refresh to true as the values have changed. |
| 228 | 252 | $output['refresh'] = true; |
| 229 | 253 | } |
| 230 | 254 | |
| 231 | 255 | // Save cache toolbar display setting |
| 232 | - $saved_enable_cache_in_admin_bar = $this->get_option('enable_cache_in_admin_bar', true); | |
| 233 | - if ($saved_enable_cache_in_admin_bar != $settings['enable_cache_in_admin_bar']) { | |
| 256 | + $saved_enable_cache_in_admin_bar = (bool) $this->get_option('enable_cache_in_admin_bar', true); | |
| 257 | + if ($saved_enable_cache_in_admin_bar !== (bool) $settings['enable_cache_in_admin_bar']) { | |
| 234 | 258 | $this->update_option('enable_cache_in_admin_bar', $settings['enable_cache_in_admin_bar']); |
| 235 | 259 | $output['refresh'] = true; |
| 236 | 260 | } |
| 237 | 261 | |
| @@ -241,15 +265,15 @@ | ||
| 241 | 265 | if (isset($settings['wpo-sites-cron'])) { |
| 242 | 266 | $this->update_option('wpo-sites-cron', $settings['wpo-sites-cron']); |
| 243 | 267 | } |
| 244 | 268 | |
| 245 | - if (isset($settings['wpo-sites'])) { | |
| 269 | + if (isset($settings['wpo-sites']) && is_array($settings['wpo-sites'])) { | |
| 246 | 270 | $this->save_wpo_sites_option($settings['wpo-sites']); |
| 247 | 271 | } |
| 248 | 272 | |
| 249 | 273 | /** Save logging options */ |
| 250 | - $logger_type = isset($settings['wpo-logger-type']) ? $settings['wpo-logger-type'] : ''; | |
| 251 | - $logger_options = isset($settings['wpo-logger-options']) ? $settings['wpo-logger-options'] : ''; | |
| 274 | + $logger_type = $settings['wpo-logger-type'] ?? ''; | |
| 275 | + $logger_options = $settings['wpo-logger-options'] ?? ''; | |
| 252 | 276 | |
| 253 | 277 | $this->update_option('logging', $logger_type); |
| 254 | 278 | $this->update_option('logging-additional', $logger_options); |
| 255 | 279 | |
| @@ -268,8 +292,11 @@ | ||
| 268 | 292 | // Save force DB optimization value. |
| 269 | 293 | $enable_db_force_optimize = (isset($settings['innodb-force-optimize']) ? 'true' : 'false'); |
| 270 | 294 | $this->update_option('enable-db-force-optimize', $enable_db_force_optimize); |
| 271 | 295 | |
| 296 | + $wpo_404_detector = $settings['404_detector'] ?? 0; | |
| 297 | + $this->update_option('404_detector', $wpo_404_detector); | |
| 298 | + | |
| 272 | 299 | $output['messages'][] = __('Settings updated.', 'wp-optimize'); |
| 273 | 300 | |
| 274 | 301 | return $output; |
| 275 | 302 | |
| @@ -277,21 +304,31 @@ | ||
| 277 | 304 | |
| 278 | 305 | /** |
| 279 | 306 | * Wipe all options from database options tables. |
| 280 | 307 | * |
| 281 | - * @return bool|false|int | |
| 308 | + * @return bool|int | |
| 282 | 309 | */ |
| 283 | 310 | public function wipe_settings() { |
| 284 | 311 | global $wpdb; |
| 285 | 312 | |
| 286 | 313 | wp_cache_flush(); |
| 314 | + | |
| 315 | + $settings = array( | |
| 316 | + 'wpo-ignores-table-deletion-warning', | |
| 317 | + 'wpo-ignores-post-meta-deletion-warning', | |
| 318 | + 'wpo-ignores-orphaned-relationship-data-deletion-warning', | |
| 319 | + 'wpo-hide-minify-information-notice', | |
| 320 | + 'wpo_hide_css_merging_notice', | |
| 321 | + 'wpo_hide_js_merging_notice', | |
| 322 | + ); | |
| 287 | 323 | |
| 288 | - // Delete the user meta if user meta is set for ignores the table delete warning | |
| 289 | - $user_query = new WP_User_Query(array('meta_key' => 'wpo-ignores-table-delete-warning', 'meta_value' => '1', 'fields' => 'ID')); | |
| 290 | - $users = $user_query->get_results(); | |
| 291 | - if (!empty($users)) { | |
| 292 | - foreach ($users as $user_id) { | |
| 293 | - delete_user_meta($user_id, 'wpo-ignores-table-delete-warning'); | |
| 324 | + foreach ($settings as $setting) { | |
| 325 | + $user_query = new WP_User_Query(array('meta_key' => $setting, 'meta_value' => '1', 'fields' => 'ID')); | |
| 326 | + $users = $user_query->get_results(); | |
| 327 | + if (!empty($users)) { | |
| 328 | + foreach ($users as $user_id) { | |
| 329 | + delete_user_meta($user_id, $setting); | |
| 330 | + } | |
| 294 | 331 | } |
| 295 | 332 | } |
| 296 | 333 | |
| 297 | 334 | // disable cache and clean any information related to WP-Optimize Cache. |
| @@ -296,19 +333,24 @@ | ||
| 296 | 333 | |
| 297 | 334 | // disable cache and clean any information related to WP-Optimize Cache. |
| 298 | 335 | WP_Optimize()->get_page_cache()->clean_up(); |
| 299 | 336 | // delete settings from .htaccess |
| 300 | - WP_Optimize::get_browser_cache()->disable(); | |
| 301 | - WP_Optimize::get_gzip_compression()->disable(); | |
| 337 | + WP_Optimize()->get_browser_cache()->disable(); | |
| 338 | + WP_Optimize()->get_gzip_compression()->disable(); | |
| 339 | + // disable Webp CRON event if enabled | |
| 340 | + WP_Optimize()->get_webp_instance()->remove_webp_cron_schedules(); | |
| 302 | 341 | |
| 303 | 342 | // delete settings from options table. |
| 304 | 343 | $keys = '"' . implode('", "', $this->get_additional_settings_keys()) . '"'; |
| 305 | 344 | |
| 345 | + // phpcs:disable | |
| 346 | + // WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only hardcoded strings are used | |
| 306 | 347 | if (is_multisite()) { |
| 307 | - $result = $wpdb->query("DELETE FROM {$wpdb->sitemeta} WHERE `meta_key` LIKE 'wp-optimize-mu-%' OR `meta_key` IN ({$keys})"); | |
| 348 | + $result = $wpdb->query("DELETE FROM `{$wpdb->sitemeta}` WHERE `meta_key` LIKE 'wp-optimize-mu%' OR `meta_key` IN ($keys)"); | |
| 308 | 349 | } else { |
| 309 | - $result = $wpdb->query("DELETE FROM {$wpdb->options} WHERE `option_name` LIKE 'wp-optimize-%' OR `option_name` IN ({$keys})"); | |
| 350 | + $result = $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'wp-optimize%' OR `option_name` IN ($keys)"); | |
| 310 | 351 | } |
| 352 | + // phpcs:enable | |
| 311 | 353 | |
| 312 | 354 | return $result; |
| 313 | 355 | } |
| 314 | 356 | |
| @@ -336,9 +378,9 @@ | ||
| 336 | 378 | |
| 337 | 379 | $optimizer = WP_Optimize()->get_optimizer(); |
| 338 | 380 | |
| 339 | 381 | if (!empty($settings["schedule_type"])) { |
| 340 | - $options_from_user = isset($settings['wp-optimize-auto']) ? $settings['wp-optimize-auto'] : array(); | |
| 382 | + $options_from_user = $settings['wp-optimize-auto'] ?? array(); | |
| 341 | 383 | |
| 342 | 384 | if (!is_array($options_from_user)) $options_from_user = array(); |
| 343 | 385 | |
| 344 | 386 | $new_auto_options = array(); |
| @@ -378,9 +420,9 @@ | ||
| 378 | 420 | * |
| 379 | 421 | * @param array $sent_options Options sent from Ajax. |
| 380 | 422 | * @param boolean $use_dom_id Parameter is legacy. |
| 381 | 423 | * @param boolean $available_for_saving_only Save only available for saving optimization state. |
| 382 | - * @return array User Options | |
| 424 | + * @return bool User Options | |
| 383 | 425 | */ |
| 384 | 426 | public function save_sent_manual_run_optimization_options($sent_options, $use_dom_id = false, $available_for_saving_only = true) { |
| 385 | 427 | $optimizations = WP_Optimize()->get_optimizer()->get_optimizations(); |
| 386 | 428 | $user_options = array(); |
| @@ -406,33 +448,39 @@ | ||
| 406 | 448 | /** |
| 407 | 449 | * Setup options if not exists already. |
| 408 | 450 | */ |
| 409 | 451 | public function set_default_options() { |
| 410 | - $deprecated = null; | |
| 411 | - $autoload_no = 'no'; | |
| 412 | - | |
| 413 | 452 | if (false === $this->get_option('schedule')) { |
| 414 | - // The option hasn't been added yet. We'll add it with $autoload_no set to 'no'. | |
| 415 | - $this->update_option('schedule', 'false', $deprecated, $autoload_no); | |
| 416 | - $this->update_option('last-optimized', 'Never', $deprecated, $autoload_no); | |
| 417 | - $this->update_option('schedule-type', 'wpo_weekly', $deprecated, $autoload_no); | |
| 453 | + $this->update_option('schedule', 'false'); | |
| 454 | + $this->update_option('last-optimized', 'Never'); | |
| 455 | + $this->update_option('schedule-type', 'wpo_weekly'); | |
| 418 | 456 | // Deactivate cron. |
| 419 | 457 | wpo_cron_deactivate(); |
| 420 | 458 | } |
| 421 | 459 | |
| 422 | 460 | if (false === $this->get_option('retention-enabled')) { |
| 423 | - $this->update_option('retention-enabled', 'false', $deprecated, $autoload_no); | |
| 424 | - $this->update_option('retention-period', '2', $deprecated, $autoload_no); | |
| 461 | + $this->update_option('retention-enabled', 'false'); | |
| 462 | + $this->update_option('retention-period', '2'); | |
| 425 | 463 | } |
| 426 | 464 | |
| 427 | 465 | if (false === $this->get_option('enable-admin-menu')) { |
| 428 | - $this->update_option('enable-admin-menu', 'false', $deprecated, $autoload_no); | |
| 466 | + $this->update_option('enable-admin-menu', 'false'); | |
| 429 | 467 | } |
| 430 | 468 | |
| 431 | 469 | if (false === $this->get_option('total-cleaned')) { |
| 432 | - $this->update_option('total-cleaned', '0', $deprecated, $autoload_no); | |
| 470 | + $this->update_option('total-cleaned', '0'); | |
| 433 | 471 | } |
| 434 | 472 | |
| 473 | + // For UDC monthly reports. | |
| 474 | + if (false === $this->get_option('total-cleaned-current-month')) { | |
| 475 | + $this->update_option('total-cleaned-current-month', '0'); | |
| 476 | + } | |
| 477 | + | |
| 478 | + // For UDC monthly reports. | |
| 479 | + if (false === $this->get_option('total-cleaned-previous-month')) { | |
| 480 | + $this->update_option('total-cleaned-previous-month', '0'); | |
| 481 | + } | |
| 482 | + | |
| 435 | 483 | $optimizer = WP_Optimize()->get_optimizer(); |
| 436 | 484 | |
| 437 | 485 | $optimizations = $optimizer->get_optimizations(); |
| 438 | 486 | |
| @@ -480,9 +528,9 @@ | ||
| 480 | 528 | private function save_additional_auto_backup_options($settings) { |
| 481 | 529 | // Save additional auto backup option values. |
| 482 | 530 | foreach ($settings as $key => $value) { |
| 483 | 531 | if (preg_match('/enable\-auto\-backup\-/', $key)) { |
| 484 | - $value = ('true' == $value) ? 'true' : 'false'; | |
| 532 | + $value = ('true' === $value) ? 'true' : 'false'; | |
| 485 | 533 | $this->update_option($key, $value); |
| 486 | 534 | } |
| 487 | 535 | } |
| 488 | 536 | } |