| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Imagify\Media; |
| 3 | 3 | |
| 4 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 4 | +use WP_Error; | |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | 7 | * Media class for the medias in the WP library. |
| 8 | 8 | * |
| @@ -19,9 +19,9 @@ | ||
| 19 | 19 | * @since 1.9.8 |
| 20 | 20 | * @access protected |
| 21 | 21 | * @author Grégory Viguier |
| 22 | 22 | */ |
| 23 | - protected $is_wp53; | |
| 23 | + protected $is_wp53 = false; | |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * The constructor. |
| 27 | 27 | * |
| @@ -215,12 +215,18 @@ | ||
| 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 | - // Σ(゚Д゚). | |
| 223 | 229 | update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata ); |
| 224 | 230 | |
| 225 | 231 | return true; |
| 226 | 232 | } |
| @@ -248,9 +254,47 @@ | ||
| 248 | 254 | |
| 249 | 255 | return true; |
| 250 | 256 | } |
| 251 | 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 | + } | |
| 252 | 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; | |
| 294 | + } | |
| 295 | + | |
| 296 | + | |
| 253 | 297 | /** ----------------------------------------------------------------------------------------- */ |
| 254 | 298 | /** MEDIA DATA ============================================================================== */ |
| 255 | 299 | /** ----------------------------------------------------------------------------------------- */ |
| 256 | 300 | |
| @@ -418,12 +462,8 @@ | ||
| 418 | 462 | * |
| 419 | 463 | * @return bool |
| 420 | 464 | */ |
| 421 | 465 | protected function is_wp_53() { |
| 422 | - if ( isset( $this->is_wp53 ) ) { | |
| 423 | - return $this->is_wp53; | |
| 424 | - } | |
| 425 | - | |
| 426 | 466 | $this->is_wp53 = function_exists( 'wp_get_original_image_path' ); |
| 427 | 467 | |
| 428 | 468 | return $this->is_wp53; |
| 429 | 469 | } |