Skip to content

fix: skip empty block attribute values during localization - #884

Open
ishitaj34 wants to merge 2 commits into
WordPress:trunkfrom
ishitaj34:fix/issue-879
Open

ishitaj34 wants to merge 2 commits into
WordPress:trunkfrom
ishitaj34:fix/issue-879

Conversation

@ishitaj34

Copy link
Copy Markdown

What?

Closes #879

Prevents the Localize Text feature from wrapping empty block attribute values in translation calls during theme export.

Why?

When exporting a theme with localized text, any block attribute that had an empty string as its value was being written as, for example:

<!-- wp:post-excerpt {"moreText":"<?php esc_attr_e('', 'my-theme');?>"} /-->

This caused two issues:

  • It introduced empty translatable strings into the theme, violating WordPress Coding Standards (WordPress.WP.I18n.NoEmptyStrings).
  • It marked the block as modified, causing unnecessary JSON decoding, placeholder replacement, and attribute re-serialization even when values were empty.

How?

In includes/create-theme/theme-locale.php:

  • escape_block_attribute_strings: Added an empty string check to the skip condition. If all localizable attributes on a block are empty, $modified stays false and the original block markup is returned unchanged without re-serializing attributes.
  • escape_block_attribute: Added defensive guards to return empty or already-escaped strings unchanged, matching the behavior of escape_text_content() and escape_attribute().

In tests/CbtThemeLocale/escapeBlockAttributes.php:

  • Added test case verifying that an empty translatable attribute is left as "" rather than wrapped in esc_attr_e().
  • Added test case verifying that blocks containing both empty and non-empty translatable attributes leave empty attributes untouched while correctly escaping non-empty ones.

Testing Instructions

Unit tests:

  1. Run npm run test:unit:php:setup to start wp-env.
  2. Run npm run test:unit:php:base -- --filter CBT_Theme_Locale_EscapeBlockAttributes. All tests should pass with 0 failures.
  3. To specifically run the new test cases:
npm run test:unit:php:base -- --filter 'test_escape_block_attributes.*(post-excerpt with empty moreText|search block with empty placeholder)'

Manual test:

  1. Activate a block theme (e.g. Twenty Twenty-Five) and enable the Create Block Theme plugin.
  2. Open the Site Editor.
  3. Insert or edit an Excerpt block and clear the "Read more" text field (setting moreText to "").
  4. Go to Appearance -> Create Block Theme and export the theme with "Localize Text" checked.
  5. Open the exported file.
  6. Verify that the excerpt block contains <!-- wp:post-excerpt {"moreText":""} /--> and is not wrapped in <?php esc_attr_e('', '...');?>.

Use of AI Tools

  • AI assistance: Used for analyzing root cause, drafting test, and PR description.
  • Tool(s) used: Gemini 3.8

@ishitaj34
ishitaj34 marked this pull request as ready for review October 14, 2026 06:10
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.

Localize Text wraps empty block attribute values in a translation call

1 participant