count_users()

count_users()函数是Wordpress函数,计算具有每个用户角色的用户数。

count_users( string $strategy = ‘time’, int|null $site_id = null )

说明(Description)

假设不存在重复或孤立的功能meta_值。假设角色名称是唯一的短语。WP_User_Query::prepare_Query()使用$strategy=’time’所做的相同假设这是CPU密集型的,应该处理大约10^7个用户。使用$strategy=’memory’这是内存密集型的,应该可以处理大约10^5个用户,但请参见WP Bug 12257。


参数(Parameters)

参数类型必填说明
$strategy(string)可选计算用户时使用的计算策略。接受“时间”或“记忆”。
$site_id(int | null)可选要计算用户数的网站ID。默认为当前网站。

返回(Return)

(array)用户计数。’total_users’(array)站点上的用户总数。


源码(Source)

/**
 * Count number of users who have each of the user roles.
 *
 * Assumes there are neither duplicated nor orphaned capabilities meta_values.
 * Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query()
 * Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users.
 * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb
 *
 * @param string $strategy 'time' or 'memory'
 * @return array Includes a grand total and an array of counts indexed by role strings.
 */
function count_users($strategy = 'time') {
	global $wpdb;
	// Initialize
	$id = get_current_blog_id();
	$blog_prefix = $wpdb->get_blog_prefix($id);
	$result = array();
	if ( 'time' == $strategy ) {
		$avail_roles = wp_roles()->get_names();
		// Build a CPU-intensive query that will return concise information.
		$select_count = array();
		foreach ( $avail_roles as $this_role => $name ) {
			$select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
		}
		$select_count = implode(', ', $select_count);
		// Add the meta_value index to the selection list, then run the query.
		$row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
		// Run the previous loop again to associate results with role names.
		$col = 0;
		$role_counts = array();
		foreach ( $avail_roles as $this_role => $name ) {
			$count = (int) $row[$col++];
			if ($count > 0) {
				$role_counts[$this_role] = $count;
			}
		}
		// Get the meta_value index from the end of the result set.
		$total_users = (int) $row[$col];
		$result['total_users'] = $total_users;
		$result['avail_roles'] =& $role_counts;
	} else {
		$avail_roles = array();
		$users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
		foreach ( $users_of_blog as $caps_meta ) {
			$b_roles = maybe_unserialize($caps_meta);
			if ( ! is_array( $b_roles ) )
				continue;
			foreach ( $b_roles as $b_role => $val ) {
				if ( isset($avail_roles[$b_role]) ) {
					$avail_roles[$b_role]++;
				} else {
					$avail_roles[$b_role] = 1;
				}
			}
		}
		$result['total_users'] = count( $users_of_blog );
		$result['avail_roles'] =& $avail_roles;
	}
	return $result;
}
//
// Private helper functions
//
更新版本源码位置使用被使用
4.9.0wp-includes/user.php:876314

笔记(Notes)

基本示例

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索