Make WordPress Core

Ticket #37061: 37061.2.diff

File 37061.2.diff, 6.1 KB (added by flixos90, 7 years ago)
  • src/wp-includes/user.php

     
    559559}
    560560
    561561/**
    562  * Get the blogs a user belongs to.
     562 * Get the sites a user belongs to.
    563563 *
    564564 * @since 3.0.0
    565565 *
     566 * @internal In 4.7.0, converted to use get_sites()
     567 *
    566568 * @global wpdb $wpdb WordPress database abstraction object.
    567569 *
    568570 * @param int  $user_id User ID
    569  * @param bool $all     Whether to retrieve all blogs, or only blogs that are not
     571 * @param bool $all     Whether to retrieve all sites, or only sites that are not
    570572 *                      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.
     573 * @return array A list of the user's sites. An empty array if the user doesn't exist
     574 *               or belongs to no sites.
    573575 */
    574576function get_blogs_of_user( $user_id, $all = false ) {
    575577        global $wpdb;
    576578
    577579        $user_id = (int) $user_id;
    578580
    579         // Logged out users can't have blogs
     581        // Logged out users can't have sites
    580582        if ( empty( $user_id ) )
    581583                return array();
    582584
     
    588590         *
    589591         * @since 4.6.0
    590592         *
    591          * @param null|array $blogs   An array of WP_Site objects of which the user is a member.
     593         * @param null|array $sites   An array of site objects of which the user is a member.
    592594         * @param int        $user_id User ID.
    593595         * @param bool       $all     Whether the returned array should contain all sites, including
    594596         *                            those marked 'deleted', 'archived', or 'spam'. Default false.
    595597         */
    596         $blogs = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
     598        $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
    597599
    598         if ( null !== $blogs ) {
    599                 return $blogs;
     600        if ( null !== $sites ) {
     601                return $sites;
    600602        }
    601603
    602604        $keys = get_user_meta( $user_id );
     
    604606                return array();
    605607
    606608        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;
     609                $site_id = get_current_blog_id();
     610                $sites = array( $site_id => new stdClass );
     611                $sites[ $site_id ]->userblog_id = $site_id;
     612                $sites[ $site_id ]->blogname = get_option('blogname');
     613                $sites[ $site_id ]->domain = '';
     614                $sites[ $site_id ]->path = '';
     615                $sites[ $site_id ]->site_id = 1;
     616                $sites[ $site_id ]->siteurl = get_option('siteurl');
     617                $sites[ $site_id ]->archived = 0;
     618                $sites[ $site_id ]->spam = 0;
     619                $sites[ $site_id ]->deleted = 0;
     620                return $sites;
    619621        }
    620622
    621         $blogs = array();
     623        $site_ids = array();
    622624
    623625        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                 }
     626                $site_ids[] = 1;
    639627                unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
    640628        }
    641629
     
    646634                        continue;
    647635                if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
    648636                        continue;
    649                 $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
    650                 if ( ! is_numeric( $blog_id ) )
     637                $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
     638                if ( ! is_numeric( $site_id ) )
    651639                        continue;
    652640
    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,
    667                         );
    668                 }
     641                $site_ids[] = (int) $site_id;
     642        }
     643
     644        $args = array(
     645                'number'   => '',
     646                'site__in' => $site_ids,
     647        );
     648        if ( ! $all ) {
     649                $args['archived'] = 0;
     650                $args['spam']     = 0;
     651                $args['deleted']  = 0;
     652        }
     653
     654        $_sites = get_sites( $args );
     655
     656        $sites = array();
     657        foreach ( $_sites as $site ) {
     658                $sites[ $site->id ] = (object) array(
     659                        'userblog_id' => $site->id,
     660                        'blogname'    => $site->blogname,
     661                        'domain'      => $site->domain,
     662                        'path'        => $site->path,
     663                        'site_id'     => $site->network_id,
     664                        'siteurl'     => $site->siteurl,
     665                        'archived'    => $site->archived,
     666                        'mature'      => $site->mature,
     667                        'spam'        => $site->spam,
     668                        'deleted'     => $site->deleted,
     669                );
    669670        }
    670671
    671672        /**
    672          * Filters the list of blogs a user belongs to.
     673         * Filters the list of sites a user belongs to.
    673674         *
    674675         * @since MU
    675676         *
    676          * @param array $blogs   An array of blog objects belonging to the user.
     677         * @param array $sites   An array of site objects belonging to the user.
    677678         * @param int   $user_id User ID.
    678          * @param bool  $all     Whether the returned blogs array should contain all blogs, including
     679         * @param bool  $all     Whether the returned sites array should contain all sites, including
    679680         *                       those marked 'deleted', 'archived', or 'spam'. Default false.
    680681         */
    681         return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
     682        return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
    682683}
    683684
    684685/**