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.

Built from https://develop.svn.wordpress.org/trunk@38682


git-svn-id: http://core.svn.wordpress.org/trunk@38625 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2016-09-30 04:35:27 +00:00
parent 5c038aefb3
commit 1e9f495a18
2 changed files with 61 additions and 58 deletions

View File

@ -559,24 +559,25 @@ function get_users( $args = array() ) {
} }
/** /**
* Get the blogs a user belongs to. * Get the sites a user belongs to.
* *
* @since 3.0.0 * @since 3.0.0
* @since 4.7.0 Converted to use get_sites().
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* *
* @param int $user_id User ID * @param int $user_id User ID
* @param bool $all Whether to retrieve all blogs, or only blogs that are not * @param bool $all Whether to retrieve all sites, or only sites that are not
* marked as deleted, archived, or spam. * marked as deleted, archived, or spam.
* @return array A list of the user's blogs. An empty array if the user doesn't exist * @return array A list of the user's sites. An empty array if the user doesn't exist
* or belongs to no blogs. * or belongs to no sites.
*/ */
function get_blogs_of_user( $user_id, $all = false ) { function get_blogs_of_user( $user_id, $all = false ) {
global $wpdb; global $wpdb;
$user_id = (int) $user_id; $user_id = (int) $user_id;
// Logged out users can't have blogs // Logged out users can't have sites
if ( empty( $user_id ) ) if ( empty( $user_id ) )
return array(); return array();
@ -588,15 +589,15 @@ function get_blogs_of_user( $user_id, $all = false ) {
* *
* @since 4.6.0 * @since 4.6.0
* *
* @param null|array $blogs An array of WP_Site objects of which the user is a member. * @param null|array $sites An array of site objects of which the user is a member.
* @param int $user_id User ID. * @param int $user_id User ID.
* @param bool $all Whether the returned array should contain all sites, including * @param bool $all Whether the returned array should contain all sites, including
* those marked 'deleted', 'archived', or 'spam'. Default false. * those marked 'deleted', 'archived', or 'spam'. Default false.
*/ */
$blogs = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all ); $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );
if ( null !== $blogs ) { if ( null !== $sites ) {
return $blogs; return $sites;
} }
$keys = get_user_meta( $user_id ); $keys = get_user_meta( $user_id );
@ -604,38 +605,24 @@ function get_blogs_of_user( $user_id, $all = false ) {
return array(); return array();
if ( ! is_multisite() ) { if ( ! is_multisite() ) {
$blog_id = get_current_blog_id(); $site_id = get_current_blog_id();
$blogs = array( $blog_id => new stdClass ); $sites = array( $site_id => new stdClass );
$blogs[ $blog_id ]->userblog_id = $blog_id; $sites[ $site_id ]->userblog_id = $site_id;
$blogs[ $blog_id ]->blogname = get_option('blogname'); $sites[ $site_id ]->blogname = get_option('blogname');
$blogs[ $blog_id ]->domain = ''; $sites[ $site_id ]->domain = '';
$blogs[ $blog_id ]->path = ''; $sites[ $site_id ]->path = '';
$blogs[ $blog_id ]->site_id = 1; $sites[ $site_id ]->site_id = 1;
$blogs[ $blog_id ]->siteurl = get_option('siteurl'); $sites[ $site_id ]->siteurl = get_option('siteurl');
$blogs[ $blog_id ]->archived = 0; $sites[ $site_id ]->archived = 0;
$blogs[ $blog_id ]->spam = 0; $sites[ $site_id ]->spam = 0;
$blogs[ $blog_id ]->deleted = 0; $sites[ $site_id ]->deleted = 0;
return $blogs; return $sites;
} }
$blogs = array(); $site_ids = array();
if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) { if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
$blog = get_blog_details( 1 ); $site_ids[] = 1;
if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
$blogs[ 1 ] = (object) array(
'userblog_id' => 1,
'blogname' => $blog->blogname,
'domain' => $blog->domain,
'path' => $blog->path,
'site_id' => $blog->site_id,
'siteurl' => $blog->siteurl,
'archived' => $blog->archived,
'mature' => $blog->mature,
'spam' => $blog->spam,
'deleted' => $blog->deleted,
);
}
unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
} }
@ -646,39 +633,55 @@ function get_blogs_of_user( $user_id, $all = false ) {
continue; continue;
if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
continue; continue;
$blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
if ( ! is_numeric( $blog_id ) ) if ( ! is_numeric( $site_id ) )
continue; continue;
$blog_id = (int) $blog_id; $site_ids[] = (int) $site_id;
$blog = get_blog_details( $blog_id ); }
if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
$blogs[ $blog_id ] = (object) array( $sites = array();
'userblog_id' => $blog_id,
'blogname' => $blog->blogname, if ( ! empty( $site_ids ) ) {
'domain' => $blog->domain, $args = array(
'path' => $blog->path, 'number' => '',
'site_id' => $blog->site_id, 'site__in' => $site_ids,
'siteurl' => $blog->siteurl, );
'archived' => $blog->archived, if ( ! $all ) {
'mature' => $blog->mature, $args['archived'] = 0;
'spam' => $blog->spam, $args['spam'] = 0;
'deleted' => $blog->deleted, $args['deleted'] = 0;
}
$_sites = get_sites( $args );
foreach ( $_sites as $site ) {
$sites[ $site->id ] = (object) array(
'userblog_id' => $site->id,
'blogname' => $site->blogname,
'domain' => $site->domain,
'path' => $site->path,
'site_id' => $site->network_id,
'siteurl' => $site->siteurl,
'archived' => $site->archived,
'mature' => $site->mature,
'spam' => $site->spam,
'deleted' => $site->deleted,
); );
} }
} }
/** /**
* Filters the list of blogs a user belongs to. * Filters the list of sites a user belongs to.
* *
* @since MU * @since MU
* *
* @param array $blogs An array of blog objects belonging to the user. * @param array $sites An array of site objects belonging to the user.
* @param int $user_id User ID. * @param int $user_id User ID.
* @param bool $all Whether the returned blogs array should contain all blogs, including * @param bool $all Whether the returned sites array should contain all sites, including
* those marked 'deleted', 'archived', or 'spam'. Default false. * those marked 'deleted', 'archived', or 'spam'. Default false.
*/ */
return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all ); return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38681'; $wp_version = '4.7-alpha-38682';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.