| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
class WP_Optimize_UpdraftCentral_Commands { |
| 5 |
|
| 6 |
private $wp_optimize; |
| 7 |
|
| 8 |
private $wp_optimize_commands; |
| 9 |
|
| 10 |
private $optimizer; |
| 11 |
|
| 12 |
protected $options; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class constructor |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
$this->wp_optimize = WP_Optimize(); |
| 19 |
$this->wp_optimize_commands = new WP_Optimize_Commands(); |
| 20 |
$this->optimizer = $this->wp_optimize->get_optimizer(); |
| 21 |
$this->options = $this->wp_optimize->get_options(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get multiple widgets data. |
| 26 |
* |
| 27 |
* @param array $args Args. |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public function get_widgets_data($args) { |
| 32 |
if (!current_user_can($this->wp_optimize->capability_required())) { |
| 33 |
// N.B. request_error already returns a WP_Error, so an is_wp_error() check is unnecessary. |
| 34 |
$error = $this->wp_optimize_commands->request_error('insufficient_privilege'); |
| 35 |
|
| 36 |
return array( |
| 37 |
'error' => true, |
| 38 |
'data' => array( |
| 39 |
'code' => $error->get_error_code(), |
| 40 |
'message' => $error->get_error_message(), |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
// Widgets. |
| 46 |
$widgets = isset($args['widgets']) ? $args['widgets'] : array(); |
| 47 |
|
| 48 |
// Return early if no widgets supplied. |
| 49 |
if (!is_array($widgets) || empty($widgets)) { |
| 50 |
return array('error' => false, 'data' => array()); |
| 51 |
} |
| 52 |
|
| 53 |
// Get the data for each widget. |
| 54 |
$data = array(); |
| 55 |
|
| 56 |
// Loop through the widgets and get their data. |
| 57 |
foreach ($widgets as $widget) { |
| 58 |
$method = 'get_' . $widget . '_data'; |
| 59 |
|
| 60 |
if (method_exists($this, $method)) { |
| 61 |
$data[$widget] = $this->$method($args); |
| 62 |
} else { |
| 63 |
if ($result = $this->get_delegated_widgets_data($widget)) { |
| 64 |
$data[$widget] = $result; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return array('error' => false, 'data' => $data); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Retrieves the widget data if it exists under WP_Optimize_Commands |
| 74 |
* |
| 75 |
* @param string $widget Widget name |
| 76 |
* |
| 77 |
* @return array|boolean |
| 78 |
*/ |
| 79 |
private function get_delegated_widgets_data($widget) { |
| 80 |
if (is_string($widget) && '' !== trim($widget) && is_callable(array($this->wp_optimize_commands, 'get_widgets_data'))) { |
| 81 |
|
| 82 |
$widget = trim($widget); |
| 83 |
$result = $this->wp_optimize_commands->get_widgets_data(array('widgets' => array($widget))); |
| 84 |
|
| 85 |
if (isset($result['data']) && isset($result['data'][$widget])) { |
| 86 |
return $result['data'][$widget]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get system status data for system overview for UDC. |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
private function get_system_status_data() { |
| 99 |
|
| 100 |
$cache = $this->wp_optimize->get_page_cache(); |
| 101 |
$admin = $this->wp_optimize->get_admin_instance(); |
| 102 |
$gzip_compression = $this->wp_optimize->get_gzip_compression(); |
| 103 |
|
| 104 |
$schedule_cleanup = array(); |
| 105 |
if ($result = $this->get_delegated_widgets_data('wp_optimize_schedule_cleanup_settings')) { |
| 106 |
$schedule_cleanup = $result; |
| 107 |
} |
| 108 |
|
| 109 |
$gzip_compression_enabled = $gzip_compression->is_gzip_compression_enabled(false); |
| 110 |
$gzip_compression_enabled = (!is_wp_error($gzip_compression_enabled) && $gzip_compression_enabled); |
| 111 |
$wpo_gzip_headers_information = $gzip_compression->get_headers_information($gzip_compression->css_resource_url()); |
| 112 |
$wpo_gzip_headers_information = (!is_wp_error($wpo_gzip_headers_information)) ? $wpo_gzip_headers_information : array(); |
| 113 |
$is_cloudflare_site = $admin->is_cloudflare_site(false); |
| 114 |
$is_gzip_compression_section_exists = $gzip_compression->is_gzip_compression_section_exists(); |
| 115 |
$gzip_compression_enabled_by_wpo = $is_gzip_compression_section_exists && $gzip_compression_enabled && !$is_cloudflare_site && !(is_array($wpo_gzip_headers_information) && isset($wpo_gzip_headers_information['compression']) && 'brotli' === $wpo_gzip_headers_information['compression']); |
| 116 |
$is_premium = $schedule_cleanup['is_premium'] ?? false; |
| 117 |
$is_multisite_mode = $this->wp_optimize->is_multisite_mode(); |
| 118 |
|
| 119 |
list($db_size, $total_gain) = $this->optimizer->get_current_db_size(); |
| 120 |
|
| 121 |
$data = array( |
| 122 |
'is_premium' => $is_premium, |
| 123 |
'is_multisite' => $is_multisite_mode, |
| 124 |
'database' => array( |
| 125 |
'overload_size' => $total_gain, |
| 126 |
'db_size' => $db_size, |
| 127 |
'scheduled_cleanup' => $schedule_cleanup['data'] ?? array(), |
| 128 |
'auto_backup' => 'true' === $this->options->get_option('enable-auto-backup-scheduled', 'false'), |
| 129 |
), |
| 130 |
'images' => array( |
| 131 |
'image_optimisation' => (bool) $this->options->get_option('autosmush', 0), |
| 132 |
'webp_conversion' => (bool) $this->options->get_option('webp_conversion', 0), |
| 133 |
'lazyload' => $this->options->get_option('lazyload', array( |
| 134 |
'images' => false, |
| 135 |
'backgrounds' => false, |
| 136 |
'iframes' => false, |
| 137 |
'youtube_preview' => false, |
| 138 |
'skip_classes' => '', |
| 139 |
)), |
| 140 |
'image_dimensions' => (bool) $this->options->get_option('image_dimensions', 0), |
| 141 |
'pending_images' => Updraft_Smush_Manager()->get_uncompressed_images('false'), |
| 142 |
'unused_images' => array(), |
| 143 |
'last_scan' => (int) $this->options->get_option('unused_images_last_scan_detect_unused_images', 0), |
| 144 |
), |
| 145 |
'cache' => array( |
| 146 |
'page_cache' => $cache->is_enabled(), |
| 147 |
'gzip_compression' => array( |
| 148 |
'enabled' => $gzip_compression_enabled, |
| 149 |
'headers' => $wpo_gzip_headers_information, |
| 150 |
'section_exists' => $is_gzip_compression_section_exists, |
| 151 |
'enabled_by_wpo' => $gzip_compression_enabled_by_wpo, |
| 152 |
'is_apache' => $this->wp_optimize->is_apache_server(), |
| 153 |
'is_cloudflare' => $is_cloudflare_site, |
| 154 |
), |
| 155 |
), |
| 156 |
'minify' => wp_optimize_minify_config()->get(), |
| 157 |
); |
| 158 |
|
| 159 |
if ($is_premium) { |
| 160 |
$optimization_details = $this->wp_optimize_commands->get_optimization_info(array( |
| 161 |
'optimization_id' => 'images', |
| 162 |
'data' => array( |
| 163 |
'mode' => 'detect_unused_images', |
| 164 |
'forced' => true, |
| 165 |
'include_ui_elements' => false, |
| 166 |
) |
| 167 |
)); |
| 168 |
$optimization_details = $optimization_details['result'] ?? null; |
| 169 |
|
| 170 |
if (is_object($optimization_details) && property_exists($optimization_details, 'meta')) { |
| 171 |
$data['images']['unused_images'] = $optimization_details->meta; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $data; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Toggle a feature on or off. |
| 180 |
* |
| 181 |
* @param array $data Array of data. |
| 182 |
* @return array Output and messages. |
| 183 |
*/ |
| 184 |
public function toggle_feature($data) { |
| 185 |
if (!current_user_can($this->wp_optimize->capability_required())) { |
| 186 |
return $this->wp_optimize_commands->request_error('insufficient_privilege'); |
| 187 |
} |
| 188 |
|
| 189 |
if (!is_array($data)) { |
| 190 |
return $this->wp_optimize_commands->request_error( |
| 191 |
'toggle_feature_error', |
| 192 |
null, |
| 193 |
array( |
| 194 |
'message' => __('Wrong data type received', 'wp-optimize'), |
| 195 |
'data' => $data, |
| 196 |
) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
if (!isset($data['name'], $data['value'])) { |
| 201 |
return $this->wp_optimize_commands->request_error( |
| 202 |
'toggle_feature_error', |
| 203 |
null, |
| 204 |
array( |
| 205 |
'message' => __('Missing data in request', 'wp-optimize'), |
| 206 |
'data' => $data, |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
$name = sanitize_key($data['name']); |
| 212 |
$value = filter_var($data['value'], FILTER_VALIDATE_BOOLEAN); |
| 213 |
|
| 214 |
try { |
| 215 |
switch ($name) { |
| 216 |
|
| 217 |
case 'page_cache': |
| 218 |
$cache = $this->wp_optimize->get_page_cache(); |
| 219 |
|
| 220 |
if ($value) { |
| 221 |
$output = $cache->enable(true); |
| 222 |
} else { |
| 223 |
$output = $cache->disable(); |
| 224 |
} |
| 225 |
|
| 226 |
if (is_wp_error($output)) { |
| 227 |
throw new Exception($output->get_error_message()); |
| 228 |
} |
| 229 |
break; |
| 230 |
|
| 231 |
case 'minify_css': |
| 232 |
case 'minify_js': |
| 233 |
$minify_config = wp_optimize_minify_config(); |
| 234 |
$current_settings = $minify_config->get(); |
| 235 |
|
| 236 |
$new_settings = array( |
| 237 |
'enabled' => true, |
| 238 |
); |
| 239 |
|
| 240 |
if ('minify_css' === $name) { |
| 241 |
$new_settings['enable_css'] = $value; |
| 242 |
$new_settings['enable_css_minification'] = $value; |
| 243 |
} |
| 244 |
|
| 245 |
if ('minify_js' === $name) { |
| 246 |
$new_settings['enable_js'] = $value; |
| 247 |
$new_settings['enable_js_minification'] = $value; |
| 248 |
} |
| 249 |
|
| 250 |
$css_enabled = $new_settings['enable_css'] ?? ($current_settings['enable_css'] ?? false); |
| 251 |
$js_enabled = $new_settings['enable_js'] ?? ($current_settings['enable_js'] ?? false); |
| 252 |
$html_enabled = $current_settings['html_minification'] ?? false; |
| 253 |
|
| 254 |
if (!$css_enabled && !$js_enabled && !$html_enabled) { |
| 255 |
$new_settings['enabled'] = false; |
| 256 |
} |
| 257 |
|
| 258 |
$minify_config->update($new_settings); |
| 259 |
break; |
| 260 |
|
| 261 |
case 'image_optimisation': |
| 262 |
Updraft_Smush_Manager()->update_smush_options( |
| 263 |
array( |
| 264 |
'autosmush' => $value, |
| 265 |
) |
| 266 |
); |
| 267 |
break; |
| 268 |
|
| 269 |
case 'webp_conversion': |
| 270 |
$this->wp_optimize->get_webp_instance()->save_webp_settings( |
| 271 |
array( |
| 272 |
'webp_conversion' => $value, |
| 273 |
) |
| 274 |
); |
| 275 |
break; |
| 276 |
|
| 277 |
case 'gzip_compression': |
| 278 |
$response = $this->wp_optimize_commands->enable_gzip_compression( |
| 279 |
array( |
| 280 |
'enable' => $value, |
| 281 |
) |
| 282 |
); |
| 283 |
|
| 284 |
if (is_array($response) && false === $response['success']) { |
| 285 |
throw new Exception($response['message']); |
| 286 |
} |
| 287 |
break; |
| 288 |
|
| 289 |
case 'lazy_load': |
| 290 |
$this->options->save_lazy_load_settings( |
| 291 |
array( |
| 292 |
'lazyload' => array( |
| 293 |
'images' => $value, |
| 294 |
'iframes' => $value, |
| 295 |
'backgrounds' => $value, |
| 296 |
'youtube_preview' => $value, |
| 297 |
'skip_classes' => '', |
| 298 |
), |
| 299 |
) |
| 300 |
); |
| 301 |
break; |
| 302 |
|
| 303 |
case 'image_dimensions': |
| 304 |
$this->options->update_option('image_dimensions', (int) $value); |
| 305 |
break; |
| 306 |
|
| 307 |
case 'auto_backup_scheduled': |
| 308 |
$this->options->update_option( |
| 309 |
'enable-auto-backup-scheduled', |
| 310 |
$value ? 'true' : 'false' |
| 311 |
); |
| 312 |
break; |
| 313 |
|
| 314 |
default: |
| 315 |
return $this->wp_optimize_commands->request_error( |
| 316 |
'toggle_feature_error', |
| 317 |
null, |
| 318 |
array( |
| 319 |
'message' => __('Invalid feature name received', 'wp-optimize'), |
| 320 |
'feature' => $name, |
| 321 |
) |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
return array( |
| 326 |
'settings' => $this->get_system_status_data(), |
| 327 |
); |
| 328 |
|
| 329 |
} catch (Throwable $e) { |
| 330 |
return $this->wp_optimize_commands->request_error( |
| 331 |
'toggle_feature_error', |
| 332 |
null, |
| 333 |
array( |
| 334 |
'message' => $e->getMessage(), |
| 335 |
'feature' => $name, |
| 336 |
'settings' => $this->get_system_status_data(), |
| 337 |
) |
| 338 |
); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|