From bc1aeb60cf95126143f1324c06957d80791c25f1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 7 Nov 2024 11:26:00 -0300 Subject: [PATCH 1/2] add figcaption escaping for video and image blocks. --- includes/create-theme/theme-locale.php | 34 ++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/includes/create-theme/theme-locale.php b/includes/create-theme/theme-locale.php index 7f105022..4a2a1bcf 100644 --- a/includes/create-theme/theme-locale.php +++ b/includes/create-theme/theme-locale.php @@ -101,10 +101,6 @@ private static function get_text_replacement_patterns_for_html( $block_name ) { return array( '/(]*>)(.*?)(<\/pre>)/' ); case 'core/button': return array( '/(]*>)(.*?)(<\/a>)/' ); - case 'core/image': - case 'core/cover': - case 'core/media-text': - return array( '/alt="(.*?)"/' ); case 'core/quote': case 'core/pullquote': return array( @@ -117,6 +113,16 @@ private static function get_text_replacement_patterns_for_html( $block_name ) { '/(]*>)(.*?)(<\/th>)/', '/(]*>)(.*?)(<\/figcaption>)/', ); + case 'core/video': + return array( '/(]*>)(.*?)(<\/figcaption>)/' ); + case 'core/image': + return array( + '/(]*>)(.*?)(<\/figcaption>)/', + '/(alt=")(.*?)(")/', + ); + case 'core/cover': + case 'core/media-text': + return array( '/(alt=")(.*?)(")/' ); default: return null; } @@ -158,19 +164,7 @@ public static function escape_text_content_of_blocks( $blocks ) { case 'core/quote': case 'core/pullquote': case 'core/table': - $replace_content_callback = function ( $content, $pattern ) { - if ( empty( $content ) ) { - return; - } - return preg_replace_callback( - $pattern, - function( $matches ) { - return $matches[1] . self::escape_text_content( $matches[2] ) . $matches[3]; - }, - $content - ); - }; - break; + case 'core/video': case 'core/image': case 'core/cover': case 'core/media-text': @@ -181,7 +175,11 @@ function( $matches ) { return preg_replace_callback( $pattern, function( $matches ) { - return 'alt="' . self::escape_attribute( $matches[1] ) . '"'; + // If the pattern is for attribute like alt="". + if ( str_ends_with( $matches[1], '="' ) ) { + return $matches[1] . self::escape_attribute( $matches[2] ) . $matches[3]; + } + return $matches[1] . self::escape_text_content( $matches[2] ) . $matches[3]; }, $content ); From 3049fef14a4659a80a7abe906e8707067caa65ca Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 7 Nov 2024 11:26:17 -0300 Subject: [PATCH 2/2] update image block test and add video block test --- .../escapeTextContentOfBlocks.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php index 50e85891..40c4f631 100644 --- a/tests/CbtThemeLocale/escapeTextContentOfBlocks.php +++ b/tests/CbtThemeLocale/escapeTextContentOfBlocks.php @@ -125,12 +125,12 @@ public function data_test_escape_text_content_of_blocks() { 'image' => array( 'block_markup' => - ' -
Windows of a building in Nuremberg, Germany
+ ' +
image alt text
Image caption
', 'expected_markup' => - ' -
<?php esc_attr_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>
+ ' +
<?php esc_attr_e(\'image alt text\', \'test-locale-theme\');?>
', ), @@ -186,6 +186,15 @@ public function data_test_escape_text_content_of_blocks() { ', ), + 'video' => array( + 'block_markup' => ' +
Video caption test
+', + 'expected_markup' => + ' +
+', + ), ); } }