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