Skip to content

Commit 3ab2f0c

Browse files
authored
Merge pull request #784 from Automattic/3.0/fix/672-wpqueryparams-prevent-false-positives-get_users
2 parents 5f34bbe + 47c1c2c commit 3ab2f0c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace WordPressVIPMinimum\Sniffs\Performance;
1010

1111
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
12+
use WordPressCS\WordPress\Helpers\ContextHelper;
1213

1314
/**
1415
* Flag suspicious WP_Query and get_posts params.
@@ -19,6 +20,13 @@
1920
*/
2021
class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff {
2122

23+
/**
24+
* Whether the current $stackPtr being scanned is nested in a function call to get_users().
25+
*
26+
* @var bool
27+
*/
28+
private $in_get_users = false;
29+
2230
/**
2331
* Groups of variables to restrict.
2432
*
@@ -48,6 +56,20 @@ public function getGroups() {
4856
];
4957
}
5058

59+
/**
60+
* Processes this test, when one of its tokens is encountered.
61+
*
62+
* @param int $stackPtr The position of the current token in the stack.
63+
*
64+
* @return void
65+
*/
66+
public function process_token( $stackPtr ) {
67+
$this->in_get_users = ContextHelper::is_in_function_call( $this->phpcsFile, $stackPtr, [ 'get_users' => true ], true, true );
68+
69+
parent::process_token( $stackPtr );
70+
}
71+
72+
5173
/**
5274
* Callback to process a confirmed key which doesn't need custom logic, but should always error.
5375
*
@@ -64,6 +86,11 @@ public function callback( $key, $val, $line, $group ) {
6486
return ( $val === 'true' );
6587

6688
case 'PostNotIn':
89+
if ( $key === 'exclude' && $this->in_get_users !== false ) {
90+
// This is not an array used by get_posts(). See #672.
91+
return false;
92+
}
93+
6794
return true;
6895

6996
default:

WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ $q = new WP_query( $query_args );
2121
get_posts( [ 'exclude' => $post_ids ] ); // Warning.
2222

2323
$exclude = [ 1, 2, 3 ];
24+
25+
// Issue #672 / #729.
26+
get_users( [ 'exclude' => $post_ids ] ); // OK.
27+
get_users( My\get_args( [ 'exclude' => $post_ids ] ) ); // OK - arbitrary as the call to `My\get_args()` on its own would be flagged, but let's allow it.
28+
29+
$context_unknown = [ 'exclude' => $post_ids ]; // Warning.
30+
other_fn_calls_still_throw_warning( [ 'exclude' => $post_ids ] ); // Warning.
31+
get_users( [ 'suppress_filters' => true ] ); // Error - not necessarily valid, but the exception being made is specifically about `exclude`.

WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getErrorList() {
2727
return [
2828
5 => 1,
2929
17 => 1,
30+
31 => 1,
3031
];
3132
}
3233

@@ -40,6 +41,8 @@ public function getWarningList() {
4041
4 => 1,
4142
11 => 1,
4243
21 => 1,
44+
29 => 1,
45+
30 => 1,
4346
];
4447
}
4548
}

0 commit comments

Comments
 (0)