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
image-optimization / classes / image / image-restore.php

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

151 lines 4.2 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\{
6 Exceptions\File_System_Operation_Error,
7 File_System,
8 };
9 use ImageOptimization\Classes\File_Utils;
10 use ImageOptimization\Classes\Image\Exceptions\{
11 Image_Restoring_Exception,
12 Invalid_Image_Exception,
13 };
14 use ImageOptimization\Classes\Logger;
15 use Throwable;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 class Image_Restore {
22 public static function restore_many( array $image_ids, bool $keep_image_meta = false ): void {
23 foreach ( $image_ids as $image_id ) {
24 try {
25 self::restore( $image_id, $keep_image_meta );
26 } catch ( Throwable $t ) {
27 Logger::error( 'Bulk images restoring error: ' . $t->getMessage() );
28
29 ( new Image_Meta( $image_id ) )
30 ->set_status( Image_Status::RESTORING_FAILED )
31 ->save();
32
33 continue;
34 }
35 }
36 }
37
38 /**
39 * @throws Invalid_Image_Exception|Image_Restoring_Exception|File_System_Operation_Error
40 */
41 public static function restore( int $image_id, bool $keep_image_meta = false ): void {
42 $image = new Image( $image_id );
43
44 if ( ! $image->can_be_restored() ) {
45 throw new Image_Restoring_Exception( esc_html( "Image $image_id cannot be restored" ) );
46 }
47
48 $meta = new Image_Meta( $image_id );
49 $wp_meta = new WP_Image_Meta( $image_id );
50
51 foreach ( $meta->get_optimized_sizes() as $image_size ) {
52 $backup_path = $meta->get_image_backup_path( $image_size );
53 $current_path = $image->get_file_path( $image_size );
54
55 if ( $backup_path && $current_path ) {
56 $resolved_backup_path = Image_Backup_Path_Validator::resolve( $backup_path );
57
58 if ( null === $resolved_backup_path ) {
59 Logger::warn(
60 "Skipped restoring invalid backup path for image {$image_id} and size {$image_size}"
61 );
62
63 continue;
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
85 $wp_meta
86 ->set_file_path( $image_size, $original_path )
87 ->set_mime_type( $image_size, $meta->get_original_mime_type( $image_size ) )
88 ->set_width( $image_size, $meta->get_original_width( $image_size ) )
89 ->set_height( $image_size, $meta->get_original_height( $image_size ) )
90 ->set_file_size( $image_size, $file_size );
91
92 if ( Image::SIZE_FULL === $image_size ) {
93 self::update_image_post(
94 $image,
95 $original_path,
96 $meta->get_original_mime_type( Image::SIZE_FULL )
97 );
98 }
99 }
100 }
101
102 $wp_meta->save();
103
104 if ( $keep_image_meta ) {
105 $meta
106 ->clear_optimized_sizes()
107 ->clear_backups()
108 ->clear_original_data()
109 ->save();
110
111 return;
112 }
113
114 $meta->delete();
115 }
116
117 private static function get_path_from_backup_path( string $backup_path ): string {
118 $extension = File_Utils::get_extension( $backup_path );
119 return str_replace( ".backup.$extension", ".$extension", $backup_path );
120 }
121
122 private static function update_image_post( Image $image, string $image_path, string $mime_type ): void {
123 $post_update_query = [
124 'post_modified' => current_time( 'mysql' ),
125 'post_modified_gmt' => current_time( 'mysql', true ),
126 'post_mime_type' => $mime_type,
127 'guid' => File_Utils::get_url_from_path( $image_path ),
128 ];
129
130 $image->update_attachment( $post_update_query );
131
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 );
149 }
150 }
151