Skip to content

Commit c711280

Browse files
Media: Delete HEIC companion file when its attachment is deleted.
When the client-side media flow sideloads a HEIC original alongside a JPEG derivative, the HEIC filename is stored in $metadata['original']. wp_delete_attachment_files() only tracks 'original_image', so without this hook the HEIC file would linger on disk after the attachment is removed. wp_delete_attachment_heic_companion_file() reads the meta key, guards against non-string values (e.g. arrays written by other flows), and deletes the file when present. Hooked into the delete_attachment action via default-filters.php. Backport of GB #76731, with the is_string() guard from GB #78128.
1 parent b261f30 commit c711280

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@
562562
add_action( 'transition_post_status', '_wp_customize_publish_changeset', 10, 3 );
563563
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
564564
add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
565+
add_action( 'delete_attachment', 'wp_delete_attachment_heic_companion_file' );
565566
add_action( 'transition_post_status', '_wp_keep_alive_customize_changeset_dependent_auto_drafts', 20, 3 );
566567

567568
// Block Theme Previews.

src/wp-includes/media.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5760,6 +5760,45 @@ function wp_show_heic_upload_error( $plupload_settings ) {
57605760
return $plupload_settings;
57615761
}
57625762

5763+
/**
5764+
* Deletes the HEIC companion file when its attachment is deleted.
5765+
*
5766+
* When the client-side media flow sideloads a HEIC original alongside a
5767+
* JPEG derivative, the HEIC filename is recorded in $metadata['original'].
5768+
* WordPress only tracks 'original_image' in wp_delete_attachment_files(),
5769+
* so without this hook the HEIC file would linger on disk after the
5770+
* attachment is deleted.
5771+
*
5772+
* @since 7.1.0
5773+
*
5774+
* @param int $post_id Attachment ID being deleted.
5775+
*/
5776+
function wp_delete_attachment_heic_companion_file( $post_id ) {
5777+
$metadata = wp_get_attachment_metadata( $post_id, true );
5778+
5779+
if ( empty( $metadata['original'] ) || ! is_string( $metadata['original'] ) ) {
5780+
return;
5781+
}
5782+
5783+
$attached_file = get_attached_file( $post_id, true );
5784+
5785+
if ( ! $attached_file ) {
5786+
return;
5787+
}
5788+
5789+
$uploads = wp_get_upload_dir();
5790+
5791+
if ( empty( $uploads['basedir'] ) ) {
5792+
return;
5793+
}
5794+
5795+
$heic_path = path_join( dirname( $attached_file ), wp_basename( (string) $metadata['original'] ) );
5796+
5797+
if ( file_exists( $heic_path ) ) {
5798+
wp_delete_file_from_directory( $heic_path, $uploads['basedir'] );
5799+
}
5800+
}
5801+
57635802
/**
57645803
* Allows PHP's getimagesize() to be debuggable when necessary.
57655804
*

0 commit comments

Comments
 (0)