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