Skip to content

Commit e94bf00

Browse files
Copilotswissspidy
andcommitted
Fix duplicate detection for WP 5.3+ scaled images via -scaled basename variant
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 04b6bdc commit e94bf00

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

src/Media_Command.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -721,23 +721,32 @@ private function make_copy( $path ) {
721721
*
722722
* Searches the `_wp_attached_file` post meta, which stores the path relative to
723723
* the uploads directory (e.g. '2026/03/image.jpg' or just 'image.jpg'). Also
724-
* checks `_wp_original_image_file` (absolute path, WP 5.3+) to handle images
725-
* that were scaled down on upload (stored as 'image-scaled.jpg') but whose
726-
* original filename is still 'image.jpg'. Matches the first attachment found
727-
* when multiple files share the same basename across different subdirectories.
724+
* checks for the WP 5.3+ big-image scaled variant (e.g. 'image-scaled.jpg') so
725+
* that re-importing a large file that was scaled on first import is correctly
726+
* detected as a duplicate. Matches the first attachment found when multiple files
727+
* share the same basename across different upload subdirectories.
728728
*
729729
* @param string $basename Filename basename to search for (e.g. 'image.jpg').
730730
* @return int|false Attachment ID if found, false otherwise.
731731
*/
732732
private function find_duplicate_attachment( $basename ) {
733733
global $wpdb;
734734

735-
$slash_basename = '/' . $basename;
735+
// WP 5.3+ big-image scaling renames 'image.jpg' → 'image-scaled.jpg' and
736+
// stores the scaled name in _wp_attached_file, so search for both variants.
737+
$ext = pathinfo( $basename, PATHINFO_EXTENSION );
738+
$name = pathinfo( $basename, PATHINFO_FILENAME );
739+
$scaled_basename = $name . '-scaled' . ( $ext ? '.' . $ext : '' );
740+
741+
$slash_basename = '/' . $basename;
742+
$slash_scaled_basename = '/' . $scaled_basename;
736743

737744
if ( function_exists( 'mb_strlen' ) ) {
738-
$slash_basename_length = mb_strlen( $slash_basename, 'UTF-8' );
745+
$slash_basename_length = mb_strlen( $slash_basename, 'UTF-8' );
746+
$slash_scaled_basename_length = mb_strlen( $slash_scaled_basename, 'UTF-8' );
739747
} else {
740-
$slash_basename_length = strlen( $slash_basename );
748+
$slash_basename_length = strlen( $slash_basename );
749+
$slash_scaled_basename_length = strlen( $slash_scaled_basename );
741750
}
742751

743752
$result = $wpdb->get_var(
@@ -748,12 +757,20 @@ private function find_duplicate_attachment( $basename ) {
748757
ON p.ID = pm.post_id
749758
WHERE p.post_type = 'attachment'
750759
AND p.post_status != 'trash'
751-
AND pm.meta_key IN ('_wp_attached_file', '_wp_original_image_file')
752-
AND ( pm.meta_value = %s OR RIGHT(pm.meta_value, %d) = %s )
760+
AND pm.meta_key = '_wp_attached_file'
761+
AND (
762+
pm.meta_value = %s
763+
OR RIGHT(pm.meta_value, %d) = %s
764+
OR pm.meta_value = %s
765+
OR RIGHT(pm.meta_value, %d) = %s
766+
)
753767
LIMIT 1",
754768
$basename,
755769
$slash_basename_length,
756-
$slash_basename
770+
$slash_basename,
771+
$scaled_basename,
772+
$slash_scaled_basename_length,
773+
$slash_scaled_basename
757774
)
758775
);
759776

0 commit comments

Comments
 (0)