-
Notifications
You must be signed in to change notification settings - Fork 41
New sniff to encourage using the best fit wp esc function #2816
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
Merged
Crabcyborg
merged 1 commit into
master
from
new_sniff_to_encourage_using_the_right_wp_esc_function
Jan 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEscHtmlESniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
| /** | ||
| * Sniff to convert echo esc_*( __( or _x( ) to the combined function. | ||
| * | ||
| * @package Formidable\Sniffs\CodeAnalysis | ||
| */ | ||
|
|
||
| namespace Formidable\Sniffs\CodeAnalysis; | ||
|
|
||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
| use PHP_CodeSniffer\Files\File; | ||
|
|
||
| /** | ||
| * Detects echo with escape and translation functions and suggests combined alternatives. | ||
| * | ||
| * Conversions: | ||
| * - echo esc_html( __( ... ) ) -> esc_html_e( ... ) | ||
| * - echo esc_attr( __( ... ) ) -> esc_attr_e( ... ) | ||
| * - echo esc_html( _x( ... ) ) -> esc_html_x( ... ) | ||
| * - echo esc_attr( _x( ... ) ) -> esc_attr_x( ... ) | ||
| */ | ||
| class PreferEscHtmlESniff implements Sniff { | ||
|
|
||
| /** | ||
| * Mapping of escape functions and translation functions to their combined equivalents. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $replacements = array( | ||
| 'esc_html' => array( | ||
| '__' => 'esc_html_e', | ||
| '_x' => 'esc_html_x', | ||
| ), | ||
| 'esc_attr' => array( | ||
| '__' => 'esc_attr_e', | ||
| '_x' => 'esc_attr_x', | ||
| ), | ||
| ); | ||
|
|
||
| /** | ||
| * Returns an array of tokens this test wants to listen for. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function register() { | ||
| return array( T_ECHO ); | ||
| } | ||
|
|
||
| /** | ||
| * Processes this test, when one of its tokens is encountered. | ||
| * | ||
| * @param File $phpcsFile The file being scanned. | ||
| * @param int $stackPtr The position of the current token in the stack passed in $tokens. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function process( File $phpcsFile, $stackPtr ) { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| // Find the next non-whitespace token after echo. | ||
| $nextToken = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); | ||
|
|
||
| if ( false === $nextToken ) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if it's an escape function we handle. | ||
| if ( $tokens[ $nextToken ]['code'] !== T_STRING ) { | ||
| return; | ||
| } | ||
|
|
||
| $escapeFunc = $tokens[ $nextToken ]['content']; | ||
|
|
||
| if ( ! isset( $this->replacements[ $escapeFunc ] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $escapeFuncToken = $nextToken; | ||
|
|
||
| // Find the opening parenthesis after the escape function. | ||
| $openParen = $phpcsFile->findNext( T_WHITESPACE, $escapeFuncToken + 1, null, true ); | ||
|
|
||
| if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the first non-whitespace token inside the escape function. | ||
| $insideToken = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); | ||
|
|
||
| if ( false === $insideToken ) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if it's a translation function we handle. | ||
| if ( $tokens[ $insideToken ]['code'] !== T_STRING ) { | ||
| return; | ||
| } | ||
|
|
||
| $translateFunc = $tokens[ $insideToken ]['content']; | ||
|
|
||
| if ( ! isset( $this->replacements[ $escapeFunc ][ $translateFunc ] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $replacementFunc = $this->replacements[ $escapeFunc ][ $translateFunc ]; | ||
| $translateToken = $insideToken; | ||
|
|
||
| // Find the opening parenthesis after the translation function. | ||
| $translateOpenParen = $phpcsFile->findNext( T_WHITESPACE, $translateToken + 1, null, true ); | ||
|
|
||
| if ( false === $translateOpenParen || $tokens[ $translateOpenParen ]['code'] !== T_OPEN_PARENTHESIS ) { | ||
| return; | ||
| } | ||
|
|
||
| // Find the closing parenthesis of the translation function. | ||
| if ( ! isset( $tokens[ $translateOpenParen ]['parenthesis_closer'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $translateCloseParen = $tokens[ $translateOpenParen ]['parenthesis_closer']; | ||
|
|
||
| // Find the closing parenthesis of the escape function. | ||
| if ( ! isset( $tokens[ $openParen ]['parenthesis_closer'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $escapeCloseParen = $tokens[ $openParen ]['parenthesis_closer']; | ||
|
|
||
| // Find the semicolon. | ||
| $semicolon = $phpcsFile->findNext( T_WHITESPACE, $escapeCloseParen + 1, null, true ); | ||
|
|
||
| if ( false === $semicolon || $tokens[ $semicolon ]['code'] !== T_SEMICOLON ) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the arguments of the translation function. | ||
| $translateArgs = $phpcsFile->getTokensAsString( $translateOpenParen + 1, $translateCloseParen - $translateOpenParen - 1 ); | ||
|
|
||
| $fix = $phpcsFile->addFixableError( | ||
| 'Use %s() instead of echo %s( %s() ).', | ||
| $stackPtr, | ||
| 'PreferCombinedFunction', | ||
| array( $replacementFunc, $escapeFunc, $translateFunc ) | ||
| ); | ||
|
|
||
| if ( true === $fix ) { | ||
| $phpcsFile->fixer->beginChangeset(); | ||
|
|
||
| // Remove everything from echo to the semicolon. | ||
| for ( $i = $stackPtr; $i <= $semicolon; $i++ ) { | ||
| $phpcsFile->fixer->replaceToken( $i, '' ); | ||
| } | ||
|
|
||
| // Add the new combined function call. | ||
| $phpcsFile->fixer->addContent( $stackPtr, $replacementFunc . '( ' . trim( $translateArgs ) . ' );' ); | ||
|
|
||
| $phpcsFile->fixer->endChangeset(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation may cause incorrect auto-fixes.
The sniff doesn't verify that there's nothing between
$translateCloseParenand$escapeCloseParenexcept whitespace. For code like:The sniff would incorrectly match and produce
esc_html_e( 'text', 'domain' );, silently losing. ' extra'.🐛 Proposed fix: Add validation after line 127
🤖 Prompt for AI Agents