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
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.7.0, at includes/class-wp-optimize-options.php

538 lines 16.5 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 * @param bool|string|null $autoload Whether to autoload the option. Ignored for multisite network options.
96 * @return bool
97 */
98 public function update_option($option, $value, $autoload = null) {
99 if (is_multisite()) {
100 return update_site_option('wp-optimize-mu-'.$option, $value);
101 } else {
102 return update_option('wp-optimize-'.$option, $value, $autoload);
103 }
104 }
105
106 /**
107 * Delete WP-Optimize.
108 *
109 * @param string $option Option name.
110 */
111 public function delete_option($option) {
112 if (is_multisite()) {
113 delete_site_option('wp-optimize-mu-'.$option);
114 } else {
115 delete_option('wp-optimize-'.$option);
116 }
117 }
118
119 /**
120 * Get option keys
121 *
122 * @return array
123 */
124 public function get_option_keys() {
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(
127 'wp_optimize_option_keys',
128 $keys
129 );
130
131 return is_array($filtered_option_keys) ? $filtered_option_keys : $keys;
132 }
133
134 /**
135 * This particular option has its own functions abstracted to make it easier to change the format in the future.
136 * To allow callers to always assume the latest format (because get_main_settings() will convert, if needed).
137 *
138 * @param array $settings Array of optimization settings.
139 * @return bool
140 */
141 private function save_manual_run_optimizations_settings($settings) {
142 $settings['last_saved_in'] = WPO_VERSION;
143 return $this->update_option('settings', $settings);
144 }
145
146 public function get_main_settings() {
147 return $this->get_option('settings');
148 }
149
150 /**
151 * This saves the tick box options for enabling auto backup
152 *
153 * @param array $settings Array of information with the state of the tick box selected.
154 * @return array Message Array for being completed.
155 */
156 public function save_auto_backup_option($settings) {
157 if (isset($settings['auto_backup']) && 'true' == $settings['auto_backup']) {
158 $this->update_option('enable-auto-backup', 'true');
159 } else {
160 $this->update_option('enable-auto-backup', 'false');
161 }
162
163 $this->save_additional_auto_backup_options($settings);
164
165 $output = array('messages' => array());
166
167 $output['messages'][] = __('Auto backup option updated.', 'wp-optimize');
168
169 return $output;
170 }
171
172 /**
173 * Save option which sites to optimize in multisite mode
174 *
175 * @param array $settings array of blog ids or "all" item for all sites.
176 * @return bool
177 */
178 public function save_wpo_sites_option($settings) {
179 return $this->update_option('wpo-sites', $settings);
180 }
181
182 /**
183 * Return list of blog ids to optimize in multisite mode
184 *
185 * @return mixed|void
186 */
187 public function get_wpo_sites_option() {
188 return $this->get_option('wpo-sites', array('all'));
189 }
190
191 /**
192 * Save options
193 *
194 * @param array $settings
195 * @return array
196 */
197 public function save_settings($settings) {
198 $output = array('messages' => array(), 'errors' => array());
199 if (!empty($settings["enable-schedule"])) {
200 $this->update_option('schedule', 'true');
201
202 wpo_cron_deactivate();
203
204 if (isset($settings["schedule_type"])) {
205 $schedule_type = (string) $settings['schedule_type'];
206 $this->update_option('schedule-type', $schedule_type);
207 } else {
208 $this->update_option('schedule-type', 'wpo_weekly');
209 }
210
211 WP_Optimize()->cron_activate();
212 } else {
213 $this->update_option('schedule', 'false');
214 $this->update_option('schedule-type', 'wpo_weekly');
215 wpo_cron_deactivate();
216 }
217
218 if (!empty($settings["enable-retention"])) {
219 $retention_period = (int) $settings['retention-period'];
220 $this->update_option('retention-enabled', 'true');
221 $this->update_option('retention-period', $retention_period);
222 } else {
223 $this->update_option('retention-enabled', 'false');
224 }
225
226 if (!empty($settings["enable-revisions-retention"])) {
227 $revisions_retention_count = (int) $settings['revisions-retention-count'];
228 $this->update_option('revisions-retention-enabled', 'true');
229 $this->update_option('revisions-retention-count', $revisions_retention_count);
230 } else {
231 $this->update_option('revisions-retention-enabled', 'false');
232 }
233
234 // Get saved admin menu value before check.
235 $saved_admin_bar = $this->get_option('enable-admin-menu', 'false');
236
237 // Set refresh of default false so it doesn't refresh after save.
238 $output['refresh'] = false;
239
240 if (!empty($settings['enable-admin-bar'])) {
241 $this->update_option('enable-admin-menu', 'true');
242 } else {
243 $this->update_option('enable-admin-menu', 'false');
244 }
245
246 // Make sure inbound input is a string.
247 $updated_admin_bar = (isset($settings['enable-admin-bar']) && $settings['enable-admin-bar']) ? 'true' : 'false';
248
249 // Check if the value is refreshed .
250 if ($saved_admin_bar !== $updated_admin_bar) {
251 // Set refresh to true as the values have changed.
252 $output['refresh'] = true;
253 }
254
255 // Save cache toolbar display setting
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']) {
258 $this->update_option('enable_cache_in_admin_bar', $settings['enable_cache_in_admin_bar']);
259 $output['refresh'] = true;
260 }
261
262 do_action("auto_option_settings", $settings);
263
264 /** Save multisite options */
265 if (isset($settings['wpo-sites-cron'])) {
266 $this->update_option('wpo-sites-cron', $settings['wpo-sites-cron']);
267 }
268
269 if (isset($settings['wpo-sites']) && is_array($settings['wpo-sites'])) {
270 $this->save_wpo_sites_option($settings['wpo-sites']);
271 }
272
273 /** Save logging options */
274 $logger_type = $settings['wpo-logger-type'] ?? '';
275 $logger_options = $settings['wpo-logger-options'] ?? '';
276
277 $this->update_option('logging', $logger_type);
278 $this->update_option('logging-additional', $logger_options);
279
280 // Save selected optimization settings.
281 if (isset($settings['optimization-options'])) {
282 $this->save_sent_manual_run_optimization_options($settings['optimization-options'], false, false);
283 }
284
285 // Save auto backup option value.
286 $enable_auto_backup = (isset($settings['enable-auto-backup']) ? 'true' : 'false');
287 $this->update_option('enable-auto-backup', $enable_auto_backup);
288
289 // Save additional auto backup option values.
290 $this->save_additional_auto_backup_options($settings);
291
292 // Save force DB optimization value.
293 $enable_db_force_optimize = (isset($settings['innodb-force-optimize']) ? 'true' : 'false');
294 $this->update_option('enable-db-force-optimize', $enable_db_force_optimize);
295
296 $wpo_404_detector = $settings['404_detector'] ?? 0;
297 $this->update_option('404_detector', $wpo_404_detector);
298
299 $output['messages'][] = __('Settings updated.', 'wp-optimize');
300
301 return $output;
302
303 }
304
305 /**
306 * Wipe all options from database options tables.
307 *
308 * @return bool|int
309 */
310 public function wipe_settings() {
311 global $wpdb;
312
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 );
323
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 }
331 }
332 }
333
334 // disable cache and clean any information related to WP-Optimize Cache.
335 WP_Optimize()->get_page_cache()->clean_up();
336 // delete settings from .htaccess
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();
341
342 // delete settings from options table.
343 $keys = '"' . implode('", "', $this->get_additional_settings_keys()) . '"';
344
345 // phpcs:disable
346 // WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only hardcoded strings are used
347 if (is_multisite()) {
348 $result = $wpdb->query("DELETE FROM `{$wpdb->sitemeta}` WHERE `meta_key` LIKE 'wp-optimize-mu%' OR `meta_key` IN ($keys)");
349 } else {
350 $result = $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'wp-optimize%' OR `option_name` IN ($keys)");
351 }
352 // phpcs:enable
353
354 return $result;
355 }
356
357 /**
358 * Get list of WP-Optimize settings database keys which are don't use default `wp-optimize-` prefix.
359 *
360 * @return array
361 */
362 public function get_additional_settings_keys() {
363 return array(
364 'wpo_cache_config',
365 'wpo_minify_config',
366 'wpo_update_version',
367 );
368 }
369
370 /**
371 * Saves auto optimization settings
372 *
373 * @param array $settings Auto optimization settings array submitted by user
374 *
375 * @return void
376 */
377 public function auto_option_settings($settings) {
378
379 $optimizer = WP_Optimize()->get_optimizer();
380
381 if (!empty($settings["schedule_type"])) {
382 $options_from_user = $settings['wp-optimize-auto'] ?? array();
383
384 if (!is_array($options_from_user)) $options_from_user = array();
385
386 $new_auto_options = array();
387
388 $optimizations = $optimizer->get_optimizations();
389
390 foreach ($optimizations as $optimization) {
391 if (empty($optimization->available_for_auto)) continue;
392 $auto_id = $optimization->get_auto_id();
393 $new_auto_options[$auto_id] = empty($options_from_user[$auto_id]) ? 'false' : 'true';
394 }
395
396 $this->update_option('auto', $new_auto_options);
397 }
398
399 }
400
401 /**
402 * Save lazy load settings
403 *
404 * @param array $settings
405 * @return bool
406 */
407 public function save_lazy_load_settings($settings) {
408 /** Save Lazy Load settings */
409 if (isset($settings['lazyload'])) {
410 $this->update_option('lazyload', $settings['lazyload']);
411 } else {
412 $this->update_option('lazyload', array());
413 }
414
415 return true;
416 }
417
418 /**
419 * 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)
420 *
421 * @param array $sent_options Options sent from Ajax.
422 * @param boolean $use_dom_id Parameter is legacy.
423 * @param boolean $available_for_saving_only Save only available for saving optimization state.
424 * @return bool User Options
425 */
426 public function save_sent_manual_run_optimization_options($sent_options, $use_dom_id = false, $available_for_saving_only = true) {
427 $optimizations = WP_Optimize()->get_optimizer()->get_optimizations();
428 $user_options = array();
429 foreach ($optimizations as $optimization_id => $optimization) {
430 // In current code, not all options can be saved.
431 // Revisions, drafts, spams, unapproved, optimize.
432 if (is_wp_error($optimization) || ($available_for_saving_only && empty($optimization->available_for_saving))) continue;
433 $setting_id = $optimization->get_setting_id();
434 $id_in_sent = (($use_dom_id) ? $optimization->get_dom_id() : $optimization_id);
435 // 'true' / 'false' are indeed strings here; this is the historical state. It may be possible to change later using our abstraction interface.
436 $user_options[$setting_id] = isset($sent_options[$id_in_sent]) ? 'true' : 'false';
437 }
438 return $this->save_manual_run_optimizations_settings($user_options);
439 }
440
441 public function delete_all_options() {
442 $option_keys = $this->get_option_keys();
443 foreach ($option_keys as $key) {
444 $this->delete_option($key);
445 }
446 }
447
448 /**
449 * Setup options if not exists already.
450 */
451 public function set_default_options() {
452 if (false === $this->get_option('schedule')) {
453 $this->update_option('schedule', 'false');
454 $this->update_option('last-optimized', 'Never');
455 $this->update_option('schedule-type', 'wpo_weekly');
456 // Deactivate cron.
457 wpo_cron_deactivate();
458 }
459
460 if (false === $this->get_option('retention-enabled')) {
461 $this->update_option('retention-enabled', 'false');
462 $this->update_option('retention-period', '2');
463 }
464
465 if (false === $this->get_option('enable-admin-menu')) {
466 $this->update_option('enable-admin-menu', 'false');
467 }
468
469 if (false === $this->get_option('total-cleaned')) {
470 $this->update_option('total-cleaned', '0');
471 }
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
483 $optimizer = WP_Optimize()->get_optimizer();
484
485 $optimizations = $optimizer->get_optimizations();
486
487 $auto_options = $this->get_option('auto');
488 $new_auto_options = array();
489
490 // Auto options doesn't exists or invalid. Set default.
491 if (empty($auto_options)) {
492 foreach ($optimizations as $optimization) {
493 if (empty($optimization->available_for_auto)) continue;
494
495 $auto_id = $optimization->get_auto_id();
496
497 $new_auto_options[$auto_id] = empty($optimization->auto_default) ? 'false' : 'true';
498 }
499 $this->update_option('auto', apply_filters('wpo_default_auto_options', $new_auto_options));
500 }
501
502
503
504 // Settings for main screen.
505 if (false === $this->get_main_settings()) {
506 $optimizer = WP_Optimize()->get_optimizer();
507
508 $optimizations = $optimizer->get_optimizations();
509
510 $new_settings = array();
511
512 foreach ($optimizations as $optimization) {
513 if (is_wp_error($optimization)) continue;
514 $setting_id = $optimization->get_setting_id();
515
516 $new_settings[$setting_id] = empty($optimization->setting_default) ? 'false' : 'true';
517 }
518
519 $this->save_manual_run_optimizations_settings($new_settings);
520 }
521 }
522
523 /**
524 * Save additional auto backup checkbox values.
525 *
526 * @param array $settings array with options.
527 */
528 private function save_additional_auto_backup_options($settings) {
529 // Save additional auto backup option values.
530 foreach ($settings as $key => $value) {
531 if (preg_match('/enable\-auto\-backup\-/', $key)) {
532 $value = ('true' === $value) ? 'true' : 'false';
533 $this->update_option($key, $value);
534 }
535 }
536 }
537 }
538