PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.10
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.10
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / 3rd-party / enable-media-replace / classes / Main.php

Main.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.10, at inc/3rd-party/enable-media-replace/classes/Main.php

163 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\ThirdParty\EnableMediaReplace;
3
4 use Imagify\Traits\InstanceGetterTrait;
5 use Imagify_Enable_Media_Replace_Deprecated;
6 use Imagify_Filesystem;
7
8 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
9
10 /**
11 * Compat class for Enable Media Replace plugin.
12 *
13 * @since 1.6.9
14 */
15 class Main extends Imagify_Enable_Media_Replace_Deprecated {
16 use InstanceGetterTrait;
17
18 /**
19 * The media ID.
20 *
21 * @var int
22 * @since 1.9
23 */
24 protected $media_id;
25
26 /**
27 * The process instance for the current attachment.
28 *
29 * @var ProcessInterface
30 * @since 1.9
31 */
32 protected $process;
33
34 /**
35 * The path to the old backup file.
36 *
37 * @var string
38 * @since 1.6.9
39 */
40 protected $old_backup_path;
41
42 /**
43 * List of paths to the old WebP files.
44 *
45 * @var array
46 * @since 1.9.8
47 */
48 protected $old_webp_paths = [];
49
50 /**
51 * Launch the hooks before the files and data are replaced.
52 *
53 * @since 1.6.9
54 * @since 1.9.10 The parameter changed from boolean to array. The method doesn’t return anything.
55 *
56 * @param array $args An array containing the post ID.
57 */
58 public function init( $args = [] ) {
59 if ( is_array( $args ) && ! empty( $args['post_id'] ) ) {
60 $this->media_id = $args['post_id'];
61 } else {
62 // Backward compatibility.
63 $this->media_id = (int) filter_input( INPUT_POST, 'ID' );
64 $this->media_id = max( 0, $this->media_id );
65
66 if ( ! $this->media_id ) {
67 return;
68 }
69 }
70
71 // Store the old backup file path.
72 $this->get_process();
73
74 if ( ! $this->process ) {
75 $this->media_id = 0;
76 return;
77 }
78
79 $this->old_backup_path = $this->process->get_media()->get_backup_path();
80
81 if ( ! $this->old_backup_path ) {
82 $this->media_id = 0;
83 return;
84 }
85
86 /**
87 * Keep track of existing WebP files.
88 *
89 * Whether the user chooses to rename the files or not, we will need to delete the current WebP files before creating new ones:
90 * - Rename the files: the old ones must be removed, they are useless now.
91 * - Do not rename the files: the thumbnails may still get new names because of the suffix containing the image dimensions, which may differ (for example when thumbnails are scaled, not cropped).
92 * In this last case, the thumbnails with the old dimensions are removed from the drive and from the WP’s post meta, so there is no need of keeping orphan WebP files that would stay on the drive for ever, even after the attachment is deleted from WP.
93 */
94 foreach ( $this->process->get_media()->get_media_files() as $media_file ) {
95 $this->old_webp_paths[] = imagify_path_to_webp( $media_file['path'] );
96 }
97
98 // Delete the old backup file and old WebP files.
99 add_action( 'imagify_before_auto_optimization', [ $this, 'delete_backup' ] );
100 add_action( 'imagify_not_optimized_attachment_updated', [ $this, 'delete_backup' ] );
101 }
102
103
104 /** ----------------------------------------------------------------------------------------- */
105 /** HOOKS =================================================================================== */
106 /** ----------------------------------------------------------------------------------------- */
107
108 /**
109 * Delete previous backup file and WebP files.
110 * This is done after the images have been already replaced by Enable Media Replace.
111 *
112 * @since 1.8.4
113 *
114 * @param int $media_id The attachment ID.
115 */
116 public function delete_backup( $media_id ) {
117 if ( ! $this->old_backup_path || ! $this->media_id || $media_id !== $this->media_id ) {
118 return;
119 }
120
121 $filesystem = Imagify_Filesystem::get_instance();
122
123 if ( $filesystem->exists( $this->old_backup_path ) ) {
124 // Delete old backup file.
125 $filesystem->delete( $this->old_backup_path );
126 $this->old_backup_path = false;
127 }
128
129 if ( ! empty( $this->old_webp_paths ) ) {
130 // Delete old WebP files.
131 $this->old_webp_paths = array_filter( $this->old_webp_paths, [ $filesystem, 'exists' ] );
132 array_map( [ $filesystem, 'delete' ], $this->old_webp_paths );
133 $this->old_webp_paths = [];
134 }
135 }
136
137
138 /** ----------------------------------------------------------------------------------------- */
139 /** TOOLS =================================================================================== */
140 /** ----------------------------------------------------------------------------------------- */
141
142 /**
143 * Get the optimization process corresponding to the current media.
144 *
145 * @since 1.9
146 *
147 * @return ProcessInterface|bool False if invalid.
148 */
149 protected function get_process() {
150 if ( isset( $this->process ) ) {
151 return $this->process;
152 }
153
154 $this->process = imagify_get_optimization_process( $this->media_id, 'wp' );
155
156 if ( ! $this->process->is_valid() ) {
157 $this->process = false;
158 }
159
160 return $this->process;
161 }
162 }
163