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

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