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 WP_User $user |
37
|
|
|
*/ |
38
|
|
|
public function __construct(WP_User $user = null) |
39
|
|
|
{ |
40
|
|
|
if (! $user) { |
41
|
|
|
$user = new WP_User; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->object = $user; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new instance from the user ID. |
49
|
|
|
* |
50
|
|
|
* @param string|int $id User ID |
51
|
|
|
* |
52
|
|
|
* @throws UserNotFoundException |
53
|
|
|
* |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public static function fromID($id) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (! $user = get_user_by('id', $id)) { |
59
|
|
|
throw new UserNotFoundException("No user found with ID $id"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new static($user); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a new instance from the username. |
67
|
|
|
* |
68
|
|
|
* @param string $username Username (login) |
69
|
|
|
* |
70
|
|
|
* @throws UserNotFoundException |
71
|
|
|
* |
72
|
|
|
* @return static |
73
|
|
|
*/ |
74
|
|
|
public static function fromUsername($username) |
75
|
|
|
{ |
76
|
|
|
if (! $user = get_user_by('login', $username)) { |
77
|
|
|
throw new UserNotFoundException("No user found with username: $username"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new static($user); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create a new instance from the user's email address. |
85
|
|
|
* |
86
|
|
|
* @param string $email User email address |
87
|
|
|
* |
88
|
|
|
* @throws UserNotFoundException |
89
|
|
|
* |
90
|
|
|
* @return static |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public static function fromEmail($email) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if (! $user = get_user_by('email', $email)) { |
95
|
|
|
throw new UserNotFoundException("No user found with email address: $email"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new static($user); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create a new instance from the user's slug. |
103
|
|
|
* |
104
|
|
|
* @param string $slug User slug (nicename) |
105
|
|
|
* |
106
|
|
|
* @throws UserNotFoundException |
107
|
|
|
* |
108
|
|
|
* @return static |
109
|
|
|
*/ |
110
|
|
|
public static function fromSlug($slug) |
111
|
|
|
{ |
112
|
|
|
if (! $user = get_user_by('slug', $slug)) { |
113
|
|
|
throw new UserNotFoundException("No user found with slug: $slug"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return new static($user); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the URL for the user's posts archive. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function postsUrl() |
125
|
|
|
{ |
126
|
|
|
return get_author_posts_url($this->id, $this->slug); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get a new query builder for the model. |
131
|
|
|
* |
132
|
|
|
* @return \Silk\Contracts\BuildsQueries |
133
|
|
|
*/ |
134
|
|
|
public function newQuery() |
135
|
|
|
{ |
136
|
|
|
return QueryBuilder::make(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Save the changes to the database. |
142
|
|
|
* |
143
|
|
|
* @throws WP_ErrorException |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function save() |
148
|
|
|
{ |
149
|
|
|
if (! $this->id) { |
150
|
|
|
$result = wp_insert_user($this->object); |
151
|
|
|
} else { |
152
|
|
|
$result = wp_update_user($this->object); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (is_wp_error($result)) { |
156
|
|
|
throw new WP_ErrorException($result); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->setId($result); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Delete the modeled record from the database. |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function delete() |
170
|
|
|
{ |
171
|
|
|
if (wp_delete_user($this->id)) { |
172
|
|
|
$this->object = new WP_User; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Reload the object from the database. |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
|
|
public function refresh() |
184
|
|
|
{ |
185
|
|
|
$this->object = new WP_User($this->id); |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.