Make WordPress Core


Ignore:
Timestamp:
08/23/2023 01:21:42 PM (3 years ago)
Author:
audrasjb
Message:

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.

Location:
branches/6.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.3

  • branches/6.3/tests/phpunit/tests/user/queryCache.php

    r55657 r56427  
    779779                $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
    780780        }
     781
     782        /**
     783         * Verifies that generate_cache_key() does not throw a fatal error for switch_to_blog()
     784         * with 'orderby' => 'post_count' and the deprecated 'who' => 'authors' parameter.
     785         *
     786         * @ticket 59011
     787         * @covers ::generate_cache_key
     788         *
     789         * @expectedDeprecated WP_User_Query
     790         */
     791        public function test_generate_cache_key_with_orderby_post_count_and_deprecated_who_parameter() {
     792                $query = new WP_User_Query(
     793                        array(
     794                                'fields'  => 'ID',
     795                                'orderby' => 'post_count',
     796                                'order'   => 'DESC',
     797                                'who'     => 'authors',
     798                        )
     799                );
     800
     801                $this->assertNotEmpty( $query->get_results() );
     802        }
    781803}
Note: See TracChangeset for help on using the changeset viewer.