From 4e220d5aba1b44cdd516e7389311d6e992775b72 Mon Sep 17 00:00:00 2001 From: Birgit Pauli-Haack Date: Thu, 30 Apr 2026 15:57:54 +0200 Subject: [PATCH] i18n: Localise fileName and downloadButtonText of the File block Refs #825 --- includes/create-theme/theme-locale.php | 8 ++ .../escapeTextContentOfBlocks.php | 85 ++++++++++++++++--- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 3016dd10..110b38d0 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -125,6 +125,13 @@ private static function get_text_replacement_patterns_for_html( $block_name ) { return array( '/(alt=")(.*?)(")/' ); case 'core/details': return array( '/(]*>)(.*?)(<\/summary>)/' ); + case 'core/file': + return array( + // File-name link: without the boolean `download` attribute. + '/(])[^>])*?>)(.*?)(<\/a>)/', + // Download button: . + '/(]*\sdownload[^>]*?>)(.*?)(<\/a>)/', + ); default: return null; } @@ -197,6 +204,7 @@ public static function escape_text_content_of_blocks( $blocks ) { case 'core/cover': case 'core/media-text': case 'core/details': + case 'core/file': $replace_content_callback = function ( $content, $pattern ) { if ( empty( $content ) ) { return; diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 40c4f631..0eb79687 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -28,12 +28,12 @@ public function test_escape_text_content_of_blocks( $block_markup, $expected_mar public function data_test_escape_text_content_of_blocks() { return array( - 'paragraph' => array( + 'paragraph' => array( 'block_markup' => '

This is a test text.

', 'expected_markup' => '

', ), - 'paragraph on nested groups' => array( + 'paragraph on nested groups' => array( 'block_markup' => '
@@ -52,7 +52,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'heading 1' => array( + 'heading 1' => array( 'block_markup' => '

A passion for creating spaces

@@ -63,7 +63,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'heading 2' => array( + 'heading 2' => array( 'block_markup' => '

A passion for creating spaces

@@ -74,7 +74,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'list item' => array( + 'list item' => array( 'block_markup' => '
    @@ -101,7 +101,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'verse' => array( + 'verse' => array( 'block_markup' => '
    Ya somos el olvido que seremos.
    El polvo elemental que nos ignora
    y que fue el rojo Adán y que es ahora
    todos los hombres, y que no veremos.
    @@ -112,7 +112,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'button' => array( + 'button' => array( 'block_markup' => '
    @@ -123,7 +123,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'image' => array( + 'image' => array( 'block_markup' => '
    image alt text
    Image caption
    @@ -134,7 +134,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'cover' => array( + 'cover' => array( 'block_markup' => '
    Alternative text for cover image
    @@ -149,7 +149,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'media-text' => array( + 'media-text' => array( 'block_markup' => '
    This is alt text
    @@ -164,7 +164,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'pullquote' => array( + 'pullquote' => array( 'block_markup' => '

    Yo me equivoqué y pagué, pero la pelota no se mancha.

    Diego Armando Maradona
    @@ -175,7 +175,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'table' => array( + 'table' => array( 'block_markup' => '
    TeamPoints
    Boca74
    River2
    Score table
    @@ -186,7 +186,7 @@ public function data_test_escape_text_content_of_blocks() { ', ), - 'video' => array( + 'video' => array( 'block_markup' => '
    Video caption test
    ', @@ -195,6 +195,65 @@ public function data_test_escape_text_content_of_blocks() {
    ', ), + + 'file' => array( + 'block_markup' => ' + +', + 'expected_markup' => + ' +
    +', + ), + + 'file without download button' => array( + 'block_markup' => ' + +', + 'expected_markup' => + ' +
    +', + ), + + 'file with custom button text' => array( + 'block_markup' => ' + +', + 'expected_markup' => + ' +
    +', + ), + + 'file with formatted file name' => array( + 'block_markup' => ' + +', + 'expected_markup' => + ' + +', + ), ); } + + /** + * Running the escape twice should not re-wrap the already-localized strings. + * + * @covers CBT_Theme_Locale::escape_text_content_of_blocks + */ + public function test_file_block_escape_is_idempotent() { + $markup = ' + +'; + + $blocks = parse_blocks( $markup ); + $once_escaped = serialize_blocks( CBT_Theme_Locale::escape_text_content_of_blocks( $blocks ) ); + + $blocks_again = parse_blocks( $once_escaped ); + $twice_escaped = serialize_blocks( CBT_Theme_Locale::escape_text_content_of_blocks( $blocks_again ) ); + + $this->assertEquals( $once_escaped, $twice_escaped, 'Re-running the escape should be a no-op.' ); + } }