Make WordPress Core

Opened 10 months ago

Closed 9 months ago

#59011 closed defect (bug) (fixed)

Fatal error on single-site get_users() under certain circumstances

Reported by: dd32's profile dd32 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.3.1 Priority: normal
Severity: normal Version: 6.3
Component: Users Keywords: has-patch has-unit-tests fixed-major dev-reviewed commit
Focuses: Cc:

Description

The following error was triggered via wp-themes.com today:

E_ERROR: Uncaught Error: Call to undefined function switch_to_blog() in wp-includes/class-wp-user-query.php:1077

This seems to stem from #40613 [55657] (cc @spacedmonkey).

The theme code that triggered it was this:
https://themes.trac.wordpress.org/browser/sequel/1.0.4/functions.php?marks=185-190#L176

$contributor_ids = get_users( array(
   'fields'  => 'ID',
   'orderby' => 'post_count',
   'order'   => 'DESC',
   'who'     => 'authors',
) );

Note that no blog_id is specified, as a result it's prefilled with get_current_blog_id() which return 1 on single-site.
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-user-query.php#L94

The $blog_id is then initialised to 0 in several places to assume no-blog or a not-blog-specific request, such as this but then it hits the problematic code here

The actual issue is this:

if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) {

It enters a branch that tries to switch, as ( $blog_id = 0 ) != get_current_blog_id().

Setting to 6.3.1 for review, as this seems like a regression.

Change History (10)

This ticket was mentioned in PR #4977 on WordPress/wordpress-develop by @dd32.


10 months ago
#1

  • Keywords has-patch has-unit-tests added

#2 @dd32
10 months ago

I was slightly wrong in my initial understanding of this issue.

This is specific to single-site with post_count and using the deprecated who=authors.

The problematic blog_id = 0 is mostly triggered from here it seems:
https://github.com/WordPress/wordpress-develop/blob/f7fbd0c25f6824f7259dbca47b38c6569901baf5/src/wp-includes/class-wp-user-query.php#L415-L417

See the PR for unit tests and patch.

#3 @austinginder
10 months ago

I can confirm this bug. Had a customer website PHP fatal when upgrading to WordPress core 6.3. Downgrading to 6.2.2 is a workaround for the error. For my testing I ran the following in wp shell

get_users( [ 'orderby' => 'post_count', 'order' => 'DESC', 'who' => 'authors' ] );

With core v6.3 the 'who' => 'authors' causes the fatal error. Works fine on version 6.2.2.

#4 @RavanH
10 months ago

Hi, confirmed with this in a plugin:

get_users( array(
        'orderby'             => 'post_count',
        'order'               => 'DESC',
        'number'              => '1000',
        'fields'              => array( 'ID' ),
        'has_published_posts' => true,
        'who'                 => 'authors'
) );

#5 @SergeyBiryukov
10 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 56381:

Users: Correct the logic for switch_to_blog() in WP_User_Query::generate_cache_key().

If $blog_id equals 0, it should be treated as the current site ID, and there is no need to switch to a different site.

This commit prevents an unnecessary call to switch_to_blog() on single site to avoid a fatal error when using 'orderby' => 'post_count' and the deprecated 'who' => 'authors' parameter:

Uncaught Error: Call to undefined function switch_to_blog() in wp-includes/class-wp-user-query.php:1077

Follow-up to [55657].

Props dd32, austinginder, RavanH, mukesh27.
Fixes #59011.

@SergeyBiryukov commented on PR #4977:


10 months ago
#6

Thanks for the PR! Merged in r56381.

#7 @SergeyBiryukov
10 months ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for backporting to the 6.3 branch.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


9 months ago

#9 @audrasjb
9 months ago

  • Keywords dev-reviewed commit added

As per today's bug scrub: the patch looks straightforward and simple enough to be included in a rapid 6.3.1 release.
Marking as dev-reviewed.

Thanks @azaozz for the co-review.

#10 @audrasjb
9 months ago

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

In 56427:

Users: Correct the logic for switch_to_blog() in WP_User_Query::generate_cache_key().

If $blog_id equals 0, it should be treated as the current site ID, and there is no need to switch to a different site.
This commit prevents an unnecessary call to switch_to_blog() on single site to avoid a fatal error when using 'orderby' => 'post_count' and the deprecated
'who' => 'authors' parameter:

Uncaught Error: Call to undefined function switch_to_blog() in wp-includes/class-wp-user-query.php:1077

Follow-up to [55657].

Props dd32, austinginder, RavanH, mukesh27.
Reviewed by azaozz, audrasjb.
Merges [56381] to the 6.3 branch.
Fixes #59011.

Note: See TracTickets for help on using tickets.