PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.1.2
Admin Columns v3.1.2
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / User.php
codepress-admin-columns / classes / Helper Last commit date
Array.php 8 years ago Date.php 8 years ago File.php 8 years ago Html.php 8 years ago Icon.php 8 years ago Image.php 8 years ago Media.php 8 years ago Network.php 8 years ago Post.php 8 years ago String.php 8 years ago Taxonomy.php 8 years ago User.php 8 years ago
User.php
177 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class AC_Helper_User {
8
9 /**
10 * @param string $field
11 * @param int $user_id
12 *
13 * @return bool|string|array
14 */
15 public function get_user_field( $field, $user_id ) {
16 $user = get_user_by( 'id', $user_id );
17
18 return isset( $user->{$field} ) ? $user->{$field} : false;
19 }
20
21 public function get_user( $user ) {
22 if ( is_numeric( $user ) ) {
23 $user = get_userdata( $user );
24 }
25
26 if ( ! $user ) {
27 return false;
28 }
29
30 if ( ! is_a( $user, 'WP_User' ) ) {
31 return false;
32 }
33
34 return $user;
35 }
36
37 /**
38 * @param array $role_names
39 *
40 * @return array
41 */
42 public function translate_roles( $role_names ) {
43 $roles = array();
44
45 $wp_roles = wp_roles()->roles;
46
47 foreach ( (array) $role_names as $role ) {
48 if ( isset( $wp_roles[ $role ] ) ) {
49 $roles[ $role ] = translate_user_role( $wp_roles[ $role ]['name'] );
50 }
51 }
52
53 return $roles;
54 }
55
56 /**
57 * @param int|WP_User $user
58 * @param false|string $format WP_user var, 'first_last_name' or 'roles'
59 *
60 * @return false|string
61 */
62 public function get_display_name( $user, $format = false ) {
63 $user = $this->get_user( $user );
64
65 if ( ! $user ) {
66 return false;
67 }
68
69 $name = $user->display_name;
70
71 switch ( $format ) {
72
73 case 'first_last_name' :
74
75 $name_parts = array();
76
77 if ( $user->first_name ) {
78 $name_parts[] = $user->first_name;
79 }
80 if ( $user->last_name ) {
81 $name_parts[] = $user->last_name;
82 }
83
84 if ( $name_parts ) {
85 $name = implode( ' ', $name_parts );
86 }
87
88 break;
89 case 'roles' :
90 $name = ac_helper()->string->enumeration_list( $this->get_roles_names( $user->roles ), 'and' );
91
92 break;
93 default :
94 if ( ! empty( $user->{$format} ) ) {
95 $name = $user->{$format};
96 }
97 }
98
99 return $name;
100 }
101
102 /**
103 * @param array $roles Role keys
104 *
105 * @return array Role nice names
106 */
107 public function get_roles_names( $roles ) {
108 $translated = $this->get_roles();
109
110 $role_names = array();
111 foreach ( $roles as $role ) {
112 if ( isset( $translated[ $role ] ) ) {
113 $role_names[ $role ] = $translated[ $role ];
114 }
115 }
116
117 return $role_names;
118 }
119
120 /**
121 * @since 3.4.4
122 */
123 public function get_postcount( $user_id, $post_type ) {
124 global $wpdb;
125 $sql = "
126 SELECT COUNT(ID)
127 FROM {$wpdb->posts}
128 WHERE post_status = 'publish'
129 AND post_author = %d
130 AND post_type = %s
131 ";
132
133 return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $post_type ) );
134 }
135
136 /**
137 * @return array Translatable roles
138 */
139 public function get_roles() {
140 $roles = array();
141 foreach ( wp_roles()->roles as $k => $role ) {
142 $roles[ $k ] = translate_user_role( $role['name'] );
143 }
144
145 return $roles;
146 }
147
148 /**
149 * @param array $roles
150 *
151 * @return array Role Names
152 */
153 public function get_role_names( $roles ) {
154 $role_names = array();
155
156 $labels = $this->get_roles();
157
158 foreach ( $roles as $role ) {
159 if ( isset( $labels[ $role ] ) ) {
160 $role_names[ $role ] = $labels[ $role ];
161 }
162 }
163
164 return $role_names;
165 }
166
167 /**
168 * @return array
169 */
170 public function get_ids() {
171 global $wpdb;
172
173 return $wpdb->get_col( "SELECT {$wpdb->users}.ID FROM {$wpdb->users}" );
174 }
175
176 }
177