Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions includes/create-theme/theme-locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ class CBT_Theme_Locale {
* @return string The escaped string.
*/
private static function escape_block_attribute( $string ) {
// Avoid escaping if the text is not a string.
if ( ! is_string( $string ) ) {
return $string;
}

// Check if string is empty.
if ( '' === $string ) {
return $string;
}

// Check if the text is already escaped.
if ( str_starts_with( $string, '<?php' ) ) {
return $string;
}

$tokenized = self::tokenize_block_attribute_for_php_string( $string );
$text_domain = CBT_Theme_Utils::escape_php_single_quoted_string( wp_get_theme()->get( 'TextDomain' ) );

Expand Down Expand Up @@ -412,8 +427,8 @@ function ( $matches ) {
$modified = false;
foreach ( $localizable_attrs as $attr_name ) {
if ( isset( $attrs[ $attr_name ] ) && is_string( $attrs[ $attr_name ] ) ) {
// Skip if already escaped.
if ( str_starts_with( $attrs[ $attr_name ], '<?php' ) ) {
// Skip if empty or already escaped.
if ( '' === $attrs[ $attr_name ] || str_starts_with( $attrs[ $attr_name ], '<?php' ) ) {
continue;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/CbtThemeLocale/escapeBlockAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ public function data_test_escape_block_attributes() {
'block_markup' => '<!-- wp:search {"showLabel":false,"buttonPosition":"button-inside"} /-->',
'expected_markup' => '<!-- wp:search {"showLabel":false,"buttonPosition":"button-inside"} /-->',
),

'post-excerpt with empty moreText should remain unchanged' => array(
'block_markup' => '<!-- wp:post-excerpt {"moreText":""} /-->',
'expected_markup' => '<!-- wp:post-excerpt {"moreText":""} /-->',
),

'search block with empty placeholder and translatable buttonText' => array(
'block_markup' => '<!-- wp:search {"placeholder":"","buttonText":"Search"} /-->',
'expected_markup' => '<!-- wp:search {"placeholder":"","buttonText":"<?php esc_attr_e(\'Search\', \'test-locale-theme\');?>"} /-->',
),
);
}
}
Loading