PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
← All changes | modules/optimization/components/media-control.php +32 -13 1.1.01.7.7 View file →
@@ -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,15 +21,18 @@
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;
28 31
32 +use ImageOptimization\Plugin;
33 +use WP_Post;
34 +
29 35 if ( ! defined( 'ABSPATH' ) ) {
30 36 exit; // Exit if accessed directly.
31 37 }
32 38
@@ -89,13 +95,13 @@
89 95 /**
90 96 * Adds optimization control to media modals.
91 97 *
92 98 * @param array $form_fields
93 - * @param \WP_Post $post
99 + * @param WP_Post $post
94 100 *
95 101 * @return array
96 102 */
97 - 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 {
98 104 $form_fields[ self::DETAILS_MODAL_FIELD_ID ] = [
99 105 'label' => '',
100 106 'input' => 'hidden',
101 107 'value' => $this->get_optimization_control_html( 'details-view', $post->ID ),
@@ -135,11 +141,14 @@
135 141 'image_id' => $image_id,
136 142 'can_be_restored' => false,
137 143 ];
138 144
145 + // @var ImageOptimizer/Modules/ConnectManager/Module
146 + $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
147 +
139 148 try {
140 - if ( ! Connect::is_connected() || ! Connect::is_activated() ) {
141 - throw new Auth_Error( 'You have to activate your license to use Image Optimizer' );
149 + if ( ! $module->connect_instance->is_connected() || ! $module->connect_instance->is_activated() ) {
150 + throw new Auth_Error( esc_html__( 'You need to connect an Elementor account', 'image-optimization' ) );
142 151 }
143 152
144 153 Validate_Image::is_valid( $image_id );
145 154
@@ -148,14 +157,17 @@
148 157
149 158 if ( Image_Status::OPTIMIZED === $meta->get_status() ) {
150 159 $global_context['can_be_restored'] = $image->can_be_restored();
151 160 } else {
161 + $ic = new Image_Conversion();
162 +
152 163 $path = $image->get_file_path( Image::SIZE_FULL );
153 - $is_webp = strtolower( File_Utils::get_extension( $path ) ) === 'webp';
154 164 $backups_enabled = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME );
155 - $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();
156 168
157 - $global_context['can_be_restored'] = ( ! $is_webp && $webp_conversion_enabled ) || $backups_enabled;
169 + $global_context['can_be_restored'] = ( $needs_conversion && $conversion_enabled ) || $backups_enabled;
158 170 }
159 171
160 172 switch ( $meta->get_status() ) {
161 173 case Image_Status::OPTIMIZATION_IN_PROGRESS:
@@ -187,16 +199,21 @@
187 199
188 200 case Image_Status::OPTIMIZED:
189 201 $stats = Optimization_Stats::get_image_stats( $image_id );
190 202 $saved = [
191 - 'relative' => round( $stats['current_image_size'] / $stats['initial_image_size'] * 100 ),
203 + 'relative' => max( 100 - round( $stats['current_image_size'] / $stats['initial_image_size'] * 100 ), 0 ),
192 204 'absolute' => $stats['initial_image_size'] - $stats['current_image_size'],
193 205 ];
194 206
207 + $is_losseless_and_webp = Image_Conversion_Option::WEBP === Settings::get( Settings::CONVERT_TO_FORMAT_OPTION_NAME )
208 + && 'lossless' === Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME );
209 +
195 210 Module::load_template( $context, 'optimized', array_merge(
196 211 $global_context, [
197 212 'sizes_optimized_count' => $stats['optimized_image_count'],
213 + 'sizes_total' => $stats['total_image_count'],
198 214 'saved' => $saved,
215 + 'is_losseless_and_webp' => $is_losseless_and_webp,
199 216 ]
200 217 ) );
201 218
202 219 break;
@@ -203,9 +220,9 @@
203 220
204 221 case Image_Status::OPTIMIZATION_FAILED:
205 222 $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
206 223 $error_message = Optimization_Error_Message::get_optimization_error_message( $error_type );
207 - $images_left = Data::images_left();
224 + $images_left = $module->connect_instance->images_left();
208 225
209 226 Module::load_template( $context, 'error', array_merge(
210 227 $global_context, [
211 228 'message' => $error_message,
@@ -220,9 +237,9 @@
220 237
221 238 case Image_Status::REOPTIMIZING_FAILED:
222 239 $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
223 240 $error_message = Optimization_Error_Message::get_reoptimization_error_message( $error_type );
224 - $images_left = Data::images_left();
241 + $images_left = $module->connect_instance->images_left();
225 242
226 243 Module::load_template( $context, 'error', array_merge(
227 244 $global_context, [
228 245 'message' => $error_message,
@@ -260,9 +277,10 @@
260 277 } catch ( Auth_Error $ae ) {
261 278 Module::load_template( $context, 'error', array_merge(
262 279 $global_context, [
263 280 'action' => 'error',
264 - 'message' => esc_html__( 'N/A', 'image-optimization' ),
281 + 'optimization_error_type' => Image_Optimization_Error_Type::AUTH_ERROR,
282 + 'message' => $ae->getMessage(),
265 283 'allow_retry' => false,
266 284 ]
267 285 ) );
268 286 } catch ( Throwable $t ) {
@@ -268,8 +286,9 @@
268 286 } catch ( Throwable $t ) {
269 287 Module::load_template( $context, 'error', array_merge(
270 288 $global_context, [
271 289 'action' => 'error',
290 + 'optimization_error_type' => Image_Optimization_Error_Type::GENERIC,
272 291 'message' => esc_html__( 'Internal server error', 'image-optimization' ),
273 292 'allow_retry' => false,
274 293 ]
275 294 ) );