| @@ -4,16 +4,19 @@ | ||
| 4 | 4 | |
| 5 | 5 | use ImageOptimization\Classes\Image\{ |
| 6 | 6 | Exceptions\Invalid_Image_Exception, |
| 7 | 7 | Image, |
| 8 | + Image_Conversion, | |
| 9 | + Image_Conversion_Option, | |
| 8 | 10 | Image_Meta, |
| 9 | 11 | Image_Optimization_Error_Type, |
| 10 | 12 | Image_Status |
| 11 | 13 | }; |
| 14 | + | |
| 12 | 15 | use ImageOptimization\Modules\Oauth\Components\{ |
| 13 | - Connect, | |
| 14 | 16 | Exceptions\Auth_Error, |
| 15 | 17 | }; |
| 18 | + | |
| 16 | 19 | use ImageOptimization\Modules\Optimization\{ |
| 17 | 20 | Classes\Exceptions\Image_Validation_Error, |
| 18 | 21 | Classes\Optimization_Error_Message, |
| 19 | 22 | Classes\Validate_Image, |
| @@ -18,10 +21,10 @@ | ||
| 18 | 21 | Classes\Optimization_Error_Message, |
| 19 | 22 | Classes\Validate_Image, |
| 20 | 23 | Module, |
| 21 | 24 | }; |
| 25 | + | |
| 22 | 26 | use ImageOptimization\Classes\File_Utils; |
| 23 | -use ImageOptimization\Modules\Oauth\Classes\Data; | |
| 24 | 27 | use ImageOptimization\Modules\Settings\Classes\Settings; |
| 25 | 28 | use ImageOptimization\Modules\Stats\Classes\Optimization_Stats; |
| 26 | 29 | |
| 27 | 30 | use Throwable; |
| @@ -26,8 +29,9 @@ | ||
| 26 | 29 | |
| 27 | 30 | use Throwable; |
| 28 | 31 | |
| 29 | 32 | use ImageOptimization\Plugin; |
| 33 | +use WP_Post; | |
| 30 | 34 | |
| 31 | 35 | if ( ! defined( 'ABSPATH' ) ) { |
| 32 | 36 | exit; // Exit if accessed directly. |
| 33 | 37 | } |
| @@ -91,13 +95,13 @@ | ||
| 91 | 95 | /** |
| 92 | 96 | * Adds optimization control to media modals. |
| 93 | 97 | * |
| 94 | 98 | * @param array $form_fields |
| 95 | - * @param \WP_Post $post | |
| 99 | + * @param WP_Post $post | |
| 96 | 100 | * |
| 97 | 101 | * @return array |
| 98 | 102 | */ |
| 99 | - public function add_media_modal_details( array $form_fields, \WP_Post $post ): array { | |
| 103 | + public function add_media_modal_details( array $form_fields, WP_Post $post ): array { | |
| 100 | 104 | $form_fields[ self::DETAILS_MODAL_FIELD_ID ] = [ |
| 101 | 105 | 'label' => '', |
| 102 | 106 | 'input' => 'hidden', |
| 103 | 107 | 'value' => $this->get_optimization_control_html( 'details-view', $post->ID ), |
| @@ -140,12 +144,11 @@ | ||
| 140 | 144 | |
| 141 | 145 | // @var ImageOptimizer/Modules/ConnectManager/Module |
| 142 | 146 | $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 143 | 147 | |
| 144 | - | |
| 145 | 148 | try { |
| 146 | 149 | if ( ! $module->connect_instance->is_connected() || ! $module->connect_instance->is_activated() ) { |
| 147 | - throw new Auth_Error( 'You have to activate your license to use Image Optimizer' ); | |
| 150 | + throw new Auth_Error( esc_html__( 'You need to connect an Elementor account', 'image-optimization' ) ); | |
| 148 | 151 | } |
| 149 | 152 | |
| 150 | 153 | Validate_Image::is_valid( $image_id ); |
| 151 | 154 | |
| @@ -154,14 +157,17 @@ | ||
| 154 | 157 | |
| 155 | 158 | if ( Image_Status::OPTIMIZED === $meta->get_status() ) { |
| 156 | 159 | $global_context['can_be_restored'] = $image->can_be_restored(); |
| 157 | 160 | } else { |
| 161 | + $ic = new Image_Conversion(); | |
| 162 | + | |
| 158 | 163 | $path = $image->get_file_path( Image::SIZE_FULL ); |
| 159 | - $is_webp = strtolower( File_Utils::get_extension( $path ) ) === 'webp'; | |
| 160 | 164 | $backups_enabled = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME ); |
| 161 | - $webp_conversion_enabled = Settings::get( Settings::CONVERT_TO_WEBP_OPTION_NAME ); | |
| 165 | + $conversion_enabled = $ic->is_enabled(); | |
| 166 | + $needs_conversion = $conversion_enabled && | |
| 167 | + strtolower( File_Utils::get_extension( $path ) ) !== $ic->get_current_file_extension(); | |
| 162 | 168 | |
| 163 | - $global_context['can_be_restored'] = ( ! $is_webp && $webp_conversion_enabled ) || $backups_enabled; | |
| 169 | + $global_context['can_be_restored'] = ( $needs_conversion && $conversion_enabled ) || $backups_enabled; | |
| 164 | 170 | } |
| 165 | 171 | |
| 166 | 172 | switch ( $meta->get_status() ) { |
| 167 | 173 | case Image_Status::OPTIMIZATION_IN_PROGRESS: |
| @@ -197,14 +203,15 @@ | ||
| 197 | 203 | 'relative' => max( 100 - round( $stats['current_image_size'] / $stats['initial_image_size'] * 100 ), 0 ), |
| 198 | 204 | 'absolute' => $stats['initial_image_size'] - $stats['current_image_size'], |
| 199 | 205 | ]; |
| 200 | 206 | |
| 201 | - $is_losseless_and_webp = Settings::get( Settings::CONVERT_TO_WEBP_OPTION_NAME ) | |
| 207 | + $is_losseless_and_webp = Image_Conversion_Option::WEBP === Settings::get( Settings::CONVERT_TO_FORMAT_OPTION_NAME ) | |
| 202 | 208 | && 'lossless' === Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME ); |
| 203 | 209 | |
| 204 | 210 | Module::load_template( $context, 'optimized', array_merge( |
| 205 | 211 | $global_context, [ |
| 206 | 212 | 'sizes_optimized_count' => $stats['optimized_image_count'], |
| 213 | + 'sizes_total' => $stats['total_image_count'], | |
| 207 | 214 | 'saved' => $saved, |
| 208 | 215 | 'is_losseless_and_webp' => $is_losseless_and_webp, |
| 209 | 216 | ] |
| 210 | 217 | ) ); |
| @@ -270,9 +277,10 @@ | ||
| 270 | 277 | } catch ( Auth_Error $ae ) { |
| 271 | 278 | Module::load_template( $context, 'error', array_merge( |
| 272 | 279 | $global_context, [ |
| 273 | 280 | 'action' => 'error', |
| 274 | - 'message' => esc_html__( 'N/A', 'image-optimization' ), | |
| 281 | + 'optimization_error_type' => Image_Optimization_Error_Type::AUTH_ERROR, | |
| 282 | + 'message' => $ae->getMessage(), | |
| 275 | 283 | 'allow_retry' => false, |
| 276 | 284 | ] |
| 277 | 285 | ) ); |
| 278 | 286 | } catch ( Throwable $t ) { |
| @@ -278,8 +286,9 @@ | ||
| 278 | 286 | } catch ( Throwable $t ) { |
| 279 | 287 | Module::load_template( $context, 'error', array_merge( |
| 280 | 288 | $global_context, [ |
| 281 | 289 | 'action' => 'error', |
| 290 | + 'optimization_error_type' => Image_Optimization_Error_Type::GENERIC, | |
| 282 | 291 | 'message' => esc_html__( 'Internal server error', 'image-optimization' ), |
| 283 | 292 | 'allow_retry' => false, |
| 284 | 293 | ] |
| 285 | 294 | ) ); |