From 9c3d6813848ed69eef182893ff1b2bf508200ea8 Mon Sep 17 00:00:00 2001 From: Gary Jones Date: Fri, 9 Oct 2026 22:40:01 +0100 Subject: [PATCH] fix: treat elseif like if in the PreGetPosts sniff The sniff only recognised an is_main_query() check in an if, so two common patterns were flagged. An early return under an elseif that checks the main query was missed, and so was a set() inside an elseif whose condition checks it, as when home and search are tweaked in separate branches. --- .../Sniffs/Hooks/PreGetPostsSniff.php | 6 ++-- .../Tests/Hooks/PreGetPostsUnitTest.inc | 29 +++++++++++++++++++ .../Tests/Hooks/PreGetPostsUnitTest.php | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php index afa81d6c..38097425 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php @@ -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 ], @@ -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, @@ -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; } diff --git a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc index 33f7416b..dddcafd4 100644 --- a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc @@ -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. + } +} ); diff --git a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php index 6598375a..2e99353a 100644 --- a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php +++ b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php @@ -43,6 +43,7 @@ public function getWarningList() { 133 => 1, 165 => 1, 174 => 1, + 210 => 1, ]; } }