image-optimization
/
modules
/
optimization
/
classes
/
bulk-optimization
/
bulk-optimization-token-manager.php
bulk-optimization-token-manager.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at modules/optimization/classes/bulk-optimization/bulk-optimization-token-manager.php
| 1 | <?php |
| 2 | |
| 3 | namespace ImageOptimization\Modules\Optimization\Classes\Bulk_Optimization; |
| 4 | |
| 5 | use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error; |
| 6 | use ImageOptimization\Classes\Logger; |
| 7 | use ImageOptimization\Classes\Utils; |
| 8 | use ImageOptimization\Modules\Optimization\Classes\Exceptions\Bulk_Token_Obtaining_Error; |
| 9 | use Throwable; |
| 10 | |
| 11 | // @codeCoverageIgnoreStart |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | // @codeCoverageIgnoreEnd |
| 16 | |
| 17 | final class Bulk_Optimization_Token_Manager { |
| 18 | private const OBTAIN_TOKEN_ENDPOINT = 'image/bulk-token'; |
| 19 | |
| 20 | /** |
| 21 | * Sends a request to the BE to obtain bulk optimization token. |
| 22 | * It prevents obtaining a token for each and every optimization operation. |
| 23 | * |
| 24 | * @param int $images_count Total number of images to optimize. |
| 25 | * @param int|null $max_batch_size Maximum batch size to try (from previous successful attempt). |
| 26 | * @return array |
| 27 | * |
| 28 | * @throws Quota_Exceeded_Error |
| 29 | */ |
| 30 | public static function obtain_token( int $images_count, int $max_batch_size = null ): array { |
| 31 | $base_sequence = [ $images_count, intval( $images_count / 2 ), 100, 50, 25, 10, 5, 1 ]; |
| 32 | |
| 33 | if ( null !== $max_batch_size ) { |
| 34 | $base_sequence = array_filter( $base_sequence, function( $size ) use ( $max_batch_size ) { |
| 35 | return $size <= $max_batch_size; |
| 36 | } ); |
| 37 | } |
| 38 | |
| 39 | $batch_size_sequence = array_unique( $base_sequence, SORT_NUMERIC ); |
| 40 | |
| 41 | foreach ( $batch_size_sequence as $batch_size ) { |
| 42 | if ( $images_count < $batch_size ) { |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | $token = self::request_token_for_count( $batch_size ); |
| 48 | |
| 49 | return [ |
| 50 | 'token' => $token, |
| 51 | 'batch_size' => $batch_size, |
| 52 | ]; |
| 53 | } catch ( Quota_Exceeded_Error |Bulk_Token_Obtaining_Error $te ) { |
| 54 | Logger::debug( "Quota exceeded for batch size {$batch_size}, trying smaller batch" ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | throw new Quota_Exceeded_Error( esc_html__( 'Images quota exceeded', 'image-optimization' ) ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Makes the actual API request for a token. |
| 63 | * |
| 64 | * @param int $count Number of images to request token for. |
| 65 | * @return string |
| 66 | * |
| 67 | * @throws Quota_Exceeded_Error |
| 68 | * @throws Bulk_Token_Obtaining_Error |
| 69 | */ |
| 70 | private static function request_token_for_count( int $count ): string { |
| 71 | try { |
| 72 | $response = Utils::get_api_client()->make_request( |
| 73 | 'POST', |
| 74 | self::OBTAIN_TOKEN_ENDPOINT, |
| 75 | [ |
| 76 | 'images_count' => $count, |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | if ( empty( $response->token ) ) { |
| 81 | throw new Bulk_Token_Obtaining_Error( 'Token not returned in response' ); |
| 82 | } |
| 83 | |
| 84 | return $response->token; |
| 85 | } catch ( Quota_Exceeded_Error $qee ) { |
| 86 | throw $qee; |
| 87 | } catch ( Throwable $t ) { |
| 88 | Logger::error( 'Error while sending bulk token request: ' . $t->getMessage() ); |
| 89 | |
| 90 | throw new Bulk_Token_Obtaining_Error( esc_html( $t->getMessage() ) ); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 |