Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 76/100
Rechercherichtung
Start in PreGetPostsSniff::isEarlyMainQueryCheck(), where the issue identifies the local findNext() search as stopping at the first statement terminator. Reproduce the minimal callback with do_log() before return and confirm the warning on the set() line. Done means the callback produces no warning when the early return is not the first statement in the if body.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Description
The PreGetPosts sniff warns when a pre_get_posts callback changes the query without checking is_main_query() first. A callback that returns early for every other query has made that check, so it should not get the warning.
It does get the warning when the early return is not the first statement inside the if. Logging a message before returning is enough to cause it.
Expected: no warning for the snippet below.
Actual: a warning on the $query->set() line. Remove the do_log() line and the warning goes away.
The cause is in PreGetPostsSniff::isEarlyMainQueryCheck(). It looks for the return with findNext() in local mode, which stops at the first statement terminator, so only the first statement in the if body is searched.
@GaryJones found this while reviewing #901.
Minimal Code Snippet
add_action( 'pre_get_posts', function( $query ) {
if ( ! $query->is_main_query() ) {
do_log( 'x' );
return;
}
$query->set( 'cat', '-5' );
} );
Error Code
WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts
Main WP_Query is being modified without
$query->is_main_query()check. Needs manual inspection.
Environment
| Question | Answer |
|---|---|
| PHP version | 8.5.11 |
| PHP_CodeSniffer version | 3.13.6 |
| PHPCSUtils version | 1.2.3 |
| VIPCS version | main at 9c47cd0 (3.1.0) and develop at bdd2160 |
| WordPressCS version | 3.4.1 |
| PHPCSExtra version | 1.5.1 |
| VariableAnalysis version | 2.13.0 |
Tested Against main branch?
- I have verified the issue still exists in the
mainbranch of VIPCS. - I have verified the issue still exists in the
developbranch of VIPCS.
- Vorherrschende Sprache
- PHP
- Sterne
- 261
- Forks
- 44
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus Automattic/VIP-Coding-Standards
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading itEvtl. vergeben @tomjn hat das vor 6 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Suppress filters in get_posts false positiveEvtl. vergeben @tomjn hat das vor 7 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 48/100
-
Breaking Change Type: Maintenance
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
Automattic/VIP-Coding-Standards#849 · 1 Kommentar ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 45/100
Alle Issues in Automattic/VIP-Coding-Standards
Ähnliche Issues
-
魚類詳細頁基本 tab 移除重複的地方知識Offenspec:debating
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
blackie0424/tao_among#93 ·
Maintainer antworten meist innerhalb von 1 Tag
-
📚 Documentation: Placeholder link `link-to-realtime-docs` in Flutter SDK changelogEvtl. vergeben @ShyneChikwapulo hat das heute übernommen. Offenapi / realtime product / auth product / messaging product / vcs
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 82/100
appwrite/appwrite#14272 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 75/100
Boavizta/boaviztapi#580 · 1 Kommentar ·
-
Add PrestashopOffenrequest
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
endoflife-date/endoflife.date#11303 ·
Maintainer antworten meist innerhalb von 1 Tag