| 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 |
|
| 316 |
// delete settings from options table. |
| 317 |
$keys = '"' . implode('", "', $this->get_additional_settings_keys()) . '"'; |
| 318 |
|
| 319 |
if (is_multisite()) { |
| 320 |
$result = $wpdb->query("DELETE FROM {$wpdb->sitemeta} WHERE `meta_key` LIKE 'wp-optimize-mu-%' OR `meta_key` IN ({$keys})"); |
| 321 |
} else { |
| 322 |
$result = $wpdb->query("DELETE FROM {$wpdb->options} WHERE `option_name` LIKE 'wp-optimize-%' OR `option_name` IN ({$keys})"); |
| 323 |
} |
| 324 |
|
| 325 |
return $result; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Get list of WP-Optimize settings database keys which are don't use default `wp-optimize-` prefix. |
| 330 |
* |
| 331 |
* @return array |
| 332 |
*/ |
| 333 |
public function get_additional_settings_keys() { |
| 334 |
return array( |
| 335 |
'wpo_cache_config', |
| 336 |
'wpo_minify_config', |
| 337 |
'wpo_update_version', |
| 338 |
); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Saves auto optimization settings |
| 343 |
* |
| 344 |
* @param array $settings Auto optimization settings array submitted by user |
| 345 |
* |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
public function auto_option_settings($settings) { |
| 349 |
|
| 350 |
$optimizer = WP_Optimize()->get_optimizer(); |
| 351 |
|
| 352 |
if (!empty($settings["schedule_type"])) { |
| 353 |
$options_from_user = isset($settings['wp-optimize-auto']) ? $settings['wp-optimize-auto'] : array(); |
| 354 |
|
| 355 |
if (!is_array($options_from_user)) $options_from_user = array(); |
| 356 |
|
| 357 |
$new_auto_options = array(); |
| 358 |
|
| 359 |
$optimizations = $optimizer->get_optimizations(); |
| 360 |
|
| 361 |
foreach ($optimizations as $optimization) { |
| 362 |
if (empty($optimization->available_for_auto)) continue; |
| 363 |
$auto_id = $optimization->get_auto_id(); |
| 364 |
$new_auto_options[$auto_id] = empty($options_from_user[$auto_id]) ? 'false' : 'true'; |
| 365 |
} |
| 366 |
|
| 367 |
$this->update_option('auto', $new_auto_options); |
| 368 |
} |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Save lazy load settings |
| 374 |
* |
| 375 |
* @param array $settings |
| 376 |
* @return bool |
| 377 |
*/ |
| 378 |
public function save_lazy_load_settings($settings) { |
| 379 |
/** Save Lazy Load settings */ |
| 380 |
if (isset($settings['lazyload'])) { |
| 381 |
$this->update_option('lazyload', $settings['lazyload']); |
| 382 |
} else { |
| 383 |
$this->update_option('lazyload', array()); |
| 384 |
} |
| 385 |
|
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* 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) |
| 391 |
* |
| 392 |
* @param array $sent_options Options sent from Ajax. |
| 393 |
* @param boolean $use_dom_id Parameter is legacy. |
| 394 |
* @param boolean $available_for_saving_only Save only available for saving optimization state. |
| 395 |
* @return array User Options |
| 396 |
*/ |
| 397 |
public function save_sent_manual_run_optimization_options($sent_options, $use_dom_id = false, $available_for_saving_only = true) { |
| 398 |
$optimizations = WP_Optimize()->get_optimizer()->get_optimizations(); |
| 399 |
$user_options = array(); |
| 400 |
foreach ($optimizations as $optimization_id => $optimization) { |
| 401 |
// In current code, not all options can be saved. |
| 402 |
// Revisions, drafts, spams, unapproved, optimize. |
| 403 |
if (is_wp_error($optimization) || ($available_for_saving_only && empty($optimization->available_for_saving))) continue; |
| 404 |
$setting_id = $optimization->get_setting_id(); |
| 405 |
$id_in_sent = (($use_dom_id) ? $optimization->get_dom_id() : $optimization_id); |
| 406 |
// 'true' / 'false' are indeed strings here; this is the historical state. It may be possible to change later using our abstraction interface. |
| 407 |
$user_options[$setting_id] = isset($sent_options[$id_in_sent]) ? 'true' : 'false'; |
| 408 |
} |
| 409 |
return $this->save_manual_run_optimizations_settings($user_options); |
| 410 |
} |
| 411 |
|
| 412 |
public function delete_all_options() { |
| 413 |
$option_keys = $this->get_option_keys(); |
| 414 |
foreach ($option_keys as $key) { |
| 415 |
$this->delete_option($key); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Setup options if not exists already. |
| 421 |
*/ |
| 422 |
public function set_default_options() { |
| 423 |
$deprecated = null; |
| 424 |
$autoload_no = 'no'; |
| 425 |
|
| 426 |
if (false === $this->get_option('schedule')) { |
| 427 |
// The option hasn't been added yet. We'll add it with $autoload_no set to 'no'. |
| 428 |
$this->update_option('schedule', 'false', $deprecated, $autoload_no); |
| 429 |
$this->update_option('last-optimized', 'Never', $deprecated, $autoload_no); |
| 430 |
$this->update_option('schedule-type', 'wpo_weekly', $deprecated, $autoload_no); |
| 431 |
// Deactivate cron. |
| 432 |
wpo_cron_deactivate(); |
| 433 |
} |
| 434 |
|
| 435 |
if (false === $this->get_option('retention-enabled')) { |
| 436 |
$this->update_option('retention-enabled', 'false', $deprecated, $autoload_no); |
| 437 |
$this->update_option('retention-period', '2', $deprecated, $autoload_no); |
| 438 |
} |
| 439 |
|
| 440 |
if (false === $this->get_option('enable-admin-menu')) { |
| 441 |
$this->update_option('enable-admin-menu', 'false', $deprecated, $autoload_no); |
| 442 |
} |
| 443 |
|
| 444 |
if (false === $this->get_option('total-cleaned')) { |
| 445 |
$this->update_option('total-cleaned', '0', $deprecated, $autoload_no); |
| 446 |
} |
| 447 |
|
| 448 |
$optimizer = WP_Optimize()->get_optimizer(); |
| 449 |
|
| 450 |
$optimizations = $optimizer->get_optimizations(); |
| 451 |
|
| 452 |
$auto_options = $this->get_option('auto'); |
| 453 |
$new_auto_options = array(); |
| 454 |
|
| 455 |
// Auto options doesn't exists or invalid. Set default. |
| 456 |
if (empty($auto_options)) { |
| 457 |
foreach ($optimizations as $optimization) { |
| 458 |
if (empty($optimization->available_for_auto)) continue; |
| 459 |
|
| 460 |
$auto_id = $optimization->get_auto_id(); |
| 461 |
|
| 462 |
$new_auto_options[$auto_id] = empty($optimization->auto_default) ? 'false' : 'true'; |
| 463 |
} |
| 464 |
$this->update_option('auto', apply_filters('wpo_default_auto_options', $new_auto_options)); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
// Settings for main screen. |
| 470 |
if (false === $this->get_main_settings()) { |
| 471 |
$optimizer = WP_Optimize()->get_optimizer(); |
| 472 |
|
| 473 |
$optimizations = $optimizer->get_optimizations(); |
| 474 |
|
| 475 |
$new_settings = array(); |
| 476 |
|
| 477 |
foreach ($optimizations as $optimization) { |
| 478 |
if (is_wp_error($optimization)) continue; |
| 479 |
$setting_id = $optimization->get_setting_id(); |
| 480 |
|
| 481 |
$new_settings[$setting_id] = empty($optimization->setting_default) ? 'false' : 'true'; |
| 482 |
} |
| 483 |
|
| 484 |
$this->save_manual_run_optimizations_settings($new_settings); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Save additional auto backup checkbox values. |
| 490 |
* |
| 491 |
* @param array $settings array with options. |
| 492 |
*/ |
| 493 |
private function save_additional_auto_backup_options($settings) { |
| 494 |
// Save additional auto backup option values. |
| 495 |
foreach ($settings as $key => $value) { |
| 496 |
if (preg_match('/enable\-auto\-backup\-/', $key)) { |
| 497 |
$value = ('true' == $value) ? 'true' : 'false'; |
| 498 |
$this->update_option($key, $value); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|