Skip to content

i18n: Handle backslashes in localized template text#854

Merged
MaggieCabrera merged 5 commits into
trunkfrom
codex/fix-locale-php-injection
Jun 25, 2026
Merged

i18n: Handle backslashes in localized template text#854
MaggieCabrera merged 5 commits into
trunkfrom
codex/fix-locale-php-injection

Conversation

@MaggieCabrera

Copy link
Copy Markdown
Contributor

Summary

Updates the Localize Text export path so template text with backslashes and quotes is preserved correctly when it is wrapped for translation.

This covers localized text in normal block content, inline formatted content, and block-comment attributes. The change keeps the exported theme markup valid for strings that include a backslash before a quote, and adds regression coverage for those cases.

Test plan

  • npm run test:unit:php:base -- --filter CBT_Theme_Locale
  • vendor/bin/phpcs --standard=phpcs.xml.dist includes/create-theme/theme-locale.php tests/CbtThemeLocale/base.php tests/CbtThemeLocale/escapeTextContent.php tests/CbtThemeLocale/escapeAttribute.php tests/CbtThemeLocale/escapeBlockAttributes.php

@MaggieCabrera MaggieCabrera requested a review from scruffian June 22, 2026 10:19
@MaggieCabrera MaggieCabrera self-assigned this Jun 22, 2026
Comment thread includes/create-theme/theme-locale.php Outdated
// If we modified any attributes, re-encode to JSON.
if ( $modified ) {
$new_attrs_json = wp_json_encode( $attrs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
$new_attrs_json = strtr( $new_attrs_json, $placeholders );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an edge case, but this strstr replaces __CBT_LOCALIZED_ATTRIBUTE_ across the content. Instead we could save the placeholders in an array and only replace them. I've added a commit to do this.

@scruffian

Copy link
Copy Markdown
Contributor

I commited a change to add a block-attribute-specific localization path. It tokenises unsafe characters into chr(...) placeholders, preserves translator context, and JSON-encodes the generated PHP string when rebuilding block attributes.

The previous escaping handled PHP single-quoted strings, but localised block attributes also have to survive JSON encoding in serialised block markup. Values containing backslashes, quotes, carriage returns, new lines, tabs, or % could otherwise produce unsafe or invalid generated PHP/JSON.

I also added tests to cover the injection payload, double quotes, control characters, percent placeholders, and JSON decode validity.

@MaggieCabrera MaggieCabrera left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing well for me but I can't approve, can you, @scruffian please?

@scruffian scruffian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MaggieCabrera MaggieCabrera merged commit 0d46151 into trunk Jun 25, 2026
13 checks passed
@MaggieCabrera MaggieCabrera deleted the codex/fix-locale-php-injection branch June 25, 2026 14:43
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.

2 participants