返回介绍

remove_user_from_blog()

发布于 2017-09-11 10:09:37 字数 4583 浏览 945 评论 0 收藏 0

remove_user_from_blog( int $user_id,  int $blog_id = '',  string $reassign = '' )

Remove a user from a blog.


description

Use the ‘remove_user_from_blog’ action to fire an event when users are removed from a blog.

Accepts an optional $reassign parameter, if you want to reassign the user’s blog posts to another user upon removal.


参数

$user_id

(int) (Required) ID of the user you're removing.

$blog_id

(int) (Optional) ID of the blog you're removing the user from.

Default value: ''

$reassign

(string) (Optional) A user to whom to reassign posts.

Default value: ''


返回值

(true|WP_Error)


源代码

File: wp-includes/ms-functions.php

function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
	global $wpdb;
	switch_to_blog($blog_id);
	$user_id = (int) $user_id;
	/**
	 * Fires before a user is removed from a site.
	 *
	 * @since MU
	 *
	 * @param int $user_id User ID.
	 * @param int $blog_id Blog ID.
	 */
	do_action( 'remove_user_from_blog', $user_id, $blog_id );

	// If being removed from the primary blog, set a new primary if the user is assigned
	// to multiple blogs.
	$primary_blog = get_user_meta($user_id, 'primary_blog', true);
	if ( $primary_blog == $blog_id ) {
		$new_id = '';
		$new_domain = '';
		$blogs = get_blogs_of_user($user_id);
		foreach ( (array) $blogs as $blog ) {
			if ( $blog->userblog_id == $blog_id )
				continue;
			$new_id = $blog->userblog_id;
			$new_domain = $blog->domain;
			break;
		}

		update_user_meta($user_id, 'primary_blog', $new_id);
		update_user_meta($user_id, '源代码_domain', $new_domain);
	}

	// wp_revoke_user($user_id);
	$user = get_userdata( $user_id );
	if ( ! $user ) {
		restore_current_blog();
		return new WP_Error('user_does_not_exist', __('That user does not exist.'));
	}

	$user->remove_all_caps();

	$blogs = get_blogs_of_user($user_id);
	if ( count($blogs) == 0 ) {
		update_user_meta($user_id, 'primary_blog', '');
		update_user_meta($user_id, '源代码_domain', '');
	}

	if ( $reassign != '' ) {
		$reassign = (int) $reassign;
		$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
		$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );

		if ( ! empty( $post_ids ) ) {
			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
			array_walk( $post_ids, 'clean_post_cache' );
		}

		if ( ! empty( $link_ids ) ) {
			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
			array_walk( $link_ids, 'clean_bookmark_cache' );
		}
	}

	restore_current_blog();

	return true;
}

更新日志

Versiondescription
MU 1.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/plugin.php: do_action()
  • wp-includes/user.php: get_user_meta()
  • wp-includes/user.php: get_blogs_of_user()
  • wp-includes/user.php: update_user_meta()
  • wp-includes/ms-functions.php: remove_user_from_blog
  • wp-includes/ms-blogs.php: switch_to_blog()
  • wp-includes/ms-blogs.php: restore_current_blog()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::query()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 8 more uses Hide more uses

Used By

  • wp-admin/includes/ms.php: wpmu_delete_user()
  • wp-admin/includes/ms.php: wpmu_delete_blog()
  • wp-admin/includes/user.php: wp_delete_user()
  • wp-includes/ms-functions.php: add_new_user_to_blog()

User Contributed Notes

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

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

发布评论

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