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