Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../classes/image
File: image-restore.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Classes\Image;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\File_System\{
[4] Fix | Delete
Exceptions\File_System_Operation_Error,
[5] Fix | Delete
File_System,
[6] Fix | Delete
};
[7] Fix | Delete
use ImageOptimization\Classes\File_Utils;
[8] Fix | Delete
use ImageOptimization\Classes\Image\Exceptions\{
[9] Fix | Delete
Image_Restoring_Exception,
[10] Fix | Delete
Invalid_Image_Exception,
[11] Fix | Delete
};
[12] Fix | Delete
use ImageOptimization\Classes\Logger;
[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 Image_Restore {
[20] Fix | Delete
public static function restore_many( array $image_ids, bool $keep_image_meta = false ): void {
[21] Fix | Delete
foreach ( $image_ids as $image_id ) {
[22] Fix | Delete
try {
[23] Fix | Delete
self::restore( $image_id, $keep_image_meta );
[24] Fix | Delete
} catch ( Throwable $t ) {
[25] Fix | Delete
Logger::log( Logger::LEVEL_ERROR, 'Bulk images restoring error: ' . $t->getMessage() );
[26] Fix | Delete
[27] Fix | Delete
( new Image_Meta( $image_id ) )
[28] Fix | Delete
->set_status( Image_Status::RESTORING_FAILED )
[29] Fix | Delete
->save();
[30] Fix | Delete
[31] Fix | Delete
continue;
[32] Fix | Delete
}
[33] Fix | Delete
}
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* @throws Invalid_Image_Exception|Image_Restoring_Exception|File_System_Operation_Error
[38] Fix | Delete
*/
[39] Fix | Delete
public static function restore( int $image_id, bool $keep_image_meta = false ): void {
[40] Fix | Delete
$image = new Image( $image_id );
[41] Fix | Delete
[42] Fix | Delete
if ( ! $image->can_be_restored() ) {
[43] Fix | Delete
throw new Image_Restoring_Exception( "Image $image_id cannot be restored" );
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
$meta = new Image_Meta( $image_id );
[47] Fix | Delete
$wp_meta = new WP_Image_Meta( $image_id );
[48] Fix | Delete
[49] Fix | Delete
foreach ( $meta->get_optimized_sizes() as $image_size ) {
[50] Fix | Delete
$backup_path = $meta->get_image_backup_path( $image_size );
[51] Fix | Delete
$current_path = $image->get_file_path( $image_size );
[52] Fix | Delete
[53] Fix | Delete
if ( $backup_path && $current_path ) {
[54] Fix | Delete
$original_path = self::get_path_from_backup_path( $backup_path );
[55] Fix | Delete
[56] Fix | Delete
if ( $original_path === $backup_path ) {
[57] Fix | Delete
File_System::delete( $current_path, false, 'f' );
[58] Fix | Delete
[59] Fix | Delete
self::update_posts( $current_path, $original_path );
[60] Fix | Delete
} else {
[61] Fix | Delete
File_System::move( $backup_path, $original_path, $original_path === $current_path );
[62] Fix | Delete
[63] Fix | Delete
if ( $original_path !== $current_path ) {
[64] Fix | Delete
File_System::delete( $current_path, false, 'f' );
[65] Fix | Delete
[66] Fix | Delete
self::update_posts( $current_path, $original_path );
[67] Fix | Delete
}
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
$file_size = $meta->get_original_file_size( $image_size ) ?? File_System::size( $original_path );
[71] Fix | Delete
[72] Fix | Delete
$wp_meta
[73] Fix | Delete
->set_file_path( $image_size, $original_path )
[74] Fix | Delete
->set_mime_type( $image_size, $meta->get_original_mime_type( $image_size ) )
[75] Fix | Delete
->set_width( $image_size, $meta->get_original_width( $image_size ) )
[76] Fix | Delete
->set_height( $image_size, $meta->get_original_height( $image_size ) )
[77] Fix | Delete
->set_file_size( $image_size, $file_size );
[78] Fix | Delete
[79] Fix | Delete
if ( Image::SIZE_FULL === $image_size ) {
[80] Fix | Delete
self::update_image_post(
[81] Fix | Delete
$image,
[82] Fix | Delete
$original_path,
[83] Fix | Delete
$meta->get_original_mime_type( Image::SIZE_FULL )
[84] Fix | Delete
);
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
$wp_meta->save();
[90] Fix | Delete
[91] Fix | Delete
if ( $keep_image_meta ) {
[92] Fix | Delete
$meta
[93] Fix | Delete
->clear_optimized_sizes()
[94] Fix | Delete
->clear_backups()
[95] Fix | Delete
->clear_original_data()
[96] Fix | Delete
->save();
[97] Fix | Delete
[98] Fix | Delete
return;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
$meta->delete();
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
private static function get_path_from_backup_path( string $backup_path ): string {
[105] Fix | Delete
$extension = File_Utils::get_extension( $backup_path );
[106] Fix | Delete
return str_replace( ".backup.$extension", ".$extension", $backup_path );
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
private static function update_image_post( Image $image, string $image_path, string $mime_type ): void {
[110] Fix | Delete
$post_update_query = [
[111] Fix | Delete
'post_modified' => current_time( 'mysql' ),
[112] Fix | Delete
'post_modified_gmt' => current_time( 'mysql', true ),
[113] Fix | Delete
'post_mime_type' => $mime_type,
[114] Fix | Delete
'guid' => File_Utils::get_url_from_path( $image_path ),
[115] Fix | Delete
];
[116] Fix | Delete
[117] Fix | Delete
$image->update_attachment( $post_update_query );
[118] Fix | Delete
[119] Fix | Delete
update_attached_file( $image->get_id(), $image_path );
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* If we change an image extension, we should walk through the wp_posts table and update all the
[124] Fix | Delete
* hardcoded image links to prevent 404s.
[125] Fix | Delete
*
[126] Fix | Delete
* @param string $old_path Previous image path
[127] Fix | Delete
* @param string $new_path Current image path
[128] Fix | Delete
*
[129] Fix | Delete
* @return void
[130] Fix | Delete
*/
[131] Fix | Delete
private static function update_posts( string $old_path, string $new_path ) {
[132] Fix | Delete
Image_DB_Update::update_posts_table_urls(
[133] Fix | Delete
File_Utils::get_url_from_path( $old_path ),
[134] Fix | Delete
File_Utils::get_url_from_path( $new_path )
[135] Fix | Delete
);
[136] Fix | Delete
}
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function