image-optimization
/
modules
/
optimization
/
classes
/
bulk-optimization
/
bulk-optimization-image-query.php
bulk-optimization-image-query.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at modules/optimization/classes/bulk-optimization/bulk-optimization-image-query.php
| 1 | <?php |
| 2 | |
| 3 | namespace ImageOptimization\Modules\Optimization\Classes\Bulk_Optimization; |
| 4 | |
| 5 | use ImageOptimization\Classes\Image\{ |
| 6 | Image_Meta, |
| 7 | Image_Query_Builder, |
| 8 | Exceptions\Invalid_Image_Exception, |
| 9 | WP_Image_Meta, |
| 10 | }; |
| 11 | |
| 12 | use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error; |
| 13 | use ImageOptimization\Modules\Core\Module as Core_Module; |
| 14 | use ImageOptimization\Modules\Optimization\Classes\{ |
| 15 | Validate_Image, |
| 16 | Exceptions\Image_Validation_Error |
| 17 | }; |
| 18 | use ImageOptimization\Modules\Settings\Classes\Settings; |
| 19 | use ImageOptimization\Plugin; |
| 20 | |
| 21 | // @codeCoverageIgnoreStart |
| 22 | if ( ! defined( 'ABSPATH' ) ) { |
| 23 | exit; // Exit if accessed directly. |
| 24 | } |
| 25 | // @codeCoverageIgnoreEnd |
| 26 | |
| 27 | final class Bulk_Optimization_Image_Query { |
| 28 | /** |
| 29 | * Looks for images for bulk optimization operations based on a query passed and the quota left. |
| 30 | * |
| 31 | * @param Image_Query_Builder $query Image query to execute. |
| 32 | * @param bool $limit_to_quota If true, it limits image query to the quota left. |
| 33 | * @return array{total_images_count: int, attachments_in_quota: array, attachments_out_of_quota: array} |
| 34 | * |
| 35 | * @throws Invalid_Image_Exception |
| 36 | * @throws Quota_Exceeded_Error |
| 37 | */ |
| 38 | public static function find_images( Image_Query_Builder $query, bool $limit_to_quota = false ): array { |
| 39 | $output = [ |
| 40 | 'total_images_count' => 0, |
| 41 | 'attachments_in_quota' => [], |
| 42 | 'attachments_out_of_quota' => [], |
| 43 | ]; |
| 44 | |
| 45 | if ( ! Core_Module::is_elementor_one() ) { |
| 46 | $images_left = Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->images_left(); |
| 47 | |
| 48 | if ( ! $images_left ) { |
| 49 | throw new Quota_Exceeded_Error( esc_html__( 'Images quota exceeded', 'image-optimization' ) ); |
| 50 | } |
| 51 | |
| 52 | if ( $limit_to_quota ) { |
| 53 | $query->set_paging_size( $images_left ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $wp_query = $query->execute(); |
| 58 | |
| 59 | if ( ! $wp_query->post_count ) { |
| 60 | return $output; |
| 61 | } |
| 62 | |
| 63 | foreach ( $wp_query->posts as $attachment_id ) { |
| 64 | try { |
| 65 | Validate_Image::is_valid( $attachment_id ); |
| 66 | $wp_meta = new WP_Image_Meta( $attachment_id ); |
| 67 | } catch ( Invalid_Image_Exception |Image_Validation_Error $ie ) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | $sizes_count = count( $wp_meta->get_size_keys() ); |
| 72 | |
| 73 | if ( ! Core_Module::is_elementor_one() ) { |
| 74 | if ( $output['total_images_count'] + $sizes_count <= $images_left ) { |
| 75 | $output['total_images_count'] += $sizes_count; |
| 76 | |
| 77 | $output['attachments_in_quota'][] = $attachment_id; |
| 78 | } else { |
| 79 | break; |
| 80 | } |
| 81 | } else { |
| 82 | $output['total_images_count'] += $sizes_count; |
| 83 | $output['attachments_in_quota'][] = $attachment_id; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $output['attachments_out_of_quota'] = array_diff( $wp_query->posts, $output['attachments_in_quota'] ); |
| 88 | |
| 89 | return $output; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Looks for images that were optimized, but not all their sizes were processed. |
| 94 | * |
| 95 | * @return Image_Query_Builder |
| 96 | */ |
| 97 | public static function query_not_fully_optimized_images(): Image_Query_Builder { |
| 98 | $result = []; |
| 99 | $sizes_enabled = Settings::get( Settings::CUSTOM_SIZES_OPTION_NAME ); |
| 100 | $optimized_images = ( new Image_Query_Builder() ) |
| 101 | ->return_optimized_images() |
| 102 | ->execute(); |
| 103 | |
| 104 | foreach ( $optimized_images->posts as $attachment_id ) { |
| 105 | try { |
| 106 | $image_meta = new Image_Meta( $attachment_id ); |
| 107 | $wp_meta = new WP_Image_Meta( $attachment_id ); |
| 108 | } catch ( Invalid_Image_Exception $iie ) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | $registered_sizes = $wp_meta->get_size_keys(); |
| 113 | $optimized_sizes = $image_meta->get_optimized_sizes(); |
| 114 | |
| 115 | if ( 'all' !== $sizes_enabled ) { |
| 116 | $registered_sizes = array_filter( $registered_sizes, function( $size ) use ( $sizes_enabled ) { |
| 117 | return in_array( $size, $sizes_enabled, true ); |
| 118 | } ); |
| 119 | |
| 120 | $optimized_sizes = array_filter( $optimized_sizes, function( $size ) use ( $sizes_enabled ) { |
| 121 | return in_array( $size, $sizes_enabled, true ); |
| 122 | } ); |
| 123 | } |
| 124 | |
| 125 | if ( count( $optimized_sizes ) < count( $registered_sizes ) ) { |
| 126 | $result[] = $attachment_id; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return ( new Image_Query_Builder() ) |
| 131 | ->set_image_ids( $result ); |
| 132 | } |
| 133 | } |
| 134 |