Array.php
9 years ago
Date.php
9 years ago
File.php
9 years ago
Html.php
9 years ago
Icon.php
9 years ago
Image.php
9 years ago
Network.php
9 years ago
Post.php
9 years ago
String.php
9 years ago
Taxonomy.php
9 years ago
User.php
9 years ago
User.php
145 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 | return $user && is_a( $user, 'WP_User' ) ? $user : false; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param array $role_names |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | public function translate_roles( $role_names ) { |
| 35 | $roles = array(); |
| 36 | |
| 37 | $wp_roles = wp_roles()->roles; |
| 38 | |
| 39 | foreach ( (array) $role_names as $role ) { |
| 40 | if ( isset( $wp_roles[ $role ] ) ) { |
| 41 | $roles[ $role ] = translate_user_role( $wp_roles[ $role ]['name'] ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return $roles; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param $user |
| 50 | * @param bool $format |
| 51 | * |
| 52 | * @return false|string |
| 53 | */ |
| 54 | public function get_display_name( $user, $format = false ) { |
| 55 | $name = false; |
| 56 | |
| 57 | if ( $user = $this->get_user( $user ) ) { |
| 58 | $name = $user->display_name; |
| 59 | |
| 60 | if ( ! empty( $user->{$format} ) ) { |
| 61 | $name = $user->{$format}; |
| 62 | } |
| 63 | |
| 64 | if ( 'first_last_name' == $format ) { |
| 65 | $name_parts = array(); |
| 66 | if ( $user->first_name ) { |
| 67 | $name_parts[] = $user->first_name; |
| 68 | } |
| 69 | if ( $user->last_name ) { |
| 70 | $name_parts[] = $user->last_name; |
| 71 | } |
| 72 | if ( $name_parts ) { |
| 73 | $name = implode( ' ', $name_parts ); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return $name; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @since 3.4.4 |
| 83 | */ |
| 84 | public function get_postcount( $user_id, $post_type ) { |
| 85 | global $wpdb; |
| 86 | $sql = " |
| 87 | SELECT COUNT(ID) |
| 88 | FROM {$wpdb->posts} |
| 89 | WHERE post_status = 'publish' |
| 90 | AND post_author = %d |
| 91 | AND post_type = %s |
| 92 | "; |
| 93 | |
| 94 | return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $post_type ) ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @return array Translatable roles |
| 99 | */ |
| 100 | public function get_roles() { |
| 101 | $roles = array(); |
| 102 | foreach ( wp_roles()->roles as $k => $role ) { |
| 103 | $roles[ $k ] = translate_user_role( $role['name'] ); |
| 104 | } |
| 105 | |
| 106 | return $roles; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return array |
| 111 | */ |
| 112 | public function get_ids() { |
| 113 | global $wpdb; |
| 114 | |
| 115 | return $wpdb->get_col( "SELECT {$wpdb->users}.ID FROM {$wpdb->users}" ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Store current user meta data that is compatible with multi sites |
| 120 | * |
| 121 | * @param string $key |
| 122 | * @param array|string $value |
| 123 | */ |
| 124 | public function update_meta_site( $key, $value, $prev_value = '' ) { |
| 125 | return update_user_meta( get_current_user_id(), $key . get_current_blog_id(), $value, $prev_value ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get current user meta data |
| 130 | * @param string $key |
| 131 | */ |
| 132 | public function get_meta_site( $key, $single = false ) { |
| 133 | return get_user_meta( get_current_user_id(), $key . get_current_blog_id(), $single ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get current user meta data |
| 138 | * @param string $key |
| 139 | */ |
| 140 | public function delete_meta_site( $key, $value = '' ) { |
| 141 | return delete_user_meta( get_current_user_id(), $key . get_current_blog_id(), $value ); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 |