PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.0
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 / backups / classes / restore-images.php

restore-images.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.0.0, at modules/backups/classes/restore-images.php

112 lines 2.5 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\Backups\Classes;
4
5 use ImageOptimizer\Classes\Async_Operation\{
6 Async_Operation,
7 Async_Operation_Hook,
8 Async_Operation_Queue,
9 };
10 use ImageOptimizer\Classes\Image\{
11 Image_Meta,
12 Image_Query_Builder,
13 Image_Status
14 };
15 use Throwable;
16
17 class Restore_Images {
18 private const CHUNK_SIZE = 100;
19
20 /**
21 * Schedules a single restoring operation.
22 *
23 * @param int $image_id
24 * @return void
25 * @throws Throwable
26 * @throws \ImageOptimizer\Classes\Async_Operation\Exceptions\Async_Operation_Exception
27 */
28 public static function schedule_single_restoring( int $image_id ): void {
29 $meta = new Image_Meta( $image_id );
30
31 try {
32 $meta
33 ->set_status( Image_Status::RESTORING_IN_PROGRESS )
34 ->save();
35
36 Async_Operation::create(
37 Async_Operation_Hook::RESTORE_SINGLE_IMAGE,
38 [ 'attachment_id' => $image_id ],
39 Async_Operation_Queue::RESTORE
40 );
41 } catch ( Throwable $t ) {
42 $meta
43 ->set_status( Image_Status::RESTORING_FAILED )
44 ->save();
45
46 throw $t;
47 }
48 }
49
50 /**
51 * Schedules restoring operations for all valid images with backups created.
52 *
53 * @param array $image_ids
54 *
55 * @return void
56 */
57 public static function find_and_schedule_restoring( array $image_ids = [] ): void {
58 $query = ( new Image_Query_Builder() )
59 ->return_images_only_with_backups();
60
61 if ( ! empty( $image_ids ) ) {
62 $query->set_image_ids( $image_ids );
63 }
64
65 $query = $query->execute();
66 $attachment_ids = $query->posts;
67 $chunks = array_chunk( $attachment_ids, self::CHUNK_SIZE );
68
69 foreach ( $chunks as $chunk ) {
70 try {
71 Async_Operation::create(
72 Async_Operation_Hook::RESTORE_MANY_IMAGES,
73 [ 'attachment_ids' => $chunk ],
74 Async_Operation_Queue::RESTORE
75 );
76
77 self::mark_chunk_as_in_progress( $chunk );
78 } catch ( Throwable $t ) {
79 self::fail_restoring_for_chunk( $chunk );
80 }
81 }
82 }
83
84 /**
85 * Updates chunk images optimization status to in-progress.
86 *
87 * @param int[] $chunk
88 * @return void
89 */
90 private static function mark_chunk_as_in_progress( array $chunk ) {
91 foreach ( $chunk as $image_id ) {
92 ( new Image_Meta( $image_id ) )
93 ->set_status( Image_Status::RESTORING_IN_PROGRESS )
94 ->save();
95 }
96 }
97
98 /**
99 * Updates chunk images optimization status to failed.
100 *
101 * @param int[] $chunk
102 * @return void
103 */
104 private static function fail_restoring_for_chunk( array $chunk ) {
105 foreach ( $chunk as $image_id ) {
106 ( new Image_Meta( $image_id ) )
107 ->set_status( Image_Status::RESTORING_FAILED )
108 ->save();
109 }
110 }
111 }
112