Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
PHPCS: i18n functions are considered safe for core translations. (#734)
Browse files Browse the repository at this point in the history
* PHPCS: i18n functions are considered safe for core translations.

* Remove obsolete phpcs:ignore comments

* Don't escape translations
  • Loading branch information
ocean90 authored and ianbelanger79 committed Oct 4, 2019
1 parent f9de427 commit edfc674
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 69 deletions.
4 changes: 2 additions & 2 deletions 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<div class="section-inner thin error404-content">

<h1 class="entry-title"><?php _e( 'Page Not Found', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></h1>
<h1 class="entry-title"><?php _e( 'Page Not Found', 'twentytwenty' ); ?></h1>

<div class="intro-text"><p><?php _e( 'The page you were looking for could not be found. It might have been removed, renamed, or did not exist in the first place.', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></p></div>
<div class="intro-text"><p><?php _e( 'The page you were looking for could not be found. It might have been removed, renamed, or did not exist in the first place.', 'twentytwenty' ); ?></p></div>

<?php
get_search_form(
Expand Down
2 changes: 1 addition & 1 deletion classes/class-twentytwenty-customize.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function register( $wp_customize ) {
$wp_customize,
'header_footer_background_color',
array(
'label' => esc_html__( 'Header & Footer Background Color', 'twentytwenty' ),
'label' => __( 'Header &amp; Footer Background Color', 'twentytwenty' ),
'section' => 'colors',
)
)
Expand Down
13 changes: 8 additions & 5 deletions classes/class-twentytwenty-walker-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ protected function html5_comment( $comment, $depth, $args ) {
}
}

/* Translators: '%1$s = comment author name */
printf( '<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>', esc_html( $comment_author ), __( 'says:', 'twentytwenty' ) ); // phpcs:ignore
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);

if ( ! empty( $comment_author_url ) ) {
echo '</a>';
Expand All @@ -71,7 +74,7 @@ protected function html5_comment( $comment, $depth, $args ) {
</a>
<?php
if ( get_edit_comment_link() ) {
echo ' &bull; <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core trusts translations
echo ' &bull; <a class="comment-edit-link" href="' . esc_url( get_edit_comment_link() ) . '">' . __( 'Edit', 'twentytwenty' ) . '</a>';
}
?>
</div><!-- .comment-metadata -->
Expand All @@ -86,7 +89,7 @@ protected function html5_comment( $comment, $depth, $args ) {

if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></p>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}

Expand Down Expand Up @@ -121,7 +124,7 @@ protected function html5_comment( $comment, $depth, $args ) {
echo $comment_reply_link; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core trusts translations
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>

Expand Down
31 changes: 14 additions & 17 deletions comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@
<h2 class="comment-reply-title">
<?php
if ( ! have_comments() ) {
esc_html_e( 'Leave a comment', 'twentytwenty' );
_e( 'Leave a comment', 'twentytwenty' );
} elseif ( '1' === $comments_number ) {
/* translators: %s: post title */
printf( esc_html_x( 'One reply on &ldquo;%s&rdquo;', 'comments title', 'twentytwenty' ), esc_html( get_the_title() ) );
printf( _x( 'One reply on &ldquo;%s&rdquo;', 'comments title', 'twentytwenty' ), esc_html( get_the_title() ) );
} else {
echo esc_html(
sprintf(
/* translators: 1: number of comments, 2: post title */
_nx(
'%1$s reply on &ldquo;%2$s&rdquo;',
'%1$s replies on &ldquo;%2$s&rdquo;',
$comments_number,
'comments title',
'twentytwenty'
),
number_format_i18n( $comments_number ),
esc_html( get_the_title() )
)
echo sprintf(
/* translators: 1: number of comments, 2: post title */
_nx(
'%1$s reply on &ldquo;%2$s&rdquo;',
'%1$s replies on &ldquo;%2$s&rdquo;',
$comments_number,
'comments title',
'twentytwenty'
),
number_format_i18n( $comments_number ),
esc_html( get_the_title() )
);
}

Expand All @@ -60,7 +58,6 @@
<div class="comments-inner section-inner thin max-percentage">

<?php

wp_list_comments(
array(
'walker' => new TwentyTwenty_Walker_Comment(),
Expand Down Expand Up @@ -127,7 +124,7 @@

<div class="comment-respond" id="respond">

<p class="comments-closed"><?php _e( 'Comments are closed.', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></p>
<p class="comments-closed"><?php _e( 'Comments are closed.', 'twentytwenty' ); ?></p>

</div><!-- #respond -->

Expand Down
16 changes: 6 additions & 10 deletions footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@

<p class="footer-copyright">&copy;
<?php
echo esc_html(
date_i18n(
/* translators: Copyright date format, see https://secure.php.net/date */
_x( 'Y', 'copyright date format', 'twentytwenty' )
)
echo date_i18n(
/* translators: Copyright date format, see https://secure.php.net/date */
_x( 'Y', 'copyright date format', 'twentytwenty' )
);
?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php echo bloginfo( 'name' ); ?></a>
</p>

<p class="powered-by-wordpress">
<a href="<?php echo esc_url( __( 'https://wordpress.org/', 'twentytwenty' ) ); ?>">
<?php
_e( 'Powered by WordPress', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations
?>
<?php _e( 'Powered by WordPress', 'twentytwenty' ); ?>
</a>
</p><!-- .powered-by-wordpress -->

Expand All @@ -44,13 +40,13 @@
<span class="to-the-top-long">
<?php
/* translators: %s: HTML character for up arrow */
printf( esc_html( __( 'To the top %s', 'twentytwenty' ) ), '<span class="arrow">&uarr;</span>' );
printf( __( 'To the top %s', 'twentytwenty' ), '<span class="arrow">&uarr;</span>' );
?>
</span><!-- .to-the-top-long -->
<span class="to-the-top-short">
<?php
/* translators: %s: HTML character for up arrow */
printf( esc_html( __( 'Up %s', 'twentytwenty' ) ), '<span class="arrow">&uarr;</span>' );
printf( __( 'Up %s', 'twentytwenty' ), '<span class="arrow">&uarr;</span>' );
?>
</span><!-- .to-the-top-short -->
</a><!-- .to-the-top -->
Expand Down
10 changes: 5 additions & 5 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function wp_body_open() {
* Include a skip to content link at the top of the page so that users can bypass the menu.
*/
function twentytwenty_skip_link() {
echo '<a class="skip-link screen-reader-text" href="#site-content">' . __( 'Skip to the content', 'twentytwenty' ) . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core trusts translations
echo '<a class="skip-link screen-reader-text" href="#site-content">' . __( 'Skip to the content', 'twentytwenty' ) . '</a>';
}

add_action( 'wp_body_open', 'twentytwenty_skip_link', 5 );
Expand Down Expand Up @@ -398,17 +398,17 @@ function twentytwenty_block_editor_settings() {
// Block Editor Palette.
$editor_color_palette = array(
array(
'name' => esc_html__( 'Accent Color', 'twentytwenty' ),
'name' => __( 'Accent Color', 'twentytwenty' ),
'slug' => 'accent',
'color' => twentytwenty_get_color_for_area( 'content', 'accent' ),
),
array(
'name' => esc_html__( 'Secondary', 'twentytwenty' ),
'name' => __( 'Secondary', 'twentytwenty' ),
'slug' => 'secondary',
'color' => twentytwenty_get_color_for_area( 'content', 'secondary' ),
),
array(
'name' => esc_html__( 'Subtle Background', 'twentytwenty' ),
'name' => __( 'Subtle Background', 'twentytwenty' ),
'slug' => 'subtle-background',
'color' => twentytwenty_get_color_for_area( 'content', 'borders' ),
),
Expand Down Expand Up @@ -480,7 +480,7 @@ function twentytwenty_read_more_tag() {
return sprintf(
'<a href="%1$s" class="more-link faux-button">%2$s <span class="screen-reader-text">"%3$s"</span></a>',
esc_url( get_permalink( get_the_ID() ) ),
esc_html__( 'Continue reading', 'twentytwenty' ),
__( 'Continue reading', 'twentytwenty' ),
esc_html( get_the_title( get_the_ID() ) )
);
}
Expand Down
8 changes: 4 additions & 4 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<span class="toggle-icon">
<?php twentytwenty_the_theme_svg( 'search' ); ?>
</span>
<span class="toggle-text"><?php _e( 'Search', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="toggle-text"><?php _e( 'Search', 'twentytwenty' ); ?></span>
</span>
</button><!-- .search-toggle -->

Expand All @@ -73,7 +73,7 @@
<span class="toggle-icon">
<?php twentytwenty_the_theme_svg( 'ellipsis' ); ?>
</span>
<span class="toggle-text"><?php _e( 'Menu', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="toggle-text"><?php _e( 'Menu', 'twentytwenty' ); ?></span>
</span>
</button><!-- .nav-toggle -->

Expand Down Expand Up @@ -143,7 +143,7 @@

<button class="toggle nav-toggle desktop-nav-toggle" data-toggle-target=".menu-modal" data-toggle-body-class="showing-menu-modal" aria-expanded="false" data-set-focus=".close-nav-toggle">
<span class="toggle-inner">
<span class="toggle-text"><?php _e( 'Menu', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="toggle-text"><?php _e( 'Menu', 'twentytwenty' ); ?></span>
<span class="toggle-icon">
<?php twentytwenty_the_theme_svg( 'ellipsis' ); ?>
</span>
Expand All @@ -161,7 +161,7 @@
<button class="toggle search-toggle desktop-search-toggle" data-toggle-target=".search-modal" data-toggle-body-class="showing-search-modal" data-set-focus=".search-modal .search-field" aria-expanded="false">
<span class="toggle-inner">
<?php twentytwenty_the_theme_svg( 'search' ); ?>
<span class="toggle-text"><?php _e( 'Search', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="toggle-text"><?php _e( 'Search', 'twentytwenty' ); ?></span>
</span>
</button><!-- .search-toggle -->

Expand Down
14 changes: 7 additions & 7 deletions inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' )
?>
<li class="post-author meta-wrapper">
<span class="meta-icon">
<span class="screen-reader-text"><?php _e( 'Post author', 'twentytwenty' );// phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="screen-reader-text"><?php _e( 'Post author', 'twentytwenty' ); ?></span>
<?php twentytwenty_the_theme_svg( 'user' ); ?>
</span>
<span class="meta-text">
<?php
printf(
/* translators: %s: Author name */
__( 'By %s', 'twentytwenty' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core trusts translations
__( 'By %s', 'twentytwenty' ),
'<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author_meta( 'display_name' ) ) . '</a>'
);
?>
Expand All @@ -316,7 +316,7 @@ function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' )
<li class="post-date">
<a class="meta-wrapper" href="<?php the_permalink(); ?>">
<span class="meta-icon">
<span class="screen-reader-text"><?php _e( 'Post date', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="screen-reader-text"><?php _e( 'Post date', 'twentytwenty' ); ?></span>
<?php twentytwenty_the_theme_svg( 'calendar' ); ?>
</span>
<span class="meta-text">
Expand All @@ -335,11 +335,11 @@ function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' )
?>
<li class="post-categories meta-wrapper">
<span class="meta-icon">
<span class="screen-reader-text"><?php _e( 'Categories', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="screen-reader-text"><?php _e( 'Categories', 'twentytwenty' ); ?></span>
<?php twentytwenty_the_theme_svg( 'folder' ); ?>
</span>
<span class="meta-text">
<?php _e( 'In', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?> <?php the_category( ', ' ); ?>
<?php _e( 'In', 'twentytwenty' ); ?> <?php the_category( ', ' ); ?>
</span>
</li>
<?php
Expand All @@ -353,7 +353,7 @@ function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' )
?>
<li class="post-tags meta-wrapper">
<span class="meta-icon">
<span class="screen-reader-text"><?php _e( 'Tags', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></span>
<span class="screen-reader-text"><?php _e( 'Tags', 'twentytwenty' ); ?></span>
<?php twentytwenty_the_theme_svg( 'tag' ); ?>
</span>
<span class="meta-text">
Expand Down Expand Up @@ -391,7 +391,7 @@ function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' )
<?php twentytwenty_the_theme_svg( 'bookmark' ); ?>
</span>
<span class="meta-text">
<?php _e( 'Sticky post', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?>
<?php _e( 'Sticky post', 'twentytwenty' ); ?>
</span>
</li>
<?php
Expand Down
33 changes: 26 additions & 7 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
</properties>
</rule>

<!-- Allow multiple parameters on one line for multi-line function calls. -->
<rule ref="PEAR.Functions.FunctionCallSignature">
<!-- Allow multiple parameters on one line for multi-line function calls. -->
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="allowMultipleArguments" value="true" />
</properties>
</rule>
</rule>

<!-- Improve code readablilty by allowing the artguments after function call. -->
<!-- Improve code readablilty by allowing the artguments after function call. -->
<rule ref="PEAR.Functions.FunctionCallSignature">
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
</rule>
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
</rule>

<!-- Check valid textdomain. -->
<rule ref="WordPress.WP.I18n">
Expand All @@ -56,4 +56,23 @@
</properties>
</rule>

<rule ref="WordPress-Extra">
<!-- _e() and _ex() are considered safe for core translations. -->
<exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>
</rule>

<rule ref="WordPress.Security.EscapeOutput">
<properties>
<property name="customAutoEscapedFunctions" type="array">
<!-- i18n functions are considered safe for core translations. -->
<element value="__"/>
<element value="_x"/>
<element value="_n"/>
<element value="_nx"/>
<element value="number_format_i18n"/>
<element value="date_i18n"/>
</property>
</properties>
</rule>

</ruleset>
4 changes: 2 additions & 2 deletions template-parts/content-cover.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
?>

<div class="entry-categories">
<span class="screen-reader-text"><?php esc_html_e( 'Categories', 'twentytwenty' ); ?></span>
<span class="screen-reader-text"><?php _e( 'Categories', 'twentytwenty' ); ?></span>
<div class="entry-categories-inner">
<?php the_category( ' ' ); ?>
</div><!-- .entry-categories-inner -->
Expand All @@ -85,7 +85,7 @@

<a href="#post-inner" class="to-the-content fill-children-current-color">
<?php twentytwenty_the_theme_svg( 'arrow-down' ); ?>
<div class="screen-reader-text"><?php _e( 'Scroll Down', 'twentytwenty' ); // phpcs:ignore WordPress.Security.EscapeOutput.UnsafePrintingFunction -- core trusts translations ?></div>
<div class="screen-reader-text"><?php _e( 'Scroll Down', 'twentytwenty' ); ?></div>
</a><!-- .to-the-content -->

</div><!-- .to-the-content-wrapper -->
Expand Down
4 changes: 2 additions & 2 deletions template-parts/entry-author-bio.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<?php
printf(
/* translators: %s: Author name */
__( 'By %s', 'twentytwenty' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- core trusts translations
__( 'By %s', 'twentytwenty' ),
esc_html( get_the_author() )
);
?>
Expand All @@ -26,7 +26,7 @@
<p class="author-description">
<?php the_author_meta( 'description' ); ?>
<a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author">
<?php esc_html_e( 'View Archive &rarr;', 'twentytwenty' ); ?>
<?php _e( 'View Archive &rarr;', 'twentytwenty' ); ?>
</a>
</p><!-- .author-description -->
</div><!-- .author-bio -->
Expand Down
2 changes: 1 addition & 1 deletion template-parts/entry-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
?>

<div class="entry-categories">
<span class="screen-reader-text"><?php esc_html_e( 'Categories', 'twentytwenty' ); ?></span>
<span class="screen-reader-text"><?php _e( 'Categories', 'twentytwenty' ); ?></span>
<div class="entry-categories-inner">
<?php the_category( ' ' ); ?>
</div><!-- .entry-categories-inner -->
Expand Down
Loading

0 comments on commit edfc674

Please sign in to comment.