PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / trunk
Image Optimizer – Compress Images and Convert to WebP or AVIF vtrunk
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
← All changes | classes/image/image-backup.php +47 -14 1.0.1 → trunk View file →
@@ -1,17 +1,18 @@
1 1 <?php
2 2
3 -namespace ImageOptimizer\Classes\Image;
3 +namespace ImageOptimization\Classes\Image;
4 4
5 -use ImageOptimizer\Classes\File_Utils;
5 +use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
6 +use ImageOptimization\Classes\File_System\File_System;
7 +use ImageOptimization\Classes\File_Utils;
8 +use ImageOptimization\Classes\Image\Exceptions\Image_Backup_Creation_Error;
9 +use ImageOptimization\Classes\Logger;
6 10
7 11 if ( ! defined( 'ABSPATH' ) ) {
8 12 exit; // Exit if accessed directly.
9 13 }
10 14
11 -require_once ABSPATH . 'wp-admin/includes/file.php';
12 -WP_Filesystem();
13 -
14 15 /**
15 16 * We're saving backup paths in meta even though the backup path could be dynamically generated.
16 17 * The next reasons affected this decision:
17 18 *
@@ -24,24 +25,35 @@
24 25 * image.
25 26 */
26 27 class Image_Backup {
27 28 /**
28 - * Creates a backup of a file by moving it to a new file with the backup extension.
29 + * Creates a backup of a file by copying it to a new file with the backup extension.
29 30 * Also, attaches a newly created backup to image's meta.
30 31 *
31 32 * @param int $image_id Attachment id.
32 33 * @param string $image_size Image size (e.g. 'full', 'thumbnail', etc.).
33 34 * @param string $image_path Path to an image we plan to back up.
34 - * @return string Backup path.
35 + *
36 + * @return string Backup path if successfully created, false otherwise.
37 + *
38 + * @throws Image_Backup_Creation_Error
35 39 */
36 40 public static function create( int $image_id, string $image_size, string $image_path ): string {
37 - global $wp_filesystem;
38 -
39 41 $extension = File_Utils::get_extension( $image_path );
40 42 $backup_path = File_Utils::replace_extension( $image_path, "backup.$extension" );
41 43
42 - $wp_filesystem->move( $image_path, $backup_path, true );
44 + try {
45 + File_System::copy( $image_path, $backup_path, true );
46 + } catch ( File_System_Operation_Error $e ) {
47 + Logger::error(
48 + "Error while creating a backup for image {$image_id} and size {$image_size}"
49 + );
43 50
51 + throw new Image_Backup_Creation_Error(
52 + esc_html( "Error while creating a backup for image $image_id and size $image_size" )
53 + );
54 + }
55 +
44 56 $meta = new Image_Meta( $image_id );
45 57
46 58 $meta->set_image_backup_path( $image_size, $backup_path );
47 59 $meta->save();
@@ -67,13 +79,12 @@
67 79 * Also, wipes removed files from image meta.
68 80 *
69 81 * @param int $image_id Attachment id.
70 82 * @param string|null $image_size Image size (e.g. 'full', 'thumbnail', etc.). All backups will be removed if no size provided.
83 + *
71 84 * @return bool Returns true if backups were removed successfully, false otherwise.
72 85 */
73 86 public static function remove( int $image_id, ?string $image_size = null ): bool {
74 - global $wp_filesystem;
75 -
76 87 $meta = new Image_Meta( $image_id );
77 88 $backups = $meta->get_image_backup_paths();
78 89
79 90 if ( empty( $backups ) ) {
@@ -84,9 +95,10 @@
84 95 if ( ! key_exists( $image_size, $backups ) ) {
85 96 return false;
86 97 }
87 98
88 - $wp_filesystem->delete( $backups[ $image_size ], false, 'f' );
99 + self::delete_backup_file( $image_id, $image_size, $backups[ $image_size ] );
100 +
89 101 $meta->remove_image_backup_path( $image_size );
90 102 $meta->save();
91 103
92 104 return true;
@@ -92,9 +104,10 @@
92 104 return true;
93 105 }
94 106
95 107 foreach ( $backups as $image_size => $backup_path ) {
96 - $wp_filesystem->delete( $backup_path, false, 'f' );
108 + self::delete_backup_file( $image_id, $image_size, $backup_path );
109 +
97 110 $meta->remove_image_backup_path( $image_size );
98 111 }
99 112
100 113 $meta->save();
@@ -99,6 +112,26 @@
99 112
100 113 $meta->save();
101 114
102 115 return true;
116 + }
117 +
118 + private static function delete_backup_file( int $image_id, string $image_size, string $backup_path ): void {
119 + $resolved_path = Image_Backup_Path_Validator::resolve( $backup_path );
120 +
121 + if ( null === $resolved_path ) {
122 + Logger::warn(
123 + "Skipped removing invalid backup path for image {$image_id} and size {$image_size}"
124 + );
125 +
126 + return;
127 + }
128 +
129 + try {
130 + File_System::delete( $resolved_path, false, 'f' );
131 + } catch ( File_System_Operation_Error $fsoe ) {
132 + Logger::error(
133 + "Error while removing a backup for image {$image_id} and size {$image_size}"
134 + );
135 + }
103 136 }
104 137 }