root/trunk/wp-includes/comment-template.php

Revision 1522, 39.0 kB (checked in by donncha, 3 days ago)

WP Merge with revision 9730

Line 
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  * @uses apply_filters() Calls 'get_comment_author' hook on the comment author
19  *
20  * @return string The comment author
21  */
22 function get_comment_author() {
23     global $comment;
24     if ( empty($comment->comment_author) )
25         $author = __('Anonymous');
26     else
27         $author = $comment->comment_author;
28     return apply_filters('get_comment_author', $author);
29 }
30
31 /**
32  * Displays the author of the current comment.
33  *
34  * @since 0.71
35  * @uses apply_filters() Calls 'comment_author' on comment author before displaying
36  */
37 function comment_author() {
38     $author = apply_filters('comment_author', get_comment_author() );
39     echo $author;
40 }
41
42 /**
43  * Retrieve the email of the author of the current comment.
44  *
45  * @since 1.5.0
46  * @uses apply_filters() Calls the 'get_comment_author_email' hook on the comment author email
47  * @uses $comment
48  *
49  * @return string The current comment author's email
50  */
51 function get_comment_author_email() {
52     global $comment;
53     return apply_filters('get_comment_author_email', $comment->comment_author_email);
54 }
55
56 /**
57  * Display the email of the author of the current global $comment.
58  *
59  * Care should be taken to protect the email address and assure that email
60  * harvesters do not capture your commentors' email address. Most assume that
61  * their email address will not appear in raw form on the blog. Doing so will
62  * enable anyone, including those that people don't want to get the email
63  * address and use it for their own means good and bad.
64  *
65  * @since 0.71
66  * @uses apply_filters() Calls 'author_email' hook on the author email
67  */
68 function comment_author_email() {
69     echo apply_filters('author_email', get_comment_author_email() );
70 }
71
72 /**
73  * Display the html email link to the author of the current comment.
74  *
75  * Care should be taken to protect the email address and assure that email
76  * harvesters do not capture your commentors' email address. Most assume that
77  * their email address will not appear in raw form on the blog. Doing so will
78  * enable anyone, including those that people don't want to get the email
79  * address and use it for their own means good and bad.
80  *
81  * @since 0.71
82  * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
83  * @uses get_comment_author_email_link() For generating the link
84  * @global object $comment The current Comment row object
85  *
86  * @param string $linktext The text to display instead of the comment author's email address
87  * @param string $before The text or HTML to display before the email link.
88  * @param string $after The text or HTML to display after the email link.
89  */
90 function comment_author_email_link($linktext='', $before='', $after='') {
91     if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
92         echo $link;
93 }
94
95 /**
96  * Return the html email link to the author of the current comment.
97  *
98  * Care should be taken to protect the email address and assure that email
99  * harvesters do not capture your commentors' email address. Most assume that
100  * their email address will not appear in raw form on the blog. Doing so will
101  * enable anyone, including those that people don't want to get the email
102  * address and use it for their own means good and bad.
103  *
104  * @since 2.7
105  * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
106  * @global object $comment The current Comment row object
107  *
108  * @param string $linktext The text to display instead of the comment author's email address
109  * @param string $before The text or HTML to display before the email link.
110  * @param string $after The text or HTML to display after the email link.
111  */
112 function get_comment_author_email_link($linktext='', $before='', $after='') {
113     global $comment;
114     $email = apply_filters('comment_email', $comment->comment_author_email);
115     if ((!empty($email)) && ($email != '@')) {
116     $display = ($linktext != '') ? $linktext : $email;
117         $return  = $before;
118         $return .= "<a href='mailto:$email'>$display</a>";
119          $return .= $after;
120         return $return;
121     } else {
122         return '';
123     }
124 }
125
126 /**
127  * Retrieve the html link to the url of the author of the current comment.
128  *
129  * @since 1.5.0
130  * @uses apply_filters() Calls 'get_comment_author_link' hook on the complete link HTML or author
131  *
132  * @return string Comment Author name or HTML link for author's URL
133  */
134 function get_comment_author_link() {
135     /** @todo Only call these functions when they are needed. Include in if... else blocks */
136     $url    = get_comment_author_url();
137     $author = get_comment_author();
138
139     if ( empty( $url ) || 'http://' == $url )
140         $return = $author;
141     else
142         $return = "<a href='$url' rel='external nofollow'>$author</a>";
143     return apply_filters('get_comment_author_link', $return);
144 }
145
146 /**
147  * Display the html link to the url of the author of the current comment.
148  *
149  * @since 0.71
150  * @see get_comment_author_link() Echos result
151  */
152 function comment_author_link() {
153     echo get_comment_author_link();
154 }
155
156 /**
157  * Retrieve the IP address of the author of the current comment.
158  *
159  * @since 1.5.0
160  * @uses $comment
161  * @uses apply_filters()
162  *
163  * @return unknown
164  */
165 function get_comment_author_IP() {
166     global $comment;
167     return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
168 }
169
170 /**
171  * Display the IP address of the author of the current comment.
172  *
173  * @since 0.71
174  * @see get_comment_author_IP() Echos Result
175  */
176 function comment_author_IP() {
177     echo get_comment_author_IP();
178 }
179
180 /**
181  * Retrieve the url of the author of the current comment.
182  *
183  * @since 1.5.0
184  * @uses apply_filters() Calls 'get_comment_author_url' hook on the comment author's URL
185  *
186  * @return string
187  */
188 function get_comment_author_url() {
189     global $comment;
190     return apply_filters('get_comment_author_url', $comment->comment_author_url);
191 }
192
193 /**
194  * Display the url of the author of the current comment.
195  *
196  * @since 0.71
197  * @uses apply_filters()
198  * @uses get_comment_author_url() Retrieves the comment author's URL
199  */
200 function comment_author_url() {
201     echo apply_filters('comment_url', get_comment_author_url());
202 }
203
204 /**
205  * Retrieves the HTML link of the url of the author of the current comment.
206  *
207  * $linktext parameter is only used if the URL does not exist for the comment
208  * author. If the URL does exist then the URL will be used and the $linktext
209  * will be ignored.
210  *
211  * Encapsulate the HTML link between the $before and $after. So it will appear
212  * in the order of $before, link, and finally $after.
213  *
214  * @since 1.5.0
215  * @uses apply_filters() Calls the 'get_comment_author_url_link' on the complete HTML before returning.
216  *
217  * @param string $linktext The text to display instead of the comment author's email address
218  * @param string $before The text or HTML to display before the email link.
219  * @param string $after The text or HTML to display after the email link.
220  * @return string The HTML link between the $before and $after parameters
221  */
222 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
223     $url = get_comment_author_url();
224     $display = ($linktext != '') ? $linktext : $url;
225     $display = str_replace( 'http://www.', '', $display );
226     $display = str_replace( 'http://', '', $display );
227     if ( '/' == substr($display, -1) )
228         $display = substr($display, 0, -1);
229     $return = "$before<a href='$url' rel='external'>$display</a>$after";
230     return apply_filters('get_comment_author_url_link', $return);
231 }
232
233 /**
234  * Displays the HTML link of the url of the author of the current comment.
235  *
236  * @since 0.71
237  * @see get_comment_author_url_link() Echos result
238  *
239  * @param string $linktext The text to display instead of the comment author's email address
240  * @param string $before The text or HTML to display before the email link.
241  * @param string $after The text or HTML to display after the email link.
242  */
243 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
244     echo get_comment_author_url_link( $linktext, $before, $after );
245 }
246
247 /**
248  * Generates semantic classes for each comment element
249  *
250  * @since 2.7.0
251  *
252  * @param string|array $class One or more classes to add to the class list
253  * @param int $comment_id An optional comment ID
254  * @param int $post_id An optional post ID
255  * @param bool $echo Whether comment_class should echo or return
256  */
257 function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
258     // Separates classes with a single space, collates classes for comment DIV
259     $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
260     if ( $echo)
261         echo $class;
262     else
263         return $class;
264 }
265
266 /**
267  * Returns the classes for the comment div as an array
268  *
269  * @since 2.7.0
270  *
271  * @param string|array $class One or more classes to add to the class list
272  * @param int $comment_id An optional comment ID
273  * @param int $post_id An optional post ID
274  * @return array Array of classes
275  */
276 function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
277     global $comment_alt, $comment_depth, $comment_thread_alt;
278
279     $comment = get_comment($comment_id);
280
281     $classes = array();
282
283     // Get the comment type (comment, trackback),
284     $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
285
286     // If the comment author has an id (registered), then print the log in name
287     if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
288         // For all registered users, 'byuser'
289         $classes[] = 'byuser comment-author-' . $user->user_nicename;
290         // For comment authors who are the author of the post
291         if ( $post = get_post($post_id) ) {
292             if ( $comment->user_id === $post->post_author )
293                 $classes[] = 'bypostauthor';
294         }
295     }
296
297     if ( empty($comment_alt) )
298         $comment_alt = 0;
299     if ( empty($comment_depth) )
300         $comment_depth = 1;
301     if ( empty($comment_thread_alt) )
302         $comment_thread_alt = 0;
303
304     if ( $comment_alt % 2 ) {
305         $classes[] = 'odd';
306         $classes[] = 'alt';
307     } else {
308         $classes[] = 'even';
309     }
310
311     $comment_alt++;
312
313     // Alt for top-level comments
314     if ( 1 == $comment_depth ) {
315         if ( $comment_thread_alt % 2 ) {
316             $classes[] = 'thread-odd';
317             $classes[] = 'thread-alt';
318         } else {
319             $classes[] = 'thread-even';
320         }
321         $comment_thread_alt++;
322     }
323
324     $classes[] = "depth-$comment_depth";
325
326     if ( !empty($class) ) {
327         if ( !is_array( $class ) )
328             $class = preg_split('#\s+#', $class);
329         $classes = array_merge($classes, $class);
330     }
331
332     return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
333 }
334
335 /**
336  * Retrieve the comment date of the current comment.
337  *
338  * @since 1.5.0
339  * @uses apply_filters() Calls 'get_comment_date' hook with the formated date and the $d parameter respectively
340  * @uses $comment
341  *
342  * @param string $d The format of the date (defaults to user's config)
343  * @return string The comment's date
344  */
345 function get_comment_date( $d = '' ) {
346     global $comment;
347     if ( '' == $d )
348         $date = mysql2date( get_option('date_format'), $comment->comment_date);
349     else
350         $date = mysql2date($d, $comment->comment_date);
351     return apply_filters('get_comment_date', $date, $d);
352 }
353
354 /**
355  * Display the comment date of the current comment.
356  *
357  * @since 0.71
358  *
359  * @param string $d The format of the date (defaults to user's config)
360  */
361 function comment_date( $d = '' ) {
362     echo get_comment_date( $d );
363 }
364
365 /**
366  * Retrieve the excerpt of the current comment.
367  *
368  * Will cut each word and only output the first 20 words with '...' at the end.
369  * If the word count is less than 20, then no truncating is done and no '...'
370  * will appear.
371  *
372  * @since 1.5.0
373  * @uses $comment
374  * @uses apply_filters() Calls 'get_comment_excerpt' on truncated comment
375  *
376  * @return string The maybe truncated comment with 20 words or less
377  */
378 function get_comment_excerpt() {
379     global $comment;
380     $comment_text = strip_tags($comment->comment_content);
381     $blah = explode(' ', $comment_text);
382     if (count($blah) > 20) {
383         $k = 20;
384         $use_dotdotdot = 1;
385     } else {
386         $k = count($blah);
387         $use_dotdotdot = 0;
388     }
389     $excerpt = '';
390     for ($i=0; $i<$k; $i++) {
391         $excerpt .= $blah[$i] . ' ';
392     }
393     $excerpt .= ($use_dotdotdot) ? '...' : '';
394     return apply_filters('get_comment_excerpt', $excerpt);
395 }
396
397 /**
398  * Display the excerpt of the current comment.
399  *
400  * @since 1.2.0
401  * @uses apply_filters() Calls 'comment_excerpt' hook before displaying excerpt
402  */
403 function comment_excerpt() {
404     echo apply_filters('comment_excerpt', get_comment_excerpt() );
405 }
406
407 /**
408  * Retrieve the comment id of the current comment.
409  *
410  * @since 1.5.0
411  * @uses $comment
412  * @uses apply_filters() Calls the 'get_comment_ID' hook for the comment ID
413  *
414  * @return int The comment ID
415  */
416 function get_comment_ID() {
417     global $comment;
418     return apply_filters('get_comment_ID', $comment->comment_ID);
419 }
420
421 /**
422  * Displays the comment id of the current comment.
423  *
424  * @since 0.71
425  * @see get_comment_ID() Echos Result
426  */
427 function comment_ID() {
428     echo get_comment_ID();
429 }
430
431 /**
432  * Retrieve the link to a given comment.
433  *
434  * @since 1.5.0
435  * @uses $comment
436  *
437  * @param object|string|int $comment Comment to retrieve.
438  * @param string|int $page The comment's page if known. Optional. Avoids extra database query.
439  * @return string The permalink to the current comment
440  */
441 function get_comment_link( $comment = null, $page = null ) {
442     global $wp_rewrite;
443
444     $comment = get_comment($comment);
445
446     if ( get_option('page_comments') ) {
447         $page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID );
448
449         if ( $wp_rewrite->using_permalinks() )
450             return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . "comment-page-$page", 'comment' ) . '#comment-' . $comment->comment_ID;
451         else
452             return add_query_arg( 'cpage', $page, get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
453     } else {
454         return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
455     }
456 }
457
458 /**
459  * Retrieves the link to the current post comments.
460  *
461  * @since 1.5.0
462  *
463  * @return string The link to the comments
464  */
465 function get_comments_link() {
466     return get_permalink() . '#comments';
467 }
468
469 /**
470  * Displays the link to the current post comments.
471  *
472  * @since 0.71
473  *
474  * @param string $deprecated Not Used
475  * @param bool $deprecated Not Used
476  */
477 function comments_link( $deprecated = '', $deprecated = '' ) {
478     echo get_comments_link();
479 }
480
481 /**
482  * Retrieve the amount of comments a post has.
483  *
484  * @since 1.5.0
485  * @uses apply_filters() Calls the 'get_comments_number' hook on the number of comments
486  *
487  * @param int $post_id The Post ID
488  * @return int The number of comments a post has
489  */
490 function get_comments_number( $post_id = 0 ) {
491     global $id;
492     $post_id = (int) $post_id;
493
494     if ( !$post_id )
495         $post_id = (int) $id;
496
497     $post = get_post($post_id);
498     if ( ! isset($post->comment_count) )
499         $count = 0;
500     else
501         $count = $post->comment_count;
502
503     return apply_filters('get_comments_number', $count);
504 }
505
506 /**
507  * Display the language string for the number of comments the current post has.
508  *
509  * @since 0.71
510  * @uses $id
511  * @uses apply_filters() Calls the 'comments_number' hook on the output and number of comments respectively.
512  *
513  * @param string $zero Text for no comments
514  * @param string $one Text for one comment
515  * @param string $more Text for more than one comment
516  * @param string $deprecated Not used.
517  */
518 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
519     global $id;
520     $number = get_comments_number($id);
521
522     if ( $number > 1 )
523         $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
524     elseif ( $number == 0 )
525         $output = ( false === $zero ) ? __('No Comments') : $zero;
526     else // must be one
527         $output = ( false === $one ) ? __('1 Comment') : $one;
528
529     echo apply_filters('comments_number', $output, $number);
530 }
531
532 /**
533  * Retrieve the text of the current comment.
534  *
535  * @since 1.5.0
536  * @uses $comment
537  *
538  * @return string The comment content
539  */
540 function get_comment_text() {
541     global $comment;
542     return apply_filters('get_comment_text', $comment->comment_content);
543 }
544
545 /**
546  * Displays the text of the current comment.
547  *
548  * @since 0.71
549  * @uses apply_filters() Passes the comment content through the 'comment_text' hook before display
550  * @uses get_comment_text() Gets the comment content
551  */
552 function comment_text() {
553     echo apply_filters('comment_text', get_comment_text() );
554 }
555
556 /**
557  * Retrieve the comment time of the current comment.
558  *
559  * @since 1.5.0
560  * @uses $comment
561  * @uses apply_filter() Calls 'get_comment_time' hook with the formatted time, the $d parameter, and $gmt parameter passed.
562  *
563  * @param string $d Optional. The format of the time (defaults to user's config)
564  * @param bool $gmt Whether to use the GMT date
565  * @return string The formatted time
566  */
567 function get_comment_time( $d = '', $gmt = false ) {
568     global $comment;
569     $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
570     if ( '' == $d )
571         $date = mysql2date(get_option('time_format'), $comment_date);
572     else
573         $date = mysql2date($d, $comment_date);
574     return apply_filters('get_comment_time', $date, $d, $gmt);
575 }
576
577 /**
578  * Display the comment time of the current comment.
579  *
580  * @since 0.71
581  *
582  * @param string $d Optional. The format of the time (defaults to user's config)
583  */
584 function comment_time( $d = '' ) {
585     echo get_comment_time($d);
586 }
587
588 /**
589  * Retrieve the comment type of the current comment.
590  *
591  * @since 1.5.0
592  * @uses $comment
593  * @uses apply_filters() Calls the 'get_comment_type' hook on the comment type
594  *
595  * @return string The comment type
596  */
597 function get_comment_type() {
598     global $comment;
599
600     if ( '' == $comment->comment_type )
601         $comment->comment_type = 'comment';
602
603     return apply_filters('get_comment_type', $comment->comment_type);
604 }
605
606 /**
607  * Display the comment type of the current comment.
608  *
609  * @since 0.71
610  *
611  * @param string $commenttxt The string to display for comment type
612  * @param string $trackbacktxt The string to display for trackback type
613  * @param string $pingbacktxt The string to display for pingback type
614  */
615 function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
616     $type = get_comment_type();
617     switch( $type ) {
618         case 'trackback' :
619             echo $trackbacktxt;
620             break;
621         case 'pingback' :
622             echo $pingbacktxt;
623             break;
624         default :
625             echo $commenttxt;
626     }
627 }
628
629 /**
630  * Retrieve The current post's trackback URL.
631  *
632  * There is a check to see if permalink's have been enabled and if so, will
633  * retrieve the pretty path. If permalinks weren't enabled, the ID of the
634  * current post is used and appended to the correct page to go to.
635  *
636  * @since 1.5.0
637  * @uses apply_filters() Calls 'trackback_url' on the resulting trackback URL
638  * @uses $id
639  *
640  * @return string The trackback URL after being filtered
641  */
642 function get_trackback_url() {
643     global $id;
644     if ( '' != get_option('permalink_structure') ) {
645         $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
646     } else {
647         $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . $id;
648     }
649     return apply_filters('trackback_url', $tb_url);
650 }
651
652 /**
653  * Displays the current post's trackback URL.
654  *
655  * @since 0.71
656  * @uses get_trackback_url() Gets the trackback url for the current post
657  *
658  * @param bool $deprecated Remove backwards compat in 2.5
659  * @return void|string Should only be used to echo the trackback URL, use get_trackback_url() for the result instead.
660  */
661 function trackback_url($deprecated = true) {
662     if ($deprecated) echo get_trackback_url();
663     else return get_trackback_url();
664 }
665
666 /**
667  * Generates and displays the RDF for the trackback information of current post.
668  *
669  * @since 0.71
670  *
671  * @param int $deprecated Not used (Was $timezone = 0)
672  */
673 function trackback_rdf($deprecated = '') {
674     if (stripos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') === false) {
675         echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
676                 xmlns:dc="http://purl.org/dc/elements/1.1/"
677                 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
678             <rdf:Description rdf:about="'