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-restore.php

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

114 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\Async_Operation\Exceptions\Async_Operation_Exception;
6 use ImageOptimizer\Classes\File_Utils;
7 use ImageOptimizer\Classes\Image\Exceptions\{
8 Image_Restoring_Exception,
9 Invalid_Image_Exception,
10 };
11 use Throwable;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 require_once ABSPATH . 'wp-admin/includes/file.php';
18 WP_Filesystem();
19
20 class Image_Restore {
21 public static function restore_many( array $image_ids, bool $keep_image_meta = false ): void {
22 foreach ( $image_ids as $image_id ) {
23 try {
24 self::restore( $image_id, $keep_image_meta );
25 } catch ( Throwable $t ) {
26 ( new Image_Meta( $image_id ) )
27 ->set_status( Image_Status::RESTORING_FAILED )
28 ->save();
29
30 continue;
31 }
32 }
33 }
34
35 /**
36 * @throws Invalid_Image_Exception
37 * @throws Image_Restoring_Exception
38 * @throws Async_Operation_Exception
39 */
40 public static function restore( int $image_id, bool $keep_image_meta = false ): void {
41 global $wp_filesystem;
42
43 $image = new Image( $image_id );
44
45 if ( ! $image->can_be_restored() ) {
46 throw new Image_Restoring_Exception( "Image $image_id cannot be restored" );
47 }
48
49 $meta = new Image_Meta( $image_id );
50 $wp_meta = new WP_Image_Meta( $image_id );
51
52 foreach ( $meta->get_optimized_sizes() as $image_size ) {
53 $backup_path = $meta->get_image_backup_path( $image_size );
54 $current_path = $image->get_file_path( $image_size );
55
56 if ( $backup_path && $current_path ) {
57 $original_path = self::get_path_from_backup_path( $backup_path );
58
59 $wp_filesystem->move( $backup_path, $original_path, true );
60
61 if ( $original_path !== $current_path ) {
62 $wp_filesystem->delete( $current_path, false, 'f' );
63 }
64
65 $wp_meta
66 ->set_file_path( $image_size, $original_path )
67 ->set_mime_type( $image_size, $meta->get_original_mime_type( $image_size ) )
68 ->set_width( $image_size, $meta->get_original_width( $image_size ) )
69 ->set_height( $image_size, $meta->get_original_height( $image_size ) )
70 ->set_file_size( $image_size, $meta->get_original_file_size( $image_size ) );
71
72 if ( Image::SIZE_FULL === $image_size ) {
73 self::update_image_post(
74 $image,
75 $original_path,
76 $meta->get_original_mime_type( Image::SIZE_FULL )
77 );
78 }
79 }
80 }
81
82 $wp_meta->save();
83
84 if ( $keep_image_meta ) {
85 $meta
86 ->clear_optimized_sizes()
87 ->clear_original_data()
88 ->save();
89
90 return;
91 }
92
93 $meta->delete();
94 }
95
96 private static function get_path_from_backup_path( string $backup_path ): string {
97 $extension = File_Utils::get_extension( $backup_path );
98 return str_replace( ".backup.$extension", ".$extension", $backup_path );
99 }
100
101 private static function update_image_post( Image $image, string $image_path, string $mime_type ): void {
102 $post_update_query = [
103 'post_modified' => current_time( 'mysql' ),
104 'post_modified_gmt' => current_time( 'mysql', true ),
105 'post_mime_type' => $mime_type,
106 'guid' => File_Utils::get_url_from_path( $image_path ),
107 ];
108
109 $image->update_attachment( $post_update_query );
110
111 update_attached_file( $image->get_id(), $image_path );
112 }
113 }
114