1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\User; |
4
|
|
|
|
5
|
|
|
use WP_User; |
6
|
|
|
use Silk\Type\Model as BaseModel; |
7
|
|
|
use Silk\Exception\WP_ErrorException; |
8
|
|
|
use Silk\User\Exception\UserNotFoundException; |
9
|
|
|
|
10
|
|
|
class Model extends BaseModel |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The object type in WordPress |
14
|
|
|
*/ |
15
|
|
|
const OBJECT_TYPE = 'user'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The primary ID property on the object |
19
|
|
|
*/ |
20
|
|
|
const ID_PROPERTY = 'ID'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Type object property aliases |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $objectAliases = [ |
27
|
|
|
'email' => 'user_email', |
28
|
|
|
'slug' => 'user_nicename', |
29
|
|
|
'username' => 'user_login', |
30
|
|
|
'password' => 'user_pass', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* User Constructor. |
35
|
|
|
* |
36
|
|
|
* @param array|WP_User $user User object or array of attributes |
37
|
|
|
*/ |
38
|
|
|
public function __construct($user = null) |
39
|
|
|
{ |
40
|
|
|
$attributes = is_array($user) ? $user : []; |
41
|
|
|
|
42
|
|
|
if (! $user instanceof WP_User) { |
|
|
|
|
43
|
|
|
$user = new WP_User(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->object = $user; |
47
|
|
|
|
48
|
|
|
$this->fill($attributes); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new instance from the user ID. |
53
|
|
|
* |
54
|
|
|
* @param string|int $id User ID |
55
|
|
|
* |
56
|
|
|
* @throws UserNotFoundException |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public static function fromID($id) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (! $user = get_user_by('id', $id)) { |
63
|
|
|
throw new UserNotFoundException("No user found with ID $id"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new static($user); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new instance from the username. |
71
|
|
|
* |
72
|
|
|
* @param string $username Username (login) |
73
|
|
|
* |
74
|
|
|
* @throws UserNotFoundException |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public static function fromUsername($username) |
79
|
|
|
{ |
80
|
|
|
if (! $user = get_user_by('login', $username)) { |
81
|
|
|
throw new UserNotFoundException("No user found with username: $username"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new static($user); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a new instance from the user's email address. |
89
|
|
|
* |
90
|
|
|
* @param string $email User email address |
91
|
|
|
* |
92
|
|
|
* @throws UserNotFoundException |
93
|
|
|
* |
94
|
|
|
* @return static |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public static function fromEmail($email) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (! $user = get_user_by('email', $email)) { |
99
|
|
|
throw new UserNotFoundException("No user found with email address: $email"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new static($user); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create a new instance from the user's slug. |
107
|
|
|
* |
108
|
|
|
* @param string $slug User slug (nicename) |
109
|
|
|
* |
110
|
|
|
* @throws UserNotFoundException |
111
|
|
|
* |
112
|
|
|
* @return static |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public static function fromSlug($slug) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (! $user = get_user_by('slug', $slug)) { |
117
|
|
|
throw new UserNotFoundException("No user found with slug: $slug"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return new static($user); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create a new instance using the currently authenticated user. |
125
|
|
|
* |
126
|
|
|
* @return static |
127
|
|
|
*/ |
128
|
|
|
public static function auth() |
129
|
|
|
{ |
130
|
|
|
return new static(wp_get_current_user()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the URL for the user's posts archive. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function postsUrl() |
139
|
|
|
{ |
140
|
|
|
return get_author_posts_url($this->id, $this->slug); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get a new query builder for the model. |
145
|
|
|
* |
146
|
|
|
* @return \Silk\Contracts\BuildsQueries |
147
|
|
|
*/ |
148
|
|
|
public function newQuery() |
149
|
|
|
{ |
150
|
|
|
return QueryBuilder::make()->setModel($this); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Save the changes to the database. |
155
|
|
|
* |
156
|
|
|
* @throws WP_ErrorException |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function save() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
if (! $this->id) { |
163
|
|
|
$result = wp_insert_user($this->object); |
164
|
|
|
} else { |
165
|
|
|
$result = wp_update_user($this->object); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (is_wp_error($result)) { |
169
|
|
|
throw new WP_ErrorException($result); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->setId($result)->refresh(); |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Delete the modeled record from the database. |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function delete() |
183
|
|
|
{ |
184
|
|
|
if (wp_delete_user($this->id)) { |
185
|
|
|
$this->object = new WP_User; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Reload the object from the database. |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function refresh() |
197
|
|
|
{ |
198
|
|
|
$this->object = new WP_User($this->id); |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|