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