Make WordPress Core

Ticket #4297: comment-template.php

File comment-template.php, 10.6 KB (added by hyp0r, 18 years ago)
Line 
1<?php
2/*
3 * Comment template functions.
4 */
5
6function get_comment_author() {
7        global $comment;
8        if ( empty($comment->comment_author) )
9                $author = __('Anonymous');
10        else
11                $author = $comment->comment_author;
12        return apply_filters('get_comment_author', $author);
13}
14
15function comment_author() {
16        $author = apply_filters('comment_author', get_comment_author() );
17        echo $author;
18}
19
20function get_comment_author_email() {
21        global $comment;
22        return apply_filters('get_comment_author_email', $comment->comment_author_email);
23}
24
25function comment_author_email() {
26        echo apply_filters('author_email', get_comment_author_email() );
27}
28
29function comment_author_email_link($linktext='', $before='', $after='') {
30        global $comment;
31        $email = apply_filters('comment_email', $comment->comment_author_email);
32        if ((!empty($email)) && ($email != '@')) {
33        $display = ($linktext != '') ? $linktext : $email;
34                echo $before;
35                echo "<a href='mailto:$email'>$display</a>";
36                echo $after;
37        }
38}
39
40function get_comment_author_link() {
41        global $comment;
42        $url    = get_comment_author_url();
43        $author = get_comment_author();
44
45        if ( empty( $url ) || 'http://' == $url )
46                $return = $author;
47        else
48                $return = "<a href='$url' rel='external nofollow'>$author</a>";
49        return apply_filters('get_comment_author_link', $return);
50}
51
52function comment_author_link() {
53        echo get_comment_author_link();
54}
55
56function get_comment_author_IP() {
57        global $comment;
58        return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
59}
60
61function comment_author_IP() {
62        echo get_comment_author_IP();
63}
64
65function get_comment_author_url() {
66        global $comment;
67        return apply_filters('get_comment_author_url', $comment->comment_author_url);
68}
69
70function comment_author_url() {
71        echo apply_filters('comment_url', get_comment_author_url());
72}
73
74function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
75        global $comment;
76        $url = get_comment_author_url();
77        $display = ($linktext != '') ? $linktext : $url;
78        $display = str_replace( 'http://www.', '', $display );
79        $display = str_replace( 'http://', '', $display );
80        if ( '/' == substr($display, -1) )
81                $display = substr($display, 0, -1);
82        $return = "$before<a href='$url' rel='external'>$display</a>$after";
83        return apply_filters('get_comment_author_url_link', $return);
84}
85
86function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
87        echo get_comment_author_url_link( $linktext, $before, $after );
88}
89
90function get_comment_date( $d = '' ) {
91        global $comment;
92        if ( '' == $d )
93                $date = mysql2date( get_option('date_format'), $comment->comment_date);
94        else
95                $date = mysql2date($d, $comment->comment_date);
96        return apply_filters('get_comment_date', $date, $d);
97}
98
99function comment_date( $d = '' ) {
100        echo get_comment_date( $d );
101}
102
103function get_comment_excerpt() {
104        global $comment;
105        $comment_text = strip_tags($comment->comment_content);
106        $blah = explode(' ', $comment_text);
107        if (count($blah) > 20) {
108                $k = 20;
109                $use_dotdotdot = 1;
110        } else {
111                $k = count($blah);
112                $use_dotdotdot = 0;
113        }
114        $excerpt = '';
115        for ($i=0; $i<$k; $i++) {
116                $excerpt .= $blah[$i] . ' ';
117        }
118        $excerpt .= ($use_dotdotdot) ? '...' : '';
119        return apply_filters('get_comment_excerpt', $excerpt);
120}
121
122function comment_excerpt() {
123        echo apply_filters('comment_excerpt', get_comment_excerpt() );
124}
125
126function get_comment_ID() {
127        global $comment;
128        return apply_filters('get_comment_ID', $comment->comment_ID);
129}
130
131function comment_ID() {
132        echo get_comment_ID();
133}
134
135function get_comment_link() {
136        global $comment;
137        return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
138}
139
140function get_comments_link() {
141        return get_permalink() . '#comments';
142}
143
144function comments_link( $file = '', $echo = true ) {
145                echo get_comments_link();
146}
147
148function get_comments_number( $post_id = 0 ) {
149        global $wpdb, $id;
150        $post_id = (int) $post_id;
151
152        if ( !$post_id )
153                $post_id = (int) $id;
154
155        $post = get_post($post_id);
156        if ( ! isset($post->comment_count) )
157                $count = 0;
158        else
159                $count = $post->comment_count;
160
161        return apply_filters('get_comments_number', $count);
162}
163
164function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
165        global $id;
166        $number = get_comments_number($id);
167
168        if ( $number > 1 )
169                $output = str_replace('%', $number, ( false === $more ) ? __('% Comments') : $more);
170        elseif ( $number == 0 )
171                $output = ( false === $zero ) ? __('No Comments') : $zero;
172        else // must be one
173                $output = ( false === $one ) ? __('1 Comment') : $one;
174
175        echo apply_filters('comments_number', $output, $number);
176}
177
178function get_comment_text() {
179        global $comment;
180        return apply_filters('get_comment_text', $comment->comment_content);
181}
182
183function comment_text() {
184        echo apply_filters('comment_text', get_comment_text() );
185}
186
187function get_comment_time( $d = '', $gmt = false ) {
188        global $comment;
189        $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
190        if ( '' == $d )
191                $date = mysql2date(get_option('time_format'), $comment_date);
192        else
193                $date = mysql2date($d, $comment_date);
194        return apply_filters('get_comment_time', $date, $d, $gmt);
195}
196
197function comment_time( $d = '' ) {
198        echo get_comment_time($d);
199}
200
201function get_comment_type() {
202        global $comment;
203
204        if ( '' == $comment->comment_type )
205                $comment->comment_type = 'comment';
206
207        return apply_filters('get_comment_type', $comment->comment_type);
208}
209
210function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
211        $type = get_comment_type();
212        switch( $type ) {
213                case 'trackback' :
214                        echo $trackbacktxt;
215                        break;
216                case 'pingback' :
217                        echo $pingbacktxt;
218                        break;
219                default :
220                        echo $commenttxt;
221        }
222}
223
224function get_trackback_url() {
225        global $id;
226        $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . $id;
227
228        if ( '' != get_option('permalink_structure') )
229                $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
230
231        return apply_filters('trackback_url', $tb_url);
232}
233function trackback_url( $display = true ) {
234        if ( $display)
235                echo get_trackback_url();
236        else
237                return get_trackback_url();
238}
239
240function trackback_rdf($timezone = 0) {
241        global $id;
242        if (strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) {
243                echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
244                                xmlns:dc="http://purl.org/dc/elements/1.1/"
245                                xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
246                        <rdf:Description rdf:about="';
247                the_permalink();
248                echo '"'."\n";
249                echo '    dc:identifier="';
250                the_permalink();
251                echo '"'."\n";
252                echo '    dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
253                echo '    trackback:ping="'.trackback_url(0).'"'." />\n";
254                echo '</rdf:RDF>';
255        }
256}
257
258function comments_open() {
259        global $post;
260        if ( 'open' == $post->comment_status )
261                return true;
262        else
263                return false;
264}
265
266function pings_open() {
267        global $post;
268        if ( 'open' == $post->ping_status )
269                return true;
270        else
271                return false;
272}
273
274function wp_comment_form_unfiltered_html_nonce() {
275        global $post;
276        if ( current_user_can('unfiltered_html') )
277                wp_nonce_field('unfiltered-html-comment_' . $post->ID, '_wp_unfiltered_html_comment', false);
278}
279
280function comments_template( $file = '/comments.php', $order = 'ASC' ) {
281        global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
282
283        if ( ! (is_single() || is_page() || $withcomments) )
284                return;
285
286        $req = get_option('require_name_email');
287        $commenter = wp_get_current_commenter();
288        extract($commenter);
289
290        // TODO: Use API instead of SELECTs.
291        if ( $user_ID) {
292                $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND (comment_approved = '1' OR ( user_id = '$user_ID' AND comment_approved = '0' ) )  ORDER BY comment_date $order");
293        } else if ( empty($comment_author) ) { 
294                $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date $order");
295        } else {
296                $author_db = $wpdb->escape($comment_author);
297                $email_db  = $wpdb->escape($comment_author_email);
298                $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date $order");
299        }
300
301        // keep $comments for legacy's sake (remember $table*? ;) )
302        $comments = $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
303        $wp_query->comment_count = count($wp_query->comments);
304
305        define('COMMENTS_TEMPLATE', true);
306        $include = apply_filters('comments_template', TEMPLATEPATH . $file );
307        if ( file_exists( $include ) )
308                require( $include );
309        else
310                require( ABSPATH . 'wp-content/themes/default/comments.php');
311}
312
313function comments_popup_script($width=400, $height=400, $file='') {
314                global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript;
315
316                if (empty ($file)) {
317                        $wpcommentspopupfile = '';  // Use the index.
318                } else {
319                        $wpcommentspopupfile = $file;
320                }
321
322                $wpcommentsjavascript = 1;
323                $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";
324                echo $javascript;
325}
326
327function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
328        global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb;
329
330        if ( is_single() || is_page() )
331                return;
332
333        $number = get_comments_number($id);
334
335        if ( 0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status ) {
336                echo '<span' . ((!empty($CSSclass)) ? ' class="' . $CSSclass . '"' : '') . '>' . $none . '</span>';
337                return;
338        }
339
340        if ( !empty($post->post_password) ) { // if there's a password
341                if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
342                        echo(__('Enter your password to view comments'));
343                        return;
344                }
345        }
346
347        echo '<a href="';
348        if ($wpcommentsjavascript) {
349                if ( empty($wpcommentspopupfile) )
350                        $home = get_option('home');
351                else
352                        $home = get_option('siteurl');
353                echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id;
354                echo '" onclick="wpopen(this.href); return false"';
355        } else { // if comments_popup_script() is not in the template, display simple comment link
356                if ( 0 == $number )
357                        echo get_permalink() . '#respond';
358                else
359                        comments_link();
360                echo '"';
361        }
362
363        if (!empty($CSSclass)) {
364                echo ' class="'.$CSSclass.'"';
365        }
366        $title = attribute_escape(apply_filters('the_title', get_the_title()));
367        echo ' title="' . sprintf( __('Comment on %s'), $title ) .'">';
368        comments_number($zero, $one, $more, $number);
369        echo '</a>';
370}
371
372?>