PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.0
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.0.0, at classes/image/image-backup.php

105 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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