1
|
|
|
<?php |
2
|
|
|
namespace Classy; |
3
|
|
|
|
4
|
|
|
class Comment extends Basis { |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Comment ID |
8
|
|
|
* @var int |
9
|
|
|
*/ |
10
|
|
|
public $comment_ID; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Comment post ID |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
public $comment_post_ID; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Comment Author name |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $comment_author; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Comment author email |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $comment_author_email; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Comment author link |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $comment_author_url; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Comment |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $comment_content; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Comment approved |
44
|
|
|
* @var boolean |
45
|
|
|
*/ |
46
|
|
|
public $comment_approved; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Comment date |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $comment_date; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* User Id |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
public $user_id; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Comment nested Level |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
public $level; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Comment Parent Id |
69
|
|
|
* @var int |
70
|
|
|
*/ |
71
|
|
|
public $comment_parent; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Child comments |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $children = array(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks if provided arg is instance of WP_Comment and inits it |
81
|
|
|
* |
82
|
|
|
* @param \WP_Comment $item |
83
|
|
|
*/ |
84
|
|
|
public function __construct( $item ) { |
85
|
|
|
|
86
|
|
|
if ( is_a( $item, '\WP_Comment' ) ) { |
87
|
|
|
|
88
|
|
|
$this->import( $item ); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns User object of comment author |
96
|
|
|
* |
97
|
|
|
* @return object User |
98
|
|
|
*/ |
99
|
|
|
public function author() { |
100
|
|
|
|
101
|
|
|
if ( $this->user_id ) { |
102
|
|
|
|
103
|
|
|
return new Models\User( $this->user_id ); |
104
|
|
|
|
105
|
|
|
} else { |
106
|
|
|
|
107
|
|
|
$author = new Models\User( 0 ); |
108
|
|
|
|
109
|
|
|
if ( isset( $this->comment_author ) && $this->comment_author ) { |
110
|
|
|
|
111
|
|
|
$author->name = $this->comment_author; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $author; |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns comment content |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function content() { |
126
|
|
|
return apply_filters( 'get_comment_text ', $this->comment_content ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return true if comment is approved |
131
|
|
|
* |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
public function approved() { |
135
|
|
|
return $this->comment_approved; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns comment date |
140
|
|
|
* |
141
|
|
|
* @param string $date_format |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function date( $date_format = '' ) { |
145
|
|
|
$df = $date_format ? $date_format : get_option( 'date_format' ); |
146
|
|
|
$the_date = (string) mysql2date( $df, $this->comment_date ); |
147
|
|
|
|
148
|
|
|
return apply_filters( 'get_comment_date ', $the_date, $df ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns true if comment has parent |
153
|
|
|
* |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
|
|
public function has_parent() { |
157
|
|
|
return $this->comment_parent > 0; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Shows gravatar |
162
|
|
|
* |
163
|
|
|
* @param integer $size avatar image size |
164
|
|
|
* @param string $default |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function avatar( $size = 92, $default = '' ) { |
168
|
|
|
|
169
|
|
|
if ( ! get_option( 'show_avatars' ) ) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ( ! is_numeric( $size ) ) { $size = '92'; } |
|
|
|
|
174
|
|
|
|
175
|
|
|
$email = $this->get_avatar_email(); |
176
|
|
|
$email_hash = ''; |
177
|
|
|
|
178
|
|
|
if ( ! empty( $email ) ) { |
179
|
|
|
$email_hash = md5( strtolower( trim( $email ) ) ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$host = $this->get_avatar_host( $email_hash ); |
183
|
|
|
$default = $this->get_default_avatar( $default, $email, $size, $host ); |
|
|
|
|
184
|
|
|
|
185
|
|
|
if ( ! empty( $email ) ) { |
186
|
|
|
$avatar = $this->get_avatar_url( $default, $host, $email_hash, $size ); |
187
|
|
|
} else { |
188
|
|
|
$avatar = $default; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $avatar; |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns email address that will be used for generating avatar |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
protected function get_avatar_email() { |
201
|
|
|
|
202
|
|
|
$id = (int) $this->user_id; |
203
|
|
|
$user = get_userdata( $id ); |
204
|
|
|
|
205
|
|
|
if ( $user ) { |
206
|
|
|
$email = $user->user_email; |
207
|
|
|
} else { |
208
|
|
|
$email = $this->comment_author_email; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $email; |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns default avatar url |
217
|
|
|
* |
218
|
|
|
* @param string $default |
219
|
|
|
* @param string $email |
220
|
|
|
* @param int $size |
221
|
|
|
* @param string $host |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
protected function get_default_avatar( $default, $email, $size, $host ) { |
225
|
|
|
|
226
|
|
|
if ( '/' === substr( $default, 0, 1 ) ) { |
227
|
|
|
$default = home_url() . $default; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ( empty( $default ) ) { |
231
|
|
|
$avatar_default = get_option( 'avatar_default' ); |
232
|
|
|
if ( empty( $avatar_default ) ) { |
233
|
|
|
$default = 'mystery'; |
|
|
|
|
234
|
|
|
} else { |
235
|
|
|
$default = $avatar_default; |
|
|
|
|
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if ( 'mystery' === $default ) { |
240
|
|
|
$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size; |
|
|
|
|
241
|
|
|
// ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]') |
242
|
|
|
} else if ( 'blank' === $default ) { |
243
|
|
|
$default = $email ? 'blank' : includes_url( 'images/blank.gif' ); |
|
|
|
|
244
|
|
|
} else if ( ! empty( $email ) && 'gravatar_default' === $default ) { |
245
|
|
|
$default = ''; |
|
|
|
|
246
|
|
|
} else if ( 'gravatar_default' === $default ) { |
247
|
|
|
$default = $host . '/avatar/?s=' . $size; |
|
|
|
|
248
|
|
|
} else if ( empty( $email ) && ! strstr( $default, 'http://' ) ) { |
249
|
|
|
$default = $host . '/avatar/?d=' . $default . '&s=' . $size; |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $default; |
253
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Returns gravatar host |
258
|
|
|
* |
259
|
|
|
* @param string $email_hash |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
protected function get_avatar_host( $email_hash ) { |
263
|
|
|
|
264
|
|
|
if ( is_ssl() ) { |
265
|
|
|
$host = 'https://secure.gravatar.com'; |
266
|
|
|
} else { |
267
|
|
|
if ( ! empty( $email_hash ) ) { |
268
|
|
|
$host = sprintf( 'http://%d.gravatar.com', (hexdec( $email_hash[0] ) % 2) ); |
269
|
|
|
} else { |
270
|
|
|
$host = 'http://0.gravatar.com'; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $host; |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns avatar url |
280
|
|
|
* |
281
|
|
|
* @param string $default |
282
|
|
|
* @param string $host |
283
|
|
|
* @param string $email_hash |
284
|
|
|
* @param int $size |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
protected function get_avatar_url( $default, $host, $email_hash, $size ) { |
288
|
|
|
|
289
|
|
|
$_return = $host . '/avatar/' . $email_hash . '?s=' . $size . '&d=' . urlencode( $default ); |
290
|
|
|
$rating = get_option( 'avatar_rating' ); |
291
|
|
|
|
292
|
|
|
if ( ! empty( $rating ) ) { |
293
|
|
|
$_return .= '&r=' . $rating; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return str_replace( '&', '&', esc_url( $_return ) ); |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Adds child to current Comment |
302
|
|
|
* |
303
|
|
|
* @param Comment $comment |
304
|
|
|
*/ |
305
|
|
|
public function add_child( $comment ) { |
306
|
|
|
|
307
|
|
|
$this->children[] = $comment; |
308
|
|
|
$item->level = $this->level + 1; |
|
|
|
|
309
|
|
|
|
310
|
|
|
if ( $item->children ) { |
311
|
|
|
$this->update_child_levels(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Updates children nesting level param |
319
|
|
|
* |
320
|
|
|
* @return boolean |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
protected function update_child_levels() { |
|
|
|
|
323
|
|
|
|
324
|
|
|
if ( is_array( $this->children ) ) { |
325
|
|
|
|
326
|
|
|
foreach ( $this->children as $child ) { |
327
|
|
|
$child->level = $this->level + 1; |
328
|
|
|
$child->update_child_levels(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return true; |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return false; |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|