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

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