PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.0.1
Admin Columns v4.0.1
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
Select 6 years ago Arrays.php 6 years ago Date.php 6 years ago File.php 6 years ago Html.php 6 years ago Icon.php 6 years ago Image.php 6 years ago Media.php 6 years ago Network.php 6 years ago Post.php 6 years ago Strings.php 6 years ago Taxonomy.php 6 years ago User.php 6 years ago
User.php
197 lines
1 <?php
2
3 namespace AC\Helper;
4
5 use WP_User;
6
7 class 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 $role_names = array();
109
110 foreach ( $roles as $role ) {
111 $name = $this->get_role_name( $role );
112
113 if ( $name ) {
114 $role_names[ $role ] = $name;
115 }
116 }
117
118 return $role_names;
119 }
120
121 /**
122 * @param string $role
123 *
124 * @return string
125 */
126 public function get_role_name( $role ) {
127 $roles = $this->get_roles();
128
129 if ( ! array_key_exists( $role, $roles ) ) {
130 return false;
131 }
132
133 return $roles[ $role ];
134 }
135
136 /**
137 * @since 3.4.4
138 *
139 * @param int $user_id
140 * @param string $post_type
141 *
142 * @return string
143 */
144 public function get_postcount( $user_id, $post_type ) {
145 global $wpdb;
146 $sql = "
147 SELECT COUNT(ID)
148 FROM {$wpdb->posts}
149 WHERE post_status = 'publish'
150 AND post_author = %d
151 AND post_type = %s
152 ";
153
154 return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $post_type ) );
155 }
156
157 /**
158 * @return array Translatable roles
159 */
160 public function get_roles() {
161 $roles = array();
162 foreach ( wp_roles()->roles as $k => $role ) {
163 $roles[ $k ] = translate_user_role( $role['name'] );
164 }
165
166 return $roles;
167 }
168
169 /**
170 * @param array $roles
171 *
172 * @return array Role Names
173 */
174 public function get_role_names( $roles ) {
175 $role_names = array();
176
177 $labels = $this->get_roles();
178
179 foreach ( $roles as $role ) {
180 if ( isset( $labels[ $role ] ) ) {
181 $role_names[ $role ] = $labels[ $role ];
182 }
183 }
184
185 return $role_names;
186 }
187
188 /**
189 * @return array
190 */
191 public function get_ids() {
192 global $wpdb;
193
194 return $wpdb->get_col( "SELECT {$wpdb->users}.ID FROM {$wpdb->users}" );
195 }
196
197 }