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

140 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\{
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::log( Logger::LEVEL_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( "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 $original_path = self::get_path_from_backup_path( $backup_path );
57
58 if ( $original_path === $backup_path ) {
59 File_System::delete( $current_path, false, 'f' );
60
61 self::update_posts( $current_path, $original_path );
62 } else {
63 File_System::move( $backup_path, $original_path, $original_path === $current_path );
64
65 if ( $original_path !== $current_path ) {
66 File_System::delete( $current_path, false, 'f' );
67
68 self::update_posts( $current_path, $original_path );
69 }
70 }
71
72 $file_size = $meta->get_original_file_size( $image_size ) ?? File_System::size( $original_path );
73
74 $wp_meta
75 ->set_file_path( $image_size, $original_path )
76 ->set_mime_type( $image_size, $meta->get_original_mime_type( $image_size ) )
77 ->set_width( $image_size, $meta->get_original_width( $image_size ) )
78 ->set_height( $image_size, $meta->get_original_height( $image_size ) )
79 ->set_file_size( $image_size, $file_size );
80
81 if ( Image::SIZE_FULL === $image_size ) {
82 self::update_image_post(
83 $image,
84 $original_path,
85 $meta->get_original_mime_type( Image::SIZE_FULL )
86 );
87 }
88 }
89 }
90
91 $wp_meta->save();
92
93 if ( $keep_image_meta ) {
94 $meta
95 ->clear_optimized_sizes()
96 ->clear_backups()
97 ->clear_original_data()
98 ->save();
99
100 return;
101 }
102
103 $meta->delete();
104 }
105
106 private static function get_path_from_backup_path( string $backup_path ): string {
107 $extension = File_Utils::get_extension( $backup_path );
108 return str_replace( ".backup.$extension", ".$extension", $backup_path );
109 }
110
111 private static function update_image_post( Image $image, string $image_path, string $mime_type ): void {
112 $post_update_query = [
113 'post_modified' => current_time( 'mysql' ),
114 'post_modified_gmt' => current_time( 'mysql', true ),
115 'post_mime_type' => $mime_type,
116 'guid' => File_Utils::get_url_from_path( $image_path ),
117 ];
118
119 $image->update_attachment( $post_update_query );
120
121 update_attached_file( $image->get_id(), $image_path );
122 }
123
124 /**
125 * If we change an image extension, we should walk through the wp_posts table and update all the
126 * hardcoded image links to prevent 404s.
127 *
128 * @param string $old_path Previous image path
129 * @param string $new_path Current image path
130 *
131 * @return void
132 */
133 private static function update_posts( string $old_path, string $new_path ) {
134 Image_DB_Update::update_posts_table_urls(
135 File_Utils::get_url_from_path( $old_path ),
136 File_Utils::get_url_from_path( $new_path )
137 );
138 }
139 }
140