| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Backups\Components; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook; |
| 6 |
use ImageOptimization\Classes\Image\{ |
| 7 |
Image_Meta, |
| 8 |
Image_Restore, |
| 9 |
Image_Status, |
| 10 |
}; |
| 11 |
|
| 12 |
use ImageOptimization\Classes\Logger; |
| 13 |
use Throwable; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Restore_Images { |
| 20 |
/** @async */ |
| 21 |
public function restore_image( int $image_id ) { |
| 22 |
try { |
| 23 |
Image_Restore::restore( $image_id ); |
| 24 |
} catch ( Throwable $t ) { |
| 25 |
Logger::log( Logger::LEVEL_ERROR, 'Image restoring error: ' . $t->getMessage() ); |
| 26 |
|
| 27 |
( new Image_Meta( $image_id ) ) |
| 28 |
->set_status( Image_Status::RESTORING_FAILED ) |
| 29 |
->save(); |
| 30 |
|
| 31 |
throw $t; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** @async */ |
| 36 |
public function restore_many_images( array $attachment_ids ) { |
| 37 |
Image_Restore::restore_many( $attachment_ids ); |
| 38 |
} |
| 39 |
|
| 40 |
public function __construct() { |
| 41 |
add_action( Async_Operation_Hook::RESTORE_SINGLE_IMAGE, [ $this, 'restore_image' ] ); |
| 42 |
add_action( Async_Operation_Hook::RESTORE_MANY_IMAGES, [ $this, 'restore_many_images' ] ); |
| 43 |
} |
| 44 |
} |
| 45 |
|