Skip to content

Commit

Permalink
add figcaption escaping for video and image blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Nov 7, 2024
1 parent 13cb16c commit bc1aeb6
Showing 1 changed file with 16 additions and 18 deletions.
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

0 comments on commit bc1aeb6

Please sign in to comment.