Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../classes/image
File: image-backup.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Classes\Image;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
[4] Fix | Delete
use ImageOptimization\Classes\File_System\File_System;
[5] Fix | Delete
use ImageOptimization\Classes\File_Utils;
[6] Fix | Delete
use ImageOptimization\Classes\Image\Exceptions\Image_Backup_Creation_Error;
[7] Fix | Delete
use ImageOptimization\Classes\Logger;
[8] Fix | Delete
[9] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[10] Fix | Delete
exit; // Exit if accessed directly.
[11] Fix | Delete
}
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* We're saving backup paths in meta even though the backup path could be dynamically generated.
[15] Fix | Delete
* The next reasons affected this decision:
[16] Fix | Delete
*
[17] Fix | Delete
* 1. Possible path conflicts with other plugins and/or custom user logic. We can't guarantee that the image
[18] Fix | Delete
* we find using the dynamically created path will be the same image we saved.
[19] Fix | Delete
* 2. If a backup doesn't exist, but stored in our meta -- we can be almost sure it's not because of us.
[20] Fix | Delete
* Probably, some other plugin removed it or even user manually did it, but it's not our fault. Makes this logic
[21] Fix | Delete
* easier to debug.
[22] Fix | Delete
* 3. We can change backup file name using `wp_unique_filename()` if needed and easily point a backup path to an
[23] Fix | Delete
* image.
[24] Fix | Delete
*/
[25] Fix | Delete
class Image_Backup {
[26] Fix | Delete
/**
[27] Fix | Delete
* Creates a backup of a file by copying it to a new file with the backup extension.
[28] Fix | Delete
* Also, attaches a newly created backup to image's meta.
[29] Fix | Delete
*
[30] Fix | Delete
* @param int $image_id Attachment id.
[31] Fix | Delete
* @param string $image_size Image size (e.g. 'full', 'thumbnail', etc.).
[32] Fix | Delete
* @param string $image_path Path to an image we plan to back up.
[33] Fix | Delete
*
[34] Fix | Delete
* @return string Backup path if successfully created, false otherwise.
[35] Fix | Delete
*
[36] Fix | Delete
* @throws Image_Backup_Creation_Error
[37] Fix | Delete
*/
[38] Fix | Delete
public static function create( int $image_id, string $image_size, string $image_path ): string {
[39] Fix | Delete
$extension = File_Utils::get_extension( $image_path );
[40] Fix | Delete
$backup_path = File_Utils::replace_extension( $image_path, "backup.$extension" );
[41] Fix | Delete
[42] Fix | Delete
try {
[43] Fix | Delete
File_System::copy( $image_path, $backup_path, true );
[44] Fix | Delete
} catch ( File_System_Operation_Error $e ) {
[45] Fix | Delete
Logger::log(
[46] Fix | Delete
Logger::LEVEL_ERROR,
[47] Fix | Delete
"Error while creating a backup for image {$image_id} and size {$image_size}"
[48] Fix | Delete
);
[49] Fix | Delete
[50] Fix | Delete
throw new Image_Backup_Creation_Error(
[51] Fix | Delete
"Error while creating a backup for image {$image_id} and size {$image_size}"
[52] Fix | Delete
);
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
$meta = new Image_Meta( $image_id );
[56] Fix | Delete
[57] Fix | Delete
$meta->set_image_backup_path( $image_size, $backup_path );
[58] Fix | Delete
$meta->save();
[59] Fix | Delete
[60] Fix | Delete
return $backup_path;
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Looks for registered backups and remove all files found.
[65] Fix | Delete
* Also, wipes removed files from image meta.
[66] Fix | Delete
*
[67] Fix | Delete
* @param int[] $image_ids Array of attachment ids.
[68] Fix | Delete
* @return void
[69] Fix | Delete
*/
[70] Fix | Delete
public static function remove_many( array $image_ids ): void {
[71] Fix | Delete
foreach ( $image_ids as $image_id ) {
[72] Fix | Delete
self::remove( $image_id );
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Removes one or all backups for a specific image.
[78] Fix | Delete
* Also, wipes removed files from image meta.
[79] Fix | Delete
*
[80] Fix | Delete
* @param int $image_id Attachment id.
[81] Fix | Delete
* @param string|null $image_size Image size (e.g. 'full', 'thumbnail', etc.). All backups will be removed if no size provided.
[82] Fix | Delete
*
[83] Fix | Delete
* @return bool Returns true if backups were removed successfully, false otherwise.
[84] Fix | Delete
*/
[85] Fix | Delete
public static function remove( int $image_id, ?string $image_size = null ): bool {
[86] Fix | Delete
$meta = new Image_Meta( $image_id );
[87] Fix | Delete
$backups = $meta->get_image_backup_paths();
[88] Fix | Delete
[89] Fix | Delete
if ( empty( $backups ) ) {
[90] Fix | Delete
return false;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
if ( $image_size ) {
[94] Fix | Delete
if ( ! key_exists( $image_size, $backups ) ) {
[95] Fix | Delete
return false;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
try {
[99] Fix | Delete
File_System::delete( $backups[ $image_size ], false, 'f' );
[100] Fix | Delete
} catch ( File_System_Operation_Error $e ) {
[101] Fix | Delete
Logger::log(
[102] Fix | Delete
Logger::LEVEL_ERROR,
[103] Fix | Delete
"Error while removing a backup for image {$image_id} and size {$image_size}"
[104] Fix | Delete
);
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$meta->remove_image_backup_path( $image_size );
[108] Fix | Delete
$meta->save();
[109] Fix | Delete
[110] Fix | Delete
return true;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
foreach ( $backups as $image_size => $backup_path ) {
[114] Fix | Delete
try {
[115] Fix | Delete
File_System::delete( $backup_path, false, 'f' );
[116] Fix | Delete
} catch ( File_System_Operation_Error $e ) {
[117] Fix | Delete
Logger::log( Logger::LEVEL_ERROR, "Error while removing backups {$image_id}" );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
$meta->remove_image_backup_path( $image_size );
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
$meta->save();
[124] Fix | Delete
[125] Fix | Delete
return true;
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function