返回介绍

get_blogs_of_user()

发布于 2017-09-10 22:52:28 字数 5982 浏览 1112 评论 0 收藏 0

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

Get the sites a user belongs to.


description


参数

$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


返回值

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


源代码

File: wp-includes/user.php

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

	$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.
	 *
	 * Passing a non-null value to the filter will effectively short circuit
	 * get_blogs_of_user(), returning that value instead.
	 *
	 * @since 4.6.0
	 *
	 * @param null|array $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[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
		$site_ids[] = 1;
		unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
	}

	$keys = array_keys( $keys );

	foreach ( $keys as $key ) {
		if ( 'capabilities' !== substr( $key, -12 ) )
			continue;
		if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) )
			continue;
		$site_id = str_replace( array( $wpdb->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,
		);
		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
	 *
	 * @param array $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 );
}

更新日志

Versiondescription
4.7.0Converted to use get_sites().
3.0.0Introduced.

相关函数

Uses

  • wp-includes/ms-blogs.php: get_sites()
  • wp-includes/user.php: pre_get_blogs_of_user
  • wp-includes/load.php: is_multisite()
  • wp-includes/load.php: get_current_blog_id()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/user.php: get_user_meta()
  • wp-includes/user.php: get_blogs_of_user
  • Show 3 more uses Hide more uses

Used By

  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::column_blogs()
  • wp-signup.php: signup_another_blog()
  • wp-admin/includes/ms.php: _access_denied_splash()
  • wp-admin/includes/ms.php: choose_primary_blog()
  • wp-admin/includes/ms.php: wpmu_delete_user()
  • wp-admin/includes/dashboard.php: wp_dashboard_quick_press()
  • wp-admin/includes/ms.php: confirm_delete_users()
  • wp-includes/link-template.php: get_dashboard_url()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::initialize()
  • wp-includes/ms-functions.php: get_most_recent_post_of_user()
  • wp-includes/ms-functions.php: get_active_blog_for_user()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getUsersBlogs()
  • Show 8 more used by Hide more used by

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文