count_users

函数
count_users ( $strategy = 'time', $site_id = null )
参数
  • (string) $strategy Optional. The computational strategy to use when counting the users. Accepts either 'time' or 'memory'. Default 'time'.
    Required:
    Default: 'time'
  • (int|null) $site_id Optional. The site ID to count users for. Defaults to the current site.
    Required:
    Default: null
返回值
  • (array) { User counts. @type int $total_users Total number of users on the site. @type int[] $avail_roles Array of user counts keyed by user role. }
定义位置
相关方法
count_user_postswp_count_termswp_count_sitesget_userscurrent_user_can
引入
3.0.0
弃用
-

WordPress中的count_users函数用来计算WordPress网站上的用户数量: 这个函数返回一个数组,包含每个用户角色的用户数量。

计算拥有每个用户角色的用户数量。

假设既不存在重复的也不存在孤儿的权限元值。
假设角色名称是唯一的短语。与WP_User_Query::prepare_query()的假设相同。
使用$strategy = ‘time’ 这是CPU密集型的,应该可以处理大约10^7个用户。
使用$strategy = ‘memory’,这是内存密集型的,应该可以处理大约10^5个用户,但请看WP Bug #12257。

function count_users( $strategy = 'time', $site_id = null ) {
	global $wpdb;

	// Initialize.
	if ( ! $site_id ) {
		$site_id = get_current_blog_id();
	}

	/**
	 * Filters the user count before queries are run.
	 *
	 * Return a non-null value to cause count_users() to return early.
	 *
	 * @since 5.1.0
	 *
	 * @param null|array $result   The value to return instead. Default null to continue with the query.
	 * @param string     $strategy Optional. The computational strategy to use when counting the users.
	 *                             Accepts either 'time' or 'memory'. Default 'time'.
	 * @param int        $site_id  The site ID to count users for.
	 */
	$pre = apply_filters( 'pre_count_users', null, $strategy, $site_id );

	if ( null !== $pre ) {
		return $pre;
	}

	$blog_prefix = $wpdb->get_blog_prefix( $site_id );
	$result      = array();

	if ( 'time' === $strategy ) {
		if ( is_multisite() && get_current_blog_id() != $site_id ) {
			switch_to_blog( $site_id );
			$avail_roles = wp_roles()->get_names();
			restore_current_blog();
		} else {
			$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[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
		$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}
			INNER JOIN {$wpdb->users} ON user_id = ID
			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;
			}
		}

		$role_counts['none'] = (int) $row[ $col++ ];

		// 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(
			'none' => 0,
		);

		$users_of_blog = $wpdb->get_col(
			"
			SELECT meta_value
			FROM {$wpdb->usermeta}
			INNER JOIN {$wpdb->users} ON user_id = ID
			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;
			}
			if ( empty( $b_roles ) ) {
				$avail_roles['none']++;
			}
			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;
}

常见问题

FAQs
查看更多 >