| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | * @subpackage Quickwebp/admin |
| 10 | 10 | */ |
| 11 | 11 | class Quickwebp_Settings { |
| 12 | 12 | |
| 13 | + const QUICKWEBP_GET_LICENSE_URL = 'https://solutions.leyoweb.com/products/quickwebp/'; | |
| 14 | + const QUICKWEBP_GET_MY_ACCOUNT_URL = 'https://solutions.leyoweb.com/customer-dashboard/'; | |
| 15 | + | |
| 13 | 16 | /** |
| 14 | 17 | * The ID of this plugin. |
| 15 | 18 | * |
| 16 | 19 | * @since 1.0.0 |
| @@ -51,10 +54,101 @@ | ||
| 51 | 54 | |
| 52 | 55 | $admin_main_settings_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/admin-main-settings.asset.php' ); |
| 53 | 56 | wp_enqueue_style( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.css', array(), $admin_main_settings_assets['version'], 'all' ); |
| 54 | 57 | wp_enqueue_script( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.js', $admin_main_settings_assets['dependencies'], $admin_main_settings_assets['version'], true ); |
| 58 | + wp_localize_script( 'quickwebpoi_admin_main_settings', 'QUICKWEBP_ADMIN_SETTINGS', array( | |
| 59 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 60 | + 'nonce' => wp_create_nonce( 'image_optimize_nonce' ), | |
| 61 | + 'preview_image_data' => $this->get_preview_image_data(), | |
| 62 | + 'default_image_url' => QUICKWEBP_PLUGIN_URL . 'public/assets/img/preview.jpg', | |
| 63 | + )); | |
| 55 | 64 | } |
| 56 | 65 | } |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Persist dismissal of the contextual AVIF notice for the current user. | |
| 69 | + */ | |
| 70 | + public function dismiss_avif_notice() { | |
| 71 | + if ( ! isset( $_GET['quickwebp_dismiss_avif_notice'] ) || ! current_user_can( 'manage_options' ) ) { | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + | |
| 75 | + check_admin_referer( 'quickwebp_dismiss_avif_notice' ); | |
| 76 | + update_user_meta( get_current_user_id(), 'quickwebp_avif_notice_dismissed', 1 ); | |
| 77 | + | |
| 78 | + wp_safe_redirect( remove_query_arg( array( 'quickwebp_dismiss_avif_notice', '_wpnonce' ) ) ); | |
| 79 | + exit; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Render the contextual AVIF upgrade notice outside QuickWebP screens. | |
| 84 | + */ | |
| 85 | + public function render_avif_notice() { | |
| 86 | + global $quickwebp_surecart_client; | |
| 87 | + | |
| 88 | + if ( ! current_user_can( 'manage_options' ) || get_user_meta( get_current_user_id(), 'quickwebp_avif_notice_dismissed', true ) ) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $screen = get_current_screen(); | |
| 93 | + if ( $screen && false !== strpos( $screen->id, 'quickwebp' ) ) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( $quickwebp_surecart_client && $quickwebp_surecart_client->license()->is_valid() ) { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $stats = get_option( 'quickwebp_optimization_stats', array() ); | |
| 102 | + $image_count = absint( $stats['images_optimized'] ?? 0 ); | |
| 103 | + $bytes_saved = absint( $stats['bytes_saved'] ?? 0 ); | |
| 104 | + $installed_at = absint( get_option( 'quickwebp_installed_at', 0 ) ); | |
| 105 | + $seven_days = 7 * DAY_IN_SECONDS; | |
| 106 | + | |
| 107 | + if ( $image_count < 20 && ( ! $installed_at || time() - $installed_at < $seven_days ) ) { | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $avif_supported = function_exists( 'wp_image_editor_supports' ) && wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ); | |
| 112 | + if ( ! $avif_supported ) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $dismiss_url = wp_nonce_url( | |
| 117 | + add_query_arg( 'quickwebp_dismiss_avif_notice', '1' ), | |
| 118 | + 'quickwebp_dismiss_avif_notice' | |
| 119 | + ); | |
| 120 | + ?> | |
| 121 | + <div class="notice notice-success quickwebp-avif-notice"> | |
| 122 | + <p> | |
| 123 | + <strong> | |
| 124 | + <?php | |
| 125 | + printf( | |
| 126 | + /* translators: %s is the number of images optimized by QuickWebP. */ | |
| 127 | + esc_html( _n( 'QuickWebP has already optimized %s image on your site.', 'QuickWebP has already optimized %s images on your site.', $image_count, 'quickwebp' ) ), | |
| 128 | + esc_html( number_format_i18n( $image_count ) ) | |
| 129 | + ); | |
| 130 | + ?> | |
| 131 | + </strong> | |
| 132 | + </p> | |
| 133 | + <p> | |
| 134 | + <?php | |
| 135 | + printf( | |
| 136 | + /* translators: %s is the disk space saved by QuickWebP. */ | |
| 137 | + esc_html__( 'You have saved %s thanks to WebP.', 'quickwebp' ), | |
| 138 | + '<strong>' . esc_html( size_format( $bytes_saved, 1 ) ) . '</strong>' | |
| 139 | + ); | |
| 140 | + ?> | |
| 141 | + </p> | |
| 142 | + <p><strong><?php esc_html_e( 'Your server also supports AVIF.', 'quickwebp' ); ?></strong></p> | |
| 143 | + <p><strong><?php esc_html_e( 'With AVIF, your WebP images could be around 30% smaller on average.', 'quickwebp' ); ?></strong></p> | |
| 144 | + <p> | |
| 145 | + <a class="button button-primary" href="<?php echo esc_url( quickwebp_get_pro_url( 'avif-admin-notice' ) ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Discover AVIF', 'quickwebp' ); ?></a> | |
| 146 | + <a class="button-link" href="<?php echo esc_url( $dismiss_url ); ?>"><?php esc_html_e( 'Dismiss', 'quickwebp' ); ?></a> | |
| 147 | + </p> | |
| 148 | + </div> | |
| 149 | + <?php | |
| 150 | + } | |
| 57 | 151 | |
| 58 | 152 | /** |
| 59 | 153 | * add_settings_menu |
| 60 | 154 | * |
| @@ -62,10 +156,10 @@ | ||
| 62 | 156 | */ |
| 63 | 157 | public function add_settings_menu() { |
| 64 | 158 | add_submenu_page( |
| 65 | 159 | 'upload.php', |
| 66 | - __('Quickwebp Settings', QUICKWEBP_TEXT_DOMAIN), | |
| 67 | - __('Quickwebp', QUICKWEBP_TEXT_DOMAIN), | |
| 160 | + __('QuickWebP Settings', 'quickwebp'), | |
| 161 | + __('QuickWebP', 'quickwebp'), | |
| 68 | 162 | 'manage_options', |
| 69 | 163 | 'quickwebp-settings', |
| 70 | 164 | array( $this, 'render_settings_page' ) |
| 71 | 165 | ); |
| @@ -76,8 +170,44 @@ | ||
| 76 | 170 | * |
| 77 | 171 | * @return void |
| 78 | 172 | */ |
| 79 | 173 | public function render_settings_page() { |
| 174 | + global $is_nginx, $quickwebp_surecart_client; | |
| 175 | + | |
| 176 | + $php_version_valid = version_compare( PHP_VERSION, '8.1', '>=' ); | |
| 177 | + $license_valid = false; | |
| 178 | + if ( $quickwebp_surecart_client ) { | |
| 179 | + $license_valid = $quickwebp_surecart_client->license()->is_valid(); | |
| 180 | + } | |
| 181 | + | |
| 182 | + //phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 183 | + if ( isset( $_POST['quickwebp_settings_conversion'] ) ) { | |
| 184 | + update_option( 'quickwebp_settings_onboarding_completed', '1' ); | |
| 185 | + } | |
| 186 | + | |
| 187 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound | |
| 188 | + $wpmtk_is_active = in_array( 'wpmastertoolkit/wp-mastertoolkit.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ); | |
| 189 | + $show_onboarding = '1' !== (string) get_option( 'quickwebp_settings_onboarding_completed', '0' ); | |
| 190 | + $conversion = get_option( 'quickwebp_settings_conversion', quickwebp_settings_default( 'quickwebp_settings_conversion' ) ); | |
| 191 | + $conversion_enabled = '1' === (string) $conversion; | |
| 192 | + $save_original = get_option( 'quickwebp_settings_conversion_save_original', quickwebp_settings_default( 'quickwebp_settings_conversion_save_original' ) ); | |
| 193 | + $save_original = is_array( $save_original ) ? $save_original : array(); | |
| 194 | + $save_original_enabled = in_array( 'checked', $save_original, true ); | |
| 195 | + $display_mode = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default( 'quickwebp_settings_conversion_display_webp_mode' ) ); | |
| 196 | + $conversion_quality = get_option( 'quickwebp_settings_conversion_quality', quickwebp_settings_default( 'quickwebp_settings_conversion_quality' ) ); | |
| 197 | + | |
| 198 | + $profile_label = __( 'Custom profile', 'quickwebp' ); | |
| 199 | + if ( $conversion_enabled && ! $save_original_enabled ) { | |
| 200 | + $profile_label = __( 'New site profile', 'quickwebp' ); | |
| 201 | + } | |
| 202 | + if ( $conversion_enabled && $save_original_enabled ) { | |
| 203 | + $profile_label = __( 'Existing site profile', 'quickwebp' ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + $preview_image_data = $this->get_preview_image_data(); | |
| 207 | + $preview_image_data_webp = $preview_image_data['1_' . $conversion_quality] ?? $preview_image_data['1_medium']; | |
| 208 | + $preview_image_data_avif = $preview_image_data['2_' . $conversion_quality] ?? $preview_image_data['2_medium']; | |
| 209 | + | |
| 80 | 210 | require_once QUICKWEBP_PLUGIN_PATH . 'admin/templates/page-settings.php'; |
| 81 | 211 | } |
| 82 | 212 | |
| 83 | 213 | /** |
| @@ -110,5 +240,219 @@ | ||
| 110 | 240 | </tr> |
| 111 | 241 | <?php |
| 112 | 242 | } |
| 113 | 243 | } |
| 114 | -} | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Add the settings link | |
| 247 | + */ | |
| 248 | + public function add_settings_link( $plugin_actions, $plugin_file ) { | |
| 249 | + | |
| 250 | + $new_actions = array(); | |
| 251 | + | |
| 252 | + if ( 'quickwebp/quickwebp.php' === $plugin_file ) { | |
| 253 | + | |
| 254 | + $new_actions['settings'] = sprintf( | |
| 255 | + // translators: %s is a placeholder for the link to the settings page. | |
| 256 | + __( '<a href="%s">Settings</a>', 'quickwebp' ), | |
| 257 | + esc_url( admin_url( 'upload.php?page=quickwebp-settings' ) ) | |
| 258 | + ); | |
| 259 | + | |
| 260 | + $new_actions['upgrade_pro'] = sprintf( | |
| 261 | + '<a href="%1$s" target="_blank" rel="noopener noreferrer" style="color: #008a20; font-weight: 600;">%2$s</a>', | |
| 262 | + esc_url( quickwebp_get_pro_url( 'plugins-list-upgrade' ) ), | |
| 263 | + esc_html__( 'Upgrade to Pro', 'quickwebp' ) | |
| 264 | + ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + return array_merge( $new_actions, $plugin_actions ); | |
| 268 | + } | |
| 269 | + | |
| 270 | + /** | |
| 271 | + * Display web mode changed | |
| 272 | + */ | |
| 273 | + public function add_rewrite_rules( $values ) { | |
| 274 | + global $is_apache, $is_iis7, $is_nginx; | |
| 275 | + | |
| 276 | + include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-apache.php'; | |
| 277 | + include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-nginx.php'; | |
| 278 | + include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-iis.php'; | |
| 279 | + | |
| 280 | + $old_value = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default('quickwebp_settings_conversion_display_webp_mode') ); | |
| 281 | + $is_rewrite = 'rewrite' === $values; | |
| 282 | + $was_rewrite = 'rewrite' === $old_value; | |
| 283 | + $add_or_remove = false; | |
| 284 | + | |
| 285 | + if ( $is_rewrite && !$was_rewrite ) { | |
| 286 | + $add_or_remove = 'add'; | |
| 287 | + } elseif ( !$is_rewrite && $was_rewrite ) { | |
| 288 | + $add_or_remove = 'remove'; | |
| 289 | + } else { | |
| 290 | + return $values; | |
| 291 | + } | |
| 292 | + | |
| 293 | + if ( $is_apache ) { | |
| 294 | + $rules = new Quickwebp_Apache(); | |
| 295 | + } elseif ( $is_iis7 ) { | |
| 296 | + $rules = new Quickwebp_IIS(); | |
| 297 | + } elseif ( $is_nginx ) { | |
| 298 | + $rules = new Quickwebp_Nginx(); | |
| 299 | + } else { | |
| 300 | + return $values; | |
| 301 | + } | |
| 302 | + | |
| 303 | + if ( 'add' === $add_or_remove ) { | |
| 304 | + $result = $rules->add(); | |
| 305 | + } else { | |
| 306 | + $result = $rules->remove(); | |
| 307 | + } | |
| 308 | + | |
| 309 | + if ( is_wp_error( $result ) ) { | |
| 310 | + add_action( 'admin_notices', function() use ( $result ) { | |
| 311 | + ?> | |
| 312 | + <div class="notice notice-error"> | |
| 313 | + <p><?php echo esc_html( $result->get_error_message() ); ?></p> | |
| 314 | + </div> | |
| 315 | + <?php | |
| 316 | + }); | |
| 317 | + } | |
| 318 | + | |
| 319 | + return $values; | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 323 | + * Ensure display mode remains consistent with conversion mode. | |
| 324 | + */ | |
| 325 | + public function sanitize_display_mode_for_consistency( $value, $option = '', $original_value = '' ) { | |
| 326 | + | |
| 327 | + $allowed_modes = array( 'disabled', 'picture', 'rewrite' ); | |
| 328 | + $value = sanitize_text_field( (string) $value ); | |
| 329 | + | |
| 330 | + if ( ! in_array( $value, $allowed_modes, true ) ) { | |
| 331 | + $value = 'disabled'; | |
| 332 | + } | |
| 333 | + | |
| 334 | + $conversion_enabled = $this->is_conversion_enabled_from_request_or_option(); | |
| 335 | + if ( ! $conversion_enabled ) { | |
| 336 | + return 'disabled'; | |
| 337 | + } | |
| 338 | + | |
| 339 | + $save_original = $this->is_save_original_enabled_from_request_or_option(); | |
| 340 | + if ( ! $save_original ) { | |
| 341 | + return 'disabled'; | |
| 342 | + } | |
| 343 | + | |
| 344 | + if ( 'disabled' === $value ) { | |
| 345 | + return 'picture'; | |
| 346 | + } | |
| 347 | + | |
| 348 | + return $value; | |
| 349 | + } | |
| 350 | + | |
| 351 | + /** | |
| 352 | + * Get whether original images should be preserved. | |
| 353 | + */ | |
| 354 | + private function is_save_original_enabled_from_request_or_option() { | |
| 355 | + | |
| 356 | + //phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 357 | + if ( isset( $_POST['quickwebp_settings_conversion_save_original'] ) ) { | |
| 358 | + //phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 359 | + $raw_value = wp_unslash( $_POST['quickwebp_settings_conversion_save_original'] ); | |
| 360 | + | |
| 361 | + if ( is_array( $raw_value ) ) { | |
| 362 | + $raw_value = array_map( 'sanitize_text_field', $raw_value ); | |
| 363 | + return in_array( 'checked', $raw_value, true ); | |
| 364 | + } | |
| 365 | + | |
| 366 | + return false; | |
| 367 | + } | |
| 368 | + | |
| 369 | + $saved_value = get_option( | |
| 370 | + 'quickwebp_settings_conversion_save_original', | |
| 371 | + quickwebp_settings_default( 'quickwebp_settings_conversion_save_original' ) | |
| 372 | + ); | |
| 373 | + | |
| 374 | + $saved_value = is_array( $saved_value ) ? $saved_value : array(); | |
| 375 | + return in_array( 'checked', $saved_value, true ); | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * Get whether image conversion is enabled. | |
| 380 | + */ | |
| 381 | + private function is_conversion_enabled_from_request_or_option() { | |
| 382 | + | |
| 383 | + //phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 384 | + if ( isset( $_POST['quickwebp_settings_conversion'] ) ) { | |
| 385 | + //phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 386 | + $raw_value = wp_unslash( $_POST['quickwebp_settings_conversion'] ); | |
| 387 | + $raw_value = sanitize_text_field( (string) $raw_value ); | |
| 388 | + | |
| 389 | + return '1' === $raw_value; | |
| 390 | + } | |
| 391 | + | |
| 392 | + $saved_value = get_option( | |
| 393 | + 'quickwebp_settings_conversion', | |
| 394 | + quickwebp_settings_default( 'quickwebp_settings_conversion' ) | |
| 395 | + ); | |
| 396 | + | |
| 397 | + return '1' === (string) $saved_value; | |
| 398 | + } | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Data for the preview image used in the settings page. | |
| 402 | + */ | |
| 403 | + private function get_preview_image_data() { | |
| 404 | + return array( | |
| 405 | + 'name' => 'preview.jpg', | |
| 406 | + 'size' => '423.17 KB', | |
| 407 | + 'dimensions' => '1264 x 848', | |
| 408 | + '1_low' => array( | |
| 409 | + 'size' => '175.32 KB', | |
| 410 | + 'save' => '247.85 KB', | |
| 411 | + 'save_percent' => '59%', | |
| 412 | + 'percent' => '41%', | |
| 413 | + ), | |
| 414 | + '1_medium' => array( | |
| 415 | + 'size' => '196.08 KB', | |
| 416 | + 'save' => '227.09 KB', | |
| 417 | + 'save_percent' => '54%', | |
| 418 | + 'percent' => '46%', | |
| 419 | + ), | |
| 420 | + '1_high' => array( | |
| 421 | + 'size' => '229.29 KB', | |
| 422 | + 'save' => '193.88 KB', | |
| 423 | + 'save_percent' => '46%', | |
| 424 | + 'percent' => '54%', | |
| 425 | + ), | |
| 426 | + '1_extra_high' => array( | |
| 427 | + 'size' => '387.46 KB', | |
| 428 | + 'save' => '35.71 KB', | |
| 429 | + 'save_percent' => '8%', | |
| 430 | + 'percent' => '92%', | |
| 431 | + ), | |
| 432 | + '2_low' => array( | |
| 433 | + 'size' => '73.44 KB', | |
| 434 | + 'save' => '349.73 KB', | |
| 435 | + 'save_percent' => '83%', | |
| 436 | + 'percent' => '17%', | |
| 437 | + ), | |
| 438 | + '2_medium' => array( | |
| 439 | + 'size' => '120.06 KB', | |
| 440 | + 'save' => '303.11 KB', | |
| 441 | + 'save_percent' => '72%', | |
| 442 | + 'percent' => '28%', | |
| 443 | + ), | |
| 444 | + '2_high' => array( | |
| 445 | + 'size' => '183.21 KB', | |
| 446 | + 'save' => '239.96 KB', | |
| 447 | + 'save_percent' => '57%', | |
| 448 | + 'percent' => '43%', | |
| 449 | + ), | |
| 450 | + '2_extra_high' => array( | |
| 451 | + 'size' => '290.54 KB', | |
| 452 | + 'save' => '132.63 KB', | |
| 453 | + 'save_percent' => '31%', | |
| 454 | + 'percent' => '69%', | |
| 455 | + ), | |
| 456 | + ); | |
| 457 | + } | |
| 458 | +} | |