| @@ -215,10 +215,17 @@ | ||
| 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 | /** ----------------------------------------------------------------------------------------- */ |