Skip to content

Commit 952cfe3

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 952cfe3

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/Media_Command.php

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

0 commit comments

Comments
 (0)