|
6 | 6 |
|
7 | 7 | class User |
8 | 8 | { |
9 | | - protected $id; |
10 | | - |
11 | | - protected $WP_User; |
12 | | - |
13 | | - /** |
14 | | - * @var string |
15 | | - */ |
16 | | - protected $username; |
17 | | - |
18 | | - /** |
19 | | - * @var string |
20 | | - */ |
21 | | - protected $nicename; |
22 | | - |
23 | | - /** |
24 | | - * @var string |
25 | | - */ |
26 | | - protected $displayName; |
27 | | - |
28 | | - /** |
29 | | - * @var string |
30 | | - */ |
31 | | - protected $email; |
32 | | - |
33 | | - /** |
34 | | - * @var string |
35 | | - */ |
36 | | - protected $url; |
37 | | - |
38 | | - /** |
39 | | - * @var string |
40 | | - */ |
41 | | - protected $registeredDate; |
42 | | - |
43 | | - /** |
44 | | - * @var string |
45 | | - */ |
46 | | - protected $first_name; |
47 | | - |
48 | | - /** |
49 | | - * @var string |
50 | | - */ |
51 | | - protected $last_name; |
52 | | - |
53 | | - /** |
54 | | - * @var string |
55 | | - */ |
56 | | - protected $nickname; |
57 | | - |
58 | | - /** |
59 | | - * @var string |
60 | | - */ |
61 | | - protected $description; |
62 | | - |
63 | | - /** |
64 | | - * Set up |
65 | | - * |
66 | | - * @param integer $id |
67 | | - */ |
68 | | - public function __construct(int $id) |
| 9 | + public int $id; |
| 10 | + |
| 11 | + protected \WP_User $WP_User; |
| 12 | + |
| 13 | + public string $username = ''; |
| 14 | + public string $nicename = ''; |
| 15 | + public string $displayName = ''; |
| 16 | + public string $email = ''; |
| 17 | + public string $url = ''; |
| 18 | + public Carbon $registeredDate; |
| 19 | + public string $first_name = ''; |
| 20 | + public string $last_name = ''; |
| 21 | + public string $full_name = ''; |
| 22 | + public string $nickname = ''; |
| 23 | + public string $description = ''; |
| 24 | + |
| 25 | + public function __construct(int $id = 0) |
69 | 26 | { |
70 | | - $this->id = $id ?: -1; |
71 | | - $this->WP_User = \get_user_by('id', $this->id()); |
72 | | - |
73 | | - if (!$this->WP_User) { |
74 | | - return; |
75 | | - } |
76 | | - |
77 | | - //---- Data |
78 | | - $this->username = $this->WPUser()->data->user_login; |
79 | | - $this->nicename = $this->WPUser()->data->user_nicename; |
80 | | - $this->displayName = $this->WPUser()->data->display_name; |
81 | | - $this->email = $this->WPUser()->data->user_email; |
82 | | - $this->url = $this->WPUser()->data->user_url; |
83 | | - $this->registeredDate = $this->WPUser()->data->user_registered; |
84 | | - |
85 | | - //---- Meta |
86 | | - $this->first_name = $this->meta('first_name'); |
87 | | - $this->last_name = $this->meta('last_name'); |
88 | | - $this->nickname = $this->meta('nickname'); |
89 | | - $this->description = $this->meta('description'); |
90 | | - } |
| 27 | + $this->id = $id; |
91 | 28 |
|
92 | | - public function id() |
93 | | - { |
94 | | - return $this->id; |
| 29 | + $this->WP_User = \get_user_by('id', $this->id); |
| 30 | + |
| 31 | + $this->full_name = \trim("{$this->first_name} {$this->last_name}"); |
95 | 32 | } |
96 | 33 |
|
97 | | - public function WPUser() |
| 34 | + public function exists() |
98 | 35 | { |
99 | | - return $this->WP_User; |
| 36 | + return $this->WP_User->exists(); |
100 | 37 | } |
101 | 38 |
|
102 | 39 | /** |
103 | 40 | * @param string $key Meta field key |
104 | 41 | * @param bool $single Whether to return a single value or array |
| 42 | + * |
105 | 43 | * @return mixed |
106 | 44 | */ |
107 | | - public function meta(string $key, bool $single = true) |
| 45 | + public function getMeta(string $key, bool $single = true) |
108 | 46 | { |
109 | | - return \get_user_meta($this->id(), $key, $single); |
| 47 | + return \get_user_meta($this->id, $key, $single); |
110 | 48 | } |
111 | 49 |
|
112 | | - public function firstName() |
| 50 | + public function withAll() |
113 | 51 | { |
114 | | - return $this->first_name; |
| 52 | + $this |
| 53 | + ->withUsername() |
| 54 | + ->withNicename() |
| 55 | + ->withEmail() |
| 56 | + ->withUrl() |
| 57 | + ->withRegisteredDate() |
| 58 | + ->withFirstName() |
| 59 | + ->withLastName() |
| 60 | + ->withNickname() |
| 61 | + ->withDescription(); |
| 62 | + |
| 63 | + return $this; |
115 | 64 | } |
116 | 65 |
|
117 | | - public function lastName() |
| 66 | + public function withUsername() |
118 | 67 | { |
119 | | - return $this->last_name; |
120 | | - } |
| 68 | + $this->username = $this->WP_User->data->user_login; |
121 | 69 |
|
122 | | - public function fullName() |
123 | | - { |
124 | | - return \trim("{$this->firstName()} {$this->lastName()}"); |
| 70 | + return $this; |
125 | 71 | } |
126 | 72 |
|
127 | | - public function nickname() |
| 73 | + public function withNicename() |
128 | 74 | { |
129 | | - return $this->nickname; |
| 75 | + $this->displayName = $this->WP_User->data->display_name; |
| 76 | + |
| 77 | + return $this; |
130 | 78 | } |
131 | 79 |
|
132 | | - public function description() |
| 80 | + public function withEmail() |
133 | 81 | { |
134 | | - return \wpautop($this->description); |
| 82 | + $this->email = $this->WP_User->data->user_email; |
| 83 | + |
| 84 | + return $this; |
135 | 85 | } |
136 | 86 |
|
137 | | - public function username() |
| 87 | + public function withUrl() |
138 | 88 | { |
139 | | - return $this->username; |
| 89 | + $this->url = $this->WP_User->data->user_url; |
| 90 | + |
| 91 | + return $this; |
140 | 92 | } |
141 | 93 |
|
142 | | - /** |
143 | | - * * A URL friendly version of username() |
144 | | - */ |
145 | | - public function nicename() |
| 94 | + public function withRegisteredDate() |
146 | 95 | { |
147 | | - return $this->nicename; |
| 96 | + $this->registeredDate = new Carbon($this->WP_User->data->user_registered); |
| 97 | + |
| 98 | + return $this; |
148 | 99 | } |
149 | 100 |
|
150 | | - public function displayName() |
| 101 | + public function withFirstName() |
151 | 102 | { |
152 | | - return $this->displayName; |
| 103 | + $this->first_name = $this->getMeta('first_name'); |
| 104 | + |
| 105 | + return $this; |
153 | 106 | } |
154 | 107 |
|
155 | | - public function email() |
| 108 | + public function withLastName() |
156 | 109 | { |
157 | | - return $this->email; |
| 110 | + $this->last_name = $this->getMeta('last_name'); |
| 111 | + |
| 112 | + return $this; |
158 | 113 | } |
159 | 114 |
|
160 | | - public function url() |
| 115 | + public function withNickname() |
161 | 116 | { |
162 | | - return $this->url; |
| 117 | + $this->nickname = $this->getMeta('nickname'); |
| 118 | + |
| 119 | + return $this; |
163 | 120 | } |
164 | 121 |
|
165 | | - public function registredDate() |
| 122 | + public function withDescription() |
166 | 123 | { |
167 | | - return new Carbon($this->registeredDate); |
| 124 | + $this->description = \wpautop($this->getMeta('description')); |
| 125 | + |
| 126 | + return $this; |
168 | 127 | } |
169 | 128 | } |
0 commit comments