Skip to content

Commit 82d1232

Browse files
committed
PreGetPosts: improve the isEarlyMainQueryCheck() method [2]
This adds a second unit test which is based on the actual code which originally triggered the error. The error was caused by the code in question using inline control structures (without braces) for the early main query check. After the previous fix, that code would now throw a false positive. I've fixed this now by adding an additional check for a `return` statement straight after the parenthesis closer of the `if()` statement. Fixes 499
1 parent 46895e9 commit 82d1232

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
321321

322322
$owner = $this->tokens[ $nestedParenthesisEnd ]['parenthesis_owner'];
323323
if ( isset( $this->tokens[ $owner ]['scope_opener'], $this->tokens[ $owner ]['scope_closer'] ) === false ) {
324+
// This may be an inline control structure (no braces).
325+
$next = $this->phpcsFile->findNext(
326+
Tokens::$emptyTokens,
327+
( $nestedParenthesisEnd + 1 ),
328+
null,
329+
true
330+
);
331+
332+
if ( false !== $next && T_RETURN === $this->tokens[ $next ]['code'] ) {
333+
return true;
334+
}
335+
324336
return false;
325337
}
326338

WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ class undefined_index_issue_499 {
106106
$wp_query->set( 'cat', '-5' );
107107
}
108108
}
109+
110+
add_action('pre_get_posts', 'inline_control_structures', 10, 1);
111+
112+
function inline_control_structures( $query ) {
113+
if( !$query->is_main_query() && !is_front_page()) return;
114+
if(is_single() || is_search() || is_archive()) return;
115+
116+
$query->set('meta_query', 'foo');
117+
return $query;
118+
}

0 commit comments

Comments
 (0)