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