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 | inc/functions/admin-stats.php +165 -105 1.10 → trunk View file →
@@ -1,6 +1,6 @@
1 1 <?php
2 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
2 +defined( 'ABSPATH' ) || exit;
3 3
4 4 /**
5 5 * Count number of attachments.
6 6 *
@@ -32,11 +32,24 @@
32 32 }
33 33
34 34 $mime_types = Imagify_DB::get_mime_types();
35 35 $statuses = Imagify_DB::get_post_statuses();
36 - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
37 - $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
38 - $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
36 + $nodata_join = '';
37 + $nodata_where = '';
38 +
39 + if ( ! imagify_has_attachments_without_required_metadata() ) {
40 + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(
41 + 'p.ID',
42 + true,
43 + true,
44 + "AND p.post_mime_type IN ( $mime_types )
45 + AND p.post_type = 'attachment'
46 + AND p.post_status IN ( $statuses )"
47 + );
48 + $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
49 + }
50 +
51 + $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
39 52 "
40 53 SELECT COUNT( p.ID )
41 54 FROM $wpdb->posts AS p
42 55 $nodata_join
@@ -87,22 +100,31 @@
87 100 }
88 101
89 102 $mime_types = Imagify_DB::get_mime_types();
90 103 $statuses = Imagify_DB::get_post_statuses();
91 - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
92 - $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
93 - $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
104 + $nodata_join = '';
105 + $nodata_where = '';
106 +
107 + if ( ! imagify_has_attachments_without_required_metadata() ) {
108 + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
109 + $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
110 + }
111 +
112 + $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
94 113 "
95 - SELECT COUNT( DISTINCT p.ID )
96 - FROM $wpdb->posts AS p
97 - $nodata_join
98 - INNER JOIN $wpdb->postmeta AS mt1
99 - ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
100 - WHERE p.post_mime_type IN ( $mime_types )
101 - AND p.post_type = 'attachment'
102 - AND p.post_status IN ( $statuses )
103 - AND mt1.meta_value = 'error'
104 - $nodata_where"
114 + SELECT COUNT(*)
115 + FROM (
116 + SELECT p.ID
117 + FROM $wpdb->posts AS p
118 + $nodata_join
119 + INNER JOIN $wpdb->postmeta AS mt1
120 + ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
121 + WHERE p.post_mime_type IN ( $mime_types )
122 + AND p.post_type = 'attachment'
123 + AND p.post_status IN ( $statuses )
124 + AND mt1.meta_value = 'error'
125 + $nodata_where GROUP BY p.ID
126 + ) AS imagify_count_error"
105 127 );
106 128
107 129 return $count;
108 130 }
@@ -138,15 +160,21 @@
138 160 }
139 161
140 162 $mime_types = Imagify_DB::get_mime_types();
141 163 $statuses = Imagify_DB::get_post_statuses();
142 - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
143 - $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
144 - $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
164 + $nodata_join = '';
165 + $nodata_where = '';
166 +
167 + if ( ! imagify_has_attachments_without_required_metadata() ) {
168 + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
169 + $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
170 + }
171 +
172 + $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
145 173 "
146 174 SELECT COUNT( DISTINCT p.ID )
147 175 FROM $wpdb->posts AS p
148 - $nodata_join
176 + $nodata_join
149 177 INNER JOIN $wpdb->postmeta AS mt1
150 178 ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
151 179 WHERE p.post_mime_type IN ( $mime_types )
152 180 AND p.post_type = 'attachment'
@@ -273,9 +301,9 @@
273 301 ++$count;
274 302 $original_data = $attachment_data['sizes']['full'];
275 303
276 304 // Increment the original sizes.
277 - $original_size += $original_data['original_size'] ? $original_data['original_size'] : 0;
305 + $original_size += $original_data['original_size'] ? $original_data['original_size'] : 0;
278 306 $optimized_size += $original_data['optimized_size'] ? $original_data['optimized_size'] : 0;
279 307
280 308 unset( $attachment_data['sizes']['full'] );
281 309
@@ -282,9 +310,9 @@
282 310 // Increment the thumbnails sizes.
283 311 if ( $attachment_data['sizes'] ) {
284 312 foreach ( $attachment_data['sizes'] as $size_data ) {
285 313 if ( ! empty( $size_data['success'] ) ) {
286 - $original_size += $size_data['original_size'] ? $size_data['original_size'] : 0;
314 + $original_size += $size_data['original_size'] ? $size_data['original_size'] : 0;
287 315 $optimized_size += $size_data['optimized_size'] ? $size_data['optimized_size'] : 0;
288 316 }
289 317 }
290 318 }
@@ -292,19 +320,27 @@
292 320 }
293 321 } else {
294 322 /**
295 323 * Filter the chunk size of the requests fetching the data.
296 - * 15,000 seems to be a good balance between memory used, speed, and number of DB hits.
324 + * 2,000 seems to be a good balance between memory used, speed, and number of DB hits,
325 + * while keeping the resulting `IN ()` clause well under hosts' SQL query size limits
326 + * (e.g. WP Engine's query governor).
297 327 *
298 328 * @param int $limit The maximum number of elements per chunk.
299 329 */
300 - $limit = apply_filters( 'imagify_count_saving_data_limit', 15000 );
330 + $limit = apply_filters( 'imagify_count_saving_data_limit', 2000 );
301 331 $limit = absint( $limit );
302 332
303 - $mime_types = Imagify_DB::get_mime_types();
304 - $statuses = Imagify_DB::get_post_statuses();
305 - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
306 - $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
333 + $mime_types = Imagify_DB::get_mime_types();
334 + $statuses = Imagify_DB::get_post_statuses();
335 + $nodata_join = '';
336 + $nodata_where = '';
337 +
338 + if ( ! imagify_has_attachments_without_required_metadata() ) {
339 + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
340 + $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
341 + }
342 +
307 343 $attachment_ids = $wpdb->get_col( // WPCS: unprepared SQL ok.
308 344 "
309 345 SELECT p.ID
310 346 FROM $wpdb->posts AS p
@@ -324,23 +360,32 @@
324 360 $attachment_ids = array_chunk( $attachment_ids, $limit );
325 361
326 362 while ( $attachment_ids ) {
327 363 $limit_ids = array_shift( $attachment_ids );
328 - $limit_ids = implode( ',', $limit_ids );
364 + // Safety net: further split the chunk if its rendered `IN ()` list would still be too long.
365 + $id_chunks = Imagify_DB::chunk_in_values( $limit_ids );
366 + $attachments = [];
329 367
330 - $attachments = $wpdb->get_col( // WPCS: unprepared SQL ok.
331 - "
332 - SELECT meta_value
333 - FROM $wpdb->postmeta
334 - WHERE post_id IN ( $limit_ids )
335 - AND meta_key = '_imagify_data'"
336 - );
368 + foreach ( $id_chunks as $id_chunk ) {
369 + $id_chunk_list = implode( ',', $id_chunk );
370 +
371 + $chunk_attachments = $wpdb->get_col( // WPCS: unprepared SQL ok.
372 + "
373 + SELECT meta_value
374 + FROM $wpdb->postmeta
375 + WHERE post_id IN ( $id_chunk_list )
376 + AND meta_key = '_imagify_data'"
377 + );
378 +
379 + if ( $chunk_attachments ) {
380 + $attachments = array_merge( $attachments, $chunk_attachments );
381 + }
382 + }
337 383 $wpdb->flush();
338 384
339 385 unset( $limit_ids );
340 386
341 387 if ( ! $attachments ) {
342 - // Uh?!
343 388 continue;
344 389 }
345 390
346 391 $attachments = array_map( 'maybe_unserialize', $attachments );
@@ -363,9 +408,9 @@
363 408
364 409 ++$count;
365 410
366 411 // Increment the original sizes.
367 - $original_size += ! empty( $original_data['original_size'] ) ? $original_data['original_size'] : 0;
412 + $original_size += ! empty( $original_data['original_size'] ) ? $original_data['original_size'] : 0;
368 413 $optimized_size += ! empty( $original_data['optimized_size'] ) ? $original_data['optimized_size'] : 0;
369 414
370 415 unset( $attachment_data['sizes']['full'], $original_data );
371 416
@@ -372,9 +417,9 @@
372 417 // Increment the thumbnails sizes.
373 418 if ( $attachment_data['sizes'] ) {
374 419 foreach ( $attachment_data['sizes'] as $size_data ) {
375 420 if ( ! empty( $size_data['success'] ) ) {
376 - $original_size += ! empty( $size_data['original_size'] ) ? $size_data['original_size'] : 0;
421 + $original_size += ! empty( $size_data['original_size'] ) ? $size_data['original_size'] : 0;
377 422 $optimized_size += ! empty( $size_data['optimized_size'] ) ? $size_data['optimized_size'] : 0;
378 423 }
379 424 }
380 425 }
@@ -382,17 +427,17 @@
382 427 unset( $size_data );
383 428 }
384 429
385 430 unset( $attachments, $attachment_data );
386 - } // End while().
387 - } // End if().
431 + }
432 + }
388 433
389 - $data = array(
434 + $data = [
390 435 'count' => $count,
391 436 'original_size' => $original_size,
392 437 'optimized_size' => $optimized_size,
393 438 'percent' => $original_size && $optimized_size ? ceil( ( ( $original_size - $optimized_size ) / $original_size ) * 100 ) : 0,
394 - );
439 + ];
395 440
396 441 if ( ! empty( $key ) ) {
397 442 return isset( $data[ $key ] ) ? $data[ $key ] : 0;
398 443 }
@@ -427,9 +472,10 @@
427 472 AND p.post_type = 'attachment'
428 473 AND p.post_status IN ( $statuses )
429 474 $nodata_where
430 475 LIMIT 250
431 - " );
476 + "
477 + );
432 478
433 479 if ( ! $image_ids ) {
434 480 return 0;
435 481 }
@@ -470,32 +516,38 @@
470 516 $nodata_where
471 517 %date_query%";
472 518
473 519 // Queries per month.
474 - $date_query = new WP_Date_Query( array(
475 - array(
476 - 'before' => 'now',
477 - 'after' => '1 month ago',
478 - ),
479 - ) );
520 + $date_query = new WP_Date_Query(
521 + [
522 + [
523 + 'before' => 'now',
524 + 'after' => '1 month ago',
525 + ],
526 + ]
527 + );
480 528
481 529 $partial_images_uploaded_last_month = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
482 530
483 - $date_query = new WP_Date_Query( array(
484 - array(
485 - 'before' => '1 month ago',
486 - 'after' => '2 months ago',
487 - ),
488 - ) );
531 + $date_query = new WP_Date_Query(
532 + [
533 + [
534 + 'before' => '1 month ago',
535 + 'after' => '2 months ago',
536 + ],
537 + ]
538 + );
489 539
490 540 $partial_images_uploaded_two_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
491 541
492 - $date_query = new WP_Date_Query( array(
493 - array(
494 - 'before' => '2 month ago',
495 - 'after' => '3 months ago',
496 - ),
497 - ) );
542 + $date_query = new WP_Date_Query(
543 + [
544 + [
545 + 'before' => '2 month ago',
546 + 'after' => '3 months ago',
547 + ],
548 + ]
549 + );
498 550
499 551 $partial_images_uploaded_three_months_ago = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query . $limit ) ); // WPCS: unprepared SQL ok.
500 552
501 553 // Total for the 3 months.
@@ -505,14 +557,16 @@
505 557 return 0;
506 558 }
507 559
508 560 // Total for the 3 months, without the "250" limit.
509 - $date_query = new WP_Date_Query( array(
510 - array(
511 - 'before' => 'now',
512 - 'after' => '3 month ago',
513 - ),
514 - ) );
561 + $date_query = new WP_Date_Query(
562 + [
563 + [
564 + 'before' => 'now',
565 + 'after' => '3 month ago',
566 + ],
567 + ]
568 + );
515 569
516 570 $images_uploaded_id = $wpdb->get_col( str_replace( '%date_query%', $date_query->get_sql(), $query ) ); // WPCS: unprepared SQL ok.
517 571
518 572 if ( ! $images_uploaded_id ) {
@@ -521,9 +575,9 @@
521 575
522 576 // Number of image attachments uploaded for the 3 latest months, limited to 250 per month.
523 577 $partial_total_images_uploaded = count( $partial_images_uploaded_id );
524 578 // Total number of image attachments uploaded for the 3 latest months.
525 - $total_images_uploaded = count( $images_uploaded_id );
579 + $total_images_uploaded = count( $images_uploaded_id );
526 580
527 581 return imagify_calculate_total_image_size( $partial_images_uploaded_id, $partial_total_images_uploaded, $total_images_uploaded ) / 3;
528 582 }
529 583
@@ -546,23 +600,26 @@
546 600 if ( ! $image_ids ) {
547 601 return 0;
548 602 }
549 603
550 - $results = Imagify_DB::get_metas( array(
551 - // Get attachments filename.
552 - 'filenames' => '_wp_attached_file',
553 - // Get attachments data.
554 - 'data' => '_wp_attachment_metadata',
555 - // Get Imagify data.
556 - 'imagify_data' => '_imagify_data',
557 - // Get attachments status.
558 - 'statuses' => '_imagify_status',
559 - ), $image_ids );
604 + $results = Imagify_DB::get_metas(
605 + [
606 + // Get attachments filename.
607 + 'filenames' => '_wp_attached_file',
608 + // Get attachments data.
609 + 'data' => '_wp_attachment_metadata',
610 + // Get Imagify data.
611 + 'imagify_data' => '_imagify_data',
612 + // Get attachments status.
613 + 'statuses' => '_imagify_status',
614 + ],
615 + $image_ids
616 + );
560 617
561 618 // Number of image attachments we're doing the calculation with. In case array_filter() removed results.
562 - $partial_total_images = count( $image_ids );
619 + $partial_total_images = count( $image_ids );
563 620 // Total size of unoptimized size.
564 - $partial_size_images = 0;
621 + $partial_size_images = 0;
565 622 // Total number of thumbnails.
566 623 $partial_total_intermediate_images = 0;
567 624
568 625 $filesystem = imagify_get_filesystem();
@@ -576,9 +633,9 @@
576 633 /**
577 634 * The image files have been optimized.
578 635 */
579 636 // Original size.
580 - $partial_size_images += isset( $results['imagify_data'][ $image_id ]['stats']['original_size'] ) ? $results['imagify_data'][ $image_id ]['stats']['original_size'] : 0;
637 + $partial_size_images += isset( $results['imagify_data'][ $image_id ]['stats']['original_size'] ) ? $results['imagify_data'][ $image_id ]['stats']['original_size'] : 0;
581 638 // Number of thumbnails.
582 639 $partial_total_intermediate_images += count( $results['imagify_data'][ $image_id ]['sizes'] );
583 640 unset(
584 641 $image_ids[ $i ],
@@ -593,13 +650,13 @@
593 650 /**
594 651 * The image files are not optimized.
595 652 */
596 653 // Create an array containing all this attachment files.
597 - $files = array(
654 + $files = [
598 655 'full' => get_imagify_attached_file( $results['filenames'][ $image_id ] ),
599 - );
656 + ];
600 657
601 - $sizes = isset( $results['data'][ $image_id ]['sizes'] ) ? $results['data'][ $image_id ]['sizes'] : array();
658 + $sizes = isset( $results['data'][ $image_id ]['sizes'] ) ? $results['data'][ $image_id ]['sizes'] : [];
602 659
603 660 if ( $sizes && is_array( $sizes ) ) {
604 661 if ( ! $is_active_for_network ) {
605 662 $sizes = array_diff_key( $sizes, $disallowed_sizes );
@@ -648,9 +705,9 @@
648 705 $results['data'][ $image_id ],
649 706 $results['imagify_data'][ $image_id ],
650 707 $results['statuses'][ $image_id ]
651 708 );
652 - } // End foreach().
709 + }
653 710
654 711 // Number of thumbnails per attachment = Number of thumbnails / Number of attachments.
655 712 $intermediate_images_per_image = $partial_total_intermediate_images / $partial_total_images;
656 713 /**
@@ -657,15 +714,15 @@
657 714 * Note: Number of attachments ($partial_total_images) === Number of full sizes.
658 715 * Average image size = Size of the images / ( Number of full sizes + Number of thumbnails ).
659 716 * Average image size = Size of the images / Number of images.
660 717 */
661 - $average_size_images = $partial_size_images / ( $partial_total_images + $partial_total_intermediate_images );
718 + $average_size_images = $partial_size_images / ( $partial_total_images + $partial_total_intermediate_images );
662 719 /**
663 720 * Note: Total number of attachments ($total_images) === Total number of full sizes.
664 721 * Total images size = Average image size * ( Total number of full sizes + ( Number of thumbnails per attachment * Total number of attachments ) ).
665 722 * Total images size = Average image size * ( Total number of full sizes + Total number of thumbnails ).
666 723 */
667 - $total_size_images = $average_size_images * ( $total_images + ( $intermediate_images_per_image * $total_images ) );
724 + $total_size_images = $average_size_images * ( $total_images + ( $intermediate_images_per_image * $total_images ) );
668 725
669 726 return $total_size_images;
670 727 }
671 728
@@ -683,16 +740,19 @@
683 740 * @type bool $formatting Some of the data is returned formatted.
684 741 * }
685 742 * @return array
686 743 */
687 -function imagify_get_bulk_stats( $types, $args = array() ) {
688 - $types = $types && is_array( $types ) ? $types : array();
689 - $args = array_merge( array(
690 - 'fullset' => false,
691 - 'formatting' => true,
692 - ), (array) $args );
744 +function imagify_get_bulk_stats( $types, $args = [] ) {
745 + $types = $types && is_array( $types ) ? $types : [];
746 + $args = array_merge(
747 + [
748 + 'fullset' => false,
749 + 'formatting' => true,
750 + ],
751 + (array) $args
752 + );
693 753
694 - $data = array(
754 + $data = [
695 755 // Global chart.
696 756 'total_attachments' => 0,
697 757 'unoptimized_attachments' => 0,
698 758 'optimized_attachments' => 0,
@@ -700,9 +760,9 @@
700 760 // Stats block.
701 761 'already_optimized_attachments' => 0,
702 762 'original_human' => 0,
703 763 'optimized_human' => 0,
704 - );
764 + ];
705 765
706 766 if ( isset( $types['library|wp'] ) ) {
707 767 /**
708 768 * Library.
@@ -709,12 +769,12 @@
709 769 */
710 770 $saving_data = imagify_count_saving_data();
711 771
712 772 // Global chart.
713 - $data['total_attachments'] += imagify_count_attachments();
714 - $data['unoptimized_attachments'] += imagify_count_unoptimized_attachments();
715 - $data['optimized_attachments'] += imagify_count_optimized_attachments();
716 - $data['errors_attachments'] += imagify_count_error_attachments();
773 + $data['total_attachments'] += imagify_count_attachments();
774 + $data['unoptimized_attachments'] += imagify_count_unoptimized_attachments();
775 + $data['optimized_attachments'] += imagify_count_optimized_attachments();
776 + $data['errors_attachments'] += imagify_count_error_attachments();
717 777 // Stats block.
718 778 $data['already_optimized_attachments'] += $saving_data['count'];
719 779 $data['original_human'] += $saving_data['original_size'];
720 780 $data['optimized_human'] += $saving_data['optimized_size'];
@@ -724,12 +784,12 @@
724 784 /**
725 785 * Custom folders.
726 786 */
727 787 // Global chart.
728 - $data['total_attachments'] += Imagify_Files_Stats::count_all_files();
729 - $data['unoptimized_attachments'] += Imagify_Files_Stats::count_no_status_files();
730 - $data['optimized_attachments'] += Imagify_Files_Stats::count_optimized_files();
731 - $data['errors_attachments'] += Imagify_Files_Stats::count_error_files();
788 + $data['total_attachments'] += Imagify_Files_Stats::count_all_files();
789 + $data['unoptimized_attachments'] += Imagify_Files_Stats::count_no_status_files();
790 + $data['optimized_attachments'] += Imagify_Files_Stats::count_optimized_files();
791 + $data['errors_attachments'] += Imagify_Files_Stats::count_error_files();
732 792 // Stats block.
733 793 $data['already_optimized_attachments'] += Imagify_Files_Stats::count_success_files();
734 794 $data['original_human'] += Imagify_Files_Stats::get_original_size();
735 795 $data['optimized_human'] += Imagify_Files_Stats::get_optimized_size();