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 +71 -24 1.0.01.7.7 View file →
@@ -1,27 +1,42 @@
1 1 <?php
2 2
3 -namespace ImageOptimizer\Modules\Optimization\Components;
3 +namespace ImageOptimization\Modules\Optimization\Components;
4 4
5 -use ImageOptimizer\Classes\Image\{
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,
11 + Image_Optimization_Error_Type,
9 12 Image_Status
10 13 };
11 -use ImageOptimizer\Modules\Oauth\Components\{
12 - Connect,
13 - Exceptions\Auth_Exception,
14 +
15 +use ImageOptimization\Modules\Oauth\Components\{
16 + Exceptions\Auth_Error,
14 17 };
15 -use ImageOptimizer\Modules\Optimization\{
18 +
19 +use ImageOptimization\Modules\Optimization\{
16 20 Classes\Exceptions\Image_Validation_Error,
21 + Classes\Optimization_Error_Message,
17 22 Classes\Validate_Image,
18 23 Module,
19 24 };
20 -use ImageOptimizer\Modules\Settings\Classes\Settings;
21 -use ImageOptimizer\Modules\Stats\Classes\Optimization_Stats;
25 +
26 +use ImageOptimization\Classes\File_Utils;
27 +use ImageOptimization\Modules\Settings\Classes\Settings;
28 +use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
29 +
22 30 use Throwable;
23 31
32 +use ImageOptimization\Plugin;
33 +use WP_Post;
34 +
35 +if ( ! defined( 'ABSPATH' ) ) {
36 + exit; // Exit if accessed directly.
37 +}
38 +
24 39 /**
25 40 * The class is responsible for rendering optimization control in all views.
26 41 */
27 42 class Media_Control {
@@ -39,9 +54,9 @@
39 54 if ( empty( $columns ) ) {
40 55 return $columns;
41 56 }
42 57
43 - $columns[ self::COLUMN_ID ] = esc_html__( 'Image optimization', 'image-optimizer' );
58 + $columns[ self::COLUMN_ID ] = esc_html__( 'Image optimization', 'image-optimization' );
44 59
45 60 return $columns;
46 61 }
47 62
@@ -67,9 +82,9 @@
67 82 */
68 83 public function add_optimization_meta_box() {
69 84 add_meta_box(
70 85 self::META_BOX_ID,
71 - __( 'Image optimization', 'image-optimizer' ),
86 + __( 'Image optimization', 'image-optimization' ),
72 87 function( $post ) {
73 88 $this->render_optimization_control( 'meta-box', $post->ID );
74 89 },
75 90 'attachment',
@@ -80,13 +95,13 @@
80 95 /**
81 96 * Adds optimization control to media modals.
82 97 *
83 98 * @param array $form_fields
84 - * @param \WP_Post $post
99 + * @param WP_Post $post
85 100 *
86 101 * @return array
87 102 */
88 - 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 {
89 104 $form_fields[ self::DETAILS_MODAL_FIELD_ID ] = [
90 105 'label' => '',
91 106 'input' => 'hidden',
92 107 'value' => $this->get_optimization_control_html( 'details-view', $post->ID ),
@@ -126,11 +141,14 @@
126 141 'image_id' => $image_id,
127 142 'can_be_restored' => false,
128 143 ];
129 144
145 + // @var ImageOptimizer/Modules/ConnectManager/Module
146 + $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
147 +
130 148 try {
131 - if ( ! Connect::is_connected() || ! Connect::is_activated() ) {
132 - throw new Auth_Exception( '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' ) );
133 151 }
134 152
135 153 Validate_Image::is_valid( $image_id );
136 154
@@ -136,12 +154,22 @@
136 154
137 155 $image = new Image( $image_id );
138 156 $meta = new Image_Meta( $image->get_id() );
139 157
140 - $global_context['can_be_restored'] = Image_Status::OPTIMIZED === $meta->get_status()
141 - ? $image->can_be_restored()
142 - : Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME );
158 + if ( Image_Status::OPTIMIZED === $meta->get_status() ) {
159 + $global_context['can_be_restored'] = $image->can_be_restored();
160 + } else {
161 + $ic = new Image_Conversion();
143 162
163 + $path = $image->get_file_path( Image::SIZE_FULL );
164 + $backups_enabled = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_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();
168 +
169 + $global_context['can_be_restored'] = ( $needs_conversion && $conversion_enabled ) || $backups_enabled;
170 + }
171 +
144 172 switch ( $meta->get_status() ) {
145 173 case Image_Status::OPTIMIZATION_IN_PROGRESS:
146 174 Module::load_template( $context, 'loading', array_merge(
147 175 $global_context, [
@@ -171,16 +199,21 @@
171 199
172 200 case Image_Status::OPTIMIZED:
173 201 $stats = Optimization_Stats::get_image_stats( $image_id );
174 202 $saved = [
175 - '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 ),
176 204 'absolute' => $stats['initial_image_size'] - $stats['current_image_size'],
177 205 ];
178 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 +
179 210 Module::load_template( $context, 'optimized', array_merge(
180 211 $global_context, [
181 212 'sizes_optimized_count' => $stats['optimized_image_count'],
213 + 'sizes_total' => $stats['total_image_count'],
182 214 'saved' => $saved,
215 + 'is_losseless_and_webp' => $is_losseless_and_webp,
183 216 ]
184 217 ) );
185 218
186 219 break;
@@ -185,11 +218,17 @@
185 218
186 219 break;
187 220
188 221 case Image_Status::OPTIMIZATION_FAILED:
222 + $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
223 + $error_message = Optimization_Error_Message::get_optimization_error_message( $error_type );
224 + $images_left = $module->connect_instance->images_left();
225 +
189 226 Module::load_template( $context, 'error', array_merge(
190 227 $global_context, [
191 - 'message' => esc_html__( 'Optimization error', 'image-optimizer' ),
228 + 'message' => $error_message,
229 + 'optimization_error_type' => $error_type,
230 + 'images_left' => $images_left,
192 231 'allow_retry' => true,
193 232 'action' => 'optimize',
194 233 ]
195 234 ) );
@@ -196,11 +235,17 @@
196 235
197 236 break;
198 237
199 238 case Image_Status::REOPTIMIZING_FAILED:
239 + $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
240 + $error_message = Optimization_Error_Message::get_reoptimization_error_message( $error_type );
241 + $images_left = $module->connect_instance->images_left();
242 +
200 243 Module::load_template( $context, 'error', array_merge(
201 244 $global_context, [
202 - 'message' => esc_html__( 'Reoptimizing error', 'image-optimizer' ),
245 + 'message' => $error_message,
246 + 'optimization_error_type' => $error_type,
247 + 'images_left' => $images_left,
203 248 'allow_retry' => true,
204 249 'action' => 'reoptimize',
205 250 ]
206 251 ) );
@@ -209,9 +254,9 @@
209 254
210 255 case Image_Status::RESTORING_FAILED:
211 256 Module::load_template( $context, 'error', array_merge(
212 257 $global_context, [
213 - 'message' => esc_html__( 'Image restoring error', 'image-optimizer' ),
258 + 'message' => esc_html__( 'Image restoring error', 'image-optimization' ),
214 259 'allow_retry' => true,
215 260 'action' => 'restore',
216 261 ]
217 262 ) );
@@ -228,13 +273,14 @@
228 273 'message' => $iie->getMessage(),
229 274 'allow_retry' => false,
230 275 ]
231 276 ) );
232 - } catch ( Auth_Exception $ae ) {
277 + } catch ( Auth_Error $ae ) {
233 278 Module::load_template( $context, 'error', array_merge(
234 279 $global_context, [
235 280 'action' => 'error',
236 - 'message' => esc_html__( 'N/A', 'image-optimizer' ),
281 + 'optimization_error_type' => Image_Optimization_Error_Type::AUTH_ERROR,
282 + 'message' => $ae->getMessage(),
237 283 'allow_retry' => false,
238 284 ]
239 285 ) );
240 286 } catch ( Throwable $t ) {
@@ -240,9 +286,10 @@
240 286 } catch ( Throwable $t ) {
241 287 Module::load_template( $context, 'error', array_merge(
242 288 $global_context, [
243 289 'action' => 'error',
244 - 'message' => esc_html__( 'Internal server error', 'image-optimizer' ),
290 + 'optimization_error_type' => Image_Optimization_Error_Type::GENERIC,
291 + 'message' => esc_html__( 'Internal server error', 'image-optimization' ),
245 292 'allow_retry' => false,
246 293 ]
247 294 ) );
248 295 }