mirror of
https://github.com/WordPress/create-block-theme.git
synced 2025-10-03 16:11:13 +08:00
Co-authored-by: Vicente Canales <vicente.canales@automattic.com> Co-authored-by: Vicente Canales <1157901+vcanales@users.noreply.github.com> Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
158 lines
4.4 KiB
PHP
158 lines
4.4 KiB
PHP
<?php
|
|
/*
|
|
* Locale related functionality
|
|
*/
|
|
class CBT_Theme_Locale {
|
|
|
|
/**
|
|
* Escape a string for localization.
|
|
*
|
|
* @param string $string The string to escape.
|
|
* @return string The escaped string.
|
|
*/
|
|
public static function escape_string( $string ) {
|
|
// Avoid escaping if the text is not a string.
|
|
if ( ! is_string( $string ) ) {
|
|
return $string;
|
|
}
|
|
|
|
// Check if the text is already escaped.
|
|
if ( str_starts_with( $string, '<?php echo' ) ) {
|
|
return $string;
|
|
}
|
|
|
|
$string = addcslashes( $string, "'" );
|
|
return "<?php echo __('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>";
|
|
}
|
|
|
|
/**
|
|
* Get a replacement pattern for escaping the text from the html content of a block.
|
|
*
|
|
* @param string $block_name The block name.
|
|
* @return array|null The regex patterns to match the content that needs to be escaped.
|
|
* Returns null if the block is not supported.
|
|
* Returns an array of regex patterns if the block has html elements that need to be escaped.
|
|
*/
|
|
private static function get_text_replacement_patterns_for_html( $block_name ) {
|
|
switch ( $block_name ) {
|
|
case 'core/paragraph':
|
|
return array( '/(<p[^>]*>)(.*?)(<\/p>)/' );
|
|
case 'core/heading':
|
|
return array( '/(<h[^>]*>)(.*?)(<\/h[^>]*>)/' );
|
|
case 'core/list-item':
|
|
return array( '/(<li[^>]*>)(.*?)(<\/li>)/' );
|
|
case 'core/verse':
|
|
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(
|
|
'/(<p[^>]*>)(.*?)(<\/p>)/',
|
|
'/(<cite[^>]*>)(.*?)(<\/cite>)/',
|
|
);
|
|
case 'core/table':
|
|
return array(
|
|
'/(<td[^>]*>)(.*?)(<\/td>)/',
|
|
'/(<th[^>]*>)(.*?)(<\/th>)/',
|
|
'/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/',
|
|
);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Localize text in text blocks.
|
|
*
|
|
* @param array $blocks The blocks to localize.
|
|
* @return array The localized blocks.
|
|
*/
|
|
public static function escape_text_content_of_blocks( $blocks ) {
|
|
foreach ( $blocks as &$block ) {
|
|
|
|
// Recursively escape the inner blocks.
|
|
if ( ! empty( $block['innerBlocks'] ) ) {
|
|
$block['innerBlocks'] = self::escape_text_content_of_blocks( $block['innerBlocks'] );
|
|
}
|
|
|
|
/*
|
|
* Set the pattern based on the block type.
|
|
* The pattern is used to match the content that needs to be escaped.
|
|
* Patterns are defined in the get_text_replacement_patterns_for_html method.
|
|
*/
|
|
$patterns = self::get_text_replacement_patterns_for_html( $block['blockName'] );
|
|
|
|
// If the block does not have any patterns leave the block as is and continue to the next block.
|
|
if ( ! $patterns ) {
|
|
continue;
|
|
}
|
|
|
|
// Builds the replacement callback function based on the block type.
|
|
switch ( $block['blockName'] ) {
|
|
case 'core/paragraph':
|
|
case 'core/heading':
|
|
case 'core/list-item':
|
|
case 'core/verse':
|
|
case 'core/button':
|
|
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_string( $matches[2] ) . $matches[3];
|
|
},
|
|
$content
|
|
);
|
|
};
|
|
break;
|
|
case 'core/image':
|
|
case 'core/cover':
|
|
case 'core/media-text':
|
|
$replace_content_callback = function ( $content, $pattern ) {
|
|
if ( empty( $content ) ) {
|
|
return;
|
|
}
|
|
return preg_replace_callback(
|
|
$pattern,
|
|
function( $matches ) {
|
|
return 'alt="' . self::escape_string( $matches[1] ) . '"';
|
|
},
|
|
$content
|
|
);
|
|
};
|
|
break;
|
|
default:
|
|
$replace_content_callback = null;
|
|
break;
|
|
}
|
|
|
|
// Apply the replacement patterns to the block content.
|
|
foreach ( $patterns as $pattern ) {
|
|
if (
|
|
! empty( $block['innerContent'] ) &&
|
|
is_callable( $replace_content_callback )
|
|
) {
|
|
$block['innerContent'] = is_array( $block['innerContent'] )
|
|
? array_map(
|
|
function( $content ) use ( $replace_content_callback, $pattern ) {
|
|
return $replace_content_callback( $content, $pattern );
|
|
},
|
|
$block['innerContent']
|
|
)
|
|
: $replace_content_callback( $block['innerContent'], $pattern );
|
|
}
|
|
}
|
|
}
|
|
return $blocks;
|
|
}
|
|
}
|