PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.3
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
wp-optimize / includes / class-wp-optimize-options.php

class-wp-optimize-options.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.3, at includes/class-wp-optimize-options.php

537 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 /**
6 * The proper way to obtain access to the instance is via WP_Optimize()->get_options().
7 */
8 class WP_Optimize_Options {
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
23 public $default_settings = array(
24 'settings' => '',
25 'schedule' => 'false',
26 'schedule-type' => 'wpo_weekly',
27 'retention-enabled' => 'false',
28 'retention-period' => '',
29 'enable-admin-menu' => 'false',
30 'enable_cache_in_admin_bar' => true,
31 'trackbacks_action' => array(),
32 'comments_action' => array(),
33 'auto' => '',
34 'logging' => '',
35 'logging-additional' => '',
36 '404_detector' => 0,
37 );
38
39 /**
40 * Returns url to WP-Optimize admin dashboard.
41 *
42 * @param string $page
43 * @return string
44 */
45 public function admin_page_url($page = 'WP-Optimize') {
46 if (is_multisite()) {
47 return network_admin_url('admin.php?page='.$page);
48 } else {
49 return admin_url('admin.php?page='.$page);
50 }
51 }
52
53 /**
54 * Returns WP-Optimize option value.
55 *
56 * @param string $option Option name.
57 * @param mixed $default
58 * @return mixed|void
59 */
60 public function get_option($option, $default = false) {
61 if (is_multisite()) {
62 $blog_changed = false;
63 // make sure that we are on main blog.
64 if (!is_main_site()) {
65 // get main blog is
66 if (function_exists('get_network')) {
67 $main_blog_id = get_network()->site_id;
68 } else {
69 global $current_site;
70 $main_blog_id = $current_site->blog_id;
71 }
72 $blog_changed = true;
73 switch_to_blog($main_blog_id);
74 }
75 // check option value for old plugin versions.
76 $old_version_option_value = get_option('wp-optimize-'.$option, null);
77 // if blog was changed.
78 if ($blog_changed) restore_current_blog();
79 // check option value for new plugin versions.
80 $new_version_option_value = get_site_option('wp-optimize-mu-'.$option, null);
81 // if it exists old version value and doesn't exist new version option then return value.
82 if (null !== $old_version_option_value && null === $new_version_option_value) return $old_version_option_value;
83
84 return get_site_option('wp-optimize-mu-'.$option, $default);
85 } else {
86 return get_option('wp-optimize-'.$option, $default);
87 }
88 }
89
90 /**
91 * Update WP-Optimize option value.
92 *
93 * @param string $option Option name.
94 * @param mixed $value Option value.
95 * @return bool
96 */
97 public function update_option($option, $value) {
98 if (is_multisite()) {
99 return update_site_option('wp-optimize-mu-'.$option, $value);
100 } else {
101 return update_option('wp-optimize-'.$option, $value);
102 }
103 }
104
105 /**
106 * Delete WP-Optimize.
107 *
108 * @param string $option Option name.
109 */
110 public function delete_option($option) {
111 if (is_multisite()) {
112 delete_site_option('wp-optimize-mu-'.$option);
113 } else {
114 delete_option('wp-optimize-'.$option);
115 }
116 }
117
118 /**
119 * Get option keys
120 *
121 * @return array
122 */
123 public function get_option_keys() {
124 $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');
125 $filtered_option_keys = apply_filters(
126 'wp_optimize_option_keys',
127 $keys
128 );
129
130 return is_array($filtered_option_keys) ? $filtered_option_keys : $keys;
131 }
132
133 /**
134 * This particular option has its own functions abstracted to make it easier to change the format in the future.
135 * To allow callers to always assume the latest format (because get_main_settings() will convert, if needed).
136 *
137 * @param array $settings Array of optimization settings.
138 * @return bool
139 */
140 private function save_manual_run_optimizations_settings($settings) {
141 $settings['last_saved_in'] = WPO_VERSION;
142 return $this->update_option('settings', $settings);
143 }
144
145 public function get_main_settings() {
146 return $this->get_option('settings');
147 }
148
149 /**
150 * This saves the tick box options for enabling auto backup
151 *
152 * @param array $settings Array of information with the state of the tick box selected.
153 * @return array Message Array for being completed.
154 */
155 public function save_auto_backup_option($settings) {
156 if (isset($settings['auto_backup']) && 'true' == $settings['auto_backup']) {
157 $this->update_option('enable-auto-backup', 'true');
158 } else {
159 $this->update_option('enable-auto-backup', 'false');
160 }
161
162 $this->save_additional_auto_backup_options($settings);
163
164 $output = array('messages' => array());
165
166 $output['messages'][] = __('Auto backup option updated.', 'wp-optimize');
167
168 return $output;
169 }
170
171 /**
172 * Save option which sites to optimize in multisite mode
173 *
174 * @param array $settings array of blog ids or "all" item for all sites.
175 * @return bool
176 */
177 public function save_wpo_sites_option($settings) {
178 return $this->update_option('wpo-sites', $settings);
179 }
180
181 /**
182 * Return list of blog ids to optimize in multisite mode
183 *
184 * @return mixed|void
185 */
186 public function get_wpo_sites_option() {
187 return $this->get_option('wpo-sites', array('all'));
188 }
189
190 /**
191 * Save options
192 *
193 * @param array $settings
194 * @return array
195 */
196 public function save_settings($settings) {
197 $output = array('messages' => array(), 'errors' => array());
198 if (!empty($settings["enable-schedule"])) {
199 $this->update_option('schedule', 'true');
200
201 wpo_cron_deactivate();
202
203 if (isset($settings["schedule_type"])) {
204 $schedule_type = (string) $settings['schedule_type'];
205 $this->update_option('schedule-type', $schedule_type);
206 } else {
207 $this->update_option('schedule-type', 'wpo_weekly');
208 }
209
210 WP_Optimize()->cron_activate();
211 } else {
212 $this->update_option('schedule', 'false');
213 $this->update_option('schedule-type', 'wpo_weekly');
214 wpo_cron_deactivate();
215 }
216
217 if (!empty($settings["enable-retention"])) {
218 $retention_period = (int) $settings['retention-period'];
219 $this->update_option('retention-enabled', 'true');
220 $this->update_option('retention-period', $retention_period);
221 } else {
222 $this->update_option('retention-enabled', 'false');
223 }
224
225 if (!empty($settings["enable-revisions-retention"])) {
226 $revisions_retention_count = (int) $settings['revisions-retention-count'];
227 $this->update_option('revisions-retention-enabled', 'true');
228 $this->update_option('revisions-retention-count', $revisions_retention_count);
229 } else {
230 $this->update_option('revisions-retention-enabled', 'false');
231 }
232
233 // Get saved admin menu value before check.
234 $saved_admin_bar = $this->get_option('enable-admin-menu', 'false');
235
236 // Set refresh of default false so it doesn't refresh after save.
237 $output['refresh'] = false;
238
239 if (!empty($settings['enable-admin-bar'])) {
240 $this->update_option('enable-admin-menu', 'true');
241 } else {
242 $this->update_option('enable-admin-menu', 'false');
243 }
244
245 // Make sure inbound input is a string.
246 $updated_admin_bar = (isset($settings['enable-admin-bar']) && $settings['enable-admin-bar']) ? 'true' : 'false';
247
248 // Check if the value is refreshed .
249 if ($saved_admin_bar !== $updated_admin_bar) {
250 // Set refresh to true as the values have changed.
251 $output['refresh'] = true;
252 }
253
254 // Save cache toolbar display setting
255 $saved_enable_cache_in_admin_bar = (bool) $this->get_option('enable_cache_in_admin_bar', true);
256 if ($saved_enable_cache_in_admin_bar !== (bool) $settings['enable_cache_in_admin_bar']) {
257 $this->update_option('enable_cache_in_admin_bar', $settings['enable_cache_in_admin_bar']);
258 $output['refresh'] = true;
259 }
260
261 do_action("auto_option_settings", $settings);
262
263 /** Save multisite options */
264 if (isset($settings['wpo-sites-cron'])) {
265 $this->update_option('wpo-sites-cron', $settings['wpo-sites-cron']);
266 }
267
268 if (isset($settings['wpo-sites']) && is_array($settings['wpo-sites'])) {
269 $this->save_wpo_sites_option($settings['wpo-sites']);
270 }
271
272 /** Save logging options */
273 $logger_type = $settings['wpo-logger-type'] ?? '';
274 $logger_options = $settings['wpo-logger-options'] ?? '';
275
276 $this->update_option('logging', $logger_type);
277 $this->update_option('logging-additional', $logger_options);
278
279 // Save selected optimization settings.
280 if (isset($settings['optimization-options'])) {
281 $this->save_sent_manual_run_optimization_options($settings['optimization-options'], false, false);
282 }
283
284 // Save auto backup option value.
285 $enable_auto_backup = (isset($settings['enable-auto-backup']) ? 'true' : 'false');
286 $this->update_option('enable-auto-backup', $enable_auto_backup);
287
288 // Save additional auto backup option values.
289 $this->save_additional_auto_backup_options($settings);
290
291 // Save force DB optimization value.
292 $enable_db_force_optimize = (isset($settings['innodb-force-optimize']) ? 'true' : 'false');
293 $this->update_option('enable-db-force-optimize', $enable_db_force_optimize);
294
295 $wpo_404_detector = $settings['404_detector'] ?? 0;
296 $this->update_option('404_detector', $wpo_404_detector);
297
298 $output['messages'][] = __('Settings updated.', 'wp-optimize');
299
300 return $output;
301
302 }
303
304 /**
305 * Wipe all options from database options tables.
306 *
307 * @return bool|int
308 */
309 public function wipe_settings() {
310 global $wpdb;
311
312 wp_cache_flush();
313
314 $settings = array(
315 'wpo-ignores-table-deletion-warning',
316 'wpo-ignores-post-meta-deletion-warning',
317 'wpo-ignores-orphaned-relationship-data-deletion-warning',
318 'wpo-hide-minify-information-notice',
319 'wpo_hide_css_merging_notice',
320 'wpo_hide_js_merging_notice',
321 );
322
323 foreach ($settings as $setting) {
324 $user_query = new WP_User_Query(array('meta_key' => $setting, 'meta_value' => '1', 'fields' => 'ID'));
325 $users = $user_query->get_results();
326 if (!empty($users)) {
327 foreach ($users as $user_id) {
328 delete_user_meta($user_id, $setting);
329 }
330 }
331 }
332
333 // disable cache and clean any information related to WP-Optimize Cache.
334 WP_Optimize()->get_page_cache()->clean_up();
335 // delete settings from .htaccess
336 WP_Optimize()->get_browser_cache()->disable();
337 WP_Optimize()->get_gzip_compression()->disable();
338 // disable Webp CRON event if enabled
339 WP_Optimize()->get_webp_instance()->remove_webp_cron_schedules();
340
341 // delete settings from options table.
342 $keys = '"' . implode('", "', $this->get_additional_settings_keys()) . '"';
343
344 // phpcs:disable
345 // WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only hardcoded strings are used
346 if (is_multisite()) {
347 $result = $wpdb->query("DELETE FROM `{$wpdb->sitemeta}` WHERE `meta_key` LIKE 'wp-optimize-mu%' OR `meta_key` IN ($keys)");
348 } else {
349 $result = $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'wp-optimize%' OR `option_name` IN ($keys)");
350 }
351 // phpcs:enable
352
353 return $result;
354 }
355
356 /**
357 * Get list of WP-Optimize settings database keys which are don't use default `wp-optimize-` prefix.
358 *
359 * @return array
360 */
361 public function get_additional_settings_keys() {
362 return array(
363 'wpo_cache_config',
364 'wpo_minify_config',
365 'wpo_update_version',
366 );
367 }
368
369 /**
370 * Saves auto optimization settings
371 *
372 * @param array $settings Auto optimization settings array submitted by user
373 *
374 * @return void
375 */
376 public function auto_option_settings($settings) {
377
378 $optimizer = WP_Optimize()->get_optimizer();
379
380 if (!empty($settings["schedule_type"])) {
381 $options_from_user = $settings['wp-optimize-auto'] ?? array();
382
383 if (!is_array($options_from_user)) $options_from_user = array();
384
385 $new_auto_options = array();
386
387 $optimizations = $optimizer->get_optimizations();
388
389 foreach ($optimizations as $optimization) {
390 if (empty($optimization->available_for_auto)) continue;
391 $auto_id = $optimization->get_auto_id();
392 $new_auto_options[$auto_id] = empty($options_from_user[$auto_id]) ? 'false' : 'true';
393 }
394
395 $this->update_option('auto', $new_auto_options);
396 }
397
398 }
399
400 /**
401 * Save lazy load settings
402 *
403 * @param array $settings
404 * @return bool
405 */
406 public function save_lazy_load_settings($settings) {
407 /** Save Lazy Load settings */
408 if (isset($settings['lazyload'])) {
409 $this->update_option('lazyload', $settings['lazyload']);
410 } else {
411 $this->update_option('lazyload', array());
412 }
413
414 return true;
415 }
416
417 /**
418 * The $use_dom_id parameter is legacy, for when saving options not with AJAX (in which case the dom ID comes via the $_POST array)
419 *
420 * @param array $sent_options Options sent from Ajax.
421 * @param boolean $use_dom_id Parameter is legacy.
422 * @param boolean $available_for_saving_only Save only available for saving optimization state.
423 * @return bool User Options
424 */
425 public function save_sent_manual_run_optimization_options($sent_options, $use_dom_id = false, $available_for_saving_only = true) {
426 $optimizations = WP_Optimize()->get_optimizer()->get_optimizations();
427 $user_options = array();
428 foreach ($optimizations as $optimization_id => $optimization) {
429 // In current code, not all options can be saved.
430 // Revisions, drafts, spams, unapproved, optimize.
431 if (is_wp_error($optimization) || ($available_for_saving_only && empty($optimization->available_for_saving))) continue;
432 $setting_id = $optimization->get_setting_id();
433 $id_in_sent = (($use_dom_id) ? $optimization->get_dom_id() : $optimization_id);
434 // 'true' / 'false' are indeed strings here; this is the historical state. It may be possible to change later using our abstraction interface.
435 $user_options[$setting_id] = isset($sent_options[$id_in_sent]) ? 'true' : 'false';
436 }
437 return $this->save_manual_run_optimizations_settings($user_options);
438 }
439
440 public function delete_all_options() {
441 $option_keys = $this->get_option_keys();
442 foreach ($option_keys as $key) {
443 $this->delete_option($key);
444 }
445 }
446
447 /**
448 * Setup options if not exists already.
449 */
450 public function set_default_options() {
451 if (false === $this->get_option('schedule')) {
452 $this->update_option('schedule', 'false');
453 $this->update_option('last-optimized', 'Never');
454 $this->update_option('schedule-type', 'wpo_weekly');
455 // Deactivate cron.
456 wpo_cron_deactivate();
457 }
458
459 if (false === $this->get_option('retention-enabled')) {
460 $this->update_option('retention-enabled', 'false');
461 $this->update_option('retention-period', '2');
462 }
463
464 if (false === $this->get_option('enable-admin-menu')) {
465 $this->update_option('enable-admin-menu', 'false');
466 }
467
468 if (false === $this->get_option('total-cleaned')) {
469 $this->update_option('total-cleaned', '0');
470 }
471
472 // For UDC monthly reports.
473 if (false === $this->get_option('total-cleaned-current-month')) {
474 $this->update_option('total-cleaned-current-month', '0');
475 }
476
477 // For UDC monthly reports.
478 if (false === $this->get_option('total-cleaned-previous-month')) {
479 $this->update_option('total-cleaned-previous-month', '0');
480 }
481
482 $optimizer = WP_Optimize()->get_optimizer();
483
484 $optimizations = $optimizer->get_optimizations();
485
486 $auto_options = $this->get_option('auto');
487 $new_auto_options = array();
488
489 // Auto options doesn't exists or invalid. Set default.
490 if (empty($auto_options)) {
491 foreach ($optimizations as $optimization) {
492 if (empty($optimization->available_for_auto)) continue;
493
494 $auto_id = $optimization->get_auto_id();
495
496 $new_auto_options[$auto_id] = empty($optimization->auto_default) ? 'false' : 'true';
497 }
498 $this->update_option('auto', apply_filters('wpo_default_auto_options', $new_auto_options));
499 }
500
501
502
503 // Settings for main screen.
504 if (false === $this->get_main_settings()) {
505 $optimizer = WP_Optimize()->get_optimizer();
506
507 $optimizations = $optimizer->get_optimizations();
508
509 $new_settings = array();
510
511 foreach ($optimizations as $optimization) {
512 if (is_wp_error($optimization)) continue;
513 $setting_id = $optimization->get_setting_id();
514
515 $new_settings[$setting_id] = empty($optimization->setting_default) ? 'false' : 'true';
516 }
517
518 $this->save_manual_run_optimizations_settings($new_settings);
519 }
520 }
521
522 /**
523 * Save additional auto backup checkbox values.
524 *
525 * @param array $settings array with options.
526 */
527 private function save_additional_auto_backup_options($settings) {
528 // Save additional auto backup option values.
529 foreach ($settings as $key => $value) {
530 if (preg_match('/enable\-auto\-backup\-/', $key)) {
531 $value = ('true' === $value) ? 'true' : 'false';
532 $this->update_option($key, $value);
533 }
534 }
535 }
536 }
537