|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This is used in Timber to represent users retrived from WordPress. You can call `$my_user = new TimberUser(123);` directly, or access it through the `{{ post.author }}` method. |
|
5
|
|
|
* @example |
|
6
|
|
|
* ```php |
|
7
|
|
|
* $context['current_user'] = new TimberUser(); |
|
8
|
|
|
* $context['post'] = new TimberPost(); |
|
9
|
|
|
* Timber::render('single.twig', $context); |
|
10
|
|
|
* ``` |
|
11
|
|
|
* ```twig |
|
12
|
|
|
* <p class="current-user-info">Your name is {{ current_user.name }}</p> |
|
13
|
|
|
* <p class="article-info">This article is called "{{ post.title }}" and it's by {{ post.author.name }} |
|
14
|
|
|
* ``` |
|
15
|
|
|
* ```html |
|
16
|
|
|
* <p class="current-user-info">Your name is Jesse Eisenberg</p> |
|
17
|
|
|
* <p class="article-info">This article is called "Consider the Lobster" and it's by David Foster Wallace |
|
18
|
|
|
* ``` |
|
19
|
|
|
*/ |
|
20
|
|
|
class TimberUser extends TimberCore implements TimberCoreInterface { |
|
21
|
|
|
|
|
22
|
|
|
public $object_type = 'user'; |
|
23
|
|
|
public static $representation = 'user'; |
|
24
|
|
|
|
|
25
|
|
|
public $_link; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @api |
|
29
|
|
|
* @var string The description from WordPress |
|
30
|
|
|
*/ |
|
31
|
|
|
public $description; |
|
32
|
|
|
public $display_name; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @api |
|
36
|
|
|
* @var string The first name of the user |
|
37
|
|
|
*/ |
|
38
|
|
|
public $first_name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @api |
|
42
|
|
|
* @var string The last name of the user |
|
43
|
|
|
*/ |
|
44
|
|
|
public $last_name; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @api |
|
48
|
|
|
* @var int The ID from WordPress |
|
49
|
|
|
*/ |
|
50
|
|
|
public $id; |
|
51
|
|
|
public $user_nicename; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param int|bool $uid |
|
55
|
|
|
*/ |
|
56
|
|
|
function __construct($uid = false) { |
|
|
|
|
|
|
57
|
|
|
$this->init($uid); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @example |
|
62
|
|
|
* ```twig |
|
63
|
|
|
* This post is by {{ post.author }} |
|
64
|
|
|
* ``` |
|
65
|
|
|
* ```html |
|
66
|
|
|
* This post is by Jared Novack |
|
67
|
|
|
* ``` |
|
68
|
|
|
* |
|
69
|
|
|
* @return string a fallback for TimberUser::name() |
|
70
|
|
|
*/ |
|
71
|
|
|
function __toString() { |
|
|
|
|
|
|
72
|
|
|
$name = $this->name(); |
|
73
|
|
|
if ( strlen($name) ) { |
|
74
|
|
|
return $name; |
|
75
|
|
|
} |
|
76
|
|
|
if ( strlen($this->name) ) { |
|
|
|
|
|
|
77
|
|
|
return $this->name; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @internal |
|
84
|
|
|
* @param string $field_name |
|
85
|
|
|
* @return null |
|
86
|
|
|
*/ |
|
87
|
|
|
function get_meta($field_name) { |
|
|
|
|
|
|
88
|
|
|
return $this->get_meta_field($field_name); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @internal |
|
93
|
|
|
* @param string $field |
|
94
|
|
|
* @param mixed $value |
|
95
|
|
|
*/ |
|
96
|
|
|
function __set($field, $value) { |
|
|
|
|
|
|
97
|
|
|
if ( $field == 'name' ) { |
|
98
|
|
|
$this->display_name = $value; |
|
99
|
|
|
} |
|
100
|
|
|
$this->$field = $value; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @internal |
|
105
|
|
|
* @param int|bool $uid The user ID to use |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function init($uid = false) { |
|
108
|
|
|
if ( $uid === false ) { |
|
109
|
|
|
$uid = get_current_user_id(); |
|
110
|
|
|
} |
|
111
|
|
|
if ( is_object($uid) || is_array($uid) ) { |
|
112
|
|
|
$data = $uid; |
|
113
|
|
|
if ( is_array($uid) ) { |
|
114
|
|
|
$data = (object) $uid; |
|
115
|
|
|
} |
|
116
|
|
|
$uid = $data->ID; |
|
117
|
|
|
} |
|
118
|
|
|
if ( is_numeric($uid) ) { |
|
119
|
|
|
$data = get_userdata($uid); |
|
120
|
|
|
} else if ( is_string($uid) ) { |
|
121
|
|
|
$data = get_user_by('login', $uid); |
|
122
|
|
|
} |
|
123
|
|
|
if ( isset($data) && is_object($data) ) { |
|
124
|
|
|
if ( isset($data->data) ) { |
|
125
|
|
|
$this->import($data->data); |
|
126
|
|
|
} else { |
|
127
|
|
|
$this->import($data); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
$this->id = $this->ID; |
|
131
|
|
|
$this->name = $this->name(); |
|
|
|
|
|
|
132
|
|
|
$custom = $this->get_custom(); |
|
133
|
|
|
$this->import($custom); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $field_name |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
*/ |
|
140
|
|
View Code Duplication |
function get_meta_field($field_name) { |
|
|
|
|
|
|
141
|
|
|
$value = null; |
|
142
|
|
|
$value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this); |
|
143
|
|
|
if ( $value === null ) { |
|
144
|
|
|
$value = get_user_meta($this->ID, $field_name, true); |
|
145
|
|
|
} |
|
146
|
|
|
$value = apply_filters('timber_user_get_meta_field', $value, $this->ID, $field_name, $this); |
|
147
|
|
|
return $value; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return array|null |
|
152
|
|
|
*/ |
|
153
|
|
|
function get_custom() { |
|
|
|
|
|
|
154
|
|
|
if ( $this->ID ) { |
|
155
|
|
|
$um = array(); |
|
156
|
|
|
$um = apply_filters('timber_user_get_meta_pre', $um, $this->ID, $this); |
|
157
|
|
|
if ( empty($um) ) { |
|
158
|
|
|
$um = get_user_meta($this->ID); |
|
159
|
|
|
} |
|
160
|
|
|
$custom = array(); |
|
161
|
|
View Code Duplication |
foreach ($um as $key => $value) { |
|
|
|
|
|
|
162
|
|
|
if ( is_array($value) && count($value) == 1 ) { |
|
163
|
|
|
$value = $value[0]; |
|
164
|
|
|
} |
|
165
|
|
|
$custom[$key] = maybe_unserialize($value); |
|
166
|
|
|
} |
|
167
|
|
|
$custom = apply_filters('timber_user_get_meta', $custom, $this->ID, $this); |
|
168
|
|
|
return $custom; |
|
169
|
|
|
} |
|
170
|
|
|
return null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @api |
|
175
|
|
|
* @return string http://example.org/author/lincoln |
|
176
|
|
|
*/ |
|
177
|
|
|
public function link() { |
|
178
|
|
|
if ( !$this->_link ) { |
|
179
|
|
|
$this->_link = untrailingslashit(get_author_posts_url($this->ID)); |
|
180
|
|
|
} |
|
181
|
|
|
return $this->_link; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @api |
|
186
|
|
|
* @return string the human-friendly name of the user (ex: "Buster Bluth") |
|
187
|
|
|
*/ |
|
188
|
|
|
function name() { |
|
|
|
|
|
|
189
|
|
|
return $this->display_name; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $field_name |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
|
|
function meta($field_name) { |
|
|
|
|
|
|
197
|
|
|
return $this->get_meta_field($field_name); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @api |
|
202
|
|
|
* @return string ex: /author/lincoln |
|
203
|
|
|
*/ |
|
204
|
|
|
public function path() { |
|
205
|
|
|
return TimberURLHelper::get_rel_url($this->get_link()); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @api |
|
210
|
|
|
* @return string ex baberaham-lincoln |
|
211
|
|
|
*/ |
|
212
|
|
|
public function slug() { |
|
213
|
|
|
return $this->user_nicename; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @deprecated 0.21.9 |
|
218
|
|
|
* @return string The link to a user's profile page |
|
219
|
|
|
*/ |
|
220
|
|
|
function get_link() { |
|
|
|
|
|
|
221
|
|
|
return $this->link(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @deprecated 0.21.8 |
|
226
|
|
|
* @return string ex: /author/lincoln |
|
227
|
|
|
*/ |
|
228
|
|
|
function get_path() { |
|
|
|
|
|
|
229
|
|
|
return $this->path(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @deprecated 0.21.8 |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
|
|
function get_permalink() { |
|
|
|
|
|
|
237
|
|
|
return $this->get_link(); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @deprecated 0.21.8 |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
|
|
function permalink() { |
|
|
|
|
|
|
245
|
|
|
return $this->get_permalink(); |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.