函数文档

get_active_blog_for_user()

💡 云策文档标注

概述

get_active_blog_for_user() 函数用于获取用户的一个活跃博客。在 WordPress 多站点环境中,它优先返回用户的主博客(如果活跃),否则返回其他活跃博客,若无则添加用户为仪表板博客的订阅者并返回该博客。

关键要点

  • 函数接受一个必需参数 $user_id(用户ID),返回 WP_Site 对象或 void。
  • 逻辑流程:先获取用户的所有博客,若非多站点则直接返回当前博客;在多站点下,检查主博客是否活跃,若不活跃则遍历其他博客寻找活跃博客。
  • 如果用户没有活跃博客,函数会调用 add_user_to_blog() 将用户添加为仪表板博客的订阅者,并更新用户元数据。
  • 函数内部使用了多个 WordPress 核心函数,如 get_blogs_of_user()、get_user_meta()、update_user_meta() 等,确保数据准确性和一致性。

代码示例

function get_active_blog_for_user( $user_id ) {
    $blogs = get_blogs_of_user( $user_id );
    if ( empty( $blogs ) ) {
        return;
    }

    if ( ! is_multisite() ) {
        return $blogs[ get_current_blog_id() ];
    }

    $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    $first_blog   = current( $blogs );
    if ( false !== $primary_blog ) {
        if ( ! isset( $blogs[ $primary_blog ] ) ) {
            update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
            $primary = get_site( $first_blog->userblog_id );
        } else {
            $primary = get_site( $primary_blog );
        }
    } else {
        $result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );

        if ( ! is_wp_error( $result ) ) {
            update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
            $primary = $first_blog;
        }
    }

    if ( ( ! is_object( $primary ) )
        || ( '1' === $primary->archived || '1' === $primary->spam || '1' === $primary->deleted )
    ) {
        $blogs = get_blogs_of_user( $user_id, true );
        $ret   = false;

        if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
            $current_network_id = get_current_network_id();

            foreach ( (array) $blogs as $blog_id => $blog ) {
                if ( $blog->site_id !== $current_network_id ) {
                    continue;
                }

                $details = get_site( $blog_id );
                if ( is_object( $details )
                    && '0' === $details->archived && '0' === $details->spam && '0' === $details->deleted
                ) {
                    $ret = $details;
                    if ( (int) get_user_meta( $user_id, 'primary_blog', true ) !== $blog_id ) {
                        update_user_meta( $user_id, 'primary_blog', $blog_id );
                    }
                    if ( ! get_user_meta( $user_id, 'source_domain', true ) ) {
                        update_user_meta( $user_id, 'source_domain', $details->domain );
                    }
                    break;
                }
            }
        } else {
            return;
        }

        return $ret;
    } else {
        return $primary;
    }
}

注意事项

  • 函数在非多站点环境中直接返回当前博客,无需复杂逻辑。
  • 如果用户的主博客已归档、标记为垃圾或删除,函数会检查用户的其他博客以找到活跃博客。
  • 使用 add_user_to_blog() 时,注意其可能返回 WP_Error,函数通过 is_wp_error() 进行错误处理。
  • 函数更新用户元数据(如 primary_blog 和 source_domain),确保后续调用的一致性。

📄 原文内容

Gets one of a user’s active blogs.

Description

Returns the user’s primary blog, if they have one and it is active. If it’s inactive, function returns another active blog of the user. If none are found, the user is added as a Subscriber to the Dashboard Blog and that blog is returned.

Parameters

$user_idintrequired
The unique ID of the user

Return

WP_Site|void The blog object

Source

function get_active_blog_for_user( $user_id ) {
	$blogs = get_blogs_of_user( $user_id );
	if ( empty( $blogs ) ) {
		return;
	}

	if ( ! is_multisite() ) {
		return $blogs[ get_current_blog_id() ];
	}

	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
	$first_blog   = current( $blogs );
	if ( false !== $primary_blog ) {
		if ( ! isset( $blogs[ $primary_blog ] ) ) {
			update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
			$primary = get_site( $first_blog->userblog_id );
		} else {
			$primary = get_site( $primary_blog );
		}
	} else {
		// TODO: Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
		$result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );

		if ( ! is_wp_error( $result ) ) {
			update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
			$primary = $first_blog;
		}
	}

	if ( ( ! is_object( $primary ) )
		|| ( '1' === $primary->archived || '1' === $primary->spam || '1' === $primary->deleted )
	) {
		$blogs = get_blogs_of_user( $user_id, true ); // If a user's primary blog is shut down, check their other blogs.
		$ret   = false;

		if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
			$current_network_id = get_current_network_id();

			foreach ( (array) $blogs as $blog_id => $blog ) {
				if ( $blog->site_id !== $current_network_id ) {
					continue;
				}

				$details = get_site( $blog_id );
				if ( is_object( $details )
					&& '0' === $details->archived && '0' === $details->spam && '0' === $details->deleted
				) {
					$ret = $details;
					if ( (int) get_user_meta( $user_id, 'primary_blog', true ) !== $blog_id ) {
						update_user_meta( $user_id, 'primary_blog', $blog_id );
					}
					if ( ! get_user_meta( $user_id, 'source_domain', true ) ) {
						update_user_meta( $user_id, 'source_domain', $details->domain );
					}
					break;
				}
			}
		} else {
			return;
		}

		return $ret;
	} else {
		return $primary;
	}
}

Changelog

Version Description
MU (3.0.0) Introduced.