PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.1
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
image-optimization / classes / image / image-backup.php

image-backup.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.1, at classes/image/image-backup.php

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