|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StarterKit\Repository; |
| 4 | + |
| 5 | +defined('ABSPATH') || exit; |
| 6 | + |
| 7 | +use WP_User; |
| 8 | +use StarterKit\Helper\Utils; |
| 9 | + |
| 10 | +class UserRepository implements WpUserRepositoryInterface |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Gets List of users. |
| 14 | + * |
| 15 | + * @param array $args |
| 16 | + * |
| 17 | + * @return \stdclass [] | int[] | WP_User[] |
| 18 | + */ |
| 19 | + public static function get(array $args = []): array |
| 20 | + { |
| 21 | + $defaults = [ |
| 22 | + 'role' => '', |
| 23 | + 'role__in' => [], |
| 24 | + 'role__not_in' => [], |
| 25 | + 'meta_key' => '', |
| 26 | + 'meta_value' => '', |
| 27 | + 'meta_compare' => '', |
| 28 | + 'meta_query' => [], |
| 29 | + 'include' => [], |
| 30 | + 'exclude' => [], |
| 31 | + 'orderby' => 'login', |
| 32 | + 'order' => 'ASC', |
| 33 | + 'offset' => '', |
| 34 | + 'search' => '', |
| 35 | + 'search_columns' => [], |
| 36 | + 'number' => '', |
| 37 | + 'paged' => 1, |
| 38 | + 'count_total' => false, |
| 39 | + 'fields' => 'all', |
| 40 | + 'who' => '', |
| 41 | + 'has_published_posts' => null, |
| 42 | + 'date_query' => [] // see WP_Date_Query |
| 43 | + ]; |
| 44 | + |
| 45 | + $args = (array)\wp_parse_args($args, $defaults); |
| 46 | + |
| 47 | + return get_users($args); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Gets user by Id. |
| 53 | + * |
| 54 | + * @param int $userId |
| 55 | + * |
| 56 | + * @return \WP_User|null |
| 57 | + */ |
| 58 | + public static function getById(int $userId): WP_User|null |
| 59 | + { |
| 60 | + return get_user_by('ID', $userId) ?: null; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets User Avatar Image Id |
| 66 | + * |
| 67 | + * @param int $userId |
| 68 | + * |
| 69 | + * @return int |
| 70 | + */ |
| 71 | + public static function getAvatarId(int $userId): int |
| 72 | + { |
| 73 | + return (int)Utils::getUserMeta($userId, 'avatar_id'); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets User Avatar Image URL. |
| 79 | + * |
| 80 | + * @param int $userId |
| 81 | + * @param string|int[] $size |
| 82 | + * |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public static function getAvatarUrl(int $userId, string|array $size = 'full'): string |
| 86 | + { |
| 87 | + $avatarId = static::getAvatarId($userId); |
| 88 | + |
| 89 | + return \wp_get_attachment_image_url($avatarId, $size) ?: ''; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * Gets User position. |
| 95 | + * |
| 96 | + * @param int $userId |
| 97 | + * |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public static function getPosition(int $userId): string |
| 101 | + { |
| 102 | + return (string)Utils::getUserMeta($userId, 'position'); |
| 103 | + } |
| 104 | +} |
0 commit comments