Skip to content

Commit a134534

Browse files
committed
More thorough removal of files with unknown or unregistered image sizes.
Log deleted and generated file names. Delete corresponding .webp images if they exist.
1 parent 8001ee9 commit a134534

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/Media_Command.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,15 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
672672
} else {
673673
wp_update_attachment_metadata( $id, $metadata );
674674

675+
// Log generated files if metadata contains sizes
676+
if ( ! empty( $metadata['sizes'] ) ) {
677+
$dir_path = dirname( $fullsizepath ) . '/';
678+
foreach ( $metadata['sizes'] as $size_name => $size_info ) {
679+
$generated_file_path = $dir_path . $size_info['file'];
680+
WP_CLI::log( "\tGenerated $generated_file_path" );
681+
}
682+
}
683+
675684
WP_CLI::log( "$progress Regenerated thumbnails for $att_desc." );
676685
}
677686
++$successes;
@@ -701,6 +710,14 @@ private function remove_old_images( $metadata, $fullsizepath, $image_size ) {
701710

702711
if ( file_exists( $intermediate_path ) ) {
703712
unlink( $intermediate_path );
713+
WP_CLI::log( "\tDeleted $intermediate_path." );
714+
}
715+
716+
// Also delete the corresponding .webp image if it exists
717+
$webp_path = $intermediate_path . '.webp';
718+
if ( file_exists( $webp_path ) ) {
719+
unlink( $webp_path );
720+
WP_CLI::log( "\tDeleted $webp_path." );
704721
}
705722
}
706723
}
@@ -1362,6 +1379,8 @@ private function get_image_name( $basename, $slug ) {
13621379
* @return void
13631380
*/
13641381
private function delete_unknown_image_sizes( $id, $fullsizepath ) {
1382+
global $wpdb;
1383+
13651384
$original_meta = wp_get_attachment_metadata( $id );
13661385

13671386
$image_sizes = wp_list_pluck( $this->get_registered_image_sizes(), 'name' );
@@ -1384,6 +1403,14 @@ private function delete_unknown_image_sizes( $id, $fullsizepath ) {
13841403

13851404
if ( file_exists( $intermediate_path ) ) {
13861405
unlink( $intermediate_path );
1406+
WP_CLI::log( "\tDeleted $intermediate_path." );
1407+
}
1408+
1409+
// Also delete the corresponding .webp image if it exists
1410+
$webp_path = $intermediate_path . '.webp';
1411+
if ( file_exists( $webp_path ) ) {
1412+
unlink( $webp_path );
1413+
WP_CLI::log( "\tDeleted $webp_path." );
13871414
}
13881415

13891416
$sizes_to_delete[] = $size_name;
@@ -1395,6 +1422,70 @@ private function delete_unknown_image_sizes( $id, $fullsizepath ) {
13951422
}
13961423
}
13971424

1425+
// Get the relative path for the attachment directory
1426+
if ( empty( $original_meta['file'] ) ) {
1427+
return;
1428+
}
1429+
$relative_path = dirname( $original_meta['file'] ) . '/';
1430+
1431+
// Whitelist legitimate thumbnails having dimensions in filename (e.g. image-123x456.jpg)
1432+
$whitelist_query = $wpdb->prepare(
1433+
"SELECT meta_value FROM {$wpdb->postmeta}
1434+
WHERE meta_key = '_wp_attached_file'
1435+
AND meta_value REGEXP %s",
1436+
'^' . preg_quote( $relative_path ) . '[^' . preg_quote( '/' ) . ']+-[0-9]+x[0-9]+\.'
1437+
);
1438+
$whitelist = array_map( 'basename', $wpdb->get_col( $whitelist_query ) );
1439+
1440+
// Get all files in the directory, filtering out directories and special files
1441+
$filelist = array();
1442+
$directory_files = scandir( $dir_path );
1443+
foreach ( $directory_files as $file ) {
1444+
$file_path = $dir_path . $file;
1445+
1446+
if ( '.' === $file || '..' === $file || ! is_file( $file_path ) ) {
1447+
continue;
1448+
}
1449+
1450+
$filelist[] = $file;
1451+
}
1452+
1453+
// Extract registered thumbnail filenames from attachment metadata
1454+
$registered_thumbnails = ! empty( $original_meta['sizes'] ) ? array_column( $original_meta['sizes'], 'file' ) : array();
1455+
1456+
// Parse the full-size image path for pattern matching
1457+
$fullsize_parts = pathinfo( $fullsizepath );
1458+
$filename_pattern = preg_quote( $fullsize_parts['filename'], '#' );
1459+
$extension_pattern = preg_quote( $fullsize_parts['extension'], '#' );
1460+
$thumbnail_regex = "#^{$filename_pattern}-[0-9]+x[0-9]+\.{$extension_pattern}$#";
1461+
1462+
// Process each file and delete orphaned thumbnails
1463+
foreach ( $filelist as $file ) {
1464+
// Skip files that are whitelisted or registered thumbnails
1465+
if ( in_array( $file, $whitelist ) || in_array( $file, $registered_thumbnails ) ) {
1466+
continue;
1467+
}
1468+
1469+
// Skip files that don't match the expected thumbnail naming pattern
1470+
if ( ! preg_match( $thumbnail_regex, $file ) ) {
1471+
continue;
1472+
}
1473+
1474+
// Delete the main thumbnail file
1475+
$thumbnail_path = $dir_path . $file;
1476+
if ( file_exists( $thumbnail_path ) ) {
1477+
unlink( $thumbnail_path );
1478+
WP_CLI::log( "\tDeleted $thumbnail_path." );
1479+
}
1480+
1481+
// Also delete the corresponding .webp image if it exists
1482+
$webp_path = $thumbnail_path . '.webp';
1483+
if ( file_exists( $webp_path ) ) {
1484+
unlink( $webp_path );
1485+
WP_CLI::log( "\tDeleted $webp_path." );
1486+
}
1487+
}
1488+
13981489
wp_update_attachment_metadata( $id, $original_meta );
13991490
}
14001491
}

0 commit comments

Comments
 (0)