PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.1
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / modules / optimization / components / bulk-optimization.php

bulk-optimization.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.0.1, at modules/optimization/components/bulk-optimization.php

116 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimizer\Modules\Optimization\Components;
4
5 use ImageOptimizer\Classes\Async_Operation\Async_Operation_Hook;
6 use ImageOptimizer\Classes\Image\{
7 Image,
8 Image_Meta,
9 Image_Optimization_Error_Type,
10 Image_Restore,
11 Image_Status
12 };
13 use ImageOptimizer\Classes\Logger;
14 use ImageOptimizer\Modules\Oauth\Classes\Exceptions\Quota_Exceeded_Error;
15 use ImageOptimizer\Modules\Optimization\Classes\Optimize_Image;
16 use ImageOptimizer\Modules\Optimization\Classes\Bulk_Optimization as Bulk_Optimization_Controller;
17 use Throwable;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // Exit if accessed directly.
21 }
22
23 class Bulk_Optimization {
24 const BULK_OPTIMIZATION_BASE_SLUG = 'image-optimizer-bulk-optimization';
25 const BULK_OPTIMIZATION_CAPABILITY = 'manage_options';
26
27 public function render_app() {
28 ?>
29 <!-- The hack required to wrap WP notifications -->
30 <div class="wrap">
31 <h1 style="display: none;" role="presentation"></h1>
32 </div>
33
34 <div id="image-optimizer-app"></div>
35 <?php
36 }
37
38 public function register_page() {
39 add_media_page(
40 '',
41 __( 'Bulk Optimization', 'image-optimizer' ),
42 self::BULK_OPTIMIZATION_CAPABILITY,
43 self::BULK_OPTIMIZATION_BASE_SLUG,
44 [ $this, 'render_app' ],
45 7
46 );
47 }
48
49 /** @async */
50 public function optimize_bulk( int $image_id, string $operation_id ) {
51 try {
52 $bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id );
53
54 $oi = new Optimize_Image(
55 $image_id,
56 'bulk',
57 $bulk_token
58 );
59
60 $oi->optimize();
61 } catch ( Quota_Exceeded_Error $qe ) {
62 ( new Image_Meta( $image_id ) )
63 ->set_status( Image_Status::OPTIMIZATION_FAILED )
64 ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
65 ->save();
66 } catch ( Throwable $t ) {
67 Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() );
68
69 ( new Image_Meta( $image_id ) )
70 ->set_status( Image_Status::OPTIMIZATION_FAILED )
71 ->set_error_type( Image_Optimization_Error_Type::GENERIC )
72 ->save();
73 }
74 }
75
76 /** @async */
77 public function reoptimize_bulk( int $image_id, string $operation_id ) {
78 try {
79 $image = new Image( $image_id );
80
81 if ( $image->can_be_restored() ) {
82 Image_Restore::restore( $image_id, true );
83 }
84
85 $bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id );
86
87 $oi = new Optimize_Image(
88 $image_id,
89 'bulk',
90 $bulk_token
91 );
92
93 $oi->optimize();
94 } catch ( Quota_Exceeded_Error $qe ) {
95 ( new Image_Meta( $image_id ) )
96 ->set_status( Image_Status::REOPTIMIZING_FAILED )
97 ->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
98 ->save();
99 } catch ( Throwable $t ) {
100 Logger::log( Logger::LEVEL_ERROR, 'Reoptimization error. Reason: ' . $t->getMessage() );
101
102 ( new Image_Meta( $image_id ) )
103 ->set_status( Image_Status::REOPTIMIZING_FAILED )
104 ->set_error_type( Image_Optimization_Error_Type::GENERIC )
105 ->save();
106 }
107 }
108
109 public function __construct() {
110 add_action( 'admin_menu', [ $this, 'register_page' ] );
111
112 add_action( Async_Operation_Hook::OPTIMIZE_BULK, [ $this, 'optimize_bulk' ], 10, 2 );
113 add_action( Async_Operation_Hook::REOPTIMIZE_BULK, [ $this, 'reoptimize_bulk' ], 10, 2 );
114 }
115 }
116