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

Revision 8800, 25.1 kB (checked in by ryan, 2 days ago)

post_password_required(). fixes #7679

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