Skip to content

Prevent overwriting localized media with colliding filenames - #922

Open
gohelkunjan wants to merge 1 commit into
WordPress:trunkfrom
gohelkunjan:fix/911-unique-localized-media-filenames
Open

gohelkunjan wants to merge 1 commit into
WordPress:trunkfrom
gohelkunjan:fix/911-unique-localized-media-filenames

Conversation

@gohelkunjan

Copy link
Copy Markdown

Fixes #911

Description

When localizing multiple distinct media URLs that share the same basename (e.g., /2024/01/hero.jpg and /2025/06/hero.jpg), the second download previously overwrote the first file in assets/images/ (and in exported ZIP archives), causing both blocks to point to the same file.

Changes

  • Added CBT_Theme_Media::get_unique_media_filename() and per-URL relative path tracking in CBT_Theme_Media so colliding filenames receive unique suffixes (hero-1.jpg, hero-2.jpg, etc.) while identical URLs continue to reuse the same local asset.
  • Updated CBT_Theme_Media::add_media_to_local() and CBT_Theme_Zip::add_media_to_zip() (with CBT_Zip_Archive::locateNameInTheme()) to check for existing files/entries before writing.
  • Updated CBT_Theme_Media::get_media_relative_path_from_url() to return the assigned unique path so make_template_images_local() and CBT_Token_Processor rewrite each block to its matching asset.
  • Added unit tests in tests/test-theme-media.php and tests/test-theme-zip.php.

Testing

  • Ran php -l on all modified PHP files and git diff --check locally.
  • PHPUnit suite will run via GitHub Actions CI on this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Localizing images with the same filename overwrites one image

1 participant