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 | } |