Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/views/xml/import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

<h2 class="frm-h2"><?php esc_html_e( 'Export', 'formidable' ); ?></h2>
<p class="howto">
<?php echo esc_html( __( 'Export your forms, entries, views, and styles so you can easily import them on another site.', 'formidable' ) ); ?>
<?php esc_html_e( 'Export your forms, entries, views, and styles so you can easily import them on another site.', 'formidable' ); ?>
</p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" id="frm_export_xml" class="frm-fields frm_grid_container">
<input type="hidden" name="action" value="frm_export_xml" />
Expand Down
160 changes: 160 additions & 0 deletions phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEscHtmlESniff.php
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;
}
Comment on lines +120 to +134

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing validation may cause incorrect auto-fixes.

The sniff doesn't verify that there's nothing between $translateCloseParen and $escapeCloseParen except whitespace. For code like:

echo esc_html( __( 'text', 'domain' ) . ' extra' );

The sniff would incorrectly match and produce esc_html_e( 'text', 'domain' );, silently losing . ' extra'.

🐛 Proposed fix: Add validation after line 127
 		$escapeCloseParen = $tokens[ $openParen ]['parenthesis_closer'];
 
+		// Ensure nothing exists between translation close paren and escape close paren except whitespace.
+		$tokenBetween = $phpcsFile->findNext( T_WHITESPACE, $translateCloseParen + 1, $escapeCloseParen, true );
+
+		if ( false !== $tokenBetween ) {
+			return;
+		}
+
 		// Find the semicolon.
 		$semicolon = $phpcsFile->findNext( T_WHITESPACE, $escapeCloseParen + 1, null, true );
🤖 Prompt for AI Agents
In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferEscHtmlESniff.php` around
lines 120 - 134, The sniff currently assumes nothing meaningful exists between
$translateCloseParen and $escapeCloseParen and can incorrectly match expressions
with concatenation (e.g. ". ' extra'"); update the validation to iterate tokens
between $translateCloseParen + 1 and $escapeCloseParen - 1 and bail out (return)
if any token other than T_WHITESPACE (or T_COMMENT if you prefer to allow
comments) is found. Locate this check near the existing $translateCloseParen and
$escapeCloseParen logic in PreferEscHtmlESniff.php and perform the token-scan
before computing $semicolon so the fixer will not auto-fix when extra
non-whitespace content is present.


// 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();
}
}
}
1 change: 1 addition & 0 deletions phpcs-sniffs/Formidable/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<rule ref="Formidable.CodeAnalysis.PreferObGetClean" />
<rule ref="Formidable.CodeAnalysis.PreferKsesEcho" />
<rule ref="Formidable.CodeAnalysis.MoveVariableBelowEarlyReturn" />
<rule ref="Formidable.CodeAnalysis.PreferEscHtmlE" />
<rule ref="Formidable.CodeAnalysis.StrictComparisonForIntFunctions" />
<rule ref="Formidable.CodeAnalysis.FlipNegativeTernary" />
<rule ref="Formidable.CodeAnalysis.FlipIfToEarlyReturn" />
Expand Down