PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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
← All changes | classes/Media/WP.php +46 -1 2.2.9 → trunk View file →
@@ -210,15 +210,22 @@
210 210 return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
211 211 }
212 212
213 213 if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
214 - require_once ABSPATH . 'wp-admin/includes/image.php'; // @phpstan-ignore-line
214 + require_once ABSPATH . 'wp-admin/includes/image.php';
215 215 }
216 216
217 217 // Store the path to the current full size file before generating the thumbnails.
218 218 $old_full_size_path = $this->get_raw_fullsize_path();
219 + $previous_metadata = wp_get_attachment_metadata( $this->get_id() );
219 220 $metadata = wp_generate_attachment_metadata( $this->get_id(), $this->get_raw_original_path() );
220 221
222 + if ( ! is_array( $metadata ) ) {
223 + $metadata = [];
224 + }
225 +
226 + $metadata = $this->keep_companion_files( $metadata, $previous_metadata );
227 +
221 228 if ( empty( $metadata['file'] ) ) {
222 229 update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata );
223 230
224 231 return true;
@@ -245,8 +252,46 @@
245 252
246 253 update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata );
247 254
248 255 return true;
256 + }
257 +
258 + /**
259 + * Carry the companion files WordPress stores next to an attachment over to fresh metadata.
260 + *
261 + * WordPress 7.1 can let the browser convert an upload and keep the file it started from:
262 + * the HEIC a photo was uploaded as, next to the JPEG the site serves, or the original GIF
263 + * next to the video it was turned into. Those file names live in the attachment metadata
264 + * and nowhere else, and `wp_generate_attachment_metadata()` does not produce them, so
265 + * replacing the metadata wholesale would lose them.
266 + *
267 + * Losing them means `wp_delete_attachment_files()` can no longer find those files, and they
268 + * stay on disk for good, even once the attachment is deleted.
269 + *
270 + * @since 2.3.3
271 + *
272 + * @param array $metadata Freshly generated attachment metadata.
273 + * @param mixed $previous_metadata Metadata as it was stored before being regenerated.
274 + * @return array
275 + */
276 + protected function keep_companion_files( $metadata, $previous_metadata ) {
277 + if ( ! is_array( $previous_metadata ) ) {
278 + return $metadata;
279 + }
280 +
281 + $companion_keys = [
282 + 'source_image',
283 + 'animated_video',
284 + 'animated_video_poster',
285 + ];
286 +
287 + foreach ( $companion_keys as $key ) {
288 + if ( ! empty( $previous_metadata[ $key ] ) && empty( $metadata[ $key ] ) ) {
289 + $metadata[ $key ] = $previous_metadata[ $key ];
290 + }
291 + }
292 +
293 + return $metadata;
249 294 }
250 295
251 296
252 297 /** ----------------------------------------------------------------------------------------- */