PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.4.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.4.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-restore.php

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

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