Make WordPress Core

Changeset 38682


Ignore:
Timestamp:
09/30/2016 04:34:18 AM (8 years ago)
Author:
jeremyfelt
Message:

Multisite: Use get_sites() in get_blogs_of_user().

Previously, we looped through all of the site IDs found via user meta and ran get_blog_details() for each site. Now, we can pass all of the site IDs at once to get_sites() and receive full WP_Site objects.

To avoid possible issues with return type, sites are still processed into a standard object that also has a userblog_id property attached. Nothing is changed for non-multisite configurations.

Props flixos90.
Fixes #37061.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r38651 r38682  
    560560
    561561/**
    562  * Get the blogs a user belongs to.
     562 * Get the sites a user belongs to.
    563563 *
    564564 * @since 3.0.0
     565 * @since 4.7.0 Converted to use get_sites().
    565566 *
    566567 * @global wpdb $wpdb WordPress database abstraction object.
    567568 *
    568569 * @param int  $user_id User ID
    569  * @param bool $all     Whether to retrieve all blogs, or only blogs that are not
     570 * @param bool $all     Whether to retrieve all sites, or only sites that are not
    570571 *                      marked as deleted, archived, or spam.
    571  * @return array A list of the user's blogs. An empty array if the user doesn't exist
    572  *               or belongs to no blogs.
     572 * @return array A list of the user's sites. An empty array if the user doesn't exist
     573 *               or belongs to no sites.
    573574 */
    574575function get_blogs_of_user( $user_id, $all = false ) {
     
    577578    $user_id = (int) $user_id;
    578579
    579     // Logged out users can't have blogs
     580    // Logged out users can't have sites
    580581    if ( empty( $user_id ) )
    581582        return array();
     
    589590     * @since 4.6.0
    590591     *
    591      * @param null|array $blogs   An array of WP_Site objects of which the user is a member.
     592     * @param null|array $sites   An array of site objects of which the user is a member.
    592593     * @param int        $user_id User ID.
    593594     * @param bool       $all     Whether the returned array should contain all sites, including
    594595     *                            those marked 'deleted', 'archived', or 'spam'. Default false.
    595596     */
    596     $blogs = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
    597 
    598     if ( null !== $blogs ) {
    599         return $blogs;
     597    $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
     598
     599    if ( null !== $sites ) {
     600        return $sites;
    600601    }
    601602
     
    605606
    606607    if ( ! is_multisite() ) {
    607         $blog_id = get_current_blog_id();
    608         $blogs = array( $blog_id => new stdClass );
    609         $blogs[ $blog_id ]->userblog_id = $blog_id;
    610         $blogs[ $blog_id ]->blogname = get_option('blogname');
    611         $blogs[ $blog_id ]->domain = '';
    612         $blogs[ $blog_id ]->path = '';
    613         $blogs[ $blog_id ]->site_id = 1;
    614         $blogs[ $blog_id ]->siteurl = get_option('siteurl');
    615         $blogs[ $blog_id ]->archived = 0;
    616         $blogs[ $blog_id ]->spam = 0;
    617         $blogs[ $blog_id ]->deleted = 0;
    618         return $blogs;
    619     }
    620 
    621     $blogs = array();
     608        $site_id = get_current_blog_id();
     609        $sites = array( $site_id => new stdClass );
     610        $sites[ $site_id ]->userblog_id = $site_id;
     611        $sites[ $site_id ]->blogname = get_option('blogname');
     612        $sites[ $site_id ]->domain = '';
     613        $sites[ $site_id ]->path = '';
     614        $sites[ $site_id ]->site_id = 1;
     615        $sites[ $site_id ]->siteurl = get_option('siteurl');
     616        $sites[ $site_id ]->archived = 0;
     617        $sites[ $site_id ]->spam = 0;
     618        $sites[ $site_id ]->deleted = 0;
     619        return $sites;
     620    }
     621
     622    $site_ids = array();
    622623
    623624    if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
    624         $blog = get_blog_details( 1 );
    625         if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
    626             $blogs[ 1 ] = (object) array(
    627                 'userblog_id' => 1,
    628                 'blogname'    => $blog->blogname,
    629                 'domain'      => $blog->domain,
    630                 'path'        => $blog->path,
    631                 'site_id'     => $blog->site_id,
    632                 'siteurl'     => $blog->siteurl,
    633                 'archived'    => $blog->archived,
    634                 'mature'      => $blog->mature,
    635                 'spam'        => $blog->spam,
    636                 'deleted'     => $blog->deleted,
    637             );
    638         }
     625        $site_ids[] = 1;
    639626        unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
    640627    }
     
    647634        if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
    648635            continue;
    649         $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
    650         if ( ! is_numeric( $blog_id ) )
     636        $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
     637        if ( ! is_numeric( $site_id ) )
    651638            continue;
    652639
    653         $blog_id = (int) $blog_id;
    654         $blog = get_blog_details( $blog_id );
    655         if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
    656             $blogs[ $blog_id ] = (object) array(
    657                 'userblog_id' => $blog_id,
    658                 'blogname'    => $blog->blogname,
    659                 'domain'      => $blog->domain,
    660                 'path'        => $blog->path,
    661                 'site_id'     => $blog->site_id,
    662                 'siteurl'     => $blog->siteurl,
    663                 'archived'    => $blog->archived,
    664                 'mature'      => $blog->mature,
    665                 'spam'        => $blog->spam,
    666                 'deleted'     => $blog->deleted,
     640        $site_ids[] = (int) $site_id;
     641    }
     642
     643    $sites = array();
     644
     645    if ( ! empty( $site_ids ) ) {
     646        $args = array(
     647            'number'   => '',
     648            'site__in' => $site_ids,
     649        );
     650        if ( ! $all ) {
     651            $args['archived'] = 0;
     652            $args['spam']     = 0;
     653            $args['deleted']  = 0;
     654        }
     655
     656        $_sites = get_sites( $args );
     657
     658        foreach ( $_sites as $site ) {
     659            $sites[ $site->id ] = (object) array(
     660                'userblog_id' => $site->id,
     661                'blogname'    => $site->blogname,
     662                'domain'      => $site->domain,
     663                'path'        => $site->path,
     664                'site_id'     => $site->network_id,
     665                'siteurl'     => $site->siteurl,
     666                'archived'    => $site->archived,
     667                'mature'      => $site->mature,
     668                'spam'        => $site->spam,
     669                'deleted'     => $site->deleted,
    667670            );
    668671        }
     
    670673
    671674    /**
    672      * Filters the list of blogs a user belongs to.
     675     * Filters the list of sites a user belongs to.
    673676     *
    674677     * @since MU
    675678     *
    676      * @param array $blogs   An array of blog objects belonging to the user.
     679     * @param array $sites   An array of site objects belonging to the user.
    677680     * @param int   $user_id User ID.
    678      * @param bool  $all     Whether the returned blogs array should contain all blogs, including
     681     * @param bool  $all     Whether the returned sites array should contain all sites, including
    679682     *                       those marked 'deleted', 'archived', or 'spam'. Default false.
    680683     */
    681     return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
     684    return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
    682685}
    683686
Note: See TracChangeset for help on using the changeset viewer.