Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if
Mantenedores costumam responder em até 1 dia
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 76/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- PHP
- Estrelas
- 261
- Forks
- 44
- Merge médio
- 3d 9h
- PRs com merge (30d)
- 10
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de Automattic/VIP-Coding-Standards
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 48/100
Automattic/VIP-Coding-Standards#876 ·
Mantenedores costumam responder em até 1 dia
-
Breaking Change Type: Maintenance
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 35/100
Automattic/VIP-Coding-Standards#849 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 45/100
Automattic/VIP-Coding-Standards#832 ·
Mantenedores costumam responder em até 1 dia
-
WordPress.WP.AlternativeFunctions.file_system_operations error messages cannot be fixed anymoreAberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 48/100
Automattic/VIP-Coding-Standards#823 ·
Mantenedores costumam responder em até 1 dia
-
Status: Awaiting feedback
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 45/100
Automattic/VIP-Coding-Standards#754 · 3 comentários ·
Mantenedores costumam responder em até 1 dia
Todas as issues de Automattic/VIP-Coding-Standards
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 83/100
nunomaduro/collision#375 ·
-
enhancement good first issue hacktoberfest help wanted
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
thetheago/laravel-seeder-versioning#19 ·
Mantenedores costumam responder em até 1 dia
-
HTTP::setGetVar() duplicates the base path for root-relative URLs when the site runs in a subfolderAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
silverstripe/silverstripe-framework#12034 ·
-
needs triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
loginWithCustomTokenExchange() fails when an organization is configuredTalvez já em andamento @awss1i assumiu hoje. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
auth0/auth0-PHP#883 · 1 comentário ·
Mantenedores costumam responder em até 2 dias