Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function isParentConditionalCheckingMainQuery( $stackPtr ): bool {
$conditionStackPtrs = array_keys( $this->tokens[ $stackPtr ]['conditions'] );
$lastConditionStackPtr = array_pop( $conditionStackPtrs );

while ( $this->tokens[ $stackPtr ]['conditions'][ $lastConditionStackPtr ] === T_IF ) {
while ( in_array( $this->tokens[ $stackPtr ]['conditions'][ $lastConditionStackPtr ], [ T_IF, T_ELSEIF ], true ) ) {

$next = $this->phpcsFile->findNext(
[ T_VARIABLE ],
Expand Down Expand Up @@ -423,7 +423,7 @@ private function isPartOfIfConditional( $stackPtr ): bool {
&& empty( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) === false
) {
$previousLocalIf = $this->phpcsFile->findPrevious(
[ T_IF ],
[ T_IF, T_ELSEIF ],
$stackPtr - 1,
null,
false,
Expand Down Expand Up @@ -455,7 +455,7 @@ private function isInsideIfConditonal( $stackPtr ): bool {
) {
$conditionStackPtrs = array_keys( $this->tokens[ $stackPtr ]['conditions'] );
$lastConditionStackPtr = array_pop( $conditionStackPtrs );
return $this->tokens[ $stackPtr ]['conditions'][ $lastConditionStackPtr ] === T_IF;
return in_array( $this->tokens[ $stackPtr ]['conditions'][ $lastConditionStackPtr ], [ T_IF, T_ELSEIF ], true );
}
return false;
}
Expand Down
29 changes: 29 additions & 0 deletions WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,32 @@ add_action( 'pre_get_posts', function( $query ) {
endif;
$query->set( 'cat', '-5' ); // Ok - alternative syntax.
} );

// An elseif checks the main query as well as an if does.
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() ) {
return;
} elseif ( ! $query->is_main_query() ) {
return;
}
$query->set( 'cat', '-5' ); // Ok.
} );

add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() ) {
return;
} else if ( ! $query->is_main_query() ) {
return;
}
$query->set( 'cat', '-5' ); // Ok.
} );

add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-5' ); // Ok.
} elseif ( $query->is_main_query() && $query->is_search() ) {
$query->set( 'cat', '-6' ); // Ok.
} elseif ( is_admin() ) {
$query->set( 'cat', '-7' ); // Warning - this elseif does not check the main query.
}
} );
1 change: 1 addition & 0 deletions WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function getWarningList() {
133 => 1,
165 => 1,
174 => 1,
210 => 1,
];
}
}
Loading