get_blogs_of_user()
云策文档标注
概述
get_blogs_of_user() 函数用于获取指定用户所属的所有站点(博客)列表。它支持单站点和多站点环境,并可通过参数控制是否包含已删除、归档或标记为垃圾的站点。
关键要点
- 函数接受两个参数:$user_id(必需,用户ID)和 $all(可选,布尔值,默认为 false,控制是否包含所有站点)。
- 返回一个对象数组,每个对象代表一个站点,包含 userblog_id、blogname、domain、path、site_id、siteurl、archived、mature、spam、deleted 等属性。
- 在非多站点环境中,函数返回当前站点的信息;在多站点环境中,通过用户元数据获取站点ID列表,并使用 get_sites() 查询站点详情。
- 提供了两个过滤器钩子:pre_get_blogs_of_user(在填充站点列表前过滤)和 get_blogs_of_user(在返回结果前过滤)。
- 函数会检查用户ID有效性,无效时返回空数组。
代码示例
$sites = get_blogs_of_user( $user_id );
var_export( $sites );注意事项
- 在多站点环境中,site_id 可能始终为 1,而 userblog_id 表示实际博客ID,需注意命名可能带来的混淆。
- 函数从版本 3.0.0 引入,4.7.0 版本改用 get_sites() 实现。
原文内容
Gets the sites a user belongs to.
Parameters
$user_idintrequired-
User ID
$allbooloptional-
Whether to retrieve all sites, or only sites that are not marked as deleted, archived, or spam.
Default:
false
Source
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.
*
* Returning 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[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
$site_ids[] = 1;
unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
}
$keys = array_keys( $keys );
foreach ( $keys as $key ) {
if ( ! str_ends_with( $key, 'capabilities' ) ) {
continue;
}
if ( $wpdb->base_prefix && ! str_starts_with( $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 (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 flagged for deletion, archived, or marked as spam.
*/
return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );
}
Hooks
- apply_filters( ‘get_blogs_of_user’, object[] $sites, int $user_id, bool $all )
-
Filters the list of sites a user belongs to.
- apply_filters( ‘pre_get_blogs_of_user’, null|object[] $sites, int $user_id, bool $all )
-
Filters the list of a user’s sites before it is populated.
Skip to note 2 content
BooSpot
Output:
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', ), )site_idappears to always be 1 in my testing. It appears thatuserblog_idrepresents the actual ID of the blog but that naming seems like it could be clarified somewhere on this page.