Skip to content

Commit 678a8ef

Browse files
committed
Process all attributes
Escape at the end. Provide better translation note.
1 parent 45c312c commit 678a8ef

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

includes/create-theme/theme-locale.php

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,59 +31,57 @@ private static function escape_text_content( $string ) {
3131
// Process the string to avoid escaping inner HTML markup.
3232
$p = new WP_HTML_Tag_Processor( $string );
3333

34-
$text = '';
35-
$tokens = array();
34+
$text = '';
35+
$tokens = array();
36+
$increment = 0;
37+
$translators_note = "\n/* Translators: ";
3638
while ( $p->next_token() ) {
3739
$token_type = $p->get_token_type();
3840
$token_name = strtolower( $p->get_token_name() );
3941
$is_tag_closer = $p->is_tag_closer();
4042

4143
if ( '#tag' === $token_type ) {
42-
// Add a placeholder for the token.
43-
$text .= '%s';
44+
$increment++;
45+
$text .= '%' . $increment . '$s';
46+
if ( 1 !== $increment ) {
47+
$translators_note .= ', ';
48+
}
4449
if ( $is_tag_closer ) {
45-
$tokens[] = "</{$token_name}>";
50+
$tokens[] = "</{$token_name}>";
51+
$translators_note .= '%' . $increment . "\$s is the end of a '" . $token_name . "' HTML element";
4652
} else {
47-
// Depending on the HTML tag, we may need to process attributes so they are correctly added to the placeholder.
48-
switch ( $token_name ) {
49-
// Handle links.
50-
case 'a':
51-
$href = esc_url( $p->get_attribute( 'href' ) );
52-
$target = empty( esc_attr( $p->get_attribute( 'target' ) ) ) ? '' : ' target="_blank"';
53-
$rel = empty( esc_attr( $p->get_attribute( 'rel' ) ) ) ? '' : ' rel="nofollow noopener noreferrer"';
54-
$tokens[] = "<a href=\"{$href}\"{$target}{$rel}>";
55-
break;
56-
// Handle inline images.
57-
case 'img':
58-
$src = esc_url( $p->get_attribute( 'src' ) );
59-
$style = esc_attr( $p->get_attribute( 'style' ) );
60-
$alt = esc_attr( $p->get_attribute( 'alt' ) );
61-
62-
CBT_Theme_Media::add_media_to_local( array( $src ) );
63-
$relative_src = CBT_Theme_Media::get_media_folder_path_from_url( $src ) . basename( $src );
64-
$tokens[] = "<img style=\"{$style}\" src=\"' . esc_url( get_stylesheet_directory_uri() ) . '{$relative_src}\" alt=\"{$alt}\">";
65-
break;
66-
// Handle highlights.
67-
case 'mark':
68-
$style = esc_attr( $p->get_attribute( 'style' ) );
69-
$class = esc_attr( $p->get_attribute( 'class' ) );
70-
$tokens[] = "<mark style=\"{$style}\" class=\"{$class}\">";
71-
break;
72-
// Otherwise, just add the tag opener.
73-
default:
74-
$tokens[] = "<{$token_name}>";
75-
break;
53+
$token = '<' . $token_name;
54+
// Get all attributes of the tag.
55+
$attributes = $p->get_attribute_names_with_prefix( '' );
56+
57+
// Add the attributes to the token, processing and escaping where necessary.
58+
foreach ( $attributes as $attr_name ) {
59+
$attr_value = $p->get_attribute( $attr_name );
60+
if ( empty( $attr_value ) ) {
61+
$token .= ' ' . $attr_name;
62+
continue;
63+
} elseif ( 'src' === $attr_name ) {
64+
CBT_Theme_Media::add_media_to_local( array( $attr_value ) );
65+
$relative_src = CBT_Theme_Media::get_media_folder_path_from_url( $attr_value ) . basename( $attr_value );
66+
$attr_value = "' . esc_url( get_stylesheet_directory_uri() ) . '{$relative_src}";
67+
} elseif ( 'href' === $attr_name ) {
68+
$attr_value = "' . esc_url( '$attr_value' ) . '";
69+
}
70+
$token .= ' ' . $attr_name . '="' . $attr_value . '"';
7671
}
72+
$token .= '>';
73+
$tokens[] = $token;
74+
$translators_note .= '%' . $increment . "\$s is the start of a '" . $token_name . "' HTML element";
7775
}
7876
} else {
79-
// If it's not a tag, just add the text content.
80-
$text .= esc_html( $p->get_modifiable_text() );
77+
$text .= $p->get_modifiable_text();
8178
}
8279
}
83-
// If tokens is not empty, format the string using sprintf.
80+
8481
if ( ! empty( $tokens ) ) {
85-
// Format the string, replacing the placeholders with the formatted tokens.
86-
return "<?php /* Translators: %s are html tags */ echo sprintf( esc_html__( '$text', '" . wp_get_theme()->get( 'TextDomain' ) . "' ), " . implode(
82+
$translators_note .= ". */\n";
83+
// Return the formatted text, substituting the placeholders with the tokens.
84+
return "<?php $translators_note echo sprintf( esc_html__( '$text', '" . wp_get_theme()->get( 'TextDomain' ) . "' ), " . implode(
8785
', ',
8886
array_map(
8987
function( $token ) {

0 commit comments

Comments
 (0)