Skip to content

Escape regex dots when stripping .min suffix#457

Merged
swissspidy merged 4 commits into
wp-cli:mainfrom
mrfade:fix/i18n/escape-regex-dots
Oct 4, 2025
Merged

Escape regex dots when stripping .min suffix#457
swissspidy merged 4 commits into
wp-cli:mainfrom
mrfade:fix/i18n/escape-regex-dots

Conversation

@mrfade
Copy link
Copy Markdown
Contributor

@mrfade mrfade commented Oct 3, 2025

Summary

Fixes an incorrect regex replacement that caused generated translation file hashes not to match WordPress’ expectations.

Fixes #456

Problem

The code used the following expression when stripping .min from file names:

preg_replace( "/.min.{$extension}$/", ".{$extension}", $file );

Because the . characters were not escaped, the regex matched any character, not a literal dot.
Example:

  • Input: my-plugin-admin.js
  • Regex consumed "dmin." (because . matched d)
  • Output: my-plugin-a.js

This resulted in WordPress computing the wrong hash for the script source, so it tried to load a non-existent JSON translation file. As a result, JavaScript translations were never found.

Fix

Escape the dot characters in the regex so they are treated literally:

preg_replace( "/\.min\.{$extension}$/", ".{$extension}", $file );

Now only .min.js (or the correct extension) is matched, and file names remain intact (my-plugin-admin.js). The translation JSON filenames are generated with the correct hash and translations load as expected.

@mrfade mrfade requested a review from a team as a code owner October 3, 2025 20:05
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy
Copy link
Copy Markdown
Member

Good catch! Thanks for the PR. At first glsnce a separate test class just for this one line change is a bit overkill. Could we just add another Behag test case instead?

@mrfade
Copy link
Copy Markdown
Contributor Author

mrfade commented Oct 3, 2025

You're absolutely right! A separate test class for testing one regex line is indeed overkill. I've removed the PHPUnit test and added a simple Behat test case instead.

@swissspidy swissspidy changed the title fix(i18n): escape regex dots when stripping .min to prevent filename truncation Escape regex dots when stripping .min suffix Oct 4, 2025
@swissspidy swissspidy added bug command:i18n-make-json Related to 'i18n make-json' command labels Oct 4, 2025
@swissspidy swissspidy added this to the 2.6.6 milestone Oct 4, 2025
@swissspidy swissspidy merged commit 5e847ba into wp-cli:main Oct 4, 2025
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:i18n-make-json Related to 'i18n make-json' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: wp-cli i18n make-json Generates Incorrect source Path with --use-map in Newer Versions

2 participants