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