PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | includes/class-wp-optimize-options.php +75 -42 3.6.0 → 4.7.0 View file →
@@ -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 */
@@ -32,13 +32,15 @@
32 32 'comments_action' => array(),
33 33 'auto' => '',
34 34 'logging' => '',
35 35 'logging-additional' => '',
36 + '404_detector' => 0,
36 37 );
37 38
38 39 /**
39 40 * Returns url to WP-Optimize admin dashboard.
40 41 *
42 + * @param string $page
41 43 * @return string
42 44 */
43 45 public function admin_page_url($page = 'WP-Optimize') {
44 46 if (is_multisite()) {
@@ -51,9 +53,9 @@
51 53 /**
52 54 * Returns WP-Optimize option value.
53 55 *
54 56 * @param string $option Option name.
55 - * @param bool $default
57 + * @param mixed $default
56 58 * @return mixed|void
57 59 */
58 60 public function get_option($option, $default = false) {
59 61 if (is_multisite()) {
@@ -75,9 +77,9 @@
75 77 // if blog was changed.
76 78 if ($blog_changed) restore_current_blog();
77 79 // check option value for new plugin versions.
78 80 $new_version_option_value = get_site_option('wp-optimize-mu-'.$option, null);
79 - // 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.
80 82 if (null !== $old_version_option_value && null === $new_version_option_value) return $old_version_option_value;
81 83
82 84 return get_site_option('wp-optimize-mu-'.$option, $default);
83 85 } else {
@@ -87,17 +89,18 @@
87 89
88 90 /**
89 91 * Update WP-Optimize option value.
90 92 *
91 - * @param string $option Option name.
92 - * @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.
93 96 * @return bool
94 97 */
95 - public function update_option($option, $value) {
98 + public function update_option($option, $value, $autoload = null) {
96 99 if (is_multisite()) {
97 100 return update_site_option('wp-optimize-mu-'.$option, $value);
98 101 } else {
99 - return update_option('wp-optimize-'.$option, $value);
102 + return update_option('wp-optimize-'.$option, $value, $autoload);
100 103 }
101 104 }
102 105
103 106 /**
@@ -112,22 +115,29 @@
112 115 delete_option('wp-optimize-'.$option);
113 116 }
114 117 }
115 118
119 + /**
120 + * Get option keys
121 + *
122 + * @return array
123 + */
116 124 public function get_option_keys() {
117 -
118 - 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(
119 127 'wp_optimize_option_keys',
120 - 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
121 129 );
130 +
131 + return is_array($filtered_option_keys) ? $filtered_option_keys : $keys;
122 132 }
123 133
124 134 /**
125 - * 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.
126 136 * To allow callers to always assume the latest format (because get_main_settings() will convert, if needed).
127 137 *
128 138 * @param array $settings Array of optimization settings.
129 - * @return array
139 + * @return bool
130 140 */
131 141 private function save_manual_run_optimizations_settings($settings) {
132 142 $settings['last_saved_in'] = WPO_VERSION;
133 143 return $this->update_option('settings', $settings);
@@ -180,8 +190,9 @@
180 190
181 191 /**
182 192 * Save options
183 193 *
194 + * @param array $settings
184 195 * @return array
185 196 */
186 197 public function save_settings($settings) {
187 198 $output = array('messages' => array(), 'errors' => array());
@@ -222,9 +233,9 @@
222 233
223 234 // Get saved admin menu value before check.
224 235 $saved_admin_bar = $this->get_option('enable-admin-menu', 'false');
225 236
226 - // Set refresh of default false so it doesnt refresh after save.
237 + // Set refresh of default false so it doesn't refresh after save.
227 238 $output['refresh'] = false;
228 239
229 240 if (!empty($settings['enable-admin-bar'])) {
230 241 $this->update_option('enable-admin-menu', 'true');
@@ -235,16 +246,16 @@
235 246 // Make sure inbound input is a string.
236 247 $updated_admin_bar = (isset($settings['enable-admin-bar']) && $settings['enable-admin-bar']) ? 'true' : 'false';
237 248
238 249 // Check if the value is refreshed .
239 - if ($saved_admin_bar != $updated_admin_bar) {
250 + if ($saved_admin_bar !== $updated_admin_bar) {
240 251 // Set refresh to true as the values have changed.
241 252 $output['refresh'] = true;
242 253 }
243 254
244 255 // Save cache toolbar display setting
245 - $saved_enable_cache_in_admin_bar = $this->get_option('enable_cache_in_admin_bar', true);
246 - 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']) {
247 258 $this->update_option('enable_cache_in_admin_bar', $settings['enable_cache_in_admin_bar']);
248 259 $output['refresh'] = true;
249 260 }
250 261
@@ -254,15 +265,15 @@
254 265 if (isset($settings['wpo-sites-cron'])) {
255 266 $this->update_option('wpo-sites-cron', $settings['wpo-sites-cron']);
256 267 }
257 268
258 - if (isset($settings['wpo-sites'])) {
269 + if (isset($settings['wpo-sites']) && is_array($settings['wpo-sites'])) {
259 270 $this->save_wpo_sites_option($settings['wpo-sites']);
260 271 }
261 272
262 273 /** Save logging options */
263 - $logger_type = isset($settings['wpo-logger-type']) ? $settings['wpo-logger-type'] : '';
264 - $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'] ?? '';
265 276
266 277 $this->update_option('logging', $logger_type);
267 278 $this->update_option('logging-additional', $logger_options);
268 279
@@ -281,8 +292,11 @@
281 292 // Save force DB optimization value.
282 293 $enable_db_force_optimize = (isset($settings['innodb-force-optimize']) ? 'true' : 'false');
283 294 $this->update_option('enable-db-force-optimize', $enable_db_force_optimize);
284 295
296 + $wpo_404_detector = $settings['404_detector'] ?? 0;
297 + $this->update_option('404_detector', $wpo_404_detector);
298 +
285 299 $output['messages'][] = __('Settings updated.', 'wp-optimize');
286 300
287 301 return $output;
288 302
@@ -290,21 +304,31 @@
290 304
291 305 /**
292 306 * Wipe all options from database options tables.
293 307 *
294 - * @return bool|false|int
308 + * @return bool|int
295 309 */
296 310 public function wipe_settings() {
297 311 global $wpdb;
298 312
299 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 + );
300 323
301 - // Delete the user meta if user meta is set for ignores the table delete warning
302 - $user_query = new WP_User_Query(array('meta_key' => 'wpo-ignores-table-delete-warning', 'meta_value' => '1', 'fields' => 'ID'));
303 - $users = $user_query->get_results();
304 - if (!empty($users)) {
305 - foreach ($users as $user_id) {
306 - 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 + }
307 331 }
308 332 }
309 333
310 334 // disable cache and clean any information related to WP-Optimize Cache.
@@ -317,13 +341,16 @@
317 341
318 342 // delete settings from options table.
319 343 $keys = '"' . implode('", "', $this->get_additional_settings_keys()) . '"';
320 344
345 + // phpcs:disable
346 + // WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only hardcoded strings are used
321 347 if (is_multisite()) {
322 - $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)");
323 349 } else {
324 - $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)");
325 351 }
352 + // phpcs:enable
326 353
327 354 return $result;
328 355 }
329 356
@@ -351,9 +378,9 @@
351 378
352 379 $optimizer = WP_Optimize()->get_optimizer();
353 380
354 381 if (!empty($settings["schedule_type"])) {
355 - $options_from_user = isset($settings['wp-optimize-auto']) ? $settings['wp-optimize-auto'] : array();
382 + $options_from_user = $settings['wp-optimize-auto'] ?? array();
356 383
357 384 if (!is_array($options_from_user)) $options_from_user = array();
358 385
359 386 $new_auto_options = array();
@@ -393,9 +420,9 @@
393 420 *
394 421 * @param array $sent_options Options sent from Ajax.
395 422 * @param boolean $use_dom_id Parameter is legacy.
396 423 * @param boolean $available_for_saving_only Save only available for saving optimization state.
397 - * @return array User Options
424 + * @return bool User Options
398 425 */
399 426 public function save_sent_manual_run_optimization_options($sent_options, $use_dom_id = false, $available_for_saving_only = true) {
400 427 $optimizations = WP_Optimize()->get_optimizer()->get_optimizations();
401 428 $user_options = array();
@@ -421,33 +448,39 @@
421 448 /**
422 449 * Setup options if not exists already.
423 450 */
424 451 public function set_default_options() {
425 - $deprecated = null;
426 - $autoload_no = 'no';
427 -
428 452 if (false === $this->get_option('schedule')) {
429 - // The option hasn't been added yet. We'll add it with $autoload_no set to 'no'.
430 - $this->update_option('schedule', 'false', $deprecated, $autoload_no);
431 - $this->update_option('last-optimized', 'Never', $deprecated, $autoload_no);
432 - $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');
433 456 // Deactivate cron.
434 457 wpo_cron_deactivate();
435 458 }
436 459
437 460 if (false === $this->get_option('retention-enabled')) {
438 - $this->update_option('retention-enabled', 'false', $deprecated, $autoload_no);
439 - $this->update_option('retention-period', '2', $deprecated, $autoload_no);
461 + $this->update_option('retention-enabled', 'false');
462 + $this->update_option('retention-period', '2');
440 463 }
441 464
442 465 if (false === $this->get_option('enable-admin-menu')) {
443 - $this->update_option('enable-admin-menu', 'false', $deprecated, $autoload_no);
466 + $this->update_option('enable-admin-menu', 'false');
444 467 }
445 468
446 469 if (false === $this->get_option('total-cleaned')) {
447 - $this->update_option('total-cleaned', '0', $deprecated, $autoload_no);
470 + $this->update_option('total-cleaned', '0');
448 471 }
449 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 +
450 483 $optimizer = WP_Optimize()->get_optimizer();
451 484
452 485 $optimizations = $optimizer->get_optimizations();
453 486
@@ -495,9 +528,9 @@
495 528 private function save_additional_auto_backup_options($settings) {
496 529 // Save additional auto backup option values.
497 530 foreach ($settings as $key => $value) {
498 531 if (preg_match('/enable\-auto\-backup\-/', $key)) {
499 - $value = ('true' == $value) ? 'true' : 'false';
532 + $value = ('true' === $value) ? 'true' : 'false';
500 533 $this->update_option($key, $value);
501 534 }
502 535 }
503 536 }