]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/comment-template.php
4ef67dd74ded18a2ff242d54a7d24d1ad520e7c7
[autoinstalls/wordpress.git] / wp-includes / comment-template.php
1 <?php
2 /**
3  * Comment template functions
4  *
5  * These functions are meant to live inside of the WordPress loop.
6  *
7  * @package WordPress
8  * @subpackage Template
9  */
10
11 /**
12  * Retrieve the author of the current comment.
13  *
14  * If the comment has an empty comment_author field, then 'Anonymous' person is
15  * assumed.
16  *
17  * @since 1.5.0
18  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
19  *
20  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to retrieve the author.
21  *                                                                       Default current comment.
22  * @return string The comment author
23  */
24 function get_comment_author( $comment_ID = 0 ) {
25         $comment = get_comment( $comment_ID );
26
27         if ( empty( $comment->comment_author ) ) {
28                 if ( $comment->user_id && $user = get_userdata( $comment->user_id ) )
29                         $author = $user->display_name;
30                 else
31                         $author = __('Anonymous');
32         } else {
33                 $author = $comment->comment_author;
34         }
35
36         /**
37          * Filter the returned comment author name.
38          *
39          * @since 1.5.0
40          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
41          *
42          * @param string     $author     The comment author's username.
43          * @param int        $comment_ID The comment ID.
44          * @param WP_Comment $comment    The comment object.
45          */
46         return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
47 }
48
49 /**
50  * Displays the author of the current comment.
51  *
52  * @since 0.71
53  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
54  *
55  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author.
56  *                                                                       Default current comment.
57  */
58 function comment_author( $comment_ID = 0 ) {
59         $comment = get_comment( $comment_ID );
60         $author  = get_comment_author( $comment );
61
62         /**
63          * Filter the comment author's name for display.
64          *
65          * @since 1.2.0
66          * @since 4.1.0 The `$comment_ID` parameter was added.
67          *
68          * @param string $author     The comment author's username.
69          * @param int    $comment_ID The comment ID.
70          */
71         echo apply_filters( 'comment_author', $author, $comment->comment_ID );
72 }
73
74 /**
75  * Retrieve the email of the author of the current comment.
76  *
77  * @since 1.5.0
78  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
79  *
80  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's email.
81  *                                                                       Default current comment.
82  * @return string The current comment author's email
83  */
84 function get_comment_author_email( $comment_ID = 0 ) {
85         $comment = get_comment( $comment_ID );
86
87         /**
88          * Filter the comment author's returned email address.
89          *
90          * @since 1.5.0
91          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
92          *
93          * @param string     $comment_author_email The comment author's email address.
94          * @param int        $comment_ID           The comment ID.
95          * @param WP_Comment $comment              The comment object.
96          */
97         return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment->comment_ID, $comment );
98 }
99
100 /**
101  * Display the email of the author of the current global $comment.
102  *
103  * Care should be taken to protect the email address and assure that email
104  * harvesters do not capture your commentors' email address. Most assume that
105  * their email address will not appear in raw form on the blog. Doing so will
106  * enable anyone, including those that people don't want to get the email
107  * address and use it for their own means good and bad.
108  *
109  * @since 0.71
110  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
111  *
112  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's email.
113  *                                                                       Default current comment.
114  */
115 function comment_author_email( $comment_ID = 0 ) {
116         $comment      = get_comment( $comment_ID );
117         $author_email = get_comment_author_email( $comment );
118
119         /**
120          * Filter the comment author's email for display.
121          *
122          * @since 1.2.0
123          * @since 4.1.0 The `$comment_ID` parameter was added.
124          *
125          * @param string $author_email The comment author's email address.
126          * @param int    $comment_ID   The comment ID.
127          */
128         echo apply_filters( 'author_email', $author_email, $comment->comment_ID );
129 }
130
131 /**
132  * Display the html email link to the author of the current comment.
133  *
134  * Care should be taken to protect the email address and assure that email
135  * harvesters do not capture your commentors' email address. Most assume that
136  * their email address will not appear in raw form on the blog. Doing so will
137  * enable anyone, including those that people don't want to get the email
138  * address and use it for their own means good and bad.
139  *
140  * @since 0.71
141  *
142  * @param string $linktext Optional. Text to display instead of the comment author's email address.
143  *                         Default empty.
144  * @param string $before   Optional. Text or HTML to display before the email link. Default empty.
145  * @param string $after    Optional. Text or HTML to display after the email link. Default empty.
146  */
147 function comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
148         if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
149                 echo $link;
150 }
151
152 /**
153  * Return the html email link to the author of the current comment.
154  *
155  * Care should be taken to protect the email address and assure that email
156  * harvesters do not capture your commentors' email address. Most assume that
157  * their email address will not appear in raw form on the blog. Doing so will
158  * enable anyone, including those that people don't want to get the email
159  * address and use it for their own means good and bad.
160  *
161  * @since 2.7.0
162  *
163  * @param string $linktext Optional. Text to display instead of the comment author's email address.
164  *                         Default empty.
165  * @param string $before   Optional. Text or HTML to display before the email link. Default empty.
166  * @param string $after    Optional. Text or HTML to display after the email link. Default empty.
167  * @return string
168  */
169 function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
170         $comment = get_comment();
171         /**
172          * Filter the comment author's email for display.
173          *
174          * Care should be taken to protect the email address and assure that email
175          * harvesters do not capture your commenter's email address.
176          *
177          * @since 1.2.0
178          * @since 4.1.0 The `$comment` parameter was added.
179          *
180          * @param string     $comment_author_email The comment author's email address.
181          * @param WP_Comment $comment              The comment object.
182          */
183         $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
184         if ((!empty($email)) && ($email != '@')) {
185         $display = ($linktext != '') ? $linktext : $email;
186                 $return  = $before;
187                 $return .= sprintf( '<a href="%1$s">%2$s</a>', esc_url( 'mailto:' . $email ), esc_html( $display ) );
188                 $return .= $after;
189                 return $return;
190         } else {
191                 return '';
192         }
193 }
194
195 /**
196  * Retrieve the HTML link to the URL of the author of the current comment.
197  *
198  * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
199  * which falls back to the global comment variable if the $comment_ID argument is empty.
200  *
201  * @since 1.5.0
202  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
203  *
204  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link.
205  *                                                                       Default current comment.
206  * @return string The comment author name or HTML link for author's URL.
207  */
208 function get_comment_author_link( $comment_ID = 0 ) {
209         $comment = get_comment( $comment_ID );
210         $url     = get_comment_author_url( $comment );
211         $author  = get_comment_author( $comment );
212
213         if ( empty( $url ) || 'http://' == $url )
214                 $return = $author;
215         else
216                 $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
217
218         /**
219          * Filter the comment author's link for display.
220          *
221          * @since 1.5.0
222          * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
223          *
224          * @param string $return     The HTML-formatted comment author link.
225          *                           Empty for an invalid URL.
226          * @param string $author     The comment author's username.
227          * @param int    $comment_ID The comment ID.
228          */
229         return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
230 }
231
232 /**
233  * Display the html link to the url of the author of the current comment.
234  *
235  * @since 0.71
236  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
237  *
238  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's link.
239  *                                                                       Default current comment.
240  */
241 function comment_author_link( $comment_ID = 0 ) {
242         echo get_comment_author_link( $comment_ID );
243 }
244
245 /**
246  * Retrieve the IP address of the author of the current comment.
247  *
248  * @since 1.5.0
249  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
250  *
251  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's IP address.
252  *                                                                       Default current comment.
253  * @return string Comment author's IP address.
254  */
255 function get_comment_author_IP( $comment_ID = 0 ) {
256         $comment = get_comment( $comment_ID );
257
258         /**
259          * Filter the comment author's returned IP address.
260          *
261          * @since 1.5.0
262          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
263          *
264          * @param string     $comment_author_IP The comment author's IP address.
265          * @param int        $comment_ID        The comment ID.
266          * @param WP_Comment $comment           The comment object.
267          */
268         return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment->comment_ID, $comment );
269 }
270
271 /**
272  * Display the IP address of the author of the current comment.
273  *
274  * @since 0.71
275  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
276  *
277  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's IP address.
278  *                                                                       Default current comment.
279  */
280 function comment_author_IP( $comment_ID = 0 ) {
281         echo esc_html( get_comment_author_IP( $comment_ID ) );
282 }
283
284 /**
285  * Retrieve the url of the author of the current comment.
286  *
287  * @since 1.5.0
288  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
289  *
290  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's URL.
291  *                                                                       Default current comment.
292  * @return string Comment author URL.
293  */
294 function get_comment_author_url( $comment_ID = 0 ) {
295         $comment = get_comment( $comment_ID );
296         $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
297         $url = esc_url( $url, array('http', 'https') );
298
299         /**
300          * Filter the comment author's URL.
301          *
302          * @since 1.5.0
303          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
304          *
305          * @param string     $url        The comment author's URL.
306          * @param int        $comment_ID The comment ID.
307          * @param WP_Comment $comment    The comment object.
308          */
309         return apply_filters( 'get_comment_author_url', $url, $comment->comment_ID, $comment );
310 }
311
312 /**
313  * Display the url of the author of the current comment.
314  *
315  * @since 0.71
316  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
317  *
318  * @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to print the author's URL.
319  *                                                                       Default current comment.
320  */
321 function comment_author_url( $comment_ID = 0 ) {
322         $comment    = get_comment( $comment_ID );
323         $author_url = get_comment_author_url( $comment );
324
325         /**
326          * Filter the comment author's URL for display.
327          *
328          * @since 1.2.0
329          * @since 4.1.0 The `$comment_ID` parameter was added.
330          *
331          * @param string $author_url The comment author's URL.
332          * @param int    $comment_ID The comment ID.
333          */
334         echo apply_filters( 'comment_url', $author_url, $comment->comment_ID );
335 }
336
337 /**
338  * Retrieves the HTML link of the url of the author of the current comment.
339  *
340  * $linktext parameter is only used if the URL does not exist for the comment
341  * author. If the URL does exist then the URL will be used and the $linktext
342  * will be ignored.
343  *
344  * Encapsulate the HTML link between the $before and $after. So it will appear
345  * in the order of $before, link, and finally $after.
346  *
347  * @since 1.5.0
348  *
349  * @param string $linktext Optional. The text to display instead of the comment
350  *                         author's email address. Default empty.
351  * @param string $before   Optional. The text or HTML to display before the email link.
352  *                         Default empty.
353  * @param string $after    Optional. The text or HTML to display after the email link.
354  *                         Default empty.
355  * @return string The HTML link between the $before and $after parameters.
356  */
357 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
358         $url = get_comment_author_url();
359         $display = ($linktext != '') ? $linktext : $url;
360         $display = str_replace( 'http://www.', '', $display );
361         $display = str_replace( 'http://', '', $display );
362
363         if ( '/' == substr($display, -1) ) {
364                 $display = substr($display, 0, -1);
365         }
366
367         $return = "$before<a href='$url' rel='external'>$display</a>$after";
368
369         /**
370          * Filter the comment author's returned URL link.
371          *
372          * @since 1.5.0
373          *
374          * @param string $return The HTML-formatted comment author URL link.
375          */
376         return apply_filters( 'get_comment_author_url_link', $return );
377 }
378
379 /**
380  * Displays the HTML link of the url of the author of the current comment.
381  *
382  * @since 0.71
383  *
384  * @param string $linktext Optional. Text to display instead of the comment author's
385  *                         email address. Default empty.
386  * @param string $before   Optional. Text or HTML to display before the email link.
387  *                         Default empty.
388  * @param string $after    Optional. Text or HTML to display after the email link.
389  *                         Default empty.
390  */
391 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
392         echo get_comment_author_url_link( $linktext, $before, $after );
393 }
394
395 /**
396  * Generates semantic classes for each comment element.
397  *
398  * @since 2.7.0
399  * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
400  *
401  * @param string|array   $class    Optional. One or more classes to add to the class list.
402  *                                 Default empty.
403  * @param int|WP_Comment $comment  Comment ID or WP_Comment object. Default current comment.
404  * @param int|WP_Post    $post_id  Post ID or WP_Post object. Default current post.
405  * @param bool           $echo     Optional. Whether to cho or return the output.
406  *                                 Default true.
407  * @return string If `$echo` is false, the class will be returned. Void otherwise.
408  */
409 function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
410         // Separates classes with a single space, collates classes for comment DIV
411         $class = 'class="' . join( ' ', get_comment_class( $class, $comment, $post_id ) ) . '"';
412         if ( $echo)
413                 echo $class;
414         else
415                 return $class;
416 }
417
418 /**
419  * Returns the classes for the comment div as an array.
420  *
421  * @since 2.7.0
422  * @since 4.4.0 Added the ability for `$comment_id` to also accept a WP_Comment object.
423  *
424  * @global int $comment_alt
425  * @global int $comment_depth
426  * @global int $comment_thread_alt
427  *
428  * @param string|array   $class      Optional. One or more classes to add to the class list. Default empty.
429  * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. Default current comment.
430  * @param int|WP_Post    $post_id    Post ID or WP_Post object. Default current post.
431  * @return array An array of classes.
432  */
433 function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
434         global $comment_alt, $comment_depth, $comment_thread_alt;
435
436         $classes = array();
437
438         $comment = get_comment( $comment_id );
439         if ( ! $comment ) {
440                 return $classes;
441         }
442
443         // Get the comment type (comment, trackback),
444         $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
445
446         // Add classes for comment authors that are registered users.
447         if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) {
448                 $classes[] = 'byuser';
449                 $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
450                 // For comment authors who are the author of the post
451                 if ( $post = get_post($post_id) ) {
452                         if ( $comment->user_id === $post->post_author ) {
453                                 $classes[] = 'bypostauthor';
454                         }
455                 }
456         }
457
458         if ( empty($comment_alt) )
459                 $comment_alt = 0;
460         if ( empty($comment_depth) )
461                 $comment_depth = 1;
462         if ( empty($comment_thread_alt) )
463                 $comment_thread_alt = 0;
464
465         if ( $comment_alt % 2 ) {
466                 $classes[] = 'odd';
467                 $classes[] = 'alt';
468         } else {
469                 $classes[] = 'even';
470         }
471
472         $comment_alt++;
473
474         // Alt for top-level comments
475         if ( 1 == $comment_depth ) {
476                 if ( $comment_thread_alt % 2 ) {
477                         $classes[] = 'thread-odd';
478                         $classes[] = 'thread-alt';
479                 } else {
480                         $classes[] = 'thread-even';
481                 }
482                 $comment_thread_alt++;
483         }
484
485         $classes[] = "depth-$comment_depth";
486
487         if ( !empty($class) ) {
488                 if ( !is_array( $class ) )
489                         $class = preg_split('#\s+#', $class);
490                 $classes = array_merge($classes, $class);
491         }
492
493         $classes = array_map('esc_attr', $classes);
494
495         /**
496          * Filter the returned CSS classes for the current comment.
497          *
498          * @since 2.7.0
499          *
500          * @param array       $classes    An array of comment classes.
501          * @param string      $class      A comma-separated list of additional classes added to the list.
502          * @param int         $comment_id The comment id.
503          * @param WP_Comment  $comment    The comment object.
504          * @param int|WP_Post $post_id    The post ID or WP_Post object.
505          */
506         return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
507 }
508
509 /**
510  * Retrieve the comment date of the current comment.
511  *
512  * @since 1.5.0
513  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
514  *
515  * @param string          $d          Optional. The format of the date. Default user's setting.
516  * @param int|WP_Comment  $comment_ID WP_Comment or ID of the comment for which to get the date.
517  *                                    Default current comment.
518  * @return string The comment's date.
519  */
520 function get_comment_date( $d = '', $comment_ID = 0 ) {
521         $comment = get_comment( $comment_ID );
522         if ( '' == $d )
523                 $date = mysql2date(get_option('date_format'), $comment->comment_date);
524         else
525                 $date = mysql2date($d, $comment->comment_date);
526         /**
527          * Filter the returned comment date.
528          *
529          * @since 1.5.0
530          *
531          * @param string|int $date    Formatted date string or Unix timestamp.
532          * @param string     $d       The format of the date.
533          * @param WP_Comment $comment The comment object.
534          */
535         return apply_filters( 'get_comment_date', $date, $d, $comment );
536 }
537
538 /**
539  * Display the comment date of the current comment.
540  *
541  * @since 0.71
542  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
543  *
544  * @param string         $d          Optional. The format of the date. Default user's settings.
545  * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to print the date.
546  *                                   Default current comment.
547  */
548 function comment_date( $d = '', $comment_ID = 0 ) {
549         echo get_comment_date( $d, $comment_ID );
550 }
551
552 /**
553  * Retrieve the excerpt of the current comment.
554  *
555  * Will cut each word and only output the first 20 words with '&hellip;' at the end.
556  * If the word count is less than 20, then no truncating is done and no '&hellip;'
557  * will appear.
558  *
559  * @since 1.5.0
560  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
561  *
562  * @param int|WP_Comment $comment_ID  WP_Comment or ID of the comment for which to get the excerpt.
563  *                                    Default current comment.
564  * @return string The maybe truncated comment with 20 words or less.
565  */
566 function get_comment_excerpt( $comment_ID = 0 ) {
567         $comment = get_comment( $comment_ID );
568         $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
569         $words = explode( ' ', $comment_text );
570
571         /**
572          * Filter the amount of words used in the comment excerpt.
573          *
574          * @since 4.4.0
575          *
576          * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
577          */
578         $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
579
580         $use_ellipsis = count( $words ) > $comment_excerpt_length;
581         if ( $use_ellipsis ) {
582                 $words = array_slice( $words, 0, $comment_excerpt_length );
583         }
584
585         $excerpt = trim( join( ' ', $words ) );
586         if ( $use_ellipsis ) {
587                 $excerpt .= '&hellip;';
588         }
589         /**
590          * Filter the retrieved comment excerpt.
591          *
592          * @since 1.5.0
593          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
594          *
595          * @param string     $excerpt    The comment excerpt text.
596          * @param int        $comment_ID The comment ID.
597          * @param WP_Comment $comment    The comment object.
598          */
599         return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );
600 }
601
602 /**
603  * Display the excerpt of the current comment.
604  *
605  * @since 1.2.0
606  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
607  *
608  * @param int|WP_Comment $comment_ID  WP_Comment or ID of the comment for which to print the excerpt.
609  *                                    Default current comment.
610  */
611 function comment_excerpt( $comment_ID = 0 ) {
612         $comment         = get_comment( $comment_ID );
613         $comment_excerpt = get_comment_excerpt( $comment );
614
615         /**
616          * Filter the comment excerpt for display.
617          *
618          * @since 1.2.0
619          * @since 4.1.0 The `$comment_ID` parameter was added.
620          *
621          * @param string $comment_excerpt The comment excerpt text.
622          * @param int    $comment_ID      The comment ID.
623          */
624         echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment->comment_ID );
625 }
626
627 /**
628  * Retrieve the comment id of the current comment.
629  *
630  * @since 1.5.0
631  *
632  * @return int The comment ID.
633  */
634 function get_comment_ID() {
635         $comment = get_comment();
636
637         /**
638          * Filter the returned comment ID.
639          *
640          * @since 1.5.0
641          * @since 4.1.0 The `$comment_ID` parameter was added.
642          *
643          * @param int        $comment_ID The current comment ID.
644          * @param WP_Comment $comment    The comment object.
645          */
646         return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );
647 }
648
649 /**
650  * Display the comment id of the current comment.
651  *
652  * @since 0.71
653  */
654 function comment_ID() {
655         echo get_comment_ID();
656 }
657
658 /**
659  * Retrieve the link to a given comment.
660  *
661  * @since 1.5.0
662  * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
663  *
664  * @see get_page_of_comment()
665  *
666  * @global WP_Rewrite $wp_rewrite
667  * @global bool       $in_comment_loop
668  *
669  * @param WP_Comment|int|null $comment Comment to retrieve. Default current comment.
670  * @param array               $args {
671  *     An array of optional arguments to override the defaults.
672  *
673  *     @type string     $type      Passed to {@see get_page_of_comment()}.
674  *     @type int        $page      Current page of comments, for calculating comment pagination.
675  *     @type int        $per_page  Per-page value for comment pagination.
676  *     @type int        $max_depth Passed to {@see get_page_of_comment()}.
677  *     @type int|string $cpage     Value to use for the comment's "comment-page" or "cpage" value. If provided, this
678  *                                 value overrides any value calculated from `$page` and `$per_page`.
679  * }
680  * @return string The permalink to the given comment.
681  */
682 function get_comment_link( $comment = null, $args = array() ) {
683         global $wp_rewrite, $in_comment_loop;
684
685         $comment = get_comment($comment);
686
687         // Backwards compat
688         if ( ! is_array( $args ) ) {
689                 $args = array( 'page' => $args );
690         }
691
692         $defaults = array(
693                 'type'      => 'all',
694                 'page'      => '',
695                 'per_page'  => '',
696                 'max_depth' => '',
697                 'cpage'     => null,
698         );
699         $args = wp_parse_args( $args, $defaults );
700
701         $link = get_permalink( $comment->comment_post_ID );
702
703         // The 'cpage' param takes precedence.
704         if ( ! is_null( $args['cpage'] ) ) {
705                 $cpage = $args['cpage'];
706
707         // No 'cpage' is provided, so we calculate one.
708         } else {
709                 if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
710                         $args['per_page'] = get_option('comments_per_page');
711                 }
712
713                 if ( empty( $args['per_page'] ) ) {
714                         $args['per_page'] = 0;
715                         $args['page'] = 0;
716                 }
717
718                 $cpage = $args['page'];
719
720                 if ( '' == $cpage ) {
721                         if ( ! empty( $in_comment_loop ) ) {
722                                 $cpage = get_query_var( 'cpage' );
723                         } else {
724                                 // Requires a database hit, so we only do it when we can't figure out from context.
725                                 $cpage = get_page_of_comment( $comment->comment_ID, $args );
726                         }
727                 }
728
729                 /*
730                  * If the default page displays the oldest comments, the permalinks for comments on the default page
731                  * do not need a 'cpage' query var.
732                  */
733                 $default_comments_page = get_option( 'default_comments_page' );
734                 if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
735                         $cpage = '';
736                 }
737         }
738
739         if ( $cpage && get_option( 'page_comments' ) ) {
740                 if ( $wp_rewrite->using_permalinks() ) {
741                         if ( $cpage ) {
742                                 $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
743                         }
744
745                         $link = user_trailingslashit( $link, 'comment' );
746                 } elseif ( $cpage ) {
747                         $link = add_query_arg( 'cpage', $cpage, $link );
748                 }
749
750         }
751
752         if ( $wp_rewrite->using_permalinks() ) {
753                 $link = user_trailingslashit( $link, 'comment' );
754         }
755
756         $link = $link . '#comment-' . $comment->comment_ID;
757
758         /**
759          * Filter the returned single comment permalink.
760          *
761          * @since 2.8.0
762          * @since 4.4.0 Added the `$cpage` parameter.
763          *
764          * @see get_page_of_comment()
765          *
766          * @param string     $link    The comment permalink with '#comment-$id' appended.
767          * @param WP_Comment $comment The current comment object.
768          * @param array      $args    An array of arguments to override the defaults.
769          * @param int        $cpage   The calculated 'cpage' value.
770          */
771         return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
772 }
773
774 /**
775  * Retrieves the link to the current post comments.
776  *
777  * @since 1.5.0
778  *
779  * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
780  * @return string The link to the comments.
781  */
782 function get_comments_link( $post_id = 0 ) {
783         $hash = get_comments_number( $post_id ) ? '#comments' : '#respond';
784         $comments_link = get_permalink( $post_id ) . $hash;
785
786         /**
787          * Filter the returned post comments permalink.
788          *
789          * @since 3.6.0
790          *
791          * @param string      $comments_link Post comments permalink with '#comments' appended.
792          * @param int|WP_Post $post_id       Post ID or WP_Post object.
793          */
794         return apply_filters( 'get_comments_link', $comments_link, $post_id );
795 }
796
797 /**
798  * Display the link to the current post comments.
799  *
800  * @since 0.71
801  *
802  * @param string $deprecated   Not Used.
803  * @param string $deprecated_2 Not Used.
804  */
805 function comments_link( $deprecated = '', $deprecated_2 = '' ) {
806         if ( !empty( $deprecated ) )
807                 _deprecated_argument( __FUNCTION__, '0.72' );
808         if ( !empty( $deprecated_2 ) )
809                 _deprecated_argument( __FUNCTION__, '1.3' );
810         echo esc_url( get_comments_link() );
811 }
812
813 /**
814  * Retrieve the amount of comments a post has.
815  *
816  * @since 1.5.0
817  *
818  * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
819  * @return int The number of comments a post has.
820  */
821 function get_comments_number( $post_id = 0 ) {
822         $post = get_post( $post_id );
823
824         if ( ! $post ) {
825                 $count = 0;
826         } else {
827                 $count = $post->comment_count;
828                 $post_id = $post->ID;
829         }
830
831         /**
832          * Filter the returned comment count for a post.
833          *
834          * @since 1.5.0
835          *
836          * @param int $count   Number of comments a post has.
837          * @param int $post_id Post ID.
838          */
839         return apply_filters( 'get_comments_number', $count, $post_id );
840 }
841
842 /**
843  * Display the language string for the number of comments the current post has.
844  *
845  * @since 0.71
846  *
847  * @param string $zero       Optional. Text for no comments. Default false.
848  * @param string $one        Optional. Text for one comment. Default false.
849  * @param string $more       Optional. Text for more than one comment. Default false.
850  * @param string $deprecated Not used.
851  */
852 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
853         if ( ! empty( $deprecated ) ) {
854                 _deprecated_argument( __FUNCTION__, '1.3' );
855         }
856         echo get_comments_number_text( $zero, $one, $more );
857 }
858
859 /**
860  * Display the language string for the number of comments the current post has.
861  *
862  * @since 4.0.0
863  *
864  * @param string $zero Optional. Text for no comments. Default false.
865  * @param string $one  Optional. Text for one comment. Default false.
866  * @param string $more Optional. Text for more than one comment. Default false.
867  */
868 function get_comments_number_text( $zero = false, $one = false, $more = false ) {
869         $number = get_comments_number();
870
871         if ( $number > 1 ) {
872                 if ( false === $more ) {
873                         /* translators: %s: number of comments */
874                         $output = sprintf( _n( '%s Comment', '%s Comments', $number ), number_format_i18n( $number ) );
875                 } else {
876                         // % Comments
877                         $output = str_replace( '%', number_format_i18n( $number ), $more );
878                 }
879         } elseif ( $number == 0 ) {
880                 $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
881         } else { // must be one
882                 $output = ( false === $one ) ? __( '1 Comment' ) : $one;
883         }
884         /**
885          * Filter the comments count for display.
886          *
887          * @since 1.5.0
888          *
889          * @see _n()
890          *
891          * @param string $output A translatable string formatted based on whether the count
892          *                       is equal to 0, 1, or 1+.
893          * @param int    $number The number of post comments.
894          */
895         return apply_filters( 'comments_number', $output, $number );
896 }
897
898 /**
899  * Retrieve the text of the current comment.
900  *
901  * @since 1.5.0
902  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
903  *
904  * @see Walker_Comment::comment()
905  *
906  * @param int|WP_Comment  $comment_ID WP_Comment or ID of the comment for which to get the text.
907  *                                    Default current comment.
908  * @param array           $args       Optional. An array of arguments. Default empty.
909  * @return string The comment content.
910  */
911 function get_comment_text( $comment_ID = 0, $args = array() ) {
912         $comment = get_comment( $comment_ID );
913
914         /**
915          * Filter the text of a comment.
916          *
917          * @since 1.5.0
918          *
919          * @see Walker_Comment::comment()
920          *
921          * @param string     $comment_content Text of the comment.
922          * @param WP_Comment $comment         The comment object.
923          * @param array      $args            An array of arguments.
924          */
925         return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
926 }
927
928 /**
929  * Display the text of the current comment.
930  *
931  * @since 0.71
932  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
933  *
934  * @see Walker_Comment::comment()
935  *
936  * @param int|WP_Comment  $comment_ID WP_Comment or ID of the comment for which to print the text.
937  *                                    Default current comment.
938  * @param array           $args       Optional. An array of arguments. Default empty array. Default empty.
939  */
940 function comment_text( $comment_ID = 0, $args = array() ) {
941         $comment = get_comment( $comment_ID );
942
943         $comment_text = get_comment_text( $comment, $args );
944         /**
945          * Filter the text of a comment to be displayed.
946          *
947          * @since 1.2.0
948          *
949          * @see Walker_Comment::comment()
950          *
951          * @param string     $comment_text Text of the current comment.
952          * @param WP_Comment $comment      The comment object.
953          * @param array      $args         An array of arguments.
954          */
955         echo apply_filters( 'comment_text', $comment_text, $comment, $args );
956 }
957
958 /**
959  * Retrieve the comment time of the current comment.
960  *
961  * @since 1.5.0
962  *
963  * @param string $d         Optional. The format of the time. Default user's settings.
964  * @param bool   $gmt       Optional. Whether to use the GMT date. Default false.
965  * @param bool   $translate Optional. Whether to translate the time (for use in feeds).
966  *                          Default true.
967  * @return string The formatted time.
968  */
969 function get_comment_time( $d = '', $gmt = false, $translate = true ) {
970         $comment = get_comment();
971
972         $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
973         if ( '' == $d )
974                 $date = mysql2date(get_option('time_format'), $comment_date, $translate);
975         else
976                 $date = mysql2date($d, $comment_date, $translate);
977
978         /**
979          * Filter the returned comment time.
980          *
981          * @since 1.5.0
982          *
983          * @param string|int $date      The comment time, formatted as a date string or Unix timestamp.
984          * @param string     $d         Date format.
985          * @param bool       $gmt       Whether the GMT date is in use.
986          * @param bool       $translate Whether the time is translated.
987          * @param WP_Comment $comment   The comment object.
988          */
989         return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
990 }
991
992 /**
993  * Display the comment time of the current comment.
994  *
995  * @since 0.71
996  *
997  * @param string $d Optional. The format of the time. Default user's settings.
998  */
999 function comment_time( $d = '' ) {
1000         echo get_comment_time($d);
1001 }
1002
1003 /**
1004  * Retrieve the comment type of the current comment.
1005  *
1006  * @since 1.5.0
1007  * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
1008  *
1009  * @param int|WP_Comment $comment_ID Optional. WP_Comment or ID of the comment for which to get the type.
1010  *                                   Default current comment.
1011  * @return string The comment type.
1012  */
1013 function get_comment_type( $comment_ID = 0 ) {
1014         $comment = get_comment( $comment_ID );
1015         if ( '' == $comment->comment_type )
1016                 $comment->comment_type = 'comment';
1017
1018         /**
1019          * Filter the returned comment type.
1020          *
1021          * @since 1.5.0
1022          * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
1023          *
1024          * @param string     $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
1025          * @param int        $comment_ID   The comment ID.
1026          * @param WP_Comment $comment      The comment object.
1027          */
1028         return apply_filters( 'get_comment_type', $comment->comment_type, $comment->comment_ID, $comment );
1029 }
1030
1031 /**
1032  * Display the comment type of the current comment.
1033  *
1034  * @since 0.71
1035  *
1036  * @param string $commenttxt   Optional. String to display for comment type. Default false.
1037  * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
1038  * @param string $pingbacktxt  Optional. String to display for pingback type. Default false.
1039  */
1040 function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
1041         if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
1042         if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
1043         if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
1044         $type = get_comment_type();
1045         switch( $type ) {
1046                 case 'trackback' :
1047                         echo $trackbacktxt;
1048                         break;
1049                 case 'pingback' :
1050                         echo $pingbacktxt;
1051                         break;
1052                 default :
1053                         echo $commenttxt;
1054         }
1055 }
1056
1057 /**
1058  * Retrieve The current post's trackback URL.
1059  *
1060  * There is a check to see if permalink's have been enabled and if so, will
1061  * retrieve the pretty path. If permalinks weren't enabled, the ID of the
1062  * current post is used and appended to the correct page to go to.
1063  *
1064  * @since 1.5.0
1065  *
1066  * @return string The trackback URL after being filtered.
1067  */
1068 function get_trackback_url() {
1069         if ( '' != get_option('permalink_structure') )
1070                 $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
1071         else
1072                 $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
1073
1074         /**
1075          * Filter the returned trackback URL.
1076          *
1077          * @since 2.2.0
1078          *
1079          * @param string $tb_url The trackback URL.
1080          */
1081         return apply_filters( 'trackback_url', $tb_url );
1082 }
1083
1084 /**
1085  * Display the current post's trackback URL.
1086  *
1087  * @since 0.71
1088  *
1089  * @param bool $deprecated_echo Not used.
1090  * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
1091  *                     for the result instead.
1092  */
1093 function trackback_url( $deprecated_echo = true ) {
1094         if ( true !== $deprecated_echo ) {
1095                 _deprecated_argument( __FUNCTION__, '2.5',
1096                         /* translators: %s: get_trackback_url() */
1097                         sprintf( __( 'Use %s instead if you do not want the value echoed.' ),
1098                                 '<code>get_trackback_url()</code>'
1099                         )
1100                 );
1101         }
1102
1103         if ( $deprecated_echo ) {
1104                 echo get_trackback_url();
1105         } else {
1106                 return get_trackback_url();
1107         }
1108 }
1109
1110 /**
1111  * Generate and display the RDF for the trackback information of current post.
1112  *
1113  * Deprecated in 3.0.0, and restored in 3.0.1.
1114  *
1115  * @since 0.71
1116  *
1117  * @param int $deprecated Not used (Was $timezone = 0).
1118  */
1119 function trackback_rdf( $deprecated = '' ) {
1120         if ( ! empty( $deprecated ) ) {
1121                 _deprecated_argument( __FUNCTION__, '2.5' );
1122         }
1123
1124         if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
1125                 return;
1126         }
1127
1128         echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1129                         xmlns:dc="http://purl.org/dc/elements/1.1/"
1130                         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
1131                 <rdf:Description rdf:about="';
1132         the_permalink();
1133         echo '"'."\n";
1134         echo '    dc:identifier="';
1135         the_permalink();
1136         echo '"'."\n";
1137         echo '    dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
1138         echo '    trackback:ping="'.get_trackback_url().'"'." />\n";
1139         echo '</rdf:RDF>';
1140 }
1141
1142 /**
1143  * Whether the current post is open for comments.
1144  *
1145  * @since 1.5.0
1146  *
1147  * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
1148  * @return bool True if the comments are open.
1149  */
1150 function comments_open( $post_id = null ) {
1151
1152         $_post = get_post($post_id);
1153
1154         $open = ( 'open' == $_post->comment_status );
1155
1156         /**
1157          * Filter whether the current post is open for comments.
1158          *
1159          * @since 2.5.0
1160          *
1161          * @param bool        $open    Whether the current post is open for comments.
1162          * @param int|WP_Post $post_id The post ID or WP_Post object.
1163          */
1164         return apply_filters( 'comments_open', $open, $post_id );
1165 }
1166
1167 /**
1168  * Whether the current post is open for pings.
1169  *
1170  * @since 1.5.0
1171  *
1172  * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
1173  * @return bool True if pings are accepted
1174  */
1175 function pings_open( $post_id = null ) {
1176
1177         $_post = get_post($post_id);
1178
1179         $open = ( 'open' == $_post->ping_status );
1180
1181         /**
1182          * Filter whether the current post is open for pings.
1183          *
1184          * @since 2.5.0
1185          *
1186          * @param bool        $open    Whether the current post is open for pings.
1187          * @param int|WP_Post $post_id The post ID or WP_Post object.
1188          */
1189         return apply_filters( 'pings_open', $open, $post_id );
1190 }
1191
1192 /**
1193  * Display form token for unfiltered comments.
1194  *
1195  * Will only display nonce token if the current user has permissions for
1196  * unfiltered html. Won't display the token for other users.
1197  *
1198  * The function was backported to 2.0.10 and was added to versions 2.1.3 and
1199  * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
1200  * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
1201  *
1202  * Backported to 2.0.10.
1203  *
1204  * @since 2.1.3
1205  */
1206 function wp_comment_form_unfiltered_html_nonce() {
1207         $post = get_post();
1208         $post_id = $post ? $post->ID : 0;
1209
1210         if ( current_user_can( 'unfiltered_html' ) ) {
1211                 wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
1212                 echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
1213         }
1214 }
1215
1216 /**
1217  * Load the comment template specified in $file.
1218  *
1219  * Will not display the comments template if not on single post or page, or if
1220  * the post does not have comments.
1221  *
1222  * Uses the WordPress database object to query for the comments. The comments
1223  * are passed through the 'comments_array' filter hook with the list of comments
1224  * and the post ID respectively.
1225  *
1226  * The $file path is passed through a filter hook called, 'comments_template'
1227  * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
1228  * first and if it fails it will require the default comment template from the
1229  * default theme. If either does not exist, then the WordPress process will be
1230  * halted. It is advised for that reason, that the default theme is not deleted.
1231  *
1232  * @uses $withcomments Will not try to get the comments if the post has none.
1233  *
1234  * @since 1.5.0
1235  *
1236  * @global WP_Query   $wp_query
1237  * @global WP_Post    $post
1238  * @global wpdb       $wpdb
1239  * @global int        $id
1240  * @global WP_Comment $comment
1241  * @global string     $user_login
1242  * @global int        $user_ID
1243  * @global string     $user_identity
1244  * @global bool       $overridden_cpage
1245  *
1246  * @param string $file              Optional. The file to load. Default '/comments.php'.
1247  * @param bool   $separate_comments Optional. Whether to separate the comments by comment type.
1248  *                                  Default false.
1249  */
1250 function comments_template( $file = '/comments.php', $separate_comments = false ) {
1251         global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
1252
1253         if ( !(is_single() || is_page() || $withcomments) || empty($post) )
1254                 return;
1255
1256         if ( empty($file) )
1257                 $file = '/comments.php';
1258
1259         $req = get_option('require_name_email');
1260
1261         /*
1262          * Comment author information fetched from the comment cookies.
1263          */
1264         $commenter = wp_get_current_commenter();
1265
1266         /*
1267          * The name of the current comment author escaped for use in attributes.
1268          * Escaped by sanitize_comment_cookies().
1269          */
1270         $comment_author = $commenter['comment_author'];
1271
1272         /*
1273          * The email address of the current comment author escaped for use in attributes.
1274          * Escaped by sanitize_comment_cookies().
1275          */
1276         $comment_author_email = $commenter['comment_author_email'];
1277
1278         /*
1279          * The url of the current comment author escaped for use in attributes.
1280          */
1281         $comment_author_url = esc_url($commenter['comment_author_url']);
1282
1283         $comment_args = array(
1284                 'orderby' => 'comment_date_gmt',
1285                 'order' => 'ASC',
1286                 'status'  => 'approve',
1287                 'post_id' => $post->ID,
1288                 'hierarchical' => 'threaded',
1289                 'no_found_rows' => false,
1290                 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
1291         );
1292
1293         if ( $user_ID ) {
1294                 $comment_args['include_unapproved'] = array( $user_ID );
1295         } elseif ( ! empty( $comment_author_email ) ) {
1296                 $comment_args['include_unapproved'] = array( $comment_author_email );
1297         }
1298
1299         $per_page = 0;
1300         if ( get_option( 'page_comments' ) ) {
1301                 $per_page = (int) get_query_var( 'comments_per_page' );
1302                 if ( 0 === $per_page ) {
1303                         $per_page = (int) get_option( 'comments_per_page' );
1304                 }
1305
1306                 $comment_args['number'] = $per_page;
1307                 $page = (int) get_query_var( 'cpage' );
1308
1309                 if ( $page ) {
1310                         $comment_args['offset'] = ( $page - 1 ) * $per_page;
1311                 } elseif ( 'oldest' === get_option( 'default_comments_page' ) ) {
1312                         $comment_args['offset'] = 0;
1313                 } else {
1314                         // If fetching the first page of 'newest', we need a top-level comment count.
1315                         $top_level_query = new WP_Comment_Query();
1316                         $top_level_args  = array(
1317                                 'count'   => true,
1318                                 'orderby' => false,
1319                                 'post_id' => $post->ID,
1320                                 'parent'  => 0,
1321                                 'status'  => 'approve',
1322                         );
1323
1324                         if ( isset( $comment_args['include_unapproved'] ) ) {
1325                                 $top_level_args['include_unapproved'] = $comment_args['include_unapproved'];
1326                         }
1327
1328                         $top_level_count = $top_level_query->query( $top_level_args );
1329
1330                         $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
1331                 }
1332         }
1333
1334         $comment_query = new WP_Comment_Query( $comment_args );
1335         $_comments = $comment_query->comments;
1336
1337         // Trees must be flattened before they're passed to the walker.
1338         $comments_flat = array();
1339         foreach ( $_comments as $_comment ) {
1340                 $comments_flat[]  = $_comment;
1341                 $comment_children = $_comment->get_children( array(
1342                         'format' => 'flat',
1343                         'status' => $comment_args['status'],
1344                         'orderby' => $comment_args['orderby']
1345                 ) );
1346
1347                 foreach ( $comment_children as $comment_child ) {
1348                         $comments_flat[] = $comment_child;
1349                 }
1350         }
1351
1352         /**
1353          * Filter the comments array.
1354          *
1355          * @since 2.1.0
1356          *
1357          * @param array $comments Array of comments supplied to the comments template.
1358          * @param int   $post_ID  Post ID.
1359          */
1360         $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
1361
1362         // Set up lazy-loading for comment metadata.
1363         add_action( 'get_comment_metadata', array( $wp_query, 'lazyload_comment_meta' ), 10, 2 );
1364
1365         $comments = &$wp_query->comments;
1366         $wp_query->comment_count = count($wp_query->comments);
1367         $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
1368
1369         if ( $separate_comments ) {
1370                 $wp_query->comments_by_type = separate_comments($comments);
1371                 $comments_by_type = &$wp_query->comments_by_type;
1372         } else {
1373                 $wp_query->comments_by_type = array();
1374         }
1375
1376         $overridden_cpage = false;
1377         if ( '' == get_query_var( 'cpage' ) && $wp_query->max_num_comment_pages > 1 ) {
1378                 set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
1379                 $overridden_cpage = true;
1380         }
1381
1382         if ( !defined('COMMENTS_TEMPLATE') )
1383                 define('COMMENTS_TEMPLATE', true);
1384
1385         $theme_template = STYLESHEETPATH . $file;
1386         /**
1387          * Filter the path to the theme template file used for the comments template.
1388          *
1389          * @since 1.5.1
1390          *
1391          * @param string $theme_template The path to the theme template file.
1392          */
1393         $include = apply_filters( 'comments_template', $theme_template );
1394         if ( file_exists( $include ) )
1395                 require( $include );
1396         elseif ( file_exists( TEMPLATEPATH . $file ) )
1397                 require( TEMPLATEPATH . $file );
1398         else // Backward compat code will be removed in a future release
1399                 require( ABSPATH . WPINC . '/theme-compat/comments.php');
1400 }
1401
1402 /**
1403  * Display the JS popup script to show a comment.
1404  *
1405  * If the $file parameter is empty, then the home page is assumed. The defaults
1406  * for the window are 400px by 400px.
1407  *
1408  * For the comment link popup to work, this function has to be called or the
1409  * normal comment link will be assumed.
1410  *
1411  * @global string $wpcommentspopupfile  The URL to use for the popup window.
1412  * @global int    $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
1413  *
1414  * @since 0.71
1415  *
1416  * @param int $width  Optional. The width of the popup window. Default 400.
1417  * @param int $height Optional. The height of the popup window. Default 400.
1418  * @param string $file Optional. Sets the location of the popup window.
1419  */
1420 function comments_popup_script( $width = 400, $height = 400, $file = '' ) {
1421         global $wpcommentspopupfile, $wpcommentsjavascript;
1422
1423         if (empty ($file)) {
1424                 $wpcommentspopupfile = '';  // Use the index.
1425         } else {
1426                 $wpcommentspopupfile = $file;
1427         }
1428
1429         $wpcommentsjavascript = 1;
1430         $javascript = "<script type='text/javascript'>\nfunction wpopen (macagna) {\n    window.open(macagna, '_blank', 'width=$width,height=$height,scrollbars=yes,status=yes');\n}\n</script>\n";
1431         echo $javascript;
1432 }
1433
1434 /**
1435  * Displays the link to the comments popup window for the current post ID.
1436  *
1437  * Is not meant to be displayed on single posts and pages. Should be used
1438  * on the lists of posts
1439  *
1440  * @global string $wpcommentspopupfile  The URL to use for the popup window.
1441  * @global int    $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
1442  *
1443  * @since 0.71
1444  *
1445  * @param string $zero      Optional. String to display when no comments. Default false.
1446  * @param string $one       Optional. String to display when only one comment is available.
1447  *                          Default false.
1448  * @param string $more      Optional. String to display when there are more than one comment.
1449  *                          Default false.
1450  * @param string $css_class Optional. CSS class to use for comments. Default empty.
1451  * @param string $none      Optional. String to display when comments have been turned off.
1452  *                          Default false.
1453  */
1454 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
1455         global $wpcommentspopupfile, $wpcommentsjavascript;
1456
1457         $id = get_the_ID();
1458         $title = get_the_title();
1459         $number = get_comments_number( $id );
1460
1461         if ( false === $zero ) {
1462                 /* translators: %s: post title */
1463                 $zero = sprintf( __( 'No Comments<span class="screen-reader-text"> on %s</span>' ), $title );
1464         }
1465
1466         if ( false === $one ) {
1467                 /* translators: %s: post title */
1468                 $one = sprintf( __( '1 Comment<span class="screen-reader-text"> on %s</span>' ), $title );
1469         }
1470
1471         if ( false === $more ) {
1472                 /* translators: 1: Number of comments 2: post title */
1473                 $more = _n( '%1$s Comment<span class="screen-reader-text"> on %2$s</span>', '%1$s Comments<span class="screen-reader-text"> on %2$s</span>', $number );
1474                 $more = sprintf( $more, number_format_i18n( $number ), $title );
1475         }
1476
1477         if ( false === $none ) {
1478                 /* translators: %s: post title */
1479                 $none = sprintf( __( 'Comments Off<span class="screen-reader-text"> on %s</span>' ), $title );
1480         }
1481
1482         if ( 0 == $number && !comments_open() && !pings_open() ) {
1483                 echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
1484                 return;
1485         }
1486
1487         if ( post_password_required() ) {
1488                 _e( 'Enter your password to view comments.' );
1489                 return;
1490         }
1491
1492         echo '<a href="';
1493         if ( $wpcommentsjavascript ) {
1494                 if ( empty( $wpcommentspopupfile ) )
1495                         $home = home_url();
1496                 else
1497                         $home = get_option('siteurl');
1498                 echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
1499                 echo '" onclick="wpopen(this.href); return false"';
1500         } else {
1501                 // if comments_popup_script() is not in the template, display simple comment link
1502                 if ( 0 == $number ) {
1503                         $respond_link = get_permalink() . '#respond';
1504                         /**
1505                          * Filter the respond link when a post has no comments.
1506                          *
1507                          * @since 4.4.0
1508                          *
1509                          * @param string $respond_link The default response link.
1510                          * @param integer $id The post ID.
1511                          */
1512                         echo apply_filters( 'respond_link', $respond_link, $id );
1513                 } else {
1514                         comments_link();
1515                 }
1516                 echo '"';
1517         }
1518
1519         if ( !empty( $css_class ) ) {
1520                 echo ' class="'.$css_class.'" ';
1521         }
1522
1523         $attributes = '';
1524         /**
1525          * Filter the comments popup link attributes for display.
1526          *
1527          * @since 2.5.0
1528          *
1529          * @param string $attributes The comments popup link attributes. Default empty.
1530          */
1531         echo apply_filters( 'comments_popup_link_attributes', $attributes );
1532
1533         echo '>';
1534         comments_number( $zero, $one, $more );
1535         echo '</a>';
1536 }
1537
1538 /**
1539  * Retrieve HTML content for reply to comment link.
1540  *
1541  * @since 2.7.0
1542  * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
1543  *
1544  * @param array $args {
1545  *     Optional. Override default arguments.
1546  *
1547  *     @type string $add_below  The first part of the selector used to identify the comment to respond below.
1548  *                              The resulting value is passed as the first parameter to addComment.moveForm(),
1549  *                              concatenated as $add_below-$comment->comment_ID. Default 'comment'.
1550  *     @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
1551  *                              to addComment.moveForm(), and appended to the link URL as a hash value.
1552  *                              Default 'respond'.
1553  *     @type string $reply_text The text of the Reply link. Default 'Reply'.
1554  *     @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
1555  *     @type int    $depth'     The depth of the new comment. Must be greater than 0 and less than the value
1556  *                              of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
1557  *     @type string $before     The text or HTML to add before the reply link. Default empty.
1558  *     @type string $after      The text or HTML to add after the reply link. Default empty.
1559  * }
1560  * @param int|WP_Comment $comment Comment being replied to. Default current comment.
1561  * @param int|WP_Post    $post    Post ID or WP_Post object the comment is going to be displayed on.
1562  *                                Default current post.
1563  * @return void|false|string Link to show comment form, if successful. False, if comments are closed.
1564  */
1565 function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
1566         $defaults = array(
1567                 'add_below'     => 'comment',
1568                 'respond_id'    => 'respond',
1569                 'reply_text'    => __( 'Reply' ),
1570                 'reply_to_text' => __( 'Reply to %s' ),
1571                 'login_text'    => __( 'Log in to Reply' ),
1572                 'depth'         => 0,
1573                 'before'        => '',
1574                 'after'         => ''
1575         );
1576
1577         $args = wp_parse_args( $args, $defaults );
1578
1579         if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
1580                 return;
1581         }
1582
1583         $comment = get_comment( $comment );
1584
1585         if ( empty( $post ) ) {
1586                 $post = $comment->comment_post_ID;
1587         }
1588
1589         $post = get_post( $post );
1590
1591         if ( ! comments_open( $post->ID ) ) {
1592                 return false;
1593         }
1594
1595         /**
1596          * Filter the comment reply link arguments.
1597          *
1598          * @since 4.1.0
1599          *
1600          * @param array      $args    Comment reply link arguments. See get_comment_reply_link()
1601          *                            for more information on accepted arguments.
1602          * @param WP_Comment $comment The object of the comment being replied to.
1603          * @param WP_Post    $post    The WP_Post object.
1604          */
1605         $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
1606
1607         if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
1608                 $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
1609                         esc_url( wp_login_url( get_permalink() ) ),
1610                         $args['login_text']
1611                 );
1612         } else {
1613                 $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
1614                         $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID
1615                 );
1616
1617                 $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
1618                         esc_url( add_query_arg( 'replytocom', $comment->comment_ID, get_permalink( $post->ID ) ) ) . "#" . $args['respond_id'],
1619                         $onclick,
1620                         esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
1621                         $args['reply_text']
1622                 );
1623         }
1624
1625         /**
1626          * Filter the comment reply link.
1627          *
1628          * @since 2.7.0
1629          *
1630          * @param string  $link    The HTML markup for the comment reply link.
1631          * @param array   $args    An array of arguments overriding the defaults.
1632          * @param object  $comment The object of the comment being replied.
1633          * @param WP_Post $post    The WP_Post object.
1634          */
1635         return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
1636 }
1637
1638 /**
1639  * Displays the HTML content for reply to comment link.
1640  *
1641  * @since 2.7.0
1642  *
1643  * @see get_comment_reply_link()
1644  *
1645  * @param array       $args    Optional. Override default options.
1646  * @param int         $comment Comment being replied to. Default current comment.
1647  * @param int|WP_Post $post    Post ID or WP_Post object the comment is going to be displayed on.
1648  *                             Default current post.
1649  * @return mixed Link to show comment form, if successful. False, if comments are closed.
1650  */
1651 function comment_reply_link($args = array(), $comment = null, $post = null) {
1652         echo get_comment_reply_link($args, $comment, $post);
1653 }
1654
1655 /**
1656  * Retrieve HTML content for reply to post link.
1657  *
1658  * @since 2.7.0
1659  *
1660  * @param array $args {
1661  *     Optional. Override default arguments.
1662  *
1663  *     @type string $add_below  The first part of the selector used to identify the comment to respond below.
1664  *                              The resulting value is passed as the first parameter to addComment.moveForm(),
1665  *                              concatenated as $add_below-$comment->comment_ID. Default is 'post'.
1666  *     @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
1667  *                              to addComment.moveForm(), and appended to the link URL as a hash value.
1668  *                              Default 'respond'.
1669  *     @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
1670  *     @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
1671  *     @type string $before     Text or HTML to add before the reply link. Default empty.
1672  *     @type string $after      Text or HTML to add after the reply link. Default empty.
1673  * }
1674  * @param int|WP_Post $post    Optional. Post ID or WP_Post object the comment is going to be displayed on.
1675  *                             Default current post.
1676  * @return false|null|string Link to show comment form, if successful. False, if comments are closed.
1677  */
1678 function get_post_reply_link($args = array(), $post = null) {
1679         $defaults = array(
1680                 'add_below'  => 'post',
1681                 'respond_id' => 'respond',
1682                 'reply_text' => __('Leave a Comment'),
1683                 'login_text' => __('Log in to leave a Comment'),
1684                 'before'     => '',
1685                 'after'      => '',
1686         );
1687
1688         $args = wp_parse_args($args, $defaults);
1689
1690         $post = get_post($post);
1691
1692         if ( ! comments_open( $post->ID ) ) {
1693                 return false;
1694         }
1695
1696         if ( get_option('comment_registration') && ! is_user_logged_in() ) {
1697                 $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
1698                         wp_login_url( get_permalink() ),
1699                         $args['login_text']
1700                 );
1701         } else {
1702                 $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
1703                         $args['add_below'], $post->ID, $args['respond_id']
1704                 );
1705
1706                 $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
1707                         get_permalink( $post->ID ) . '#' . $args['respond_id'],
1708                         $onclick,
1709                         $args['reply_text']
1710                 );
1711         }
1712         $formatted_link = $args['before'] . $link . $args['after'];
1713
1714         /**
1715          * Filter the formatted post comments link HTML.
1716          *
1717          * @since 2.7.0
1718          *
1719          * @param string      $formatted The HTML-formatted post comments link.
1720          * @param int|WP_Post $post      The post ID or WP_Post object.
1721          */
1722         return apply_filters( 'post_comments_link', $formatted_link, $post );
1723 }
1724
1725 /**
1726  * Displays the HTML content for reply to post link.
1727  *
1728  * @since 2.7.0
1729  *
1730  * @see get_post_reply_link()
1731  *
1732  * @param array       $args Optional. Override default options,
1733  * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
1734  *                          Default current post.
1735  * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1736  */
1737 function post_reply_link($args = array(), $post = null) {
1738         echo get_post_reply_link($args, $post);
1739 }
1740
1741 /**
1742  * Retrieve HTML content for cancel comment reply link.
1743  *
1744  * @since 2.7.0
1745  *
1746  * @param string $text Optional. Text to display for cancel reply link. Default empty.
1747  * @return string
1748  */
1749 function get_cancel_comment_reply_link( $text = '' ) {
1750         if ( empty($text) )
1751                 $text = __('Click here to cancel reply.');
1752
1753         $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
1754         $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
1755
1756         $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
1757
1758         /**
1759          * Filter the cancel comment reply link HTML.
1760          *
1761          * @since 2.7.0
1762          *
1763          * @param string $formatted_link The HTML-formatted cancel comment reply link.
1764          * @param string $link           Cancel comment reply link URL.
1765          * @param string $text           Cancel comment reply link text.
1766          */
1767         return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
1768 }
1769
1770 /**
1771  * Display HTML content for cancel comment reply link.
1772  *
1773  * @since 2.7.0
1774  *
1775  * @param string $text Optional. Text to display for cancel reply link. Default empty.
1776  */
1777 function cancel_comment_reply_link( $text = '' ) {
1778         echo get_cancel_comment_reply_link($text);
1779 }
1780
1781 /**
1782  * Retrieve hidden input HTML for replying to comments.
1783  *
1784  * @since 3.0.0
1785  *
1786  * @param int $id Optional. Post ID. Default current post ID.
1787  * @return string Hidden input HTML for replying to comments
1788  */
1789 function get_comment_id_fields( $id = 0 ) {
1790         if ( empty( $id ) )
1791                 $id = get_the_ID();
1792
1793         $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1794         $result  = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
1795         $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
1796
1797         /**
1798          * Filter the returned comment id fields.
1799          *
1800          * @since 3.0.0
1801          *
1802          * @param string $result    The HTML-formatted hidden id field comment elements.
1803          * @param int    $id        The post ID.
1804          * @param int    $replytoid The id of the comment being replied to.
1805          */
1806         return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
1807 }
1808
1809 /**
1810  * Output hidden input HTML for replying to comments.
1811  *
1812  * @since 2.7.0
1813  *
1814  * @param int $id Optional. Post ID. Default current post ID.
1815  */
1816 function comment_id_fields( $id = 0 ) {
1817         echo get_comment_id_fields( $id );
1818 }
1819
1820 /**
1821  * Display text based on comment reply status.
1822  *
1823  * Only affects users with JavaScript disabled.
1824  *
1825  * @since 2.7.0
1826  *
1827  * @param string $noreplytext  Optional. Text to display when not replying to a comment.
1828  *                             Default false.
1829  * @param string $replytext    Optional. Text to display when replying to a comment.
1830  *                             Default false. Accepts "%s" for the author of the comment
1831  *                             being replied to.
1832  * @param string $linktoparent Optional. Boolean to control making the author's name a link
1833  *                             to their comment. Default true.
1834  */
1835 function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
1836         $comment = get_comment();
1837
1838         if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
1839         if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
1840
1841         $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1842
1843         if ( 0 == $replytoid )
1844                 echo $noreplytext;
1845         else {
1846                 $comment = get_comment($replytoid);
1847                 $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author( $comment ) . '</a>' : get_comment_author( $comment );
1848                 printf( $replytext, $author );
1849         }
1850 }
1851
1852 /**
1853  * List comments.
1854  *
1855  * Used in the comments.php template to list comments for a particular post.
1856  *
1857  * @since 2.7.0
1858  *
1859  * @see WP_Query->comments
1860  *
1861  * @global WP_Query $wp_query
1862  * @global int      $comment_alt
1863  * @global int      $comment_depth
1864  * @global int      $comment_thread_alt
1865  * @global bool     $overridden_cpage
1866  * @global bool     $in_comment_loop
1867  *
1868  * @param string|array $args {
1869  *     Optional. Formatting options.
1870  *
1871  *     @type object $walker            Instance of a Walker class to list comments. Default null.
1872  *     @type int    $max_depth         The maximum comments depth. Default empty.
1873  *     @type string $style             The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
1874  *     @type string $callback          Callback function to use. Default null.
1875  *     @type string $end-callback      Callback function to use at the end. Default null.
1876  *     @type string $type              Type of comments to list.
1877  *                                     Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
1878  *     @type int    $page              Page ID to list comments for. Default empty.
1879  *     @type int    $per_page          Number of comments to list per page. Default empty.
1880  *     @type int    $avatar_size       Height and width dimensions of the avatar size. Default 32.
1881  *     @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'.
1882  *     @type bool   $reverse_children  Whether to reverse child comments in the list. Default null.
1883  *     @type string $format            How to format the comments list.
1884  *                                     Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
1885  *     @type bool   $short_ping        Whether to output short pings. Default false.
1886  *     @type bool   $echo              Whether to echo the output or return it. Default true.
1887  * }
1888  * @param array $comments Optional. Array of WP_Comment objects.
1889  */
1890 function wp_list_comments( $args = array(), $comments = null ) {
1891         global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
1892
1893         $in_comment_loop = true;
1894
1895         $comment_alt = $comment_thread_alt = 0;
1896         $comment_depth = 1;
1897
1898         $defaults = array(
1899                 'walker'            => null,
1900                 'max_depth'         => '',
1901                 'style'             => 'ul',
1902                 'callback'          => null,
1903                 'end-callback'      => null,
1904                 'type'              => 'all',
1905                 'page'              => '',
1906                 'per_page'          => '',
1907                 'avatar_size'       => 32,
1908                 'reverse_top_level' => null,
1909                 'reverse_children'  => '',
1910                 'format'            => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
1911                 'short_ping'        => false,
1912                 'echo'              => true,
1913         );
1914
1915         $r = wp_parse_args( $args, $defaults );
1916
1917         /**
1918          * Filter the arguments used in retrieving the comment list.
1919          *
1920          * @since 4.0.0
1921          *
1922          * @see wp_list_comments()
1923          *
1924          * @param array $r An array of arguments for displaying comments.
1925          */
1926         $r = apply_filters( 'wp_list_comments_args', $r );
1927
1928         /*
1929          * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
1930          * perform a separate comment query and allow Walker_Comment to paginate.
1931          */
1932         if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
1933                 $current_cpage = get_query_var( 'cpage' );
1934                 if ( ! $current_cpage ) {
1935                         $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
1936                 }
1937
1938                 $current_per_page = get_query_var( 'comments_per_page' );
1939                 if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
1940                         $comments = get_comments( array(
1941                                 'post_id' => get_queried_object_id(),
1942                                 'orderby' => 'comment_date_gmt',
1943                                 'order' => 'ASC',
1944                                 'status' => 'all',
1945                         ) );
1946                 }
1947         }
1948
1949         // Figure out what comments we'll be looping through ($_comments)
1950         if ( null !== $comments ) {
1951                 $comments = (array) $comments;
1952                 if ( empty($comments) )
1953                         return;
1954                 if ( 'all' != $r['type'] ) {
1955                         $comments_by_type = separate_comments($comments);
1956                         if ( empty($comments_by_type[$r['type']]) )
1957                                 return;
1958                         $_comments = $comments_by_type[$r['type']];
1959                 } else {
1960                         $_comments = $comments;
1961                 }
1962         } else {
1963                 if ( empty($wp_query->comments) )
1964                         return;
1965                 if ( 'all' != $r['type'] ) {
1966                         if ( empty($wp_query->comments_by_type) )
1967                                 $wp_query->comments_by_type = separate_comments($wp_query->comments);
1968                         if ( empty($wp_query->comments_by_type[$r['type']]) )
1969                                 return;
1970                         $_comments = $wp_query->comments_by_type[$r['type']];
1971                 } else {
1972                         $_comments = $wp_query->comments;
1973                 }
1974
1975                 // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
1976                 if ( $wp_query->max_num_comment_pages ) {
1977                         $default_comments_page = get_option( 'default_comments_page' );
1978                         $cpage = get_query_var( 'cpage' );
1979                         if ( 'newest' === $default_comments_page ) {
1980                                 $r['cpage'] = $cpage;
1981
1982                         // When first page shows oldest comments, post permalink is the same as the comment permalink.
1983                         } elseif ( $cpage == 1 ) {
1984                                 $r['cpage'] = '';
1985                         } else {
1986                                 $r['cpage'] = $cpage;
1987                         }
1988
1989                         $r['page'] = 0;
1990                         $r['per_page'] = 0;
1991                 }
1992         }
1993
1994         if ( '' === $r['per_page'] && get_option( 'page_comments' ) ) {
1995                 $r['per_page'] = get_query_var('comments_per_page');
1996         }
1997
1998         if ( empty($r['per_page']) ) {
1999                 $r['per_page'] = 0;
2000                 $r['page'] = 0;
2001         }
2002
2003         if ( '' === $r['max_depth'] ) {
2004                 if ( get_option('thread_comments') )
2005                         $r['max_depth'] = get_option('thread_comments_depth');
2006                 else
2007                         $r['max_depth'] = -1;
2008         }
2009
2010         if ( '' === $r['page'] ) {
2011                 if ( empty($overridden_cpage) ) {
2012                         $r['page'] = get_query_var('cpage');
2013                 } else {
2014                         $threaded = ( -1 != $r['max_depth'] );
2015                         $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
2016                         set_query_var( 'cpage', $r['page'] );
2017                 }
2018         }
2019         // Validation check
2020         $r['page'] = intval($r['page']);
2021         if ( 0 == $r['page'] && 0 != $r['per_page'] )
2022                 $r['page'] = 1;
2023
2024         if ( null === $r['reverse_top_level'] )
2025                 $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
2026
2027         if ( empty( $r['walker'] ) ) {
2028                 $walker = new Walker_Comment;
2029         } else {
2030                 $walker = $r['walker'];
2031         }
2032
2033         $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
2034
2035         $in_comment_loop = false;
2036
2037         if ( $r['echo'] ) {
2038                 echo $output;
2039         } else {
2040                 return $output;
2041         }
2042 }
2043
2044 /**
2045  * Output a complete commenting form for use within a template.
2046  *
2047  * Most strings and form fields may be controlled through the $args array passed
2048  * into the function, while you may also choose to use the comment_form_default_fields
2049  * filter to modify the array of default fields if you'd just like to add a new
2050  * one or remove a single field. All fields are also individually passed through
2051  * a filter of the form comment_form_field_$name where $name is the key used
2052  * in the array of fields.
2053  *
2054  * @since 3.0.0
2055  * @since 4.1.0 Introduced the 'class_submit' argument.
2056  * @since 4.2.0 Introduced the 'submit_button' and 'submit_fields' arguments.
2057  * @since 4.4.0 Introduced the 'class_form', 'title_reply_before', 'title_reply_after',
2058  *              'cancel_reply_before', and 'cancel_reply_after' arguments.
2059  *
2060  * @param array       $args {
2061  *     Optional. Default arguments and form fields to override.
2062  *
2063  *     @type array $fields {
2064  *         Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
2065  *
2066  *         @type string $author Comment author field HTML.
2067  *         @type string $email  Comment author email field HTML.
2068  *         @type string $url    Comment author URL field HTML.
2069  *     }
2070  *     @type string $comment_field        The comment textarea field HTML.
2071  *     @type string $must_log_in          HTML element for a 'must be logged in to comment' message.
2072  *     @type string $logged_in_as         HTML element for a 'logged in as [user]' message.
2073  *     @type string $comment_notes_before HTML element for a message displayed before the comment fields
2074  *                                        if the user is not logged in.
2075  *                                        Default 'Your email address will not be published.'.
2076  *     @type string $comment_notes_after  HTML element for a message displayed after the textarea field.
2077  *     @type string $id_form              The comment form element id attribute. Default 'commentform'.
2078  *     @type string $id_submit            The comment submit element id attribute. Default 'submit'.
2079  *     @type string $class_form           The comment form element class attribute. Default 'comment-form'.
2080  *     @type string $class_submit         The comment submit element class attribute. Default 'submit'.
2081  *     @type string $name_submit          The comment submit element name attribute. Default 'submit'.
2082  *     @type string $title_reply          The translatable 'reply' button label. Default 'Leave a Reply'.
2083  *     @type string $title_reply_to       The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
2084  *                                        where %s is the author of the comment being replied to.
2085  *     @type string $title_reply_before   HTML displayed before the comment form title.
2086  *                                        Default: '<h3 id="reply-title" class="comment-reply-title">'.
2087  *     @type string $title_reply_after    HTML displayed after the comment form title.
2088  *                                        Default: '</h3>'.
2089  *     @type string $cancel_reply_before  HTML displayed before the cancel reply link.
2090  *     @type string $cancel_reply_after   HTML displayed after the cancel reply link.
2091  *     @type string $cancel_reply_link    The translatable 'cancel reply' button label. Default 'Cancel reply'.
2092  *     @type string $label_submit         The translatable 'submit' button label. Default 'Post a comment'.
2093  *     @type string $submit_button        HTML format for the Submit button.
2094  *                                        Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
2095  *     @type string $submit_field         HTML format for the markup surrounding the Submit button and comment hidden
2096  *                                        fields. Default: '<p class="form-submit">%1$s %2$s</a>', where %1$s is the
2097  *                                        submit button markup and %2$s is the comment hidden fields.
2098  *     @type string $format               The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
2099  * }
2100  * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
2101  */
2102 function comment_form( $args = array(), $post_id = null ) {
2103         if ( null === $post_id )
2104                 $post_id = get_the_ID();
2105
2106         $commenter = wp_get_current_commenter();
2107         $user = wp_get_current_user();
2108         $user_identity = $user->exists() ? $user->display_name : '';
2109
2110         $args = wp_parse_args( $args );
2111         if ( ! isset( $args['format'] ) )
2112                 $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
2113
2114         $req      = get_option( 'require_name_email' );
2115         $aria_req = ( $req ? " aria-required='true'" : '' );
2116         $html_req = ( $req ? " required='required'" : '' );
2117         $html5    = 'html5' === $args['format'];
2118         $fields   =  array(
2119                 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
2120                             '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . $html_req . ' /></p>',
2121                 'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
2122                             '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
2123                 'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
2124                             '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
2125         );
2126
2127         $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
2128
2129         /**
2130          * Filter the default comment form fields.
2131          *
2132          * @since 3.0.0
2133          *
2134          * @param array $fields The default comment fields.
2135          */
2136         $fields = apply_filters( 'comment_form_default_fields', $fields );
2137         $defaults = array(
2138                 'fields'               => $fields,
2139                 'comment_field'        => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8"  aria-required="true" required="required"></textarea></p>',
2140                 /** This filter is documented in wp-includes/link-template.php */
2141                 'must_log_in'          => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
2142                 /** This filter is documented in wp-includes/link-template.php */
2143                 'logged_in_as'         => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s" aria-label="Logged in as %2$s. Edit your profile.">Logged in as %2$s</a>. <a href="%3$s">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
2144                 'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
2145                 'comment_notes_after'  => '',
2146                 'id_form'              => 'commentform',
2147                 'id_submit'            => 'submit',
2148                 'class_form'           => 'comment-form',
2149                 'class_submit'         => 'submit',
2150                 'name_submit'          => 'submit',
2151                 'title_reply'          => __( 'Leave a Reply' ),
2152                 'title_reply_to'       => __( 'Leave a Reply to %s' ),
2153                 'title_reply_before'   => '<h3 id="reply-title" class="comment-reply-title">',
2154                 'title_reply_after'    => '</h3>',
2155                 'cancel_reply_before'  => ' <small>',
2156                 'cancel_reply_after'   => '</small>',
2157                 'cancel_reply_link'    => __( 'Cancel reply' ),
2158                 'label_submit'         => __( 'Post Comment' ),
2159                 'submit_button'        => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
2160                 'submit_field'         => '<p class="form-submit">%1$s %2$s</p>',
2161                 'format'               => 'xhtml',
2162         );
2163
2164         /**
2165          * Filter the comment form default arguments.
2166          *
2167          * Use 'comment_form_default_fields' to filter the comment fields.
2168          *
2169          * @since 3.0.0
2170          *
2171          * @param array $defaults The default comment form arguments.
2172          */
2173         $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
2174
2175         // Ensure that the filtered args contain all required default values.
2176         $args = array_merge( $defaults, $args );
2177
2178         if ( comments_open( $post_id ) ) : ?>
2179                 <?php
2180                 /**
2181                  * Fires before the comment form.
2182                  *
2183                  * @since 3.0.0
2184                  */
2185                 do_action( 'comment_form_before' );
2186                 ?>
2187                 <div id="respond" class="comment-respond">
2188                         <?php
2189                         echo $args['title_reply_before'];
2190
2191                         comment_form_title( $args['title_reply'], $args['title_reply_to'] );
2192
2193                         echo $args['cancel_reply_before'];
2194
2195                         cancel_comment_reply_link( $args['cancel_reply_link'] );
2196
2197                         echo $args['cancel_reply_after'];
2198
2199                         echo $args['title_reply_after'];
2200
2201                         if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) :
2202                                 echo $args['must_log_in'];
2203                                 /**
2204                                  * Fires after the HTML-formatted 'must log in after' message in the comment form.
2205                                  *
2206                                  * @since 3.0.0
2207                                  */
2208                                 do_action( 'comment_form_must_log_in_after' );
2209                         else : ?>
2210                                 <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="<?php echo esc_attr( $args['class_form'] ); ?>"<?php echo $html5 ? ' novalidate' : ''; ?>>
2211                                         <?php
2212                                         /**
2213                                          * Fires at the top of the comment form, inside the form tag.
2214                                          *
2215                                          * @since 3.0.0
2216                                          */
2217                                         do_action( 'comment_form_top' );
2218
2219                                         if ( is_user_logged_in() ) :
2220                                                 /**
2221                                                  * Filter the 'logged in' message for the comment form for display.
2222                                                  *
2223                                                  * @since 3.0.0
2224                                                  *
2225                                                  * @param string $args_logged_in The logged-in-as HTML-formatted message.
2226                                                  * @param array  $commenter      An array containing the comment author's
2227                                                  *                               username, email, and URL.
2228                                                  * @param string $user_identity  If the commenter is a registered user,
2229                                                  *                               the display name, blank otherwise.
2230                                                  */
2231                                                 echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
2232
2233                                                 /**
2234                                                  * Fires after the is_user_logged_in() check in the comment form.
2235                                                  *
2236                                                  * @since 3.0.0
2237                                                  *
2238                                                  * @param array  $commenter     An array containing the comment author's
2239                                                  *                              username, email, and URL.
2240                                                  * @param string $user_identity If the commenter is a registered user,
2241                                                  *                              the display name, blank otherwise.
2242                                                  */
2243                                                 do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
2244
2245                                         else :
2246
2247                                                 echo $args['comment_notes_before'];
2248
2249                                         endif;
2250
2251                                         // Prepare an array of all fields, including the textarea
2252                                         $comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];
2253
2254                                         /**
2255                                          * Filter the comment form fields.
2256                                          *
2257                                          * @since 4.4.0
2258                                          *
2259                                          * @param array $comment_fields The comment fields.
2260                                          */
2261                                         $comment_fields = apply_filters( 'comment_form_fields', $comment_fields );
2262
2263                                         // Get an array of field names, excluding the textarea
2264                                         $comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );
2265
2266                                         // Get the first and the last field name, excluding the textarea
2267                                         $first_field = reset( $comment_field_keys );
2268                                         $last_field  = end( $comment_field_keys );
2269
2270                                         foreach ( $comment_fields as $name => $field ) {
2271
2272                                                 if ( 'comment' === $name ) {
2273
2274                                                         /**
2275                                                          * Filter the content of the comment textarea field for display.
2276                                                          *
2277                                                          * @since 3.0.0
2278                                                          *
2279                                                          * @param string $args_comment_field The content of the comment textarea field.
2280                                                          */
2281                                                         echo apply_filters( 'comment_form_field_comment', $field );
2282
2283                                                         echo $args['comment_notes_after'];
2284
2285                                                 } elseif ( ! is_user_logged_in() ) {
2286
2287                                                         if ( $first_field === $name ) {
2288                                                                 /**
2289                                                                  * Fires before the comment fields in the comment form, excluding the textarea.
2290                                                                  *
2291                                                                  * @since 3.0.0
2292                                                                  */
2293                                                                 do_action( 'comment_form_before_fields' );
2294                                                         }
2295
2296                                                         /**
2297                                                          * Filter a comment form field for display.
2298                                                          *
2299                                                          * The dynamic portion of the filter hook, `$name`, refers to the name
2300                                                          * of the comment form field. Such as 'author', 'email', or 'url'.
2301                                                          *
2302                                                          * @since 3.0.0
2303                                                          *
2304                                                          * @param string $field The HTML-formatted output of the comment form field.
2305                                                          */
2306                                                         echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
2307
2308                                                         if ( $last_field === $name ) {
2309                                                                 /**
2310                                                                  * Fires after the comment fields in the comment form, excluding the textarea.
2311                                                                  *
2312                                                                  * @since 3.0.0
2313                                                                  */
2314                                                                 do_action( 'comment_form_after_fields' );
2315                                                         }
2316                                                 }
2317                                         }
2318
2319                                         $submit_button = sprintf(
2320                                                 $args['submit_button'],
2321                                                 esc_attr( $args['name_submit'] ),
2322                                                 esc_attr( $args['id_submit'] ),
2323                                                 esc_attr( $args['class_submit'] ),
2324                                                 esc_attr( $args['label_submit'] )
2325                                         );
2326
2327                                         /**
2328                                          * Filter the submit button for the comment form to display.
2329                                          *
2330                                          * @since 4.2.0
2331                                          *
2332                                          * @param string $submit_button HTML markup for the submit button.
2333                                          * @param array  $args          Arguments passed to `comment_form()`.
2334                                          */
2335                                         $submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );
2336
2337                                         $submit_field = sprintf(
2338                                                 $args['submit_field'],
2339                                                 $submit_button,
2340                                                 get_comment_id_fields( $post_id )
2341                                         );
2342
2343                                         /**
2344                                          * Filter the submit field for the comment form to display.
2345                                          *
2346                                          * The submit field includes the submit button, hidden fields for the
2347                                          * comment form, and any wrapper markup.
2348                                          *
2349                                          * @since 4.2.0
2350                                          *
2351                                          * @param string $submit_field HTML markup for the submit field.
2352                                          * @param array  $args         Arguments passed to comment_form().
2353                                          */
2354                                         echo apply_filters( 'comment_form_submit_field', $submit_field, $args );
2355
2356                                         /**
2357                                          * Fires at the bottom of the comment form, inside the closing </form> tag.
2358                                          *
2359                                          * @since 1.5.0
2360                                          *
2361                                          * @param int $post_id The post ID.
2362                                          */
2363                                         do_action( 'comment_form', $post_id );
2364                                         ?>
2365                                 </form>
2366                         <?php endif; ?>
2367                 </div><!-- #respond -->
2368                 <?php
2369                 /**
2370                  * Fires after the comment form.
2371                  *
2372                  * @since 3.0.0
2373                  */
2374                 do_action( 'comment_form_after' );
2375         else :
2376                 /**
2377                  * Fires after the comment form if comments are closed.
2378                  *
2379                  * @since 3.0.0
2380                  */
2381                 do_action( 'comment_form_comments_closed' );
2382         endif;
2383 }