PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
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-restore.php +54 -18 1.0.2 → 1.7.7 View file →
@@ -1,14 +1,18 @@
1 1 <?php
2 2
3 -namespace ImageOptimizer\Classes\Image;
3 +namespace ImageOptimization\Classes\Image;
4 4
5 -use ImageOptimizer\Classes\Async_Operation\Exceptions\Async_Operation_Exception;
6 -use ImageOptimizer\Classes\File_Utils;
7 -use ImageOptimizer\Classes\Image\Exceptions\{
5 +use ImageOptimization\Classes\File_System\{
6 + Exceptions\File_System_Operation_Error,
7 + File_System,
8 +};
9 +use ImageOptimization\Classes\File_Utils;
10 +use ImageOptimization\Classes\Image\Exceptions\{
8 11 Image_Restoring_Exception,
9 12 Invalid_Image_Exception,
10 13 };
14 +use ImageOptimization\Classes\Logger;
11 15 use Throwable;
12 16
13 17 if ( ! defined( 'ABSPATH' ) ) {
14 18 exit; // Exit if accessed directly.
@@ -13,11 +17,8 @@
13 17 if ( ! defined( 'ABSPATH' ) ) {
14 18 exit; // Exit if accessed directly.
15 19 }
16 20
17 -require_once ABSPATH . 'wp-admin/includes/file.php';
18 -WP_Filesystem();
19 -
20 21 class Image_Restore {
21 22 public static function restore_many( array $image_ids, bool $keep_image_meta = false ): void {
22 23 foreach ( $image_ids as $image_id ) {
23 24 try {
@@ -22,8 +23,10 @@
22 23 foreach ( $image_ids as $image_id ) {
23 24 try {
24 25 self::restore( $image_id, $keep_image_meta );
25 26 } catch ( Throwable $t ) {
27 + Logger::error( 'Bulk images restoring error: ' . $t->getMessage() );
28 +
26 29 ( new Image_Meta( $image_id ) )
27 30 ->set_status( Image_Status::RESTORING_FAILED )
28 31 ->save();
29 32
@@ -32,19 +35,15 @@
32 35 }
33 36 }
34 37
35 38 /**
36 - * @throws Invalid_Image_Exception
37 - * @throws Image_Restoring_Exception
38 - * @throws Async_Operation_Exception
39 + * @throws Invalid_Image_Exception|Image_Restoring_Exception|File_System_Operation_Error
39 40 */
40 41 public static function restore( int $image_id, bool $keep_image_meta = false ): void {
41 - global $wp_filesystem;
42 -
43 42 $image = new Image( $image_id );
44 43
45 44 if ( ! $image->can_be_restored() ) {
46 - throw new Image_Restoring_Exception( "Image $image_id cannot be restored" );
45 + throw new Image_Restoring_Exception( esc_html( "Image $image_id cannot be restored" ) );
47 46 }
48 47
49 48 $meta = new Image_Meta( $image_id );
50 49 $wp_meta = new WP_Image_Meta( $image_id );
@@ -53,22 +52,43 @@
53 52 $backup_path = $meta->get_image_backup_path( $image_size );
54 53 $current_path = $image->get_file_path( $image_size );
55 54
56 55 if ( $backup_path && $current_path ) {
57 - $original_path = self::get_path_from_backup_path( $backup_path );
56 + $resolved_backup_path = Image_Backup_Path_Validator::resolve( $backup_path );
58 57
59 - $wp_filesystem->move( $backup_path, $original_path, true );
58 + if ( null === $resolved_backup_path ) {
59 + Logger::warn(
60 + "Skipped restoring invalid backup path for image {$image_id} and size {$image_size}"
61 + );
60 62
61 - if ( $original_path !== $current_path ) {
62 - $wp_filesystem->delete( $current_path, false, 'f' );
63 + continue;
63 64 }
64 65
66 + $original_path = self::get_path_from_backup_path( $resolved_backup_path );
67 +
68 + if ( $original_path === $resolved_backup_path ) {
69 +
70 + File_System::delete( $current_path, false, 'f' );
71 +
72 + self::update_posts( $current_path, $original_path );
73 + } else {
74 + File_System::move( $resolved_backup_path, $original_path, $original_path === $current_path );
75 +
76 + if ( $original_path !== $current_path ) {
77 + File_System::delete( $current_path, false, 'f' );
78 +
79 + self::update_posts( $current_path, $original_path );
80 + }
81 + }
82 +
83 + $file_size = $meta->get_original_file_size( $image_size ) ?? File_System::size( $original_path );
84 +
65 85 $wp_meta
66 86 ->set_file_path( $image_size, $original_path )
67 87 ->set_mime_type( $image_size, $meta->get_original_mime_type( $image_size ) )
68 88 ->set_width( $image_size, $meta->get_original_width( $image_size ) )
69 89 ->set_height( $image_size, $meta->get_original_height( $image_size ) )
70 - ->set_file_size( $image_size, $meta->get_original_file_size( $image_size ) );
90 + ->set_file_size( $image_size, $file_size );
71 91
72 92 if ( Image::SIZE_FULL === $image_size ) {
73 93 self::update_image_post(
74 94 $image,
@@ -109,6 +129,22 @@
109 129
110 130 $image->update_attachment( $post_update_query );
111 131
112 132 update_attached_file( $image->get_id(), $image_path );
133 + }
134 +
135 + /**
136 + * If we change an image extension, we should walk through the wp_posts table and update all the
137 + * hardcoded image links to prevent 404s.
138 + *
139 + * @param string $old_path Previous image path
140 + * @param string $new_path Current image path
141 + *
142 + * @return void
143 + */
144 + private static function update_posts( string $old_path, string $new_path ) {
145 + Image_DB_Update::update_posts_table_urls(
146 + File_Utils::get_url_from_path( $old_path ),
147 + File_Utils::get_url_from_path( $new_path )
148 + );
113 149 }
114 150 }