Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 76/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- PHP
- Star
- 261
- Fork
- 44
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Automattic/VIP-Coding-Standards
-
AlwaysReturnInFilter: isInsideIfConditonal() guards the conditions array after reading itCó thể đã có người làm @tomjn đã nhận 5 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Suppress filters in get_posts false positiveCó thể đã có người làm @tomjn đã nhận 6 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Breaking Change Type: Maintenance
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Automattic/VIP-Coding-Standards#849 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của Automattic/VIP-Coding-Standards
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
coollabsio/shoutrrr#190 ·
-
Bug Enhancement Performance
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Feature Status: Needs Triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
Maintainer thường phản hồi trong vòng 1 ngày
-
frontend low-priority
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
mplodowski/dynamicpdf-plugin#336 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
AdvancedCustomFields/acf#1044 ·