Automattic / VIP-Coding-Standards

PHP_CodeSniffer ruleset to enforce WordPress VIP coding standards.
https://wpvip.com/documentation/how-to-install-php-code-sniffer-for-wordpress-com-vip/
Other
237 stars 40 forks source link

Wrong escaping function, using esc_attr_e() #807

Closed farhadsakhaei closed 10 months ago

farhadsakhaei commented 10 months ago

My code:

<label for="<?php echo esc_attr( $this->get_field_id( 'display-mode' ) ); ?>">

or

<label for="<?php esc_attr_e( $this->get_field_id( 'display-mode' ) ); ?>">

I get this error:

Wrong escaping function, usingesc_attr_e()in a context outside of HTML attributes may not escape properly.PHPCS(WordPressVIPMinimum.Security.ProperEscapingFunction.notAttrEscAttr)

GaryJones commented 10 months ago

@farhadsakhaei Can you please share more of the code, indicating line numbers, and the line numbers from the error message please, and the precise command you ran?

I'm unable to replicate with a test file of just:

<label for="<?php esc_attr_e( $this->get_field_id( 'display-mode' ) ); ?>">

and a command of:

phpcs --standard=WordPress-VIP-Go phpcs-test.php --severity=1 -s
farhadsakhaei commented 10 months ago

@GaryJones Hi, Thank you for reply, this is my full line of code:

<label for="<?php echo esc_attr( $this->get_field_id( 'display-mode' ) ); ?>"><?php esc_attr_e( 'Display Mode:', 'aparat-feed' ); ?></label>

4PoiabZsCU

jrfnl commented 10 months ago

@farhadsakhaei Your error is for the <?php esc_attr_e( 'Display Mode:', 'aparat-feed' ); ?> and the sniff is correct as that's HTML context, not attribute context.

farhadsakhaei commented 10 months ago

@jrfnl

Yes, You are right, That was because the pointer was pointing another tag Thank you

jrfnl commented 10 months ago

@farhadsakhaei When in doubt, you can use the --report=code option to see which bit of the code is being flagged by which sniff. Hope that helps.

Shall we close this issue ?

farhadsakhaei commented 10 months ago

@jrfnl Thank you so much for help and recommendation Yes, of course Sincerly