assets
2 days ago
views
2 days ago
class-admin.php
2 days ago
class-ajax.php
2 days ago
class-interface-page.php
2 days ago
class-media-library.php
2 days ago
class-settings-row.php
2 days ago
class-ajax.php
674 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Smush class for storing all Ajax related functionality: Ajax class |
| 4 | * |
| 5 | * @package Smush\App |
| 6 | * @since 2.9.0 |
| 7 | * |
| 8 | * @copyright (c) 2018, Incsub (http://incsub.com) |
| 9 | */ |
| 10 | |
| 11 | namespace Smush\App; |
| 12 | |
| 13 | use Smush\Core\Bulk\Bulk_Optimize; |
| 14 | use Smush\Core\Configs; |
| 15 | use Smush\Core\Core; |
| 16 | use Smush\Core\Error_Handler; |
| 17 | use Smush\Core\Helper; |
| 18 | use Smush\Core\Media\Media_Item_Cache; |
| 19 | use Smush\Core\Membership\Membership; |
| 20 | use Smush\Core\Modules\CDN; |
| 21 | use Smush\Core\Modules\Smush; |
| 22 | use Smush\Core\Settings; |
| 23 | use WP_Smush; |
| 24 | |
| 25 | if ( ! defined( 'WPINC' ) ) { |
| 26 | die; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Class Ajax for storing all Ajax related functionality. |
| 31 | * |
| 32 | * @since 2.9.0 |
| 33 | */ |
| 34 | class Ajax { |
| 35 | |
| 36 | /** |
| 37 | * Settings instance. |
| 38 | * |
| 39 | * @since 3.3.0 |
| 40 | * @var Settings |
| 41 | */ |
| 42 | private $settings; |
| 43 | private $membership; |
| 44 | /** |
| 45 | * @var Bulk_Optimize |
| 46 | */ |
| 47 | private $bulk_optimize; |
| 48 | |
| 49 | /** |
| 50 | * Ajax constructor. |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | $this->settings = Settings::get_instance(); |
| 54 | $this->membership = Membership::get_instance(); |
| 55 | $this->bulk_optimize = new Bulk_Optimize(); |
| 56 | |
| 57 | /** |
| 58 | * QUICK SETUP |
| 59 | */ |
| 60 | // Handle skip quick setup action. |
| 61 | add_action( 'wp_ajax_skip_smush_setup', array( $this, 'skip_smush_setup' ) ); |
| 62 | // Handle resume quick setup action. |
| 63 | add_action( 'wp_ajax_resume_smush_setup', array( $this, 'resume_smush_setup' ) ); |
| 64 | // Ajax request for quick setup. |
| 65 | add_action( 'wp_ajax_smush_setup', array( $this, 'smush_setup' ) ); |
| 66 | add_action( 'wp_ajax_smush_free_setup', array( $this, 'smush_free_setup' ) ); |
| 67 | |
| 68 | /** |
| 69 | * NOTICES |
| 70 | */ |
| 71 | // Handle the smush pro dismiss features notice ajax. |
| 72 | add_action( 'wp_ajax_dismiss_upgrade_notice', array( $this, 'dismiss_upgrade_notice' ) ); |
| 73 | // Handle the smush pro dismiss features notice ajax. |
| 74 | add_action( 'wp_ajax_dismiss_update_info', array( $this, 'dismiss_update_info' ) ); |
| 75 | // Handle ajax request to dismiss the s3 warning. |
| 76 | add_action( 'wp_ajax_dismiss_s3support_alert', array( $this, 'dismiss_s3support_alert' ) ); |
| 77 | // Hide API message. |
| 78 | add_action( 'wp_ajax_hide_api_message', array( $this, 'hide_api_message' ) ); |
| 79 | add_action( 'wp_ajax_smush_show_warning', array( $this, 'show_warning_ajax' ) ); |
| 80 | // Detect conflicting plugins. |
| 81 | add_action( 'wp_ajax_smush_dismiss_notice', array( $this, 'dismiss_notice' ) ); |
| 82 | |
| 83 | /** |
| 84 | * SMUSH |
| 85 | */ |
| 86 | // Scan images as per the latest settings. |
| 87 | add_action( 'wp_ajax_scan_for_resmush', array( $this, 'scan_images' ) ); |
| 88 | // Send smush stats. |
| 89 | add_action( 'wp_ajax_get_stats', array( $this, 'get_stats' ) ); |
| 90 | |
| 91 | /** |
| 92 | * BULK SMUSH |
| 93 | */ |
| 94 | |
| 95 | /** |
| 96 | * DIRECTORY SMUSH |
| 97 | */ |
| 98 | // Handle Ajax request for directory smush stats (stats meta box). |
| 99 | add_action( 'wp_ajax_get_dir_smush_stats', array( $this, 'get_dir_smush_stats' ) ); |
| 100 | |
| 101 | /** |
| 102 | * LAZY LOADING |
| 103 | */ |
| 104 | add_action( 'wp_ajax_smush_toggle_lazy_load', array( $this, 'smush_toggle_lazy_load' ) ); |
| 105 | add_action( 'wp_ajax_smush_remove_icon', array( $this, 'remove_icon' ) ); |
| 106 | |
| 107 | /** |
| 108 | * Configs |
| 109 | */ |
| 110 | add_action( 'wp_ajax_smush_upload_config', array( $this, 'upload_config' ) ); |
| 111 | add_action( 'wp_ajax_smush_save_config', array( $this, 'save_config' ) ); |
| 112 | add_action( 'wp_ajax_smush_apply_config', array( $this, 'apply_config' ) ); |
| 113 | |
| 114 | /** |
| 115 | * Review Prompts Notice. |
| 116 | */ |
| 117 | add_action( 'wp_ajax_wp_smush_review_prompts_remind_later', array( $this, 'remind_later_review_prompts' ) ); |
| 118 | } |
| 119 | |
| 120 | /*************************************** |
| 121 | * |
| 122 | * QUICK SETUP |
| 123 | */ |
| 124 | |
| 125 | /** |
| 126 | * Process ajax action for skipping Smush setup. |
| 127 | */ |
| 128 | public function skip_smush_setup() { |
| 129 | check_ajax_referer( 'smush_quick_setup' ); |
| 130 | // Check capability. |
| 131 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 132 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 133 | } |
| 134 | update_option( 'skip-smush-setup', true ); |
| 135 | wp_send_json_success(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Process ajax action for resuming Smush setup. |
| 140 | */ |
| 141 | public function resume_smush_setup() { |
| 142 | check_ajax_referer( 'wp-smush-ajax' ); |
| 143 | // Check capability. |
| 144 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 145 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 146 | } |
| 147 | |
| 148 | delete_option( 'skip-smush-setup' ); |
| 149 | wp_send_json_success(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Ajax action to save settings from quick setup. |
| 154 | */ |
| 155 | public function smush_setup() { |
| 156 | check_ajax_referer( 'smush_quick_setup', '_wpnonce' ); |
| 157 | |
| 158 | // Check capability. |
| 159 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 160 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 161 | } |
| 162 | |
| 163 | $quick_settings = array(); |
| 164 | // Get the settings from $_POST. |
| 165 | if ( ! empty( $_POST['smush_settings'] ) ) { |
| 166 | // Required $quick_settings data is escaped later on in code. |
| 167 | $quick_settings = json_decode( wp_unslash( $_POST['smush_settings'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 168 | } |
| 169 | |
| 170 | // Check the last settings stored in db. |
| 171 | $settings = $this->settings->get(); |
| 172 | |
| 173 | // Available settings for free/pro version. |
| 174 | $available = array( 'auto', 'lossy', 'strip_exif', 'original', 'preload_images', 'lazy_load', 'usage' ); |
| 175 | $highest_lossy_level = $this->settings->get_highest_lossy_level(); |
| 176 | |
| 177 | foreach ( $settings as $name => $values ) { |
| 178 | // Update only specified settings. |
| 179 | if ( ! in_array( $name, $available, true ) ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // Skip premium features if not a member. |
| 184 | if ( ! in_array( $name, Settings::$basic_features, true ) && 'usage' !== $name && ! $this->membership->is_pro() ) { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | // Update value in settings. |
| 189 | if ( 'lossy' === $name ) { |
| 190 | $settings['lossy'] = ! empty( $quick_settings->{$name} ) ? $highest_lossy_level : Settings::get_level_lossless(); |
| 191 | } elseif ( 'original' === $name ) { |
| 192 | $optimize_originals = ! empty( $quick_settings->{$name} ); |
| 193 | $settings[ $name ] = $optimize_originals; |
| 194 | $settings['backup'] = $optimize_originals; |
| 195 | } else { |
| 196 | $settings[ $name ] = (bool) $quick_settings->{$name}; |
| 197 | } |
| 198 | |
| 199 | // If lazy load enabled - init defaults. |
| 200 | if ( 'lazy_load' === $name && $quick_settings->{$name} ) { |
| 201 | $this->settings->init_lazy_load_defaults(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Update the resize sizes. |
| 206 | $this->settings->set_setting( 'wp-smush-settings', $settings ); |
| 207 | |
| 208 | update_option( 'skip-smush-setup', true ); |
| 209 | |
| 210 | wp_send_json_success(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Ajax action to save settings from quick setup. |
| 215 | */ |
| 216 | public function smush_free_setup() { |
| 217 | check_ajax_referer( 'smush_quick_setup', '_wpnonce' ); |
| 218 | |
| 219 | // Check capability. |
| 220 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 221 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 222 | } |
| 223 | |
| 224 | $quick_settings = array(); |
| 225 | // Get the settings from $_POST. |
| 226 | if ( ! empty( $_POST['smush_settings'] ) ) { |
| 227 | // Required $quick_settings data is escaped later on in code. |
| 228 | $quick_settings = json_decode( wp_unslash( $_POST['smush_settings'] ), true ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 229 | } |
| 230 | |
| 231 | // Check the last settings stored in db. |
| 232 | $settings = $this->settings->get_site_settings(); |
| 233 | |
| 234 | // Available settings for free/pro version. |
| 235 | $available = array( 'auto', 'lossy', 'strip_exif', 'compress_backup', 'lazy_load', 'usage' ); |
| 236 | |
| 237 | foreach ( $quick_settings as $name => $values ) { |
| 238 | // Update only specified settings. |
| 239 | if ( ! in_array( $name, $available, true ) ) { |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | $setting_enabled = ! empty( $quick_settings[ $name ] ); |
| 244 | |
| 245 | // Update value in settings. |
| 246 | if ( 'lossy' === $name ) { |
| 247 | $settings['lossy'] = $setting_enabled ? Settings::get_level_super_lossy() : Settings::get_level_lossless(); |
| 248 | } elseif ( 'compress_backup' === $name ) { |
| 249 | // If Smush originals is selected, enable backups. |
| 250 | $settings['original'] = $setting_enabled; |
| 251 | $settings['backup'] = $setting_enabled; |
| 252 | } else { |
| 253 | $settings[ $name ] = $setting_enabled; |
| 254 | } |
| 255 | |
| 256 | // If lazy load enabled - init defaults. |
| 257 | if ( 'lazy_load' === $name && $setting_enabled ) { |
| 258 | $this->settings->init_lazy_load_defaults(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Update the resize sizes. |
| 263 | $this->settings->set_setting( 'wp-smush-settings', $settings ); |
| 264 | |
| 265 | update_option( 'skip-smush-setup', true ); |
| 266 | |
| 267 | wp_send_json_success(); |
| 268 | } |
| 269 | |
| 270 | /*************************************** |
| 271 | * |
| 272 | * NOTICES |
| 273 | */ |
| 274 | |
| 275 | /** |
| 276 | * Store a key/value to hide the smush features on bulk page |
| 277 | * |
| 278 | * There is no js code related to this action, it seems we are no longer use it, better to clean it? |
| 279 | */ |
| 280 | public function dismiss_upgrade_notice() { |
| 281 | check_ajax_referer( 'wp-smush-ajax' ); |
| 282 | |
| 283 | // Check capability. |
| 284 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 285 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 286 | } |
| 287 | update_site_option( 'wp-smush-hide_upgrade_notice', true ); |
| 288 | // No Need to send json response for other requests. |
| 289 | wp_send_json_success(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Remove the Update info |
| 294 | * |
| 295 | * @param bool $remove_notice Remove notice. |
| 296 | */ |
| 297 | public function dismiss_update_info( $remove_notice = false ) { |
| 298 | check_ajax_referer( 'wp-smush-ajax' ); |
| 299 | |
| 300 | // Check capability. |
| 301 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 302 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 303 | } |
| 304 | WP_Smush::get_instance()->core()->mod->smush->dismiss_update_info( $remove_notice ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Hide S3 support alert by setting a flag. |
| 309 | */ |
| 310 | public function dismiss_s3support_alert() { |
| 311 | check_ajax_referer( 'wp-smush-ajax' ); |
| 312 | // Check capability. |
| 313 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 314 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 315 | } |
| 316 | // Just set a flag. |
| 317 | update_site_option( 'wp-smush-hide_s3support_alert', 1 ); |
| 318 | wp_send_json_success(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Hide API Message |
| 323 | */ |
| 324 | public function hide_api_message() { |
| 325 | check_ajax_referer( 'wp-smush-ajax' ); |
| 326 | |
| 327 | // Check capability. |
| 328 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 329 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 330 | } |
| 331 | |
| 332 | $api_message = get_site_option( 'wp-smush-api_message', array() ); |
| 333 | if ( ! empty( $api_message ) && is_array( $api_message ) ) { |
| 334 | $api_message[ key( $api_message ) ]['status'] = 'hide'; |
| 335 | update_site_option( 'wp-smush-api_message', $api_message ); |
| 336 | } |
| 337 | |
| 338 | wp_send_json_success(); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Send JSON response whether to show or not the warning |
| 343 | */ |
| 344 | public function show_warning_ajax() { |
| 345 | check_ajax_referer( 'wp-smush-ajax' ); |
| 346 | // Check capability. |
| 347 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 348 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 349 | } |
| 350 | $show = WP_Smush::get_instance()->core()->mod->smush->show_warning(); |
| 351 | wp_send_json( (int) $show ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Dismiss the plugin conflicts notice. |
| 356 | * |
| 357 | * @since 3.6.0 |
| 358 | */ |
| 359 | public function dismiss_notice() { |
| 360 | check_ajax_referer( 'wp-smush-ajax' ); |
| 361 | |
| 362 | // Check capability. |
| 363 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 364 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 365 | } |
| 366 | |
| 367 | if ( empty( $_REQUEST['key'] ) ) { |
| 368 | wp_send_json_error(); |
| 369 | } |
| 370 | |
| 371 | $this->set_notice_dismissed( sanitize_key( $_REQUEST['key'] ) ); |
| 372 | wp_send_json_success(); |
| 373 | } |
| 374 | |
| 375 | private function set_notice_dismissed( $notice ) { |
| 376 | $option_id = 'wp-smush-dismissed-notices'; |
| 377 | $dismissed_notices = get_option( $option_id, array() ); |
| 378 | $dismissed_notices[ $notice ] = true; |
| 379 | update_option( $option_id, $dismissed_notices ); |
| 380 | } |
| 381 | |
| 382 | /*************************************** |
| 383 | * |
| 384 | * SMUSH |
| 385 | */ |
| 386 | |
| 387 | /** |
| 388 | * Scans all the smushed attachments to check if they need to be resmushed as per the |
| 389 | * current settings, as user might have changed one of the configurations "Lossy", "Keep Original", "Preserve Exif" |
| 390 | * |
| 391 | * @todo: Needs some refactoring big time |
| 392 | */ |
| 393 | public function scan_images() { |
| 394 | check_ajax_referer( 'save_wp_smush_options', 'wp_smush_options_nonce' ); |
| 395 | |
| 396 | // Check capability. |
| 397 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 398 | wp_send_json_error( |
| 399 | array( |
| 400 | 'notice' => esc_html__( "You don't have permission to do this.", 'wp-smushit' ), |
| 401 | 'noticeType' => 'error', |
| 402 | ) |
| 403 | ); |
| 404 | } |
| 405 | |
| 406 | // Scanning for NextGen or Media Library. |
| 407 | $type = isset( $_REQUEST['type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['type'] ) ) : ''; |
| 408 | |
| 409 | /** |
| 410 | * @hooked wp_smush_nextgen_scan_stats Smush\Core\Integrations\NextGen\Admin::scan_images() |
| 411 | */ |
| 412 | $stats = apply_filters( "wp_smush_{$type}_scan_stats", array() ); |
| 413 | |
| 414 | return wp_send_json_success( $stats ); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Return Latest stats. |
| 419 | */ |
| 420 | public function get_stats() { |
| 421 | check_ajax_referer( 'wp-smush-ajax', '_nonce' ); |
| 422 | |
| 423 | // Check capability. |
| 424 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 425 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 426 | } |
| 427 | |
| 428 | $admin = WP_Smush::get_instance()->admin(); |
| 429 | $stats = $admin->get_global_stats_with_bulk_smush_content(); |
| 430 | |
| 431 | wp_send_json_success( $stats ); |
| 432 | } |
| 433 | |
| 434 | /*************************************** |
| 435 | * |
| 436 | * BULK SMUSH |
| 437 | */ |
| 438 | |
| 439 | /*************************************** |
| 440 | * |
| 441 | * DIRECTORY SMUSH |
| 442 | */ |
| 443 | |
| 444 | /** |
| 445 | * Returns Directory Smush stats and Cumulative stats |
| 446 | */ |
| 447 | public function get_dir_smush_stats() { |
| 448 | check_ajax_referer( 'wp-smush-ajax' ); |
| 449 | |
| 450 | // Check capability. |
| 451 | $capability = is_multisite() ? 'manage_network' : 'manage_options'; |
| 452 | if ( ! Helper::is_user_allowed( $capability ) ) { |
| 453 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 454 | } |
| 455 | |
| 456 | $result = array(); |
| 457 | |
| 458 | // Store the Total/Smushed count. |
| 459 | $stats = WP_Smush::get_instance()->core()->mod->dir->total_stats(); |
| 460 | |
| 461 | $result['dir_smush'] = $stats; |
| 462 | |
| 463 | // Cumulative Stats. |
| 464 | // $result['combined_stats'] = WP_Smush::get_instance()->core()->mod->dir->combine_stats( $stats ); |
| 465 | |
| 466 | // Store the stats in options table. |
| 467 | update_option( 'dir_smush_stats', $result, false ); |
| 468 | |
| 469 | // Send ajax response. |
| 470 | wp_send_json_success( $result ); |
| 471 | } |
| 472 | |
| 473 | /*************************************** |
| 474 | * |
| 475 | * CDN |
| 476 | * |
| 477 | * @since 3.0 |
| 478 | */ |
| 479 | |
| 480 | /*************************************** |
| 481 | * |
| 482 | * LAZY LOADING |
| 483 | * |
| 484 | * @since 3.2.0 |
| 485 | */ |
| 486 | |
| 487 | /** |
| 488 | * Toggle lazy loading module. |
| 489 | * |
| 490 | * Handles "Activate" button press on the disabled lazy loading meta box. |
| 491 | * Handles "Deactivate" button press on the lazy loading meta box. |
| 492 | * Refreshes page on success. |
| 493 | * |
| 494 | * @since 3.2.0 |
| 495 | */ |
| 496 | public function smush_toggle_lazy_load() { |
| 497 | check_ajax_referer( 'save_wp_smush_options' ); |
| 498 | |
| 499 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 500 | wp_send_json_error( |
| 501 | array( |
| 502 | 'message' => __( 'User can not modify options', 'wp-smushit' ), |
| 503 | ), |
| 504 | 403 |
| 505 | ); |
| 506 | } |
| 507 | |
| 508 | $param = isset( $_POST['param'] ) ? sanitize_text_field( wp_unslash( $_POST['param'] ) ) : false; |
| 509 | |
| 510 | if ( 'true' === $param ) { |
| 511 | $settings = $this->settings->get_setting( 'wp-smush-lazy_load' ); |
| 512 | |
| 513 | // No settings, during init - set defaults. |
| 514 | if ( ! $settings ) { |
| 515 | $this->settings->init_lazy_load_defaults(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | $this->settings->set( 'lazy_load', 'true' === $param ); |
| 520 | |
| 521 | wp_send_json_success(); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Remove spinner/placeholder icon from lazy-loading. |
| 526 | * |
| 527 | * @since 3.2.2 |
| 528 | */ |
| 529 | public function remove_icon() { |
| 530 | check_ajax_referer( 'save_wp_smush_options' ); |
| 531 | |
| 532 | // Check for permission. |
| 533 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 534 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 535 | } |
| 536 | |
| 537 | $id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ); |
| 538 | $type = filter_input( INPUT_POST, 'type', FILTER_SANITIZE_SPECIAL_CHARS ); |
| 539 | if ( $id && $type ) { |
| 540 | $settings = $this->settings->get_setting( 'wp-smush-lazy_load' ); |
| 541 | if ( false !== ( $key = array_search( $id, $settings['animation'][ $type ]['custom'] ) ) ) { |
| 542 | unset( $settings['animation'][ $type ]['custom'][ $key ] ); |
| 543 | $this->settings->set_setting( 'wp-smush-lazy_load', $settings ); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | wp_send_json_success(); |
| 548 | } |
| 549 | |
| 550 | /*************************************** |
| 551 | * |
| 552 | * CONFIGS |
| 553 | * |
| 554 | * @since 3.8.5 |
| 555 | */ |
| 556 | |
| 557 | /** |
| 558 | * Handles the upload of a config file. |
| 559 | * |
| 560 | * @since 3.8.5 |
| 561 | */ |
| 562 | public function upload_config() { |
| 563 | check_ajax_referer( 'smush_handle_config' ); |
| 564 | |
| 565 | $capability = is_multisite() ? 'manage_network' : 'manage_options'; |
| 566 | if ( ! Helper::is_user_allowed( $capability ) ) { |
| 567 | wp_send_json_error( null, 403 ); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Data escaped and sanitized via \Smush\Core\Configs::save_uploaded_config() |
| 572 | * |
| 573 | * @see \Smush\Core\Configs::decode_and_validate_config_file() |
| 574 | */ |
| 575 | $file = isset( $_FILES['file'] ) ? wp_unslash( $_FILES['file'] ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 576 | |
| 577 | $configs_handler = Configs::get_instance(); |
| 578 | $new_config = $configs_handler->save_uploaded_config( $file ); |
| 579 | |
| 580 | if ( ! is_wp_error( $new_config ) ) { |
| 581 | wp_send_json_success( $new_config ); |
| 582 | } |
| 583 | |
| 584 | wp_send_json_error( |
| 585 | array( 'error_msg' => $new_config->get_error_message() ) |
| 586 | ); |
| 587 | } |
| 588 | /** |
| 589 | * Handles the upload of a config file. |
| 590 | * |
| 591 | * @since 3.8.5 |
| 592 | */ |
| 593 | public function save_config() { |
| 594 | check_ajax_referer( 'smush_handle_config' ); |
| 595 | |
| 596 | $capability = is_multisite() ? 'manage_network' : 'manage_options'; |
| 597 | if ( ! Helper::is_user_allowed( $capability ) ) { |
| 598 | wp_send_json_error( null, 403 ); |
| 599 | } |
| 600 | |
| 601 | $configs_handler = Configs::get_instance(); |
| 602 | wp_send_json_success( $configs_handler->get_config_from_current() ); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Applies the given config. |
| 607 | * |
| 608 | * @since 3.8.5 |
| 609 | */ |
| 610 | public function apply_config() { |
| 611 | check_ajax_referer( 'smush_handle_config' ); |
| 612 | |
| 613 | $capability = is_multisite() ? 'manage_network' : 'manage_options'; |
| 614 | if ( ! Helper::is_user_allowed( $capability ) ) { |
| 615 | wp_send_json_error( null, 403 ); |
| 616 | } |
| 617 | |
| 618 | $id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ); |
| 619 | if ( ! $id ) { |
| 620 | // Abort if no config ID was given. |
| 621 | wp_send_json_error( |
| 622 | array( 'error_msg' => esc_html__( 'Missing config ID', 'wp-smushit' ) ) |
| 623 | ); |
| 624 | } |
| 625 | |
| 626 | $configs_handler = Configs::get_instance(); |
| 627 | $response = $configs_handler->apply_config_by_id( $id ); |
| 628 | |
| 629 | if ( ! is_wp_error( $response ) ) { |
| 630 | wp_send_json_success(); |
| 631 | } |
| 632 | |
| 633 | wp_send_json_error( |
| 634 | array( 'error_msg' => esc_html( $response->get_error_message() ) ) |
| 635 | ); |
| 636 | } |
| 637 | |
| 638 | /*************************************** |
| 639 | * |
| 640 | * SETTINGS |
| 641 | * |
| 642 | * @since 3.2.0.2 |
| 643 | */ |
| 644 | |
| 645 | /*************************************** |
| 646 | * |
| 647 | * MODALS |
| 648 | * |
| 649 | * @since 3.7.0 |
| 650 | */ |
| 651 | |
| 652 | /** |
| 653 | * Hides the new features modal. |
| 654 | */ |
| 655 | public function remind_later_review_prompts() { |
| 656 | check_ajax_referer( 'wp-smush-ajax' ); |
| 657 | |
| 658 | // Check for permission. |
| 659 | if ( ! Helper::is_user_allowed( 'manage_options' ) ) { |
| 660 | wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); |
| 661 | } |
| 662 | |
| 663 | update_option( |
| 664 | Admin::get_review_prompts_option_key(), |
| 665 | array( |
| 666 | 'time' => time() + WEEK_IN_SECONDS, |
| 667 | 'type' => 'remind_later', |
| 668 | ) |
| 669 | ); |
| 670 | |
| 671 | wp_send_json_success(); |
| 672 | } |
| 673 | } |
| 674 |