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