3 * Comment template functions
5 * These functions are meant to live inside of the WordPress loop.
12 * Retrieve the author of the current comment.
14 * If the comment has an empty comment_author field, then 'Anonymous' person is
18 * @uses apply_filters() Calls 'get_comment_author' hook on the comment author
20 * @param int $comment_ID The ID of the comment for which to retrieve the author. Optional.
21 * @return string The comment author
23 function get_comment_author( $comment_ID = 0 ) {
24 $comment = get_comment( $comment_ID );
25 if ( empty($comment->comment_author) ) {
26 if (!empty($comment->user_id)){
27 $user=get_userdata($comment->user_id);
28 $author=$user->user_login;
30 $author = __('Anonymous');
33 $author = $comment->comment_author;
35 return apply_filters('get_comment_author', $author);
39 * Displays the author of the current comment.
42 * @uses apply_filters() Calls 'comment_author' on comment author before displaying
44 * @param int $comment_ID The ID of the comment for which to print the author. Optional.
46 function comment_author( $comment_ID = 0 ) {
47 $author = apply_filters('comment_author', get_comment_author( $comment_ID ) );
52 * Retrieve the email of the author of the current comment.
55 * @uses apply_filters() Calls the 'get_comment_author_email' hook on the comment author email
58 * @param int $comment_ID The ID of the comment for which to get the author's email. Optional.
59 * @return string The current comment author's email
61 function get_comment_author_email( $comment_ID = 0 ) {
62 $comment = get_comment( $comment_ID );
63 return apply_filters('get_comment_author_email', $comment->comment_author_email);
67 * Display the email of the author of the current global $comment.
69 * Care should be taken to protect the email address and assure that email
70 * harvesters do not capture your commentors' email address. Most assume that
71 * their email address will not appear in raw form on the blog. Doing so will
72 * enable anyone, including those that people don't want to get the email
73 * address and use it for their own means good and bad.
76 * @uses apply_filters() Calls 'author_email' hook on the author email
78 * @param int $comment_ID The ID of the comment for which to print the author's email. Optional.
80 function comment_author_email( $comment_ID = 0 ) {
81 echo apply_filters('author_email', get_comment_author_email( $comment_ID ) );
85 * Display the html email link to the author of the current comment.
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.
94 * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
95 * @uses get_comment_author_email_link() For generating the link
96 * @global object $comment The current Comment row object
98 * @param string $linktext The text to display instead of the comment author's email address
99 * @param string $before The text or HTML to display before the email link.
100 * @param string $after The text or HTML to display after the email link.
102 function comment_author_email_link($linktext='', $before='', $after='') {
103 if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
108 * Return the html email link to the author of the current comment.
110 * Care should be taken to protect the email address and assure that email
111 * harvesters do not capture your commentors' email address. Most assume that
112 * their email address will not appear in raw form on the blog. Doing so will
113 * enable anyone, including those that people don't want to get the email
114 * address and use it for their own means good and bad.
117 * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
118 * @global object $comment The current Comment row object
120 * @param string $linktext The text to display instead of the comment author's email address
121 * @param string $before The text or HTML to display before the email link.
122 * @param string $after The text or HTML to display after the email link.
124 function get_comment_author_email_link($linktext='', $before='', $after='') {
126 $email = apply_filters('comment_email', $comment->comment_author_email);
127 if ((!empty($email)) && ($email != '@')) {
128 $display = ($linktext != '') ? $linktext : $email;
130 $return .= "<a href='mailto:$email'>$display</a>";
139 * Retrieve the html link to the url of the author of the current comment.
142 * @uses apply_filters() Calls 'get_comment_author_link' hook on the complete link HTML or author
144 * @param int $comment_ID The ID of the comment for which to get the author's link. Optional.
145 * @return string Comment Author name or HTML link for author's URL
147 function get_comment_author_link( $comment_ID = 0 ) {
148 /** @todo Only call these functions when they are needed. Include in if... else blocks */
149 $url = get_comment_author_url( $comment_ID );
150 $author = get_comment_author( $comment_ID );
152 if ( empty( $url ) || 'http://' == $url )
155 $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
156 return apply_filters('get_comment_author_link', $return);
160 * Display the html link to the url of the author of the current comment.
163 * @see get_comment_author_link() Echoes result
165 * @param int The ID of the comment for which to print the author's link. Optional.
167 function comment_author_link( $comment_ID = 0 ) {
168 echo get_comment_author_link( $comment_ID );
172 * Retrieve the IP address of the author of the current comment.
176 * @uses apply_filters()
178 * @param int $comment_ID The ID of the comment for which to get the author's IP address. Optional.
179 * @return string The comment author's IP address.
181 function get_comment_author_IP( $comment_ID = 0 ) {
182 $comment = get_comment( $comment_ID );
183 return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
187 * Display the IP address of the author of the current comment.
190 * @see get_comment_author_IP() Echoes Result
192 * @param int $comment_ID The ID of the comment for which to print the author's IP address. Optional.
194 function comment_author_IP( $comment_ID = 0 ) {
195 echo get_comment_author_IP( $comment_ID );
199 * Retrieve the url of the author of the current comment.
202 * @uses apply_filters() Calls 'get_comment_author_url' hook on the comment author's URL
204 * @param int $comment_ID The ID of the comment for which to get the author's URL. Optional.
207 function get_comment_author_url( $comment_ID = 0 ) {
208 $comment = get_comment( $comment_ID );
209 $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
210 $url = esc_url( $url, array('http', 'https') );
211 return apply_filters('get_comment_author_url', $url);
215 * Display the url of the author of the current comment.
218 * @uses apply_filters()
219 * @uses get_comment_author_url() Retrieves the comment author's URL
221 * @param int $comment_ID The ID of the comment for which to print the author's URL. Optional.
223 function comment_author_url( $comment_ID = 0 ) {
224 echo apply_filters('comment_url', get_comment_author_url( $comment_ID ));
228 * Retrieves the HTML link of the url of the author of the current comment.
230 * $linktext parameter is only used if the URL does not exist for the comment
231 * author. If the URL does exist then the URL will be used and the $linktext
234 * Encapsulate the HTML link between the $before and $after. So it will appear
235 * in the order of $before, link, and finally $after.
238 * @uses apply_filters() Calls the 'get_comment_author_url_link' on the complete HTML before returning.
240 * @param string $linktext The text to display instead of the comment author's email address
241 * @param string $before The text or HTML to display before the email link.
242 * @param string $after The text or HTML to display after the email link.
243 * @return string The HTML link between the $before and $after parameters
245 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
246 $url = get_comment_author_url();
247 $display = ($linktext != '') ? $linktext : $url;
248 $display = str_replace( 'http://www.', '', $display );
249 $display = str_replace( 'http://', '', $display );
250 if ( '/' == substr($display, -1) )
251 $display = substr($display, 0, -1);
252 $return = "$before<a href='$url' rel='external'>$display</a>$after";
253 return apply_filters('get_comment_author_url_link', $return);
257 * Displays the HTML link of the url of the author of the current comment.
260 * @see get_comment_author_url_link() Echoes result
262 * @param string $linktext The text to display instead of the comment author's email address
263 * @param string $before The text or HTML to display before the email link.
264 * @param string $after The text or HTML to display after the email link.
266 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
267 echo get_comment_author_url_link( $linktext, $before, $after );
271 * Generates semantic classes for each comment element
275 * @param string|array $class One or more classes to add to the class list
276 * @param int $comment_id An optional comment ID
277 * @param int $post_id An optional post ID
278 * @param bool $echo Whether comment_class should echo or return
280 function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
281 // Separates classes with a single space, collates classes for comment DIV
282 $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
290 * Returns the classes for the comment div as an array
294 * @param string|array $class One or more classes to add to the class list
295 * @param int $comment_id An optional comment ID
296 * @param int $post_id An optional post ID
297 * @return array Array of classes
299 function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
300 global $comment_alt, $comment_depth, $comment_thread_alt;
302 $comment = get_comment($comment_id);
306 // Get the comment type (comment, trackback),
307 $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
309 // If the comment author has an id (registered), then print the log in name
310 if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
311 // For all registered users, 'byuser'
312 $classes[] = 'byuser';
313 $classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
314 // For comment authors who are the author of the post
315 if ( $post = get_post($post_id) ) {
316 if ( $comment->user_id === $post->post_author )
317 $classes[] = 'bypostauthor';
321 if ( empty($comment_alt) )
323 if ( empty($comment_depth) )
325 if ( empty($comment_thread_alt) )
326 $comment_thread_alt = 0;
328 if ( $comment_alt % 2 ) {
337 // Alt for top-level comments
338 if ( 1 == $comment_depth ) {
339 if ( $comment_thread_alt % 2 ) {
340 $classes[] = 'thread-odd';
341 $classes[] = 'thread-alt';
343 $classes[] = 'thread-even';
345 $comment_thread_alt++;
348 $classes[] = "depth-$comment_depth";
350 if ( !empty($class) ) {
351 if ( !is_array( $class ) )
352 $class = preg_split('#\s+#', $class);
353 $classes = array_merge($classes, $class);
356 $classes = array_map('esc_attr', $classes);
358 return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
362 * Retrieve the comment date of the current comment.
365 * @uses apply_filters() Calls 'get_comment_date' hook with the formated date and the $d parameter respectively
368 * @param string $d The format of the date (defaults to user's config)
369 * @param int $comment_ID The ID of the comment for which to get the date. Optional.
370 * @return string The comment's date
372 function get_comment_date( $d = '', $comment_ID = 0 ) {
373 $comment = get_comment( $comment_ID );
375 $date = mysql2date(get_option('date_format'), $comment->comment_date);
377 $date = mysql2date($d, $comment->comment_date);
378 return apply_filters('get_comment_date', $date, $d);
382 * Display the comment date of the current comment.
386 * @param string $d The format of the date (defaults to user's config)
387 * @param int $comment_ID The ID of the comment for which to print the date. Optional.
389 function comment_date( $d = '', $comment_ID = 0 ) {
390 echo get_comment_date( $d, $comment_ID );
394 * Retrieve the excerpt of the current comment.
396 * Will cut each word and only output the first 20 words with '...' at the end.
397 * If the word count is less than 20, then no truncating is done and no '...'
402 * @uses apply_filters() Calls 'get_comment_excerpt' on truncated comment
404 * @param int $comment_ID The ID of the comment for which to get the excerpt. Optional.
405 * @return string The maybe truncated comment with 20 words or less
407 function get_comment_excerpt( $comment_ID = 0 ) {
408 $comment = get_comment( $comment_ID );
409 $comment_text = strip_tags($comment->comment_content);
410 $blah = explode(' ', $comment_text);
411 if (count($blah) > 20) {
419 for ($i=0; $i<$k; $i++) {
420 $excerpt .= $blah[$i] . ' ';
422 $excerpt .= ($use_dotdotdot) ? '...' : '';
423 return apply_filters('get_comment_excerpt', $excerpt);
427 * Display the excerpt of the current comment.
430 * @uses apply_filters() Calls 'comment_excerpt' hook before displaying excerpt
432 * @param int $comment_ID The ID of the comment for which to print the excerpt. Optional.
434 function comment_excerpt( $comment_ID = 0 ) {
435 echo apply_filters('comment_excerpt', get_comment_excerpt($comment_ID) );
439 * Retrieve the comment id of the current comment.
443 * @uses apply_filters() Calls the 'get_comment_ID' hook for the comment ID
445 * @return int The comment ID
447 function get_comment_ID() {
449 return apply_filters('get_comment_ID', $comment->comment_ID);
453 * Displays the comment id of the current comment.
456 * @see get_comment_ID() Echoes Result
458 function comment_ID() {
459 echo get_comment_ID();
463 * Retrieve the link to a given comment.
468 * @param object|string|int $comment Comment to retrieve.
469 * @param array $args Optional args.
470 * @return string The permalink to the given comment.
472 function get_comment_link( $comment = null, $args = array() ) {
473 global $wp_rewrite, $in_comment_loop;
475 $comment = get_comment($comment);
478 if ( !is_array($args) ) {
481 $args['page'] = $page;
484 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
485 $args = wp_parse_args( $args, $defaults );
487 if ( '' === $args['per_page'] && get_option('page_comments') )
488 $args['per_page'] = get_option('comments_per_page');
490 if ( empty($args['per_page']) ) {
491 $args['per_page'] = 0;
495 if ( $args['per_page'] ) {
496 if ( '' == $args['page'] )
497 $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
499 if ( $wp_rewrite->using_permalinks() )
500 $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
502 $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
504 $link = get_permalink( $comment->comment_post_ID );
507 return apply_filters( 'get_comment_link', $link . '#comment-' . $comment->comment_ID, $comment, $args );
511 * Retrieves the link to the current post comments.
515 * @param int $post_id Optional post id
516 * @return string The link to the comments
518 function get_comments_link($post_id = 0) {
519 return get_permalink($post_id) . '#comments';
523 * Displays the link to the current post comments.
527 * @param string $deprecated Not Used
528 * @param bool $deprecated_2 Not Used
530 function comments_link( $deprecated = '', $deprecated_2 = '' ) {
531 if ( !empty( $deprecated ) )
532 _deprecated_argument( __FUNCTION__, '0.72' );
533 if ( !empty( $deprecated_2 ) )
534 _deprecated_argument( __FUNCTION__, '1.3' );
535 echo get_comments_link();
539 * Retrieve the amount of comments a post has.
542 * @uses apply_filters() Calls the 'get_comments_number' hook on the number of comments
544 * @param int $post_id The Post ID
545 * @return int The number of comments a post has
547 function get_comments_number( $post_id = 0 ) {
549 $post_id = (int) $post_id;
552 $post_id = (int) $id;
554 $post = get_post($post_id);
555 if ( ! isset($post->comment_count) )
558 $count = $post->comment_count;
560 return apply_filters('get_comments_number', $count, $post_id);
564 * Display the language string for the number of comments the current post has.
568 * @uses apply_filters() Calls the 'comments_number' hook on the output and number of comments respectively.
570 * @param string $zero Text for no comments
571 * @param string $one Text for one comment
572 * @param string $more Text for more than one comment
573 * @param string $deprecated Not used.
575 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
578 if ( !empty( $deprecated ) )
579 _deprecated_argument( __FUNCTION__, '1.3' );
581 $number = get_comments_number($id);
584 $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
585 elseif ( $number == 0 )
586 $output = ( false === $zero ) ? __('No Comments') : $zero;
588 $output = ( false === $one ) ? __('1 Comment') : $one;
590 echo apply_filters('comments_number', $output, $number);
594 * Retrieve the text of the current comment.
599 * @param int $comment_ID The ID of the comment for which to get the text. Optional.
600 * @return string The comment content
602 function get_comment_text( $comment_ID = 0 ) {
603 $comment = get_comment( $comment_ID );
604 return apply_filters('get_comment_text', $comment->comment_content);
608 * Displays the text of the current comment.
611 * @uses apply_filters() Passes the comment content through the 'comment_text' hook before display
612 * @uses get_comment_text() Gets the comment content
614 * @param int $comment_ID The ID of the comment for which to print the text. Optional.
616 function comment_text( $comment_ID = 0 ) {
617 echo apply_filters('comment_text', get_comment_text() );
621 * Retrieve the comment time of the current comment.
625 * @uses apply_filter() Calls 'get_comment_time' hook with the formatted time, the $d parameter, and $gmt parameter passed.
627 * @param string $d Optional. The format of the time (defaults to user's config)
628 * @param bool $gmt Whether to use the GMT date
629 * @param bool $translate Whether to translate the time (for use in feeds)
630 * @return string The formatted time
632 function get_comment_time( $d = '', $gmt = false, $translate = true ) {
634 $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
636 $date = mysql2date(get_option('time_format'), $comment_date, $translate);
638 $date = mysql2date($d, $comment_date, $translate);
639 return apply_filters('get_comment_time', $date, $d, $gmt, $translate);
643 * Display the comment time of the current comment.
647 * @param string $d Optional. The format of the time (defaults to user's config)
649 function comment_time( $d = '' ) {
650 echo get_comment_time($d);
654 * Retrieve the comment type of the current comment.
658 * @uses apply_filters() Calls the 'get_comment_type' hook on the comment type
660 * @param int $comment_ID The ID of the comment for which to get the type. Optional.
661 * @return string The comment type
663 function get_comment_type( $comment_ID = 0 ) {
664 $comment = get_comment( $comment_ID );
665 if ( '' == $comment->comment_type )
666 $comment->comment_type = 'comment';
668 return apply_filters('get_comment_type', $comment->comment_type);
672 * Display the comment type of the current comment.
676 * @param string $commenttxt The string to display for comment type
677 * @param string $trackbacktxt The string to display for trackback type
678 * @param string $pingbacktxt The string to display for pingback type
680 function comment_type($commenttxt = false, $trackbacktxt = false, $pingbacktxt = false) {
681 if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
682 if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
683 if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
684 $type = get_comment_type();
698 * Retrieve The current post's trackback URL.
700 * There is a check to see if permalink's have been enabled and if so, will
701 * retrieve the pretty path. If permalinks weren't enabled, the ID of the
702 * current post is used and appended to the correct page to go to.
705 * @uses apply_filters() Calls 'trackback_url' on the resulting trackback URL
708 * @return string The trackback URL after being filtered
710 function get_trackback_url() {
712 if ( '' != get_option('permalink_structure') ) {
713 $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
715 $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . $id;
717 return apply_filters('trackback_url', $tb_url);
721 * Displays the current post's trackback URL.
724 * @uses get_trackback_url() Gets the trackback url for the current post
726 * @param bool $deprecated_echo Remove backwards compat in 2.5
727 * @return void|string Should only be used to echo the trackback URL, use get_trackback_url() for the result instead.
729 function trackback_url( $deprecated_echo = true ) {
730 if ( $deprecated_echo !== true )
731 _deprecated_argument( __FUNCTION__, '2.5', __('Use <code>get_trackback_url()</code> instead if you do not want the value echoed.') );
732 if ( $deprecated_echo )
733 echo get_trackback_url();
735 return get_trackback_url();
739 * Whether the current post is open for comments.
744 * @param int $post_id An optional post ID to check instead of the current post.
745 * @return bool True if the comments are open
747 function comments_open( $post_id=NULL ) {
749 $_post = get_post($post_id);
751 $open = ( 'open' == $_post->comment_status );
752 return apply_filters( 'comments_open', $open, $post_id );
756 * Whether the current post is open for pings.
761 * @param int $post_id An optional post ID to check instead of the current post.
762 * @return bool True if pings are accepted
764 function pings_open( $post_id = NULL ) {
766 $_post = get_post($post_id);
768 $open = ( 'open' == $_post->ping_status );
769 return apply_filters( 'pings_open', $open, $post_id );
773 * Displays form token for unfiltered comments.
775 * Will only display nonce token if the current user has permissions for
776 * unfiltered html. Won't display the token for other users.
778 * The function was backported to 2.0.10 and was added to versions 2.1.3 and
779 * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
780 * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
782 * Backported to 2.0.10.
785 * @uses $post Gets the ID of the current post for the token
787 function wp_comment_form_unfiltered_html_nonce() {
792 $post_id = $post->ID;
794 if ( current_user_can('unfiltered_html') )
795 wp_nonce_field('unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment', false);
799 * Loads the comment template specified in $file.
801 * Will not display the comments template if not on single post or page, or if
802 * the post does not have comments.
804 * Uses the WordPress database object to query for the comments. The comments
805 * are passed through the 'comments_array' filter hook with the list of comments
806 * and the post ID respectively.
808 * The $file path is passed through a filter hook called, 'comments_template'
809 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
810 * first and if it fails it will require the default comment themplate from the
811 * default theme. If either does not exist, then the WordPress process will be
812 * halted. It is advised for that reason, that the default theme is not deleted.
815 * @global array $comment List of comment objects for the current post
819 * @uses $withcomments Will not try to get the comments if the post has none.
821 * @param string $file Optional, default '/comments.php'. The file to load
822 * @param bool $separate_comments Optional, whether to separate the comments by comment type. Default is false.
823 * @return null Returns null if no comments appear
825 function comments_template( $file = '/comments.php', $separate_comments = false ) {
826 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
828 if ( !(is_single() || is_page() || $withcomments) || empty($post) )
832 $file = '/comments.php';
834 $req = get_option('require_name_email');
837 * Comment author information fetched from the comment cookies.
839 * @uses wp_get_current_commenter()
841 $commenter = wp_get_current_commenter();
844 * The name of the current comment author escaped for use in attributes.
846 $comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies()
849 * The email address of the current comment author escaped for use in attributes.
851 $comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies()
854 * The url of the current comment author escaped for use in attributes.
856 $comment_author_url = esc_url($commenter['comment_author_url']);
858 /** @todo Use API instead of SELECTs. */
860 $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));
861 } else if ( empty($comment_author) ) {
862 $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
864 $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));
867 // keep $comments for legacy's sake
868 $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
869 $comments = &$wp_query->comments;
870 $wp_query->comment_count = count($wp_query->comments);
871 update_comment_cache($wp_query->comments);
873 if ( $separate_comments ) {
874 $wp_query->comments_by_type = &separate_comments($comments);
875 $comments_by_type = &$wp_query->comments_by_type;
878 $overridden_cpage = FALSE;
879 if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
880 set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
881 $overridden_cpage = TRUE;
884 if ( !defined('COMMENTS_TEMPLATE') || !COMMENTS_TEMPLATE)
885 define('COMMENTS_TEMPLATE', true);
887 $include = apply_filters('comments_template', STYLESHEETPATH . $file );
888 if ( file_exists( $include ) )
890 elseif ( file_exists( TEMPLATEPATH . $file ) )
891 require( TEMPLATEPATH . $file );
892 else // Backward compat code will be removed in a future release
893 require( WPINC . '/theme-compat/comments.php');
897 * Displays the JS popup script to show a comment.
899 * If the $file parameter is empty, then the home page is assumed. The defaults
900 * for the window are 400px by 400px.
902 * For the comment link popup to work, this function has to be called or the
903 * normal comment link will be assumed.
906 * @global string $wpcommentspopupfile The URL to use for the popup window
907 * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called
909 * @param int $width Optional. The width of the popup window
910 * @param int $height Optional. The height of the popup window
911 * @param string $file Optional. Sets the location of the popup window
913 function comments_popup_script($width=400, $height=400, $file='') {
914 global $wpcommentspopupfile, $wpcommentsjavascript;
917 $wpcommentspopupfile = ''; // Use the index.
919 $wpcommentspopupfile = $file;
922 $wpcommentsjavascript = 1;
923 $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";
928 * Displays the link to the comments popup window for the current post ID.
930 * Is not meant to be displayed on single posts and pages. Should be used on the
935 * @uses $wpcommentspopupfile
936 * @uses $wpcommentsjavascript
939 * @param string $zero The string to display when no comments
940 * @param string $one The string to display when only one comment is available
941 * @param string $more The string to display when there are more than one comment
942 * @param string $css_class The CSS class to use for comments
943 * @param string $none The string to display when comments have been turned off
944 * @return null Returns null on single posts and pages.
946 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
947 global $id, $wpcommentspopupfile, $wpcommentsjavascript;
949 if ( false === $zero ) $zero = __( 'No Comments' );
950 if ( false === $one ) $one = __( '1 Comment' );
951 if ( false === $more ) $more = __( '% Comments' );
952 if ( false === $none ) $none = __( 'Comments Off' );
954 $number = get_comments_number( $id );
956 if ( 0 == $number && !comments_open() && !pings_open() ) {
957 echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
961 if ( post_password_required() ) {
962 echo __('Enter your password to view comments.');
967 if ( $wpcommentsjavascript ) {
968 if ( empty( $wpcommentspopupfile ) )
971 $home = get_option('siteurl');
972 echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
973 echo '" onclick="wpopen(this.href); return false"';
974 } else { // if comments_popup_script() is not in the template, display simple comment link
976 echo get_permalink() . '#respond';
982 if ( !empty( $css_class ) ) {
983 echo ' class="'.$css_class.'" ';
985 $title = the_title_attribute( array('echo' => 0 ) );
987 echo apply_filters( 'comments_popup_link_attributes', '' );
989 echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
990 comments_number( $zero, $one, $more );
995 * Retrieve HTML content for reply to comment link.
997 * The default arguments that can be override are 'add_below', 'respond_id',
998 * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
999 * used, if the user must log in or register first before posting a comment. The
1000 * 'reply_text' will be used, if they can post a reply. The 'add_below' and
1001 * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
1006 * @param array $args Optional. Override default options.
1007 * @param int $comment Optional. Comment being replied to.
1008 * @param int $post Optional. Post that the comment is going to be displayed on.
1009 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1011 function get_comment_reply_link($args = array(), $comment = null, $post = null) {
1014 $defaults = array('add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __('Reply'),
1015 'login_text' => __('Log in to Reply'), 'depth' => 0, 'before' => '', 'after' => '');
1017 $args = wp_parse_args($args, $defaults);
1019 if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
1022 extract($args, EXTR_SKIP);
1024 $comment = get_comment($comment);
1026 $post = $comment->comment_post_ID;
1027 $post = get_post($post);
1029 if ( !comments_open($post->ID) )
1034 if ( get_option('comment_registration') && !$user_ID )
1035 $link = '<a rel="nofollow" class="comment-reply-login" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . $login_text . '</a>';
1037 $link = "<a rel='nofollow' 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>";
1038 return apply_filters('comment_reply_link', $before . $link . $after, $args, $comment, $post);
1042 * Displays the HTML content for reply to comment link.
1045 * @see get_comment_reply_link() Echoes result
1047 * @param array $args Optional. Override default options.
1048 * @param int $comment Optional. Comment being replied to.
1049 * @param int $post Optional. Post that the comment is going to be displayed on.
1050 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1052 function comment_reply_link($args = array(), $comment = null, $post = null) {
1053 echo get_comment_reply_link($args, $comment, $post);
1057 * Retrieve HTML content for reply to post link.
1059 * The default arguments that can be override are 'add_below', 'respond_id',
1060 * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
1061 * used, if the user must log in or register first before posting a comment. The
1062 * 'reply_text' will be used, if they can post a reply. The 'add_below' and
1063 * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
1068 * @param array $args Optional. Override default options.
1069 * @param int|object $post Optional. Post that the comment is going to be displayed on. Defaults to current post.
1070 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1072 function get_post_reply_link($args = array(), $post = null) {
1075 $defaults = array('add_below' => 'post', 'respond_id' => 'respond', 'reply_text' => __('Leave a Comment'),
1076 'login_text' => __('Log in to leave a Comment'), 'before' => '', 'after' => '');
1078 $args = wp_parse_args($args, $defaults);
1079 extract($args, EXTR_SKIP);
1080 $post = get_post($post);
1082 if ( !comments_open($post->ID) )
1085 if ( get_option('comment_registration') && !$user_ID ) {
1086 $link = '<a rel="nofollow" href="' . wp_login_url( get_permalink() ) . '">' . $login_text . '</a>';
1088 $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>";
1090 return apply_filters('post_comments_link', $before . $link . $after, $post);
1094 * Displays the HTML content for reply to post link.
1096 * @see get_post_reply_link()
1098 * @param array $args Optional. Override default options.
1099 * @param int|object $post Optional. Post that the comment is going to be displayed on.
1100 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1102 function post_reply_link($args = array(), $post = null) {
1103 echo get_post_reply_link($args, $post);
1107 * Retrieve HTML content for cancel comment reply link.
1111 * @param string $text Optional. Text to display for cancel reply link.
1113 function get_cancel_comment_reply_link($text = '') {
1115 $text = __('Click here to cancel reply.');
1117 $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
1118 $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
1119 return apply_filters('cancel_comment_reply_link', '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>', $link, $text);
1123 * Display HTML content for cancel comment reply link.
1127 * @param string $text Optional. Text to display for cancel reply link.
1129 function cancel_comment_reply_link($text = '') {
1130 echo get_cancel_comment_reply_link($text);
1134 * Retrieve hidden input HTML for replying to comments.
1138 * @return string Hidden input HTML for replying to comments
1140 function get_comment_id_fields() {
1143 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1144 $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
1145 $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
1146 return apply_filters('comment_id_fields', $result, $id, $replytoid);
1150 * Output hidden input HTML for replying to comments.
1153 * @see get_comment_id_fields() Echoes result
1155 function comment_id_fields() {
1156 echo get_comment_id_fields();
1160 * Display text based on comment reply status. Only affects users with Javascript disabled.
1164 * @param string $noreplytext Optional. Text to display when not replying to a comment.
1165 * @param string $replytext Optional. Text to display when replying to a comment. Accepts "%s" for the author of the comment being replied to.
1166 * @param string $linktoparent Optional. Boolean to control making the author's name a link to their comment.
1168 function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = TRUE ) {
1171 if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
1172 if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
1174 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1176 if ( 0 == $replytoid )
1179 $comment = get_comment($replytoid);
1180 $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author();
1181 printf( $replytext, $author );
1186 * HTML comment list class.
1188 * @package WordPress
1192 class Walker_Comment extends Walker {
1194 * @see Walker::$tree_type
1198 var $tree_type = 'comment';
1201 * @see Walker::$db_fields
1205 var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
1208 * @see Walker::start_lvl()
1211 * @param string $output Passed by reference. Used to append additional content.
1212 * @param int $depth Depth of comment.
1213 * @param array $args Uses 'style' argument for type of HTML list.
1215 function start_lvl(&$output, $depth, $args) {
1216 $GLOBALS['comment_depth'] = $depth + 1;
1218 switch ( $args['style'] ) {
1222 echo "<ol class='children'>\n";
1226 echo "<ul class='children'>\n";
1232 * @see Walker::end_lvl()
1235 * @param string $output Passed by reference. Used to append additional content.
1236 * @param int $depth Depth of comment.
1237 * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
1239 function end_lvl(&$output, $depth, $args) {
1240 $GLOBALS['comment_depth'] = $depth + 1;
1242 switch ( $args['style'] ) {
1256 * This function is designed to enhance Walker::display_element() to
1257 * display children of higher nesting levels than selected inline on
1258 * the highest depth level displayed. This prevents them being orphaned
1259 * at the end of the comment list.
1261 * Example: max_depth = 2, with 5 levels of nested content.
1273 function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
1278 $id_field = $this->db_fields['id'];
1279 $id = $element->$id_field;
1281 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
1283 // If we're at the max depth, and the current element still has children, loop over those and display them at this level
1284 // This is to prevent them being orphaned to the end of the list.
1285 if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
1286 foreach ( $children_elements[ $id ] as $child )
1287 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
1289 unset( $children_elements[ $id ] );
1295 * @see Walker::start_el()
1298 * @param string $output Passed by reference. Used to append additional content.
1299 * @param object $comment Comment data object.
1300 * @param int $depth Depth of comment in reference to parents.
1301 * @param array $args
1303 function start_el(&$output, $comment, $depth, $args) {
1305 $GLOBALS['comment_depth'] = $depth;
1307 if ( !empty($args['callback']) ) {
1308 call_user_func($args['callback'], $comment, $args, $depth);
1312 $GLOBALS['comment'] = $comment;
1313 extract($args, EXTR_SKIP);
1315 if ( 'div' == $args['style'] ) {
1317 $add_below = 'comment';
1320 $add_below = 'div-comment';
1323 <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
1324 <?php if ( 'div' != $args['style'] ) : ?>
1325 <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
1327 <div class="comment-author vcard">
1328 <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
1329 <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
1331 <?php if ($comment->comment_approved == '0') : ?>
1332 <em><?php _e('Your comment is awaiting moderation.') ?></em>
1336 <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
1338 /* translators: 1: date, 2: time */
1339 printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','' );
1343 <?php comment_text() ?>
1346 <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
1348 <?php if ( 'div' != $args['style'] ) : ?>
1355 * @see Walker::end_el()
1358 * @param string $output Passed by reference. Used to append additional content.
1359 * @param object $comment
1360 * @param int $depth Depth of comment.
1361 * @param array $args
1363 function end_el(&$output, $comment, $depth, $args) {
1364 if ( !empty($args['end-callback']) ) {
1365 call_user_func($args['end-callback'], $comment, $args, $depth);
1368 if ( 'div' == $args['style'] )
1379 * Used in the comments.php template to list comments for a particular post
1382 * @uses Walker_Comment
1384 * @param string|array $args Formatting options
1385 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
1387 function wp_list_comments($args = array(), $comments = null ) {
1388 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
1390 $in_comment_loop = true;
1392 $comment_alt = $comment_thread_alt = 0;
1395 $defaults = array('walker' => null, 'max_depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all',
1396 'page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => null, 'reverse_children' => '');
1398 $r = wp_parse_args( $args, $defaults );
1400 // Figure out what comments we'll be looping through ($_comments)
1401 if ( null !== $comments ) {
1402 $comments = (array) $comments;
1403 if ( empty($comments) )
1405 if ( 'all' != $r['type'] ) {
1406 $comments_by_type = &separate_comments($comments);
1407 if ( empty($comments_by_type[$r['type']]) )
1409 $_comments = $comments_by_type[$r['type']];
1411 $_comments = $comments;
1414 if ( empty($wp_query->comments) )
1416 if ( 'all' != $r['type'] ) {
1417 if ( empty($wp_query->comments_by_type) )
1418 $wp_query->comments_by_type = &separate_comments($wp_query->comments);
1419 if ( empty($wp_query->comments_by_type[$r['type']]) )
1421 $_comments = $wp_query->comments_by_type[$r['type']];
1423 $_comments = $wp_query->comments;
1427 if ( '' === $r['per_page'] && get_option('page_comments') )
1428 $r['per_page'] = get_query_var('comments_per_page');
1430 if ( empty($r['per_page']) ) {
1435 if ( '' === $r['max_depth'] ) {
1436 if ( get_option('thread_comments') )
1437 $r['max_depth'] = get_option('thread_comments_depth');
1439 $r['max_depth'] = -1;
1442 if ( '' === $r['page'] ) {
1443 if ( empty($overridden_cpage) ) {
1444 $r['page'] = get_query_var('cpage');
1446 $threaded = ( -1 != $r['max_depth'] );
1447 $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
1448 set_query_var( 'cpage', $r['page'] );
1452 $r['page'] = intval($r['page']);
1453 if ( 0 == $r['page'] && 0 != $r['per_page'] )
1456 if ( null === $r['reverse_top_level'] )
1457 $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
1459 extract( $r, EXTR_SKIP );
1461 if ( empty($walker) )
1462 $walker = new Walker_Comment;
1464 $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
1465 $wp_query->max_num_comment_pages = $walker->max_pages;
1467 $in_comment_loop = false;
1471 * Outputs a complete commenting form for use within a template.
1472 * Most strings and form fields may be controlled through the $args array passed
1473 * into the function, while you may also choose to use the comment_form_default_fields
1474 * filter to modify the array of default fields if you'd just like to add a new
1475 * one or remove a single field. All fields are also individually passed through
1476 * a filter of the form comment_form_field_$name where $name is the key used
1477 * in the array of fields.
1480 * @param array $args Options for strings, fields etc in the form
1481 * @param mixed $post_id Post ID to generate the form for, uses the current post if null
1484 function comment_form( $args = array(), $post_id = null ) {
1485 global $user_identity, $id;
1487 if ( null === $post_id )
1492 $commenter = wp_get_current_commenter();
1494 $req = get_option( 'require_name_email' );
1495 $aria_req = ( $req ? " aria-required='true'" : '' );
1497 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
1498 '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
1499 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
1500 '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
1501 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
1502 '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
1505 $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
1507 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
1508 '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>',
1509 '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>',
1510 '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>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
1511 'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
1512 '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>',
1513 'id_form' => 'commentform',
1514 'id_submit' => 'submit',
1515 'title_reply' => __( 'Leave a Reply' ),
1516 'title_reply_to' => __( 'Leave a Reply to %s' ),
1517 'cancel_reply_link' => __( 'Cancel reply' ),
1518 'label_submit' => __( 'Post Comment' ),
1521 $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
1524 <?php if ( comments_open() ) : ?>
1525 <?php do_action( 'comment_form_before' ); ?>
1527 <h3 id="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>
1528 <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
1529 <?php echo $args['must_log_in']; ?>
1530 <?php do_action( 'comment_form_must_log_in_after' ); ?>
1532 <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>">
1533 <?php do_action( 'comment_form_top' ); ?>
1534 <?php if ( is_user_logged_in() ) : ?>
1535 <?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
1536 <?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
1538 <?php echo $args['comment_notes_before']; ?>
1540 do_action( 'comment_form_before_fields' );
1541 foreach ( (array) $args['fields'] as $name => $field ) {
1542 echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
1544 do_action( 'comment_form_after_fields' );
1547 <?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
1548 <?php echo $args['comment_notes_after']; ?>
1549 <p class="form-submit">
1550 <input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
1551 <?php comment_id_fields(); ?>
1553 <?php do_action( 'comment_form', $post_id ); ?>
1556 </div><!-- #respond -->
1557 <?php do_action( 'comment_form_after' ); ?>
1559 <?php do_action( 'comment_form_comments_closed' ); ?>