Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add figcaption escaping for image and video blocks #745

Merged
Merged
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
34 changes: 16 additions & 18 deletions includes/create-theme/theme-locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ private static function get_text_replacement_patterns_for_html( $block_name ) {
return array( '/(<pre[^>]*>)(.*?)(<\/pre>)/' );
case 'core/button':
return array( '/(<a[^>]*>)(.*?)(<\/a>)/' );
case 'core/image':
case 'core/cover':
case 'core/media-text':
return array( '/alt="(.*?)"/' );
case 'core/quote':
case 'core/pullquote':
return array(
Expand All @@ -117,6 +113,16 @@ private static function get_text_replacement_patterns_for_html( $block_name ) {
'/(<th[^>]*>)(.*?)(<\/th>)/',
'/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/',
);
case 'core/video':
return array( '/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/' );
case 'core/image':
return array(
'/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/',
'/(alt=")(.*?)(")/',
);
case 'core/cover':
case 'core/media-text':
return array( '/(alt=")(.*?)(")/' );
default:
return null;
}
Expand Down Expand Up @@ -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':
Expand All @@ -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
);
Expand Down
17 changes: 13 additions & 4 deletions tests/CbtThemeLocale/escapeTextContentOfBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public function data_test_escape_text_content_of_blocks() {

'image' => array(
'block_markup' =>
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} -->
<figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="Windows of a building in Nuremberg, Germany"/></figure>
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="http://example.org/image.webp" alt="image alt text" class="wp-image-151"/><figcaption class="wp-element-caption">Image caption</figcaption></figure>
<!-- /wp:image -->',
'expected_markup' =>
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} -->
<figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>"/></figure>
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="http://example.org/image.webp" alt="<?php esc_attr_e(\'image alt text\', \'test-locale-theme\');?>" class="wp-image-151"/><figcaption class="wp-element-caption"><?php esc_html_e(\'Image caption\', \'test-locale-theme\');?></figcaption></figure>
<!-- /wp:image -->',
),

Expand Down Expand Up @@ -186,6 +186,15 @@ public function data_test_escape_text_content_of_blocks() {
<!-- /wp:table -->',
),

'video' => array(
'block_markup' => '<!-- wp:video -->
<figure class="wp-block-video"><video controls src="http://example.org/video.mp4"></video><figcaption class="wp-element-caption">Video caption test</figcaption></figure>
<!-- /wp:video -->',
'expected_markup' =>
'<!-- wp:video -->
<figure class="wp-block-video"><video controls src="http://example.org/video.mp4"></video><figcaption class="wp-element-caption"><?php esc_html_e(\'Video caption test\', \'test-locale-theme\');?></figcaption></figure>
<!-- /wp:video -->',
),
);
}
}
Loading