Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55722 closed enhancement (fixed)

Inconsistent guard conditions in query.php

Reported by: vdankbaar's profile vdankbaar Owned by: vdankbaar's profile vdankbaar
Milestone: 6.1 Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

While working on #55104 we noticed that other functions were also missing guard conditions.
In these cases, $wp_query could be null which would cause a fatal error. It would be safer to check if $wp_query is set before.

Change History (5)

This ticket was mentioned in PR #2714 on WordPress/wordpress-develop by vdankbaar.

2 years ago

  • Keywords has-patch added

Fixed inconsistent guard conditions in query.php in line with; #55104

Trac ticket:

#2 @SergeyBiryukov
2 years ago

  • Description modified (diff)
  • Milestone changed from Awaiting Review to 6.1

SergeyBiryukov commented on PR #2714:

2 years ago

Hi there, thanks for the PR!

For reference, here is the list of functions affected by this change:

  • have_posts()
  • in_the_loop()
  • rewind_posts()
  • the_post()
  • have_comments()
  • the_comment()

I had some notes when looking at the initial commit:

  • These functions are not exactly conditional tags like the other is_*() functions: is_single(), is_home(), etc. So the same _doing_it_wrong() messages referencing conditional tags are not quite accurate here.
  • Not all of these functions have a return value, e.g. rewind_posts(), the_post(), or the_comment(). Introducing a return value seems a bit out of scope for this change and could use a separate discussion.

If the goal is to avoid a fatal error if $wp_query is not set, I would like to suggest doing just that and returning early. That might not necessarily be consistent with the other is_*() functions, but would be in line with the current return values of these particular functions. A _doing_it_wrong() message could then be considered later as a separate enhancement.

With that in mind, I have made some adjustments to the PR and added a unit test. Any thoughts are welcome 🙂

#4 @SergeyBiryukov
2 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 53429:

Query: Check if $wp_query is set in query loop functions.

This avoids a PHP fatal error if any of these functions are called too early:

  • have_posts()
  • in_the_loop()
  • rewind_posts()
  • the_post()
  • have_comments()
  • the_comment()

bringing some consistency with conditional tags: is_single(), is_home(), etc.

This commit also removes unnecessary return from the_comment(), for consistency with the_post(). As WP_Query::the_comment() does not have a return value, this statement did not have any effect in practice.

Follow-up to [4934], [8807], [16947], [17068], [17083], [49147], [53395], [53396], [53400].

Props vdankbaar, thijso, teunvgisteren, timkersten655, SergeyBiryukov.
Fixes #55722.

Note: See TracTickets for help on using tickets.