root/branches/1.5/wp-includes/comment-functions.php

Revision 2777, 21.0 kB (checked in by matt, 3 years ago)

Fixes #1575

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 // Template functions
4
5 function comments_template( $file = '/comments.php' ) {
6     global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
7
8     if ( is_single() || is_page() || $withcomments ) :
9         $req = get_settings('require_name_email');
10         $comment_author = isset($_COOKIE['comment_author_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_'.COOKIEHASH])) : '';
11         $comment_author_email = isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_email_'.COOKIEHASH])) : '';
12         $comment_author_url = isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_url_'.COOKIEHASH])) : '';
13     if ( empty($comment_author) ) {
14         $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date");
15     } else {
16         $author_db = addslashes($comment_author);
17         $email_db  = addslashes($comment_author_email);
18         $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");
19     }
20
21     get_currentuserinfo();
22
23     define('COMMENTS_TEMPLATE', true);
24     $include = apply_filters('comments_template', TEMPLATEPATH . $file );
25     if ( file_exists( $include ) )
26         require( $include );
27     else
28         require( ABSPATH . 'wp-content/themes/default/comments.php');
29
30     endif;
31 }
32
33 function clean_url( $url ) {
34     if ('' == $url) return $url;
35     $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $url);
36     $url = str_replace(';//', '://', $url);
37     $url = (!strstr($url, '://')) ? 'http://'.$url : $url;
38     $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
39     return $url;
40 }
41
42 function get_comments_number( $comment_id ) {
43     global $wpdb, $comment_count_cache;
44     $comment_id = (int) $comment_id;
45     if (!isset($comment_count_cache[$comment_id]))
46         $comment_count_cache[$comment_id] =  $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_id' AND comment_approved = '1'");
47     
48     return apply_filters('get_comments_number', $comment_count_cache[$comment_id]);
49 }
50
51 function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) {
52     global $id, $comment;
53     $number = get_comments_number( $id );
54     if ($number == 0) {
55         $blah = $zero;
56     } elseif ($number == 1) {
57         $blah = $one;
58     } elseif ($number  > 1) {
59         $blah = str_replace('%', $number, $more);
60     }
61     echo apply_filters('comments_number', $blah);
62 }
63
64 function get_comments_link() {
65     return get_permalink() . '#comments';
66 }
67
68 function get_comment_link() {
69     global $comment;
70     return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
71 }
72
73 function comments_link( $file = '', $echo = true ) {
74     echo get_comments_link();
75 }
76
77 function comments_popup_script($width=400, $height=400, $file='') {
78     global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript;
79
80         if (empty ($file)) {
81             $wpcommentspopupfile = ''// Use the index.
82         } else {
83             $wpcommentspopupfile = $file;
84         }
85
86     $wpcommentsjavascript = 1;
87     $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";
88     echo $javascript;
89 }
90
91 function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
92     global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb;
93     global $comment_count_cache;
94
95     if (! is_single() && ! is_page()) {
96     if ( !isset($comment_count_cache[$id]))
97             $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';");
98
99         $number = $comment_count_cache[$id];
100
101     if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) {
102         echo $none;
103         return;
104     } else {
105         if (!empty($post->post_password)) { // if there's a password
106             if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
107                 echo('Enter your password to view comments');
108                 return;
109             }
110         }
111         echo '<a href="';
112         if ($wpcommentsjavascript) {
113                     if ( empty($wpcommentspopupfile) )
114                         $home = get_settings('home');
115                     else
116                         $home = get_settings('siteurl');
117                     echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id;
118                     echo '" onclick="wpopen(this.href); return false"';
119         } else {
120             // if comments_popup_script() is not in the template, display simple comment link
121             comments_link();
122             echo '"';
123         }
124         if (!empty($CSSclass)) {
125             echo ' class="'.$CSSclass.'"';
126         }
127         echo '>';
128         comments_number($zero, $one, $more, $number);
129         echo '</a>';
130     }
131     }
132 }
133
134 function get_comment_ID() {
135     global $comment;
136     return apply_filters('get_comment_ID', $comment->comment_ID);
137 }
138
139 function comment_ID() {
140     echo get_comment_ID();
141 }
142
143 function get_comment_author() {
144     global $comment;
145     if ( empty($comment->comment_author) )
146         $author = 'Anonymous';
147     else
148         $author = $comment->comment_author;
149     return apply_filters('get_comment_author', $author);
150 }
151
152 function comment_author() {
153     $author = apply_filters('comment_author', get_comment_author() );
154     echo $author;
155 }
156
157 function get_comment_author_email() {
158     global $comment;
159     return apply_filters('get_comment_author_email', $comment->comment_author_email);   
160 }
161
162 function comment_author_email() {
163     echo apply_filters('author_email', get_comment_author_email() );
164 }
165
166 function get_comment_author_link() {
167     global $comment;
168     $url    = get_comment_author_url();
169     $author = get_comment_author();
170
171     if ( empty( $url ) )
172         $return = $author;
173     else
174         $return = "<a href='$url' rel='external nofollow'>$author</a>";
175     return apply_filters('get_comment_author_link', $return);
176 }
177
178 function comment_author_link() {
179     echo get_comment_author_link();
180 }
181
182 function get_comment_type() {
183     global $comment;
184
185     if ( '' == $comment->comment_type )
186         $comment->comment_type = 'comment';
187
188     return apply_filters('get_comment_type', $comment->comment_type);
189 }
190
191 function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
192     $type = get_comment_type();
193     switch( $type ) {
194         case 'trackback' :
195             echo $trackbacktxt;
196             break;
197         case 'pingback' :
198             echo $pingbacktxt;
199             break;
200         default :
201             echo $commenttxt;
202     }
203 }
204
205 function get_comment_author_url() {
206     global $comment;
207     return apply_filters('get_comment_author_url', $comment->comment_author_url);
208 }
209
210 function comment_author_url() {
211     echo apply_filters('comment_url', get_comment_author_url());
212 }
213
214 function comment_author_email_link($linktext='', $before='', $after='') {
215     global $comment;
216     $email = apply_filters('comment_email', $comment->comment_author_email);
217     if ((!empty($email)) && ($email != '@')) {
218     $display = ($linktext != '') ? $linktext : $email;
219         echo $before;
220         echo "<a href='mailto:$email'>$display</a>";
221         echo $after;
222     }
223 }
224
225 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
226     global $comment;
227     $url = get_comment_author_url();
228     $display = ($linktext != '') ? $linktext : $url;
229     $return = "$before<a href='$url' rel='external'>$display</a>$after";
230     return apply_filters('get_comment_author_url_link', $return);
231 }
232
233 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
234     echo get_comment_author_url_link( $linktext, $before, $after );
235 }
236
237 function get_comment_author_IP() {
238     global $comment;
239     return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
240 }
241
242 function comment_author_IP() {
243     echo get_comment_author_IP();
244 }
245
246 function get_comment_text() {
247     global $comment;
248     return apply_filters('get_comment_text', $comment->comment_content);
249 }
250
251 function comment_text() {
252     echo apply_filters('comment_text', get_comment_text() );
253 }
254
255 function get_comment_excerpt() {
256     global $comment;
257     $comment_text = strip_tags($comment->comment_content);
258     $blah = explode(' ', $comment_text);
259     if (count($blah) > 20) {
260         $k = 20;
261         $use_dotdotdot = 1;
262     } else {
263         $k = count($blah);
264         $use_dotdotdot = 0;
265     }
266     $excerpt = '';
267     for ($i=0; $i<$k; $i++) {
268         $excerpt .= $blah[$i] . ' ';
269     }
270     $excerpt .= ($use_dotdotdot) ? '...' : '';
271     return apply_filters('get_comment_excerpt', $excerpt);
272 }
273
274 function comment_excerpt() {
275     echo apply_filters('comment_excerpt', get_comment_excerpt() );
276 }
277
278 function get_comment_date( $d = '' ) {
279     global $comment;
280     if ( '' == $d )
281         $date = mysql2date( get_settings('date_format'), $comment->comment_date);
282     else
283         $date = mysql2date($d, $comment->comment_date);
284     return apply_filters('get_comment_date', $date);
285 }
286
287 function comment_date( $d = '' ) {
288     echo get_comment_date( $d );
289 }
290
291 function get_comment_time( $d = '', $gmt = false ) {
292     global $comment;
293     $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
294     if ( '' == $d )
295         $date = mysql2date(get_settings('time_format'), $comment_date);
296     else
297         $date = mysql2date($d, $comment_date);
298     return apply_filters('get_comment_time', $date);
299 }
300
301 function comment_time( $d = '' ) {
302     echo get_comment_time($d);
303 }
304
305 function get_trackback_url() {
306     global $id;
307     $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id;
308
309     if ( '' != get_settings('permalink_structure') )
310         $tb_url = trailingslashit(get_permalink()) . 'trackback/';
311
312     return $tb_url;
313 }
314 function trackback_url( $display = true ) {
315     if ( $display)
316         echo get_trackback_url();
317     else
318         return get_trackback_url();
319 }
320
321 function trackback_rdf($timezone = 0) {
322     global $id;
323     if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
324     echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
325         xmlns:dc="http://purl.org/dc/elements/1.1/"
326         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
327         <rdf:Description rdf:about="';
328     the_permalink();
329     echo '"'."\n";
330     echo '    dc:identifier="';
331     the_permalink();
332     echo '"'."\n";
333     echo '    dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n";
334     echo '    trackback:ping="'.trackback_url(0).'"'." />\n";
335     echo '</rdf:RDF>';
336     }
337 }
338
339 function comments_open() {
340     global $post;
341     if ( 'open' == $post->comment_status )
342         return true;
343     else
344         return false;
345 }
346
347 function pings_open() {
348     global $post;
349     if ( 'open' == $post->ping_status )
350         return true;
351     else
352         return false;
353 }
354
355 // Non-template functions
356
357 function get_lastcommentmodified($timezone = 'server') {
358     global $cache_lastcommentmodified, $pagenow, $wpdb;
359     $add_seconds_blog = get_settings('gmt_offset') * 3600;
360     $add_seconds_server = date('Z');
361     $now = current_time('mysql', 1);
362     if ( !isset($cache_lastcommentmodified[$timezone]) ) {
363         switch(strtolower($timezone)) {
364             case 'gmt':
365                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
366                 break;
367             case 'blog':
368                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
369                 break;
370             case 'server':
371                 $lastcommentmodified = $wpdb->get_var("SELECT DATE_ADD(comment_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
372                 break;
373         }
374         $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
375     } else {
376         $lastcommentmodified = $cache_lastcommentmodified[$timezone];
377     }
378     return $lastcommentmodified;
379 }
380
381 function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
382     global $postc, $id, $commentdata, $wpdb;
383     if ($no_cache) {
384         $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
385         if (false == $include_unapproved) {
386             $query .= " AND comment_approved = '1'";
387         }
388             $myrow = $wpdb->get_row($query, ARRAY_A);
389     } else {
390         $myrow['comment_ID'] = $postc->comment_ID;
391         $myrow['comment_post_ID'] = $postc->comment_post_ID;
392         $myrow['comment_author'] = $postc->comment_author;
393         $myrow['comment_author_email'] = $postc->comment_author_email;
394         $myrow['comment_author_url'] = $postc->comment_author_url;
395         $myrow['comment_author_IP'] = $postc->comment_author_IP;
396         $myrow['comment_date'] = $postc->comment_date;
397         $myrow['comment_content'] = $postc->comment_content;
398         $myrow['comment_karma'] = $postc->comment_karma;
399         $myrow['comment_approved'] = $postc->comment_approved;
400         $myrow['comment_type'] = $postc->comment_type;
401     }
402     return $myrow;
403 }
404
405 function pingback($content, $post_ID) {
406     global $wp_version, $wpdb;
407     include_once (ABSPATH . WPINC . '/class-IXR.php');
408
409     // original code by Mort (http://mort.mine.nu:8080)
410     $log = debug_fopen(ABSPATH . '/pingback.log', 'a');
411     $post_links = array();
412     debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
413
414     $pung = get_pung($post_ID);
415
416     // Variables
417     $ltrs = '\w';
418     $gunk = '/#~:.?+=&%@!\-';
419     $punc = '.:?\-';
420     $any = $ltrs . $gunk . $punc;
421
422     // Step 1
423     // Parsing the post, external links (if any) are stored in the $post_links array
424     // This regexp comes straight from phpfreaks.com
425     // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
426     preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
427
428     // Debug
429     debug_fwrite($log, 'Post contents:');
430     debug_fwrite($log, $content."\n");
431     
432     // Step 2.
433     // Walking thru the links array
434     // first we get rid of links pointing to sites, not to specific files
435     // Example:
436     // http://dummy-weblog.org
437     // http://dummy-weblog.org/
438     // http://dummy-weblog.org/post.php
439     // We don't wanna ping first and second types, even if they have a valid <link/>
440
441     foreach($post_links_temp[0] as $link_test) :
442         if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
443             $test = parse_url($link_test);
444             if (isset($test['query']))
445                 $post_links[] = $link_test;
446             elseif(($test['path'] != '/') && ($test['path'] != ''))
447                 $post_links[] = $link_test;
448         endif;
449     endforeach;
450
451     foreach ($post_links as $pagelinkedto){
452         debug_fwrite($log, "Processing -- $pagelinkedto\n");
453         $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
454
455         if ($pingback_server_url) {
456                         set_time_limit( 60 );
457              // Now, the RPC call
458             debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
459             debug_fwrite($log, 'Page Linked From: ');
460             $pagelinkedfrom = get_permalink($post_ID);
461             debug_fwrite($log, $pagelinkedfrom."\n");
462
463             // using a timeout of 3 seconds should be enough to cover slow servers
464             $client = new IXR_Client($pingback_server_url);
465             $client->timeout = 3;
466             $client->useragent .= ' -- WordPress/' . $wp_version;
467
468             // when set to true, this outputs debug messages by itself
469             $client->debug = false;
470             
471             if ( $client->query('pingback.ping', array($pagelinkedfrom, $pagelinkedto) ) )
472                 add_ping( $post_ID, $pagelinkedto );
473             else
474                 debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
475         }
476     }
477
478     debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
479     debug_fclose($log);
480 }
481
482 function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
483     global $wp_version;
484
485     $byte_count = 0;
486     $contents = '';
487     $headers = '';
488