Skip to content

Commit e05b727

Browse files
committed
EscapeOutput: allow for map_deep() to output escape arrays
Includes unit test.
1 parent 14c77a8 commit e05b727

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

WordPress/Sniffs/Security/EscapeOutputSniff.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,20 +382,32 @@ public function process_token( $stackPtr ) {
382382

383383
if ( false !== $function_opener ) {
384384

385-
if ( 'array_map' === $functionName ) {
386-
387-
// Get the first parameter (name of function being used on the array).
388-
$mapped_function = $this->phpcsFile->findNext(
389-
Tokens::$emptyTokens,
390-
( $function_opener + 1 ),
391-
$this->tokens[ $function_opener ]['parenthesis_closer'],
392-
true
385+
if ( isset( $this->arrayWalkingFunctions[ $functionName ] ) ) {
386+
387+
// Get the callback parameter.
388+
$callback = $this->get_function_call_parameter(
389+
$ptr,
390+
$this->arrayWalkingFunctions[ $functionName ]
393391
);
394392

395-
// If we're able to resolve the function name, do so.
396-
if ( $mapped_function && \T_CONSTANT_ENCAPSED_STRING === $this->tokens[ $mapped_function ]['code'] ) {
397-
$functionName = $this->strip_quotes( $this->tokens[ $mapped_function ]['content'] );
398-
$ptr = $mapped_function;
393+
if ( ! empty( $callback ) ) {
394+
/*
395+
* If this is a function callback (not a method callback array) and we're able
396+
* to resolve the function name, do so.
397+
*/
398+
$mapped_function = $this->phpcsFile->findNext(
399+
Tokens::$emptyTokens,
400+
$callback['start'],
401+
( $callback['end'] + 1 ),
402+
true
403+
);
404+
405+
if ( false !== $mapped_function
406+
&& \T_CONSTANT_ENCAPSED_STRING === $this->tokens[ $mapped_function ]['code']
407+
) {
408+
$functionName = $this->strip_quotes( $this->tokens[ $mapped_function ]['content'] );
409+
$ptr = $mapped_function;
410+
}
399411
}
400412
}
401413

WordPress/Tests/Security/EscapeOutputUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,6 @@ echo esc_html( $something ),
289289
echo get_the_title(); // Bad.
290290
echo wp_kses_post( get_the_title() ); // Ok.
291291
echo esc_html( get_the_title() ); // Ok.
292+
293+
echo implode( '<br>', map_deep( $items, 'esc_html' ) ); // Ok.
294+
echo implode( '<br>', map_deep( $items, 'foo' ) ); // Bad.

WordPress/Tests/Security/EscapeOutputUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function getErrorList() {
7979
264 => 1,
8080
266 => 1,
8181
289 => 1,
82+
294 => 1,
8283
);
8384
}
8485

0 commit comments

Comments
 (0)