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