Skip to content

Commit 97b8cfc

Browse files
authored
Merge pull request #589 from Automattic/fix/369-exclude-arg-in-get_posts
WPQueryParams: sniff for `exclude` array key being set
2 parents d374355 + 20fdf8b commit 97b8cfc

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,54 @@
88

99
namespace WordPressVIPMinimum\Sniffs\Performance;
1010

11-
use WordPressVIPMinimum\Sniffs\Sniff;
11+
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
1212
use PHP_CodeSniffer\Util\Tokens;
1313

1414
/**
1515
* Flag suspicious WP_Query and get_posts params.
1616
*
1717
* @package VIPCS\WordPressVIPMinimum
1818
*/
19-
class WPQueryParamsSniff extends Sniff {
19+
class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff {
2020

2121
/**
2222
* Returns an array of tokens this test wants to listen for.
2323
*
2424
* @return array
2525
*/
2626
public function register() {
27+
$targets = parent::register();
28+
29+
// Add the target for the "old" implementation.
30+
$targets[] = T_CONSTANT_ENCAPSED_STRING;
31+
32+
return $targets;
33+
}
34+
35+
/**
36+
* Groups of variables to restrict.
37+
* This should be overridden in extending classes.
38+
*
39+
* Example: groups => array(
40+
* 'groupname' => array(
41+
* 'type' => 'error' | 'warning',
42+
* 'message' => 'Dont use this one please!',
43+
* 'keys' => array( 'key1', 'another_key' ),
44+
* 'callback' => array( 'class', 'method' ), // Optional.
45+
* )
46+
* )
47+
*
48+
* @return array
49+
*/
50+
public function getGroups() {
2751
return [
28-
T_CONSTANT_ENCAPSED_STRING,
52+
'PostNotIn' => [
53+
'type' => 'warning',
54+
'message' => 'Using `exclude`, which is subsequently used by `post__not_in`, should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.',
55+
'keys' => [
56+
'exclude',
57+
],
58+
],
2959
];
3060
}
3161

@@ -54,6 +84,22 @@ public function process_token( $stackPtr ) {
5484
$message = 'Using `post__not_in` should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.';
5585
$this->phpcsFile->addWarning( $message, $stackPtr, 'PostNotIn' );
5686
}
87+
88+
parent::process_token( $stackPtr );
89+
}
90+
91+
/**
92+
* Callback to process a confirmed key which doesn't need custom logic, but should always error.
93+
*
94+
* @param string $key Array index / key.
95+
* @param mixed $val Assigned value.
96+
* @param int $line Token line.
97+
* @param array $group Group definition.
98+
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
99+
* with custom error message passed to ->process().
100+
*/
101+
public function callback( $key, $val, $line, $group ) {
102+
return true;
57103
}
58104

59105
}

WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ $q = new WP_Query( $query_args );
1616

1717
$query_args[ 'suppress_filters' ] = true;
1818

19-
$q = new WP_query( $query_args );
19+
$q = new WP_query( $query_args );
20+
21+
get_posts( [ 'exclude' => $post_ids ] ); // Warning.
22+
23+
$exclude = [ 1, 2, 3 ];

WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getWarningList() {
3939
return [
4040
4 => 1,
4141
11 => 1,
42+
21 => 1,
4243
];
4344
}
4445

0 commit comments

Comments
 (0)