get_blogs_of_user()

最后更新于:2021-11-26 06:08:20

get_blogs_of_user( int$user_id, bool$all=false)

Get the sites a user belongs to.

参数

$user_id

(int) (Required) User ID

$all

(bool) (Optional) Whether to retrieve all sites, or only sites that are not marked as deleted, archived, or spam.

Default value: false

响应

(object[]) A list of the user’s sites. An empty array if the user doesn’t exist or belongs to no sites.

源文件

文件: gc-includes/user.php

function get_blogs_of_user( $user_id, $all = false ) {
	global $gcdb;

	$user_id = (int) $user_id;

	// Logged out users can't have sites.
	if ( empty( $user_id ) ) {
		return array();
	}

	/**
	 * Filters the list of a user's sites before it is populated.
	 *
	 * 响应ing a non-null value from the filter will effectively short circuit
	 * get_blogs_of_user(), returning that value instead.
	 *
	 * @since 4.6.0
	 *
	 * @param null|object[] $sites   An array of site objects of which the user is a member.
	 * @param int           $user_id User ID.
	 * @param bool          $all     Whether the returned array should contain all sites, including
	 *                               those marked 'deleted', 'archived', or 'spam'. Default false.
	 */
	$sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );

	if ( null !== $sites ) {
		return $sites;
	}

	$keys = get_user_meta( $user_id );
	if ( empty( $keys ) ) {
		return array();
	}

	if ( ! is_multisite() ) {
		$site_id                        = get_current_blog_id();
		$sites                          = array( $site_id => new stdClass );
		$sites[ $site_id ]->userblog_id = $site_id;
		$sites[ $site_id ]->blogname    = get_option( 'blogname' );
		$sites[ $site_id ]->domain      = '';
		$sites[ $site_id ]->path        = '';
		$sites[ $site_id ]->site_id     = 1;
		$sites[ $site_id ]->siteurl     = get_option( 'siteurl' );
		$sites[ $site_id ]->archived    = 0;
		$sites[ $site_id ]->spam        = 0;
		$sites[ $site_id ]->deleted     = 0;
		return $sites;
	}

	$site_ids = array();

	if ( isset( $keys[ $gcdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
		$site_ids[] = 1;
		unset( $keys[ $gcdb->base_prefix . 'capabilities' ] );
	}

	$keys = array_keys( $keys );

	foreach ( $keys as $key ) {
		if ( 'capabilities' !== substr( $key, -12 ) ) {
			continue;
		}
		if ( $gcdb->base_prefix && 0 !== strpos( $key, $gcdb->base_prefix ) ) {
			continue;
		}
		$site_id = str_replace( array( $gcdb->base_prefix, '_capabilities' ), '', $key );
		if ( ! is_numeric( $site_id ) ) {
			continue;
		}

		$site_ids[] = (int) $site_id;
	}

	$sites = array();

	if ( ! empty( $site_ids ) ) {
		$args = array(
			'number'                 => '',
			'site__in'               => $site_ids,
			'update_site_meta_cache' => false,
		);
		if ( ! $all ) {
			$args['archived'] = 0;
			$args['spam']     = 0;
			$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 sites a user belongs to.
	 *
	 * @since MU (3.0.0)
	 *
	 * @param object[] $sites   An array of site objects belonging to the user.
	 * @param int      $user_id User ID.
	 * @param bool     $all     Whether the returned sites array should contain all sites, including
	 *                          those marked 'deleted', 'archived', or 'spam'. Default false.
	 */
	return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
}
array (
  15 => 
  (object) array(
     'userblog_id' => 15,
     'blogname' => 'sub-site-A',
     'domain' => 'primarysite.test',
     'path' => '/site-a/',
     'site_id' => 1,
     'siteurl' => 'http://primarysite.test/site-a',
     'archived' => '0',
     'mature' => '0',
     'spam' => '0',
     'deleted' => '0',
  ),
  26 => 
  (object) array(
     'userblog_id' => 26,
     'blogname' => 'sub-site-B',
     'domain' => 'primarysite.test',
     'path' => '/site-b/',
     'site_id' => 1,
     'siteurl' => 'http://primarysite.test/site-b',
     'archived' => '0',
     'mature' => '0',
     'spam' => '0',
     'deleted' => '0',
  ),
)