Make WordPress Core

Ticket #45640: get_blogs_of_users_new.patch

File get_blogs_of_users_new.patch, 15.7 KB (added by maniu, 6 years ago)

modified version that does not include tweaks to $all parameters as they are confusing and not crucial for performance improvement.

  • src/wp-admin/includes/dashboard.php

    diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php
    index e3160bb390..4220e9ea3b 100644
    a b function wp_dashboard_quick_press( $error_msg = false ) { 
    502502                $post    = get_default_post_to_edit( 'post', true );
    503503                $user_id = get_current_user_id();
    504504                // Don't create an option if this is a super admin who does not belong to this site.
    505                 if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ) ) ) {
     505                if ( in_array( get_current_blog_id(), get_blogs_of_user( $user_id, false, array( 'fields' => 'ids' ) ) ) )
    506506                        update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID
    507507                }
    508508        }
  • src/wp-admin/includes/ms.php

    diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php
    index f8e3fc09e9..68f78af7b9 100644
    a b function wpmu_delete_user( $id ) { 
    165165         */
    166166        do_action( 'wpmu_delete_user', $id );
    167167
    168         $blogs = get_blogs_of_user( $id );
     168        $blogs_ids = get_blogs_of_user( $id, false, array( 'fields' => 'ids' ) );
    169169
    170         if ( ! empty( $blogs ) ) {
    171                 foreach ( $blogs as $blog ) {
    172                         switch_to_blog( $blog->userblog_id );
    173                         remove_user_from_blog( $id, $blog->userblog_id );
     170        if ( ! empty( $blogs_ids ) ) {
     171                foreach ( $blogs_ids as $blog_id ) {
     172                        switch_to_blog( $blog_id );
     173                        remove_user_from_blog( $id, $blog_id );
    174174
    175175                        $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
    176176                        foreach ( (array) $post_ids as $post_id ) {
    function _access_denied_splash() { 
    630630                return;
    631631        }
    632632
    633         $blogs = get_blogs_of_user( get_current_user_id() );
     633        /**
     634         * Filters arguments when getting user's blogs for access denied message.
     635         *
     636         * @param array $args An array of optional arguments used when getting blogs.
     637         */
     638        $args = apply_filters( 'access_denied_splash_get_blogs_of_user_args', array() );
     639        $blogs = get_blogs_of_user( get_current_user_id(), false, $args );
    634640
    635641        if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) {
    636642                return;
    function choose_primary_blog() { 
    808814                <th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th>
    809815                <td>
    810816                <?php
    811                 $all_blogs    = get_blogs_of_user( get_current_user_id() );
    812                 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
    813                 if ( count( $all_blogs ) > 1 ) {
     817                $all_blogs_ids = get_blogs_of_user( get_current_user_id(), false, array( 'fields' => 'ids' ) );
     818                $primary_blog  = get_user_meta( get_current_user_id(), 'primary_blog', true );
     819                if ( count( $all_blogs_ids ) > 1 ) {
    814820                        $found = false;
    815821                        ?>
    816822                        <select name="primary_blog" id="primary_blog">
  • src/wp-admin/network/users.php

    diff --git a/src/wp-admin/network/users.php b/src/wp-admin/network/users.php
    index 7f212329f6..40561263ef 100644
    a b if ( isset( $_GET['action'] ) ) { 
    7575                                                                }
    7676
    7777                                                                $userfunction = 'all_spam';
    78                                                                 $blogs        = get_blogs_of_user( $user_id, true );
    79                                                                 foreach ( (array) $blogs as $details ) {
    80                                                                         if ( $details->userblog_id != get_network()->site_id ) { // main blog not a spam !
    81                                                                                 update_blog_status( $details->userblog_id, 'spam', '1' );
     78                                                                $blogs_ids    = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) );
     79                                                                foreach ( (array) $blogs_ids as $blog_id ) {
     80                                                                        if ( $blog_id != get_network()->site_id ) { // main blog not a spam !
     81                                                                                update_blog_status( $blog_id, 'spam', '1' );
    8282                                                                        }
    8383                                                                }
    8484                                                                update_user_status( $user_id, 'spam', '1' );
    if ( isset( $_GET['action'] ) ) { 
    8686
    8787                                                        case 'notspam':
    8888                                                                $userfunction = 'all_notspam';
    89                                                                 $blogs        = get_blogs_of_user( $user_id, true );
    90                                                                 foreach ( (array) $blogs as $details ) {
    91                                                                         update_blog_status( $details->userblog_id, 'spam', '0' );
     89                                                                $blogs_ids    = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) );
     90                                                                foreach ( (array) $blogs_ids as $blog_id ) {
     91                                                                        update_blog_status( $blog_id, 'spam', '0' );
    9292                                                                }
    9393
    9494                                                                update_user_status( $user_id, 'spam', '0' );
  • src/wp-admin/user-new.php

    diff --git a/src/wp-admin/user-new.php b/src/wp-admin/user-new.php
    index a63a329d6d..fae976a41e 100644
    a b if ( isset( $_REQUEST['action'] ) && 'adduser' == $_REQUEST['action'] ) { 
    6363        $redirect       = 'user-new.php';
    6464        $username       = $user_details->user_login;
    6565        $user_id        = $user_details->ID;
    66         if ( $username != null && array_key_exists( $blog_id, get_blogs_of_user( $user_id ) ) ) {
     66        if ( $username != null && in_array( $blog_id, get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) ) ) ) {
    6767                $redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
    6868        } else {
    6969                if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
  • src/wp-includes/link-template.php

    diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php
    index 20d04e1a85..cf5ad6d2fc 100644
    a b function set_url_scheme( $url, $scheme = null ) { 
    36013601function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
    36023602        $user_id = $user_id ? (int) $user_id : get_current_user_id();
    36033603
    3604         $blogs = get_blogs_of_user( $user_id );
    3605         if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
     3604        $blogs_exist = get_blogs_of_user( $user_id, false, array( 'number' => 1, 'fields' => 'ids' ) );
     3605        if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs_exist ) ) {
    36063606                $url = user_admin_url( $path, $scheme );
    36073607        } elseif ( ! is_multisite() ) {
    36083608                $url = admin_url( $path, $scheme );
    36093609        } else {
    36103610                $current_blog = get_current_blog_id();
    3611                 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
     3611                $blogs_ids = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) );
     3612                if ( $current_blog  && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, $blogs_ids ) ) ) {
    36123613                        $url = admin_url( $path, $scheme );
    36133614                } else {
    36143615                        $active = get_active_blog_for_user( $user_id );
  • src/wp-includes/ms-functions.php

    diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php
    index d27f71954c..daaf9bef89 100644
    a b function get_sitestats() { 
    3838 * @return WP_Site|void The blog object
    3939 */
    4040function get_active_blog_for_user( $user_id ) {
    41         $blogs = get_blogs_of_user( $user_id );
    42         if ( empty( $blogs ) ) {
     41        $blogs_ids = get_blogs_of_user( $user_id, false, array( 'fields' => 'ids' ) );
     42        if ( empty( $blogs_ids ) ) {
    4343                return;
    4444        }
    4545
    4646        if ( ! is_multisite() ) {
    47                 return $blogs[ get_current_blog_id() ];
     47                return get_site( get_current_blog_id() );
    4848        }
    4949
    50         $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    51         $first_blog   = current( $blogs );
     50        $primary_blog  = get_user_meta( $user_id, 'primary_blog', true );
     51        $first_blog_id = current($blogs_ids);
    5252        if ( false !== $primary_blog ) {
    53                 if ( ! isset( $blogs[ $primary_blog ] ) ) {
    54                         update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
    55                         $primary = get_site( $first_blog->userblog_id );
     53                if ( ! in_array( $primary_blog, $blogs_ids ) ) {
     54                        update_user_meta( $user_id, 'primary_blog', $first_blog_id );
     55                        $primary = get_site( $first_blog_id );
    5656                } else {
    5757                        $primary = get_site( $primary_blog );
    5858                }
    5959        } else {
    6060                //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
    61                 $result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
     61                $result = add_user_to_blog( $first_blog_id, $user_id, 'subscriber' );
    6262
    6363                if ( ! is_wp_error( $result ) ) {
    64                         update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
    65                         $primary = $first_blog;
     64                        update_user_meta( $user_id, 'primary_blog', $first_blog_id );
     65                        $primary = get_site( $first_blog_id );
    6666                }
    6767        }
    6868
    6969        if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
    70                 $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
    71                 $ret   = false;
    72                 if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
    73                         foreach ( (array) $blogs as $blog_id => $blog ) {
     70                $blogs_ids = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) ); // if a user's primary blog is shut down, check their other blogs.
     71                $ret       = false;
     72                if ( is_array( $blogs_ids ) && count( $blogs_ids ) > 0 ) {
     73                        foreach ( (array) $blogs_ids as $blog_id ) {
    7474                                if ( $blog->site_id != get_current_network_id() ) {
    7575                                        continue;
    7676                                }
    function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) { 
    250250        if ( $primary_blog == $blog_id ) {
    251251                $new_id     = '';
    252252                $new_domain = '';
    253                 $blogs      = get_blogs_of_user( $user_id );
    254                 foreach ( (array) $blogs as $blog ) {
    255                         if ( $blog->userblog_id == $blog_id ) {
     253                $user_blogs_ids = get_blogs_of_user($user_id, false, array( 'fields' => 'ids' ));
     254                foreach ( (array) $user_blogs_ids as $user_blog_id ) {
     255                        if ( $user_blog_id == $blog_id ) {
    256256                                continue;
    257257                        }
    258258                        $new_id     = $blog->userblog_id;
    function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) { 
    273273
    274274        $user->remove_all_caps();
    275275
    276         $blogs = get_blogs_of_user( $user_id );
     276        $blogs = get_blogs_of_user($user_id, false, array( 'fields' => 'ids' ));
    277277        if ( count( $blogs ) == 0 ) {
    278278                update_user_meta( $user_id, 'primary_blog', '' );
    279279                update_user_meta( $user_id, 'source_domain', '' );
    function get_current_site() { 
    17451745function get_most_recent_post_of_user( $user_id ) {
    17461746        global $wpdb;
    17471747
    1748         $user_blogs       = get_blogs_of_user( (int) $user_id );
     1748        $user_blogs_ids   = get_blogs_of_user( (int) $user_id, false, array( 'fields' => 'ids' ) );
    17491749        $most_recent_post = array();
    17501750
    17511751        // Walk through each blog and get the most recent post
    17521752        // published by $user_id
    1753         foreach ( (array) $user_blogs as $blog ) {
    1754                 $prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
     1753        foreach ( (array) $user_blogs_ids as $blog_id ) {
     1754                $prefix      = $wpdb->get_blog_prefix( $blog_id );
    17551755                $recent_post = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A );
    17561756
    17571757                // Make sure we found a post
    function get_most_recent_post_of_user( $user_id ) { 
    17631763                        // most recent post.
    17641764                        if ( ! isset( $most_recent_post['post_gmt_ts'] ) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
    17651765                                $most_recent_post = array(
    1766                                         'blog_id'       => $blog->userblog_id,
     1766                                        'blog_id'       => $blog_id,
    17671767                                        'post_id'       => $recent_post['ID'],
    17681768                                        'post_date_gmt' => $recent_post['post_date_gmt'],
    17691769                                        'post_gmt_ts'   => $post_gmt_ts,
  • src/wp-includes/user.php

    diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php
    index 9696789dd5..b9fc415e4b 100644
    a b function get_users( $args = array() ) { 
    593593 *
    594594 * @global wpdb $wpdb WordPress database abstraction object.
    595595 *
    596  * @param int  $user_id User ID
    597  * @param bool $all     Whether to retrieve all sites, or only sites that are not
    598  *                      marked as deleted, archived, or spam.
     596 * @param int         $user_id User ID
     597 * @param bool|string $all     Whether to retrieve all sites, or only sites that are not
     598         *                         marked as deleted, archived, or spam.
     599 * @param array       $args    An array of optional arguments that will be passed to
     600 *                            'get_sites' function.
    599601 * @return array A list of the user's sites. An empty array if the user doesn't exist
    600602 *               or belongs to no sites.
    601603 */
    602 function get_blogs_of_user( $user_id, $all = false ) {
     604function get_blogs_of_user( $user_id, $all = false, $args = false ) {
    603605        global $wpdb;
    604606
    605607        $user_id = (int) $user_id;
    function get_blogs_of_user( $user_id, $all = false ) { 
    617619         *
    618620         * @since 4.6.0
    619621         *
    620          * @param null|array $sites   An array of site objects of which the user is a member.
    621          * @param int        $user_id User ID.
    622          * @param bool       $all     Whether the returned array should contain all sites, including
    623          *                            those marked 'deleted', 'archived', or 'spam'. Default false.
     622         * @param null|array  $sites   An array of site objects of which the user is a member.
     623         * @param int         $user_id User ID.
     624         * @param bool|string $all     Whether to retrieve all sites, or only sites that are not
     625         *                             marked as deleted, archived, or spam.
     626         * @param array        $args   An array of optional arguments that will be passed to
     627         *                             'get_sites' function.
    624628         */
    625         $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
     629        $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all, $args );
    626630
    627631        if ( null !== $sites ) {
    628632                return $sites;
    function get_blogs_of_user( $user_id, $all = false ) { 
    675679        $sites = array();
    676680
    677681        if ( ! empty( $site_ids ) ) {
    678                 $args = array(
     682                $default_args = array(
    679683                        'number'                 => '',
    680684                        'site__in'               => $site_ids,
    681685                        'update_site_meta_cache' => false,
    682686                );
    683687                if ( ! $all ) {
    684                         $args['archived'] = 0;
    685                         $args['spam']     = 0;
    686                         $args['deleted']  = 0;
     688                        $default_args['archived'] = 0;
     689                        $default_args['spam']     = 0;
     690                        $default_args['deleted']  = 0;
    687691                }
    688692
     693                $args = wp_parse_args( $args, $default_args );
     694
     695                /**
     696                 * Filters the list of arguments that are passed to get_sites function.
     697                 *
     698                 * @param array       $args    An array of optional arguments that will be passed to
     699                 *                             'get_sites' function.
     700                 * @param int         $user_id User ID.
     701                 * @param bool|string $all     Whether to retrieve all sites, or only sites that are not
     702                 *                             marked as deleted, archived, or spam.
     703                 */
     704                $args = apply_filters( 'get_blogs_of_user_get_sites_args', $args, $user_id, $all );
     705
    689706                $_sites = get_sites( $args );
    690707
    691                 foreach ( $_sites as $site ) {
    692                         $sites[ $site->id ] = (object) array(
    693                                 'userblog_id' => $site->id,
    694                                 'blogname'    => $site->blogname,
    695                                 'domain'      => $site->domain,
    696                                 'path'        => $site->path,
    697                                 'site_id'     => $site->network_id,
    698                                 'siteurl'     => $site->siteurl,
    699                                 'archived'    => $site->archived,
    700                                 'mature'      => $site->mature,
    701                                 'spam'        => $site->spam,
    702                                 'deleted'     => $site->deleted,
    703                         );
     708                if( isset( $args[ 'fields' ] ) && $args[ 'fields' ] === 'ids' ) {
     709                        return $_sites;
     710                } else {
     711                        foreach ( $_sites as $site ) {
     712                                $sites[ $site->id ] = (object) array(
     713                                        'userblog_id' => $site->id,
     714                                        'blogname'    => $site->blogname,
     715                                        'domain'      => $site->domain,
     716                                        'path'        => $site->path,
     717                                        'site_id'     => $site->network_id,
     718                                        'siteurl'     => $site->siteurl,
     719                                        'archived'    => $site->archived,
     720                                        'mature'      => $site->mature,
     721                                        'spam'        => $site->spam,
     722                                        'deleted'     => $site->deleted,
     723                                );
     724                        }
    704725                }
    705726        }
    706727
    function get_blogs_of_user( $user_id, $all = false ) { 
    714735         * @param bool  $all     Whether the returned sites array should contain all sites, including
    715736         *                       those marked 'deleted', 'archived', or 'spam'. Default false.
    716737         */
    717         return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
     738        return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all, $args );
    718739}
    719740
    720741/**