| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimizer\Modules\Stats\Classes; |
| 4 |
|
| 5 |
use ImageOptimizer\Classes\Image\{ |
| 6 |
Image, |
| 7 |
Image_Meta, |
| 8 |
Image_Query_Builder, |
| 9 |
WP_Image_Meta, |
| 10 |
Exceptions\Invalid_Image_Exception |
| 11 |
}; |
| 12 |
use ImageOptimizer\Classes\Logger; |
| 13 |
use ImageOptimizer\Modules\Settings\Classes\Settings; |
| 14 |
|
| 15 |
class Optimization_Stats { |
| 16 |
const PAGING_SIZE = 25000; |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns image stats. |
| 20 |
* If the library is too big, it queries images in chunks. |
| 21 |
* |
| 22 |
* @return array{total_image_count: int, optimized_image_count: int, current_image_size: int, initial_image_size: int} |
| 23 |
*/ |
| 24 |
public static function get_image_stats( ?int $image_id = null ): array { |
| 25 |
$output = self::get_image_stats_chunk( 1, $image_id ); |
| 26 |
$pages_count = $output['pages']; |
| 27 |
|
| 28 |
if ( $pages_count > 1 ) { |
| 29 |
// $i initially is 2 bc we already got the first page, so we don't have to query it again |
| 30 |
for ( $i = 2; $i <= $pages_count; $i ++ ) { |
| 31 |
$chunk = self::get_image_stats_chunk( $i ); |
| 32 |
|
| 33 |
foreach ( array_keys( $chunk ) as $key ) { |
| 34 |
if ( isset( $output[ $key ] ) ) { |
| 35 |
$output[ $key ] += $chunk[ $key ]; |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
$output[ $key ] = $chunk[ $key ]; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
unset( $output['pages'] ); |
| 45 |
|
| 46 |
return $output; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @return array{pages: int, total_image_count: int, optimized_image_count: int, current_image_size: int, initial_image_size: int} |
| 51 |
*/ |
| 52 |
public static function get_image_stats_chunk( int $paged = 1, ?int $image_id = null ): array { |
| 53 |
$output = [ |
| 54 |
'pages' => 1, |
| 55 |
'total_image_count' => 0, |
| 56 |
'optimized_image_count' => 0, |
| 57 |
'current_image_size' => 0, |
| 58 |
'initial_image_size' => 0, |
| 59 |
]; |
| 60 |
|
| 61 |
$query = ( new Image_Query_Builder() ) |
| 62 |
->set_paging_size( self::PAGING_SIZE ) |
| 63 |
->set_current_page( $paged ); |
| 64 |
|
| 65 |
if ( $image_id ) { |
| 66 |
$query->set_image_ids( [ $image_id ] ); |
| 67 |
} |
| 68 |
|
| 69 |
$query = $query->execute(); |
| 70 |
|
| 71 |
$output['pages'] = $query->max_num_pages; |
| 72 |
|
| 73 |
foreach ( $query->posts as $attachment_id ) { |
| 74 |
try { |
| 75 |
$wp_meta = new WP_Image_Meta( $attachment_id ); |
| 76 |
} catch ( Invalid_Image_Exception $ii ) { |
| 77 |
Logger::log( Logger::LEVEL_ERROR, $ii->getMessage() ); |
| 78 |
|
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
$meta = new Image_Meta( $attachment_id ); |
| 83 |
$image_sizes = $wp_meta->get_size_keys(); |
| 84 |
|
| 85 |
$current_sizes = self::filter_only_enabled_sizes( $image_sizes ); |
| 86 |
$optimized_sizes = self::filter_only_enabled_sizes( $meta->get_optimized_sizes() ); |
| 87 |
|
| 88 |
$output['total_image_count'] += count( $current_sizes ); |
| 89 |
$output['optimized_image_count'] += count( $optimized_sizes ); |
| 90 |
|
| 91 |
foreach ( $image_sizes as $image_size ) { |
| 92 |
$output['current_image_size'] += $wp_meta->get_file_size( $image_size ); |
| 93 |
$output['initial_image_size'] += $meta->get_original_file_size( $image_size ) ?? $wp_meta->get_file_size( $image_size ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $output; |
| 98 |
} |
| 99 |
|
| 100 |
private static function filter_only_enabled_sizes( array $size_keys ): array { |
| 101 |
$enabled_sizes = Settings::get( Settings::CUSTOM_SIZES_OPTION_NAME ); |
| 102 |
|
| 103 |
if ( 'all' === $enabled_sizes ) { |
| 104 |
return $size_keys; |
| 105 |
} |
| 106 |
|
| 107 |
return array_filter($size_keys, function( string $size ) use ( $enabled_sizes ) { |
| 108 |
if ( Image::SIZE_FULL === $size ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
if ( in_array( $size, $enabled_sizes, true ) ) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
}); |
| 118 |
} |
| 119 |
} |
| 120 |
|