1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The TimberComment class is used to view the output of comments. 99% of the time this will be in the context of the comments on a post. However you can also fetch a comment directly using its comment ID. |
5
|
|
|
* @example |
6
|
|
|
* ```php |
7
|
|
|
* $comment = new TimberComment($comment_id); |
8
|
|
|
* $context['comment_of_the_day'] = $comment; |
9
|
|
|
* Timber::render('index.twig', $context); |
10
|
|
|
* ``` |
11
|
|
|
* |
12
|
|
|
* ```twig |
13
|
|
|
* <p class="comment">{{comment_of_the_day.content}}</p> |
14
|
|
|
* <p class="comment-attribution">- {{comment.author.name}}</p> |
15
|
|
|
* ``` |
16
|
|
|
* |
17
|
|
|
* ```html |
18
|
|
|
* <p class="comment">But, O Sarah! If the dead can come back to this earth and flit unseen around those they loved, I shall always be near you; in the garish day and in the darkest night -- amidst your happiest scenes and gloomiest hours - always, always; and if there be a soft breeze upon your cheek, it shall be my breath; or the cool air fans your throbbing temple, it shall be my spirit passing by.</p> |
19
|
|
|
* <p class="comment-attribution">- Sullivan Ballou</p> |
20
|
|
|
* ``` |
21
|
|
|
*/ |
22
|
|
|
class TimberComment extends TimberCore implements TimberCoreInterface { |
23
|
|
|
|
24
|
|
|
public $PostClass = 'TimberPost'; |
25
|
|
|
public $object_type = 'comment'; |
26
|
|
|
|
27
|
|
|
public static $representation = 'comment'; |
28
|
|
|
|
29
|
|
|
public $ID; |
30
|
|
|
public $id; |
31
|
|
|
public $comment_author_email; |
32
|
|
|
public $comment_content; |
33
|
|
|
public $comment_date; |
34
|
|
|
public $comment_ID; |
35
|
|
|
public $user_id; |
36
|
|
|
public $comment_author; |
37
|
|
|
|
38
|
|
|
public $children = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $cid |
42
|
|
|
*/ |
43
|
|
|
function __construct($cid) { |
|
|
|
|
44
|
|
|
$this->init($cid); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function __toString(){ |
|
|
|
|
48
|
|
|
return $this->content(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @internal |
53
|
|
|
* @param integer $cid |
54
|
|
|
*/ |
55
|
|
|
function init($cid) { |
|
|
|
|
56
|
|
|
$comment_data = $cid; |
57
|
|
|
if (is_integer($cid)) { |
|
|
|
|
58
|
|
|
$comment_data = get_comment($cid); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
$this->import($comment_data); |
|
|
|
|
61
|
|
|
$this->ID = $this->comment_ID; |
62
|
|
|
$this->id = $this->comment_ID; |
63
|
|
|
$comment_meta_data = $this->get_meta_fields($this->ID); |
|
|
|
|
64
|
|
|
$this->import($comment_meta_data); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @api |
69
|
|
|
* @example |
70
|
|
|
* ```twig |
71
|
|
|
* <h3>Comments by...</h3> |
72
|
|
|
* <ol> |
73
|
|
|
* {% for comment in post.comments %} |
74
|
|
|
* <li>{{comment.author.name}}, who is a {{comment.author.role}}</li> |
75
|
|
|
* {% endfor %} |
76
|
|
|
* </ol> |
77
|
|
|
* ``` |
78
|
|
|
* ```html |
79
|
|
|
* <h3>Comments by...</h3> |
80
|
|
|
* <ol> |
81
|
|
|
* <li>Jared Novack, who is a contributor</li> |
82
|
|
|
* <li>Katie Ricci, who is a subscriber</li> |
83
|
|
|
* <li>Rebecca Pearl, who is a author</li> |
84
|
|
|
* </ol> |
85
|
|
|
* ``` |
86
|
|
|
* @return TimberUser |
87
|
|
|
*/ |
88
|
|
|
public function author() { |
89
|
|
|
if ($this->user_id) { |
|
|
|
|
90
|
|
|
return new TimberUser($this->user_id); |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
$author = new TimberUser(0); |
|
|
|
|
93
|
|
|
if (isset($this->comment_author) && $this->comment_author) { |
|
|
|
|
94
|
|
|
$author->name = $this->comment_author; |
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
$author->name = 'Anonymous'; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $author; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Fetches the Gravatar |
104
|
|
|
* @api |
105
|
|
|
* @example |
106
|
|
|
* ```twig |
107
|
|
|
* <img src="{{comment.avatar(36,template_uri~"/img/dude.jpg")}}" alt="Image of {{comment.author.name}}" /> |
108
|
|
|
* ``` |
109
|
|
|
* ```html |
110
|
|
|
* <img src="http://gravatar.com/i/sfsfsdfasdfsfa.jpg" alt="Image of Katherine Rich" /> |
111
|
|
|
* ``` |
112
|
|
|
* @param int $size |
113
|
|
|
* @param string $default |
114
|
|
|
* @return bool|mixed|string |
115
|
|
|
*/ |
116
|
|
|
public function avatar($size = 92, $default = '') { |
117
|
|
|
if (!get_option('show_avatars')) { |
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
if (!is_numeric($size)) { |
|
|
|
|
121
|
|
|
$size = '92'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$email = $this->avatar_email(); |
125
|
|
|
$email_hash = ''; |
126
|
|
|
if (!empty($email)) { |
|
|
|
|
127
|
|
|
$email_hash = md5(strtolower(trim($email))); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
$host = $this->avatar_host($email_hash); |
|
|
|
|
130
|
|
|
$default = $this->avatar_default($default, $email, $size, $host); |
|
|
|
|
131
|
|
|
if (!empty($email)) { |
|
|
|
|
132
|
|
|
$avatar = $this->avatar_out($default, $host, $email_hash, $size); |
|
|
|
|
133
|
|
|
} else { |
134
|
|
|
$avatar = $default; |
135
|
|
|
} |
136
|
|
|
return $avatar; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @api |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function content() { |
144
|
|
|
return apply_filters('get_comment_text ', $this->comment_content); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @api |
149
|
|
|
* @example |
150
|
|
|
* ```twig |
151
|
|
|
* {% if comment.approved %} |
152
|
|
|
* Your comment is good |
153
|
|
|
* {% else %} |
154
|
|
|
* Do you kiss your mother with that mouth? |
155
|
|
|
* {% endif %} |
156
|
|
|
* ``` |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
|
|
public function approved() { |
160
|
|
|
return $this->comment_approved; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @api |
165
|
|
|
* @example |
166
|
|
|
* ```twig |
167
|
|
|
* {% for comment in post.comments %} |
168
|
|
|
* <article class="comment"> |
169
|
|
|
* <p class="date">Posted on {{ comment.date }}:</p> |
170
|
|
|
* <p class="comment">{{ comment.content }}</p> |
171
|
|
|
* </article> |
172
|
|
|
* {% endfor %} |
173
|
|
|
* ``` |
174
|
|
|
* ```html |
175
|
|
|
* <article class="comment"> |
176
|
|
|
* <p class="date">Posted on September 28, 2015:</p> |
177
|
|
|
* <p class="comment">Happy Birthday!</p> |
178
|
|
|
* </article> |
179
|
|
|
* ``` |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function date( $date_format = '' ) { |
183
|
|
|
$df = $date_format ? $date_format : get_option('date_format'); |
|
|
|
|
184
|
|
|
$the_date = (string)mysql2date($df, $this->comment_date); |
|
|
|
|
185
|
|
|
return apply_filters('get_comment_date ', $the_date, $df); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @api |
190
|
|
|
* @example |
191
|
|
|
* ```twig |
192
|
|
|
* {% for comment in post.comments %} |
193
|
|
|
* <article class="comment"> |
194
|
|
|
* <p class="date">Posted on {{ comment.date }} at {{comment.time}}:</p> |
195
|
|
|
* <p class="comment">{{ comment.content }}</p> |
196
|
|
|
* </article> |
197
|
|
|
* {% endfor %} |
198
|
|
|
* ``` |
199
|
|
|
* ```html |
200
|
|
|
* <article class="comment"> |
201
|
|
|
* <p class="date">Posted on September 28, 2015 at 12:45 am:</p> |
202
|
|
|
* <p class="comment">Happy Birthday!</p> |
203
|
|
|
* </article> |
204
|
|
|
* ``` |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function time( $time_format = '' ) { |
208
|
|
|
$tf = $time_format ? $time_format : get_option('time_format'); |
|
|
|
|
209
|
|
|
$the_time = (string)mysql2date($tf, $this->comment_date); |
|
|
|
|
210
|
|
|
return apply_filters('get_comment_time', $the_time, $tf); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $field_name |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public function meta($field_name) { |
218
|
|
|
return $this->get_meta_field($field_name); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @api |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function is_child() { |
226
|
|
|
return $this->comment_parent > 0; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @internal |
231
|
|
|
* @param int $comment_id |
|
|
|
|
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
|
|
protected function get_meta_fields($comment_id = null) { |
235
|
|
|
if ($comment_id === null) { |
|
|
|
|
236
|
|
|
$comment_id = $this->ID; |
237
|
|
|
} |
238
|
|
|
//Could not find a WP function to fetch all comment meta data, so I made one. |
239
|
|
|
apply_filters('timber_comment_get_meta_pre', array(), $comment_id); |
|
|
|
|
240
|
|
|
$comment_metas = get_comment_meta($comment_id); |
|
|
|
|
241
|
|
|
foreach ($comment_metas as &$cm) { |
|
|
|
|
242
|
|
|
if (is_array($cm) && count($cm) == 1) { |
|
|
|
|
243
|
|
|
$cm = $cm[0]; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
$comment_metas = apply_filters('timber_comment_get_meta', $comment_metas, $comment_id); |
|
|
|
|
247
|
|
|
return $comment_metas; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @internal |
252
|
|
|
* @param string $field_name |
253
|
|
|
* @return mixed |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
protected function get_meta_field($field_name) { |
|
|
|
|
256
|
|
|
$value = apply_filters('timber_comment_get_meta_field_pre', null, $this->ID, $field_name, $this); |
|
|
|
|
257
|
|
|
if ($value === null) { |
|
|
|
|
258
|
|
|
$value = get_comment_meta($this->ID, $field_name, true); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
$value = apply_filters('timber_comment_get_meta_field', $value, $this->ID, $field_name, $this); |
|
|
|
|
261
|
|
|
return $value; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Enqueue the WP threaded comments javascript, |
266
|
|
|
* and fetch the reply link for various comments. |
267
|
|
|
* @api |
268
|
|
|
* @param int $comment_id |
|
|
|
|
269
|
|
|
* @param int $post_id |
|
|
|
|
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
public function reply_link( $reply_text = 'Reply' ) { |
273
|
|
|
if ( is_singular() && comments_open() && get_option('thread_comments') ) { |
|
|
|
|
274
|
|
|
wp_enqueue_script( 'comment-reply' ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// Get the comments depth option from the admin panel |
278
|
|
|
$max_depth = get_option('thread_comments_depth'); |
|
|
|
|
279
|
|
|
|
280
|
|
|
// Default args |
281
|
|
|
$args = array( |
282
|
|
|
'add_below' => 'comment', |
283
|
|
|
'respond_id' => 'respond', |
284
|
|
|
'reply_text' => $reply_text, |
285
|
|
|
'depth' => 1, |
286
|
|
|
'max_depth' => $max_depth, |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
return get_comment_reply_link( $args, $this->ID, $this->post_id ); |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/* AVATAR Stuff |
293
|
|
|
======================= */ |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @internal |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
protected function avatar_email() { |
300
|
|
|
$id = (int)$this->user_id; |
|
|
|
|
301
|
|
|
$user = get_userdata($id); |
|
|
|
|
302
|
|
|
if ($user) { |
|
|
|
|
303
|
|
|
$email = $user->user_email; |
304
|
|
|
} else { |
305
|
|
|
$email = $this->comment_author_email; |
306
|
|
|
} |
307
|
|
|
return $email; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @internal |
312
|
|
|
* @param string $email_hash |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
|
|
protected function avatar_host($email_hash) { |
316
|
|
|
if (is_ssl()) { |
|
|
|
|
317
|
|
|
$host = 'https://secure.gravatar.com'; |
318
|
|
|
} else { |
319
|
|
|
if (!empty($email_hash)) { |
|
|
|
|
320
|
|
|
$host = sprintf("http://%d.gravatar.com", (hexdec($email_hash[0]) % 2)); |
|
|
|
|
321
|
|
|
} else { |
322
|
|
|
$host = 'http://0.gravatar.com'; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
return $host; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @internal |
330
|
|
|
* @todo what if it's relative? |
331
|
|
|
* @param string $default |
332
|
|
|
* @param string $email |
333
|
|
|
* @param string $size |
334
|
|
|
* @param string $host |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
|
|
protected function avatar_default($default, $email, $size, $host) { |
338
|
|
|
if (substr($default, 0, 1) == '/') { |
|
|
|
|
339
|
|
|
$default = home_url() . $default; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (empty($default)) { |
|
|
|
|
343
|
|
|
$avatar_default = get_option('avatar_default'); |
|
|
|
|
344
|
|
|
if (empty($avatar_default)) { |
|
|
|
|
345
|
|
|
$default = 'mystery'; |
346
|
|
|
} else { |
347
|
|
|
$default = $avatar_default; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
if ('mystery' == $default) { |
|
|
|
|
351
|
|
|
$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size; |
352
|
|
|
// ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]') |
353
|
|
|
} else if ('blank' == $default) { |
|
|
|
|
354
|
|
|
$default = $email ? 'blank' : includes_url('images/blank.gif'); |
|
|
|
|
355
|
|
|
} else if (!empty($email) && 'gravatar_default' == $default) { |
|
|
|
|
356
|
|
|
$default = ''; |
357
|
|
|
} else if ('gravatar_default' == $default) { |
|
|
|
|
358
|
|
|
$default = $host . '/avatar/?s=' . $size; |
359
|
|
|
} else if (empty($email) && !strstr($default, 'http://')) { |
|
|
|
|
360
|
|
|
$default = $host . '/avatar/?d=' . $default . '&s=' . $size; |
361
|
|
|
} |
362
|
|
|
return $default; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @internal |
367
|
|
|
* @param string $default |
368
|
|
|
* @param string $host |
369
|
|
|
* @param string $email_hash |
370
|
|
|
* @param string $size |
371
|
|
|
* @return mixed |
372
|
|
|
*/ |
373
|
|
|
protected function avatar_out($default, $host, $email_hash, $size) { |
374
|
|
|
$out = $host . '/avatar/' . $email_hash . '?s=' . $size . '&d=' . urlencode($default); |
|
|
|
|
375
|
|
|
$rating = get_option('avatar_rating'); |
|
|
|
|
376
|
|
|
if (!empty($rating)) { |
|
|
|
|
377
|
|
|
$out .= '&r=' . $rating; |
378
|
|
|
} |
379
|
|
|
return str_replace('&', '&', esc_url($out)); |
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
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.