WordPress函数文档count_users()
统计每个角色及网站全部用户数量 描述 返回每个角色的用户数量,以及所有用户的总数。 用法 <?php …
统计每个角色及网站全部用户数量
描述
返回每个角色的用户数量,以及所有用户的总数。
用法
<?php count_users( $strategy ); ?>
参数
$strategy
(string) (可选) ‘time’ or ‘memory’. Generally, the default value ‘time’ is optimal due to the current schema design.
默认值: ‘time’
历史
- 添加于 版本: 3.0.0
源文件
count_users() 函数的代码位于 wp-includes/user.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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
//
|
相关
Count Tags:
wp_count_posts(),
wp_count_terms(),
wp_count_comments(),
count_users()
- 原文:http://codex.wordpress.org/Function_Reference/count_users
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!