PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.4.1
Admin Columns v3.4.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
Arrays.php 7 years ago Date.php 7 years ago File.php 7 years ago Html.php 7 years ago Icon.php 7 years ago Image.php 7 years ago Media.php 7 years ago Network.php 7 years ago Post.php 7 years ago Strings.php 7 years ago Taxonomy.php 7 years ago User.php 7 years ago
User.php
195 lines
1 <?php
2
3 namespace AC\Helper;
4
5 class User {
6
7 /**
8 * @param string $field
9 * @param int $user_id
10 *
11 * @return bool|string|array
12 */
13 public function get_user_field( $field, $user_id ) {
14 $user = get_user_by( 'id', $user_id );
15
16 return isset( $user->{$field} ) ? $user->{$field} : false;
17 }
18
19 public function get_user( $user ) {
20 if ( is_numeric( $user ) ) {
21 $user = get_userdata( $user );
22 }
23
24 if ( ! $user ) {
25 return false;
26 }
27
28 if ( ! is_a( $user, 'WP_User' ) ) {
29 return false;
30 }
31
32 return $user;
33 }
34
35 /**
36 * @param array $role_names
37 *
38 * @return array
39 */
40 public function translate_roles( $role_names ) {
41 $roles = array();
42
43 $wp_roles = wp_roles()->roles;
44
45 foreach ( (array) $role_names as $role ) {
46 if ( isset( $wp_roles[ $role ] ) ) {
47 $roles[ $role ] = translate_user_role( $wp_roles[ $role ]['name'] );
48 }
49 }
50
51 return $roles;
52 }
53
54 /**
55 * @param int|\WP_User $user
56 * @param false|string $format WP_user var, 'first_last_name' or 'roles'
57 *
58 * @return false|string
59 */
60 public function get_display_name( $user, $format = false ) {
61 $user = $this->get_user( $user );
62
63 if ( ! $user ) {
64 return false;
65 }
66
67 $name = $user->display_name;
68
69 switch ( $format ) {
70
71 case 'first_last_name' :
72
73 $name_parts = array();
74
75 if ( $user->first_name ) {
76 $name_parts[] = $user->first_name;
77 }
78 if ( $user->last_name ) {
79 $name_parts[] = $user->last_name;
80 }
81
82 if ( $name_parts ) {
83 $name = implode( ' ', $name_parts );
84 }
85
86 break;
87 case 'roles' :
88 $name = ac_helper()->string->enumeration_list( $this->get_roles_names( $user->roles ), 'and' );
89
90 break;
91 default :
92 if ( ! empty( $user->{$format} ) ) {
93 $name = $user->{$format};
94 }
95 }
96
97 return $name;
98 }
99
100 /**
101 * @param array $roles Role keys
102 *
103 * @return array Role nice names
104 */
105 public function get_roles_names( $roles ) {
106 $role_names = array();
107
108 foreach ( $roles as $role ) {
109 $name = $this->get_role_name( $role );
110
111 if ( $name ) {
112 $role_names[ $role ] = $name;
113 }
114 }
115
116 return $role_names;
117 }
118
119 /**
120 * @param string $role
121 *
122 * @return string
123 */
124 public function get_role_name( $role ) {
125 $roles = $this->get_roles();
126
127 if ( ! array_key_exists( $role, $roles ) ) {
128 return false;
129 }
130
131 return $roles[ $role ];
132 }
133
134 /**
135 * @since 3.4.4
136 *
137 * @param int $user_id
138 * @param string $post_type
139 *
140 * @return string
141 */
142 public function get_postcount( $user_id, $post_type ) {
143 global $wpdb;
144 $sql = "
145 SELECT COUNT(ID)
146 FROM {$wpdb->posts}
147 WHERE post_status = 'publish'
148 AND post_author = %d
149 AND post_type = %s
150 ";
151
152 return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $post_type ) );
153 }
154
155 /**
156 * @return array Translatable roles
157 */
158 public function get_roles() {
159 $roles = array();
160 foreach ( wp_roles()->roles as $k => $role ) {
161 $roles[ $k ] = translate_user_role( $role['name'] );
162 }
163
164 return $roles;
165 }
166
167 /**
168 * @param array $roles
169 *
170 * @return array Role Names
171 */
172 public function get_role_names( $roles ) {
173 $role_names = array();
174
175 $labels = $this->get_roles();
176
177 foreach ( $roles as $role ) {
178 if ( isset( $labels[ $role ] ) ) {
179 $role_names[ $role ] = $labels[ $role ];
180 }
181 }
182
183 return $role_names;
184 }
185
186 /**
187 * @return array
188 */
189 public function get_ids() {
190 global $wpdb;
191
192 return $wpdb->get_col( "SELECT {$wpdb->users}.ID FROM {$wpdb->users}" );
193 }
194
195 }