PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
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 2.3.1, at inc/3rd-party/enable-media-replace/classes/Main.php

153 lines 4.1 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 /**
9 * Compat class for Enable Media Replace plugin.
10 *
11 * @since 1.6.9
12 */
13 class Main extends Imagify_Enable_Media_Replace_Deprecated {
14 use InstanceGetterTrait;
15
16 /**
17 * The media ID.
18 *
19 * @var int
20 * @since 1.9
21 */
22 protected $media_id;
23
24 /**
25 * The process instance for the current attachment.
26 *
27 * @var ProcessInterface
28 * @since 1.9
29 */
30 protected $process;
31
32 /**
33 * The path to the old backup file.
34 *
35 * @var string
36 * @since 1.6.9
37 */
38 protected $old_backup_path;
39
40 /**
41 * List of paths to the old next-gen files.
42 *
43 * @var array
44 * @since 1.9.8
45 */
46 protected $old_nextgen_paths = [];
47
48 /**
49 * Launch the hooks before the files and data are replaced.
50 *
51 * @since 1.6.9
52 * @since 1.9.10 The parameter changed from boolean to array. The method doesn’t return anything.
53 *
54 * @param array $args An array containing the post ID.
55 */
56 public function init( $args = [] ) {
57 if ( is_array( $args ) && ! empty( $args['post_id'] ) ) {
58 $this->media_id = $args['post_id'];
59 } else {
60 // Backward compatibility.
61 $this->media_id = (int) filter_input( INPUT_POST, 'ID' );
62 $this->media_id = max( 0, $this->media_id );
63
64 if ( ! $this->media_id ) {
65 return;
66 }
67 }
68
69 // Store the old backup file path.
70 $this->get_process();
71
72 if ( ! $this->process ) {
73 $this->media_id = 0;
74 return;
75 }
76
77 $this->old_backup_path = $this->process->get_media()->get_backup_path();
78
79 if ( ! $this->old_backup_path ) {
80 $this->media_id = 0;
81 return;
82 }
83
84 /**
85 * Keep track of existing next-gen files.
86 *
87 * Whether the user chooses to rename the files or not, we will need to delete the current next-gen files before creating new ones:
88 * - Rename the files: the old ones must be removed, they are useless now.
89 * - 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).
90 * 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 next-gen files that would stay on the drive for ever, even after the attachment is deleted from WP.
91 */
92 foreach ( $this->process->get_media()->get_media_files() as $media_file ) {
93 foreach ( [ 'avif', 'webp' ] as $format ) {
94 $this->old_nextgen_paths[] = imagify_path_to_nextgen( $media_file['path'], $format );
95 }
96 }
97
98 // Delete the old backup file and old next-gen 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 * Delete previous backup file and next-gen files.
105 * This is done after the images have been already replaced by Enable Media Replace.
106 *
107 * @since 1.8.4
108 *
109 * @param int $media_id The attachment ID.
110 */
111 public function delete_backup( $media_id ) {
112 if ( ! $this->old_backup_path || ! $this->media_id || $media_id !== $this->media_id ) {
113 return;
114 }
115
116 $filesystem = Imagify_Filesystem::get_instance();
117
118 if ( $filesystem->exists( $this->old_backup_path ) ) {
119 // Delete old backup file.
120 $filesystem->delete( $this->old_backup_path );
121 $this->old_backup_path = false;
122 }
123
124 if ( ! empty( $this->old_nextgen_paths ) ) {
125 // Delete old next-gen files.
126 $this->old_nextgen_paths = array_filter( $this->old_nextgen_paths, [ $filesystem, 'exists' ] );
127 array_map( [ $filesystem, 'delete' ], $this->old_nextgen_paths );
128 $this->old_nextgen_paths = [];
129 }
130 }
131
132 /**
133 * Get the optimization process corresponding to the current media.
134 *
135 * @since 1.9
136 *
137 * @return ProcessInterface|bool False if invalid.
138 */
139 protected function get_process() {
140 if ( isset( $this->process ) ) {
141 return $this->process;
142 }
143
144 $this->process = imagify_get_optimization_process( $this->media_id, 'wp' );
145
146 if ( ! $this->process->is_valid() ) {
147 $this->process = false;
148 }
149
150 return $this->process;
151 }
152 }
153