PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
← All changes | classes/Optimization/Data/AbstractData.php +43 -28 1.10 → trunk View file →
@@ -2,10 +2,8 @@
2 2 namespace Imagify\Optimization\Data;
3 3
4 4 use Imagify\Media\MediaInterface;
5 5
6 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
7 -
8 6 /**
9 7 * Abstract class used to handle the optimization data of "media groups" (aka attachments).
10 8 *
11 9 * @since 1.9
@@ -23,12 +21,13 @@
23 21 * @see $this->get_optimization_data()
24 22 * @author Grégory Viguier
25 23 */
26 24 protected $default_optimization_data = [
27 - 'status' => '',
28 - 'level' => false,
29 - 'sizes' => [],
30 - 'stats' => [
25 + 'status' => '',
26 + 'message' => '',
27 + 'level' => false,
28 + 'sizes' => [],
29 + 'stats' => [
31 30 'original_size' => 0,
32 31 'optimized_size' => 0,
33 32 'percent' => 0,
34 33 ],
@@ -231,9 +230,9 @@
231 230 }
232 231
233 232 foreach ( $data['sizes'] as $size ) {
234 233 if ( ! empty( $size['success'] ) ) {
235 - $count++;
234 + ++$count;
236 235 }
237 236 }
238 237
239 238 return $count;
@@ -279,9 +278,9 @@
279 278 }
280 279
281 280 /**
282 281 * Get the file size of the full size file.
283 - * If the WebP size is available, it is used.
282 + * If the Nextgen size is available, it is used.
284 283 *
285 284 * @since 1.9
286 285 * @access public
287 286 * @author Grégory Viguier
@@ -287,30 +286,38 @@
287 286 * @author Grégory Viguier
288 287 *
289 288 * @param bool $human_format True to display the image human format size (1Mb).
290 289 * @param int $decimals Precision of number of decimal places.
291 - * @param bool $use_webp Use the WebP size if available.
290 + * @param bool $use_nextgen Use the Nextgen size if available.
292 291 * @return string|int
293 292 */
294 - public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ) {
293 + public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = true ) {
295 294 if ( ! $this->is_valid() ) {
296 295 return $human_format ? imagify_size_format( 0, $decimals ) : 0;
297 296 }
298 297
299 - $data = $this->get_optimization_data();
300 - $media = $this->get_media();
298 + $data = $this->get_optimization_data();
299 + $media = $this->get_media();
300 + $format = 'webp';
301 301
302 - if ( $use_webp ) {
303 - $process_class_name = imagify_get_optimization_process_class_name( $media->get_context() );
304 - $webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
305 - }
302 + $process_class_name = imagify_get_optimization_process_class_name( $media->get_context() );
303 + $nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' );
304 + $nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
306 305
307 - if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['optimized_size'] ) ) {
308 - $size = (int) $data['sizes'][ $webp_size_name ]['optimized_size'];
306 + $size = 0;
307 +
308 + if ( $use_nextgen ) {
309 + /**Checking for success status before size, some cases the response is false
310 + * because the image is already compressed, or we have a connection timed out
311 + * */
312 + $size = ! empty( $data['sizes'][ $nextgen_webp_size_name ] ) && $data['sizes'][ $nextgen_webp_size_name ]['success'] ?
313 + (int) $data['sizes'][ $nextgen_webp_size_name ]['optimized_size'] : 0;
314 + if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['optimized_size'] ) &&
315 + $data['sizes'][ $nextgen_avif_size_name ] ) {
316 + $size = (int) $data['sizes'][ $nextgen_avif_size_name ]['optimized_size'];
317 + }
309 318 } elseif ( ! empty( $data['sizes']['full']['optimized_size'] ) ) {
310 319 $size = (int) $data['sizes']['full']['optimized_size'];
311 - } else {
312 - $size = 0;
313 320 }
314 321
315 322 if ( $size ) {
316 323 return $human_format ? imagify_size_format( $size, $decimals ) : $size;
@@ -318,12 +325,15 @@
318 325
319 326 // If nothing in the database, try to get the info from the file.
320 327 $filepath = false;
321 328
322 - if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['success'] ) ) {
323 - // Try with the WebP file first.
329 + if ( $use_nextgen ) {
330 + if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['success'] ) ) {
331 + $format = 'avif';
332 + }
333 + // Try with the Nextgen file first.
324 334 $filepath = $media->get_raw_fullsize_path();
325 - $filepath = $filepath ? imagify_path_to_webp( $filepath ) : false;
335 + $filepath = $filepath ? imagify_path_to_nextgen( $filepath, $format ) : false;
326 336
327 337 if ( ! $filepath || ! $this->filesystem->exists( $filepath ) ) {
328 338 $filepath = false;
329 339 }
@@ -329,9 +339,9 @@
329 339 }
330 340 }
331 341
332 342 if ( ! $filepath ) {
333 - // No WebP? The full size then.
343 + // No Nextgen? The full size then.
334 344 $filepath = $media->get_fullsize_path();
335 345 }
336 346
337 347 if ( ! $filepath ) {
@@ -415,17 +425,22 @@
415 425 if ( ! $this->is_valid() ) {
416 426 return round( (float) 0, 2 );
417 427 }
418 428
419 - $process_class_name = imagify_get_optimization_process_class_name( $this->get_media()->get_context() );
420 - $webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
429 + $process_class_name = imagify_get_optimization_process_class_name( $this->get_media()->get_context() );
430 + $nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
431 + $nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' );
421 432
422 - $percent = $this->get_size_data( $webp_size_name, 'percent' );
433 + $percent = $this->get_size_data( $nextgen_avif_size_name, 'percent' );
423 434
435 + // Check for webp version if avif is not found.
424 436 if ( ! $percent ) {
437 + $percent = $this->get_size_data( $nextgen_webp_size_name, 'percent' );
438 + }
439 +
440 + if ( ! $percent ) {
425 441 $percent = $this->get_size_data( 'full', 'percent' );
426 442 }
427 -
428 443 $percent = $percent ? $percent : 0;
429 444
430 445 return round( (float) $percent, 2 );
431 446 }