Ticket #2525: comment-funcs.diff

File comment-funcs.diff, 76.8 kB (added by ryan, 2 years ago)

Move comment functions into comment.php and comment-template.php

  • wp-includes/comment.php

    old new  
    11<?php 
    22 
    3 // Template functions 
     3function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { 
     4        global $wpdb; 
    45 
    5 function comments_template( $file = '/comments.php' ) { 
    6         global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity; 
     6        if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual 
    77 
    8         if ( is_single() || is_page() || $withcomments ) : 
    9                 $req = get_settings('require_name_email'); 
    10                 $comment_author = ''; 
    11                 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) { 
    12                         $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]); 
    13                         $comment_author = stripslashes($comment_author); 
    14                         $comment_author = wp_specialchars($comment_author, true); 
    15                 } 
    16                 $comment_author_email = ''; 
    17                 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) { 
    18                         $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]); 
    19                         $comment_author_email = stripslashes($comment_author_email); 
    20                         $comment_author_email = wp_specialchars($comment_author_email, true);            
    21                 } 
    22                 $comment_author_url = ''; 
    23                 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) { 
    24                         $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]); 
    25                         $comment_author_url = stripslashes($comment_author_url); 
    26                         $comment_author_url = wp_specialchars($comment_author_url, true);                
    27                 } 
     8        if ( (count(explode('http:', $comment)) - 1) >= get_settings('comment_max_links') ) 
     9                return false; // Check # of external links 
    2810 
    29         if ( empty($comment_author) ) { 
    30                 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date"); 
    31         } else { 
    32                 $author_db = $wpdb->escape($comment_author); 
    33                 $email_db  = $wpdb->escape($comment_author_email); 
    34                 $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"); 
    35         } 
     11        $mod_keys = trim( get_settings('moderation_keys') ); 
     12        if ( !empty($mod_keys) ) { 
     13                $words = explode("\n", $mod_keys ); 
    3614 
    37         define('COMMENTS_TEMPLATE', true); 
    38         $include = apply_filters('comments_template', TEMPLATEPATH . $file ); 
    39         if ( file_exists( $include ) ) 
    40                 require( $include ); 
    41         else 
    42                 require( ABSPATH . 'wp-content/themes/default/comments.php'); 
     15                foreach ($words as $word) { 
     16                        $word = trim($word); 
    4317 
    44         endif; 
    45 
     18                       // Skip empty lines 
     19                       if (empty($word)) { continue;
    4620 
    47 function wp_new_comment( $commentdata ) { 
    48         $commentdata = apply_filters('preprocess_comment', $commentdata); 
     21                        // Do some escaping magic so that '#' chars in the  
     22                        // spam words don't break things: 
     23                        $word = preg_quote($word, '#'); 
    4924 
    50         $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; 
    51         $commentdata['user_ID']         = (int) $commentdata['user_ID']; 
     25                        $pattern = "#$word#i";  
     26                        if ( preg_match($pattern, $author) ) return false; 
     27                        if ( preg_match($pattern, $email) ) return false; 
     28                        if ( preg_match($pattern, $url) ) return false; 
     29                        if ( preg_match($pattern, $comment) ) return false; 
     30                        if ( preg_match($pattern, $user_ip) ) return false; 
     31                        if ( preg_match($pattern, $user_agent) ) return false; 
     32                } 
     33        } 
    5234 
    53         $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; 
    54         $commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT']; 
    55  
    56         $commentdata['comment_date']     = current_time('mysql'); 
    57         $commentdata['comment_date_gmt'] = current_time('mysql', 1); 
    58  
    59  
    60         $commentdata = wp_filter_comment($commentdata); 
    61  
    62         $commentdata['comment_approved'] = wp_allow_comment($commentdata); 
    63  
    64         $comment_ID = wp_insert_comment($commentdata); 
    65  
    66         do_action('comment_post', $comment_ID, $commentdata['comment_approved']); 
    67  
    68         if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching 
    69                 if ( '0' == $commentdata['comment_approved'] ) 
    70                         wp_notify_moderator($comment_ID); 
    71  
    72                 $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment 
    73  
    74                 if ( get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) 
    75                         wp_notify_postauthor($comment_ID, $commentdata['comment_type']); 
     35        // Comment whitelisting: 
     36        if ( 1 == get_settings('comment_whitelist')) { 
     37                if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll 
     38                        $uri = parse_url($url); 
     39                        $domain = $uri['host']; 
     40                        $uri = parse_url( get_option('home') ); 
     41                        $home_domain = $uri['host']; 
     42                        if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain ) 
     43                                return true; 
     44                        else 
     45                                return false; 
     46                } elseif( $author != '' && $email != '' ) { 
     47                        $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1"); 
     48                        if ( ( 1 == $ok_to_comment ) && 
     49                                ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) ) 
     50                                        return true; 
     51                        else 
     52                                return false; 
     53                } else { 
     54                        return false; 
     55                } 
    7656        } 
    7757 
    78         return $comment_ID
     58        return true
    7959} 
    8060 
    81 function wp_insert_comment($commentdata) { 
     61function get_approved_comments($post_id) { 
    8262        global $wpdb; 
    83         extract($commentdata); 
     63        return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post_id AND comment_approved = '1' ORDER BY comment_date"); 
     64
    8465 
    85         if ( ! isset($comment_author_IP) ) 
    86                 $comment_author_IP = $_SERVER['REMOTE_ADDR']; 
    87         if ( ! isset($comment_date) ) 
    88                 $comment_date = current_time('mysql'); 
    89         if ( ! isset($comment_date_gmt) ) 
    90                 $comment_date_gmt = gmdate('Y-m-d H:i:s', strtotime($comment_date) ); 
    91         if ( ! isset($comment_parent) ) 
    92                 $comment_parent = 0; 
    93         if ( ! isset($comment_approved) ) 
    94                 $comment_approved = 1; 
    95         if ( ! isset($user_id) ) 
    96                 $user_id = 0; 
     66// Retrieves comment data given a comment ID or comment object. 
     67// Handles comment caching. 
     68function &get_comment(&$comment, $output = OBJECT) { 
     69        global $comment_cache, $wpdb; 
    9770 
    98         $result = $wpdb->query("INSERT INTO $wpdb->comments  
    99         (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id) 
    100         VALUES  
    101         ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id') 
    102         "); 
     71        if ( empty($comment) ) 
     72                return null; 
    10373 
    104         $id = $wpdb->insert_id; 
     74        if ( is_object($comment) ) { 
     75                if ( !isset($comment_cache[$comment->comment_ID]) ) 
     76                        $comment_cache[$comment->comment_ID] = &$comment; 
     77                $_comment = & $comment_cache[$comment->comment_ID]; 
     78        } else { 
     79                if ( !isset($comment_cache[$comment]) ) { 
     80                        $_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1"); 
     81                        $comment_cache[$comment->comment_ID] = & $_comment; 
     82                } else { 
     83                        $_comment = & $comment_cache[$comment]; 
     84                } 
     85        } 
    10586 
    106         if ( $comment_approved == 1) { 
    107                 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'"); 
    108                 $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$comment_post_ID'" ); 
     87        if ( $output == OBJECT ) { 
     88                return $_comment; 
     89        } elseif ( $output == ARRAY_A ) { 
     90                return get_object_vars($_comment); 
     91        } elseif ( $output == ARRAY_N ) { 
     92                return array_values(get_object_vars($_comment)); 
     93        } else { 
     94                return $_comment; 
    10995        } 
    110         return $id; 
    11196} 
    11297 
    113 function wp_filter_comment($commentdata) { 
    114         $commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']); 
    115         $commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']); 
    116         $commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']); 
    117         $commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']); 
    118         $commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']); 
    119         $commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']); 
    120         $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']); 
    121         $commentdata['filtered'] = true; 
    122         return $commentdata; 
     98// Deprecate in favor of get_comment()? 
     99function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries 
     100        global $postc, $id, $commentdata, $wpdb; 
     101        if ($no_cache) { 
     102                $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'"; 
     103                if (false == $include_unapproved) { 
     104                    $query .= " AND comment_approved = '1'"; 
     105                } 
     106                $myrow = $wpdb->get_row($query, ARRAY_A); 
     107        } else { 
     108                $myrow['comment_ID'] = $postc->comment_ID; 
     109                $myrow['comment_post_ID'] = $postc->comment_post_ID; 
     110                $myrow['comment_author'] = $postc->comment_author; 
     111                $myrow['comment_author_email'] = $postc->comment_author_email; 
     112                $myrow['comment_author_url'] = $postc->comment_author_url; 
     113                $myrow['comment_author_IP'] = $postc->comment_author_IP; 
     114                $myrow['comment_date'] = $postc->comment_date; 
     115                $myrow['comment_content'] = $postc->comment_content; 
     116                $myrow['comment_karma'] = $postc->comment_karma; 
     117                $myrow['comment_approved'] = $postc->comment_approved; 
     118                $myrow['comment_type'] = $postc->comment_type; 
     119        } 
     120        return $myrow; 
    123121} 
    124122 
     123function get_lastcommentmodified($timezone = 'server') { 
     124        global $cache_lastcommentmodified, $pagenow, $wpdb; 
     125        $add_seconds_blog = get_settings('gmt_offset') * 3600; 
     126        $add_seconds_server = date('Z'); 
     127        $now = current_time('mysql', 1); 
     128        if ( !isset($cache_lastcommentmodified[$timezone]) ) { 
     129                switch(strtolower($timezone)) { 
     130                        case 'gmt': 
     131                                $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); 
     132                                break; 
     133                        case 'blog': 
     134                                $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); 
     135                                break; 
     136                        case 'server': 
     137                                $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"); 
     138                                break; 
     139                } 
     140                $cache_lastcommentmodified[$timezone] = $lastcommentmodified; 
     141        } else { 
     142                $lastcommentmodified = $cache_lastcommentmodified[$timezone]; 
     143        } 
     144        return $lastcommentmodified; 
     145} 
     146 
    125147function wp_allow_comment($commentdata) { 
    126148        global $wpdb; 
    127149        extract($commentdata); 
     
    171193        return $approved; 
    172194} 
    173195 
    174  
    175 function wp_update_comment($commentarr) { 
    176         global $wpdb; 
    177  
    178         // First, get all of the original fields 
    179         $comment = get_comment($commentarr['comment_ID'], ARRAY_A); 
    180  
    181         // Escape data pulled from DB. 
    182         foreach ($comment as $key => $value) 
    183                 $comment[$key] = $wpdb->escape($value); 
    184  
    185         // Merge old and new fields with new fields overwriting old ones. 
    186         $commentarr = array_merge($comment, $commentarr); 
    187  
    188         $commentarr = wp_filter_comment( $commentarr ); 
    189  
    190         // Now extract the merged array. 
    191         extract($commentarr); 
    192  
    193         $comment_content = apply_filters('comment_save_pre', $comment_content); 
    194  
    195         $result = $wpdb->query( 
    196                 "UPDATE $wpdb->comments SET 
    197                         comment_content = '$comment_content', 
    198                         comment_author = '$comment_author', 
    199                         comment_author_email = '$comment_author_email', 
    200                         comment_approved = '$comment_approved', 
    201                         comment_author_url = '$comment_author_url', 
    202                         comment_date = '$comment_date' 
    203                 WHERE comment_ID = $comment_ID" ); 
    204  
    205         $rval = $wpdb->rows_affected; 
    206  
    207         $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); 
    208         if( is_object( $c ) ) 
    209                 $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); 
    210  
    211         do_action('edit_comment', $comment_ID); 
    212  
    213         return $rval; 
    214 } 
    215  
    216196function wp_delete_comment($comment_id) { 
    217197        global $wpdb; 
    218198        do_action('delete_comment', $comment_id); 
     
    230210        return true; 
    231211} 
    232212 
    233 function get_comments_number( $post_id = 0 ) { 
    234         global $wpdb, $comment_count_cache, $id; 
    235         $post_id = (int) $post_id; 
     213function wp_get_comment_status($comment_id) { 
     214        global $wpdb; 
    236215 
    237         if ( !$post_id ) 
    238                 $post_id = $id; 
    239  
    240         if ( !isset($comment_count_cache[$post_id]) ) 
    241                 $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'"); 
    242  
    243         return apply_filters('get_comments_number', $comment_count_cache[$post_id]); 
    244 
    245  
    246 function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) { 
    247         global $id, $comment; 
    248         $number = get_comments_number( $id ); 
    249         if ($number == 0) { 
    250                 $blah = $zero; 
    251         } elseif ($number == 1) { 
    252                 $blah = $one; 
    253         } elseif ($number  > 1) { 
    254                 $blah = str_replace('%', $number, $more); 
     216        $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1"); 
     217        if ($result == NULL) { 
     218                return 'deleted'; 
     219        } else if ($result == '1') { 
     220                return 'approved'; 
     221        } else if ($result == '0') { 
     222                return 'unapproved'; 
     223        } else if ($result == 'spam') { 
     224                return 'spam'; 
     225        } else { 
     226                return false; 
    255227        } 
    256         echo apply_filters('comments_number', $blah); 
    257228} 
    258229 
    259 function get_comments_link() { 
    260         return get_permalink() . '#comments'
    261 
     230function wp_insert_comment($commentdata) { 
     231        global $wpdb
     232        extract($commentdata); 
    262233 
    263 function get_comment_link() { 
    264         global $comment; 
    265         return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; 
    266 
     234        if ( ! isset($comment_author_IP) ) 
     235                $comment_author_IP = $_SERVER['REMOTE_ADDR']; 
     236        if ( ! isset($comment_date) ) 
     237                $comment_date = current_time('mysql'); 
     238        if ( ! isset($comment_date_gmt) ) 
     239                $comment_date_gmt = gmdate('Y-m-d H:i:s', strtotime($comment_date) ); 
     240        if ( ! isset($comment_parent) ) 
     241                $comment_parent = 0; 
     242        if ( ! isset($comment_approved) ) 
     243                $comment_approved = 1; 
     244        if ( ! isset($user_id) ) 
     245                $user_id = 0; 
    267246 
    268 function comments_link( $file = '', $echo = true ) { 
    269     echo get_comments_link(); 
    270 
     247        $result = $wpdb->query("INSERT INTO $wpdb->comments  
     248        (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id) 
     249        VALUES  
     250        ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id') 
     251        "); 
    271252 
    272 function comments_popup_script($width=400, $height=400, $file='') { 
    273     global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript; 
     253        $id = $wpdb->insert_id; 
    274254 
    275                 if (empty ($file)) { 
    276                         $wpcommentspopupfile = '';  // Use the index. 
    277                 } else { 
    278                         $wpcommentspopupfile = $file; 
    279                 } 
    280  
    281     $wpcommentsjavascript = 1; 
    282     $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"; 
    283     echo $javascript; 
    284 
    285  
    286 function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') { 
    287         global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb; 
    288         global $comment_count_cache; 
    289  
    290         if (! is_single() && ! is_page()) { 
    291         if ( !isset($comment_count_cache[$id]) ) 
    292                 $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';"); 
    293  
    294         $number = $comment_count_cache[$id]; 
    295  
    296         if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) { 
    297                 echo $none; 
    298                 return; 
    299         } else { 
    300                 if (!empty($post->post_password)) { // if there's a password 
    301                         if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie 
    302                                 echo(__('Enter your password to view comments')); 
    303                                 return; 
    304                         } 
    305                 } 
    306                 echo '<a href="'; 
    307                 if ($wpcommentsjavascript) { 
    308                         if ( empty($wpcommentspopupfile) ) 
    309                                 $home = get_settings('home'); 
    310                         else 
    311                                 $home = get_settings('siteurl'); 
    312                         echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id; 
    313                         echo '" onclick="wpopen(this.href); return false"'; 
    314                 } else { // if comments_popup_script() is not in the template, display simple comment link 
    315                         if ( 0 == $number ) 
    316                                 echo get_permalink() . '#respond'; 
    317                         else 
    318                                 comments_link(); 
    319                         echo '"'; 
    320                 } 
    321                 if (!empty($CSSclass)) { 
    322                         echo ' class="'.$CSSclass.'"'; 
    323                 } 
    324                 echo ' title="' . sprintf( __('Comment on %s'), $post->post_title ) .'">'; 
    325                 comments_number($zero, $one, $more, $number); 
    326                 echo '</a>'; 
     255        if ( $comment_approved == 1) { 
     256                $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'"); 
     257                $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$comment_post_ID'" ); 
    327258        } 
    328         } 
     259        return $id; 
    329260} 
    330261 
    331 function get_comment_ID() { 
    332         global $comment; 
    333         return apply_filters('get_comment_ID', $comment->comment_ID); 
     262function wp_filter_comment($commentdata) { 
     263        $commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']); 
     264        $commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']); 
     265        $commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']); 
     266        $commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']); 
     267        $commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']); 
     268        $commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']); 
     269        $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']); 
     270        $commentdata['filtered'] = true; 
     271        return $commentdata; 
    334272} 
    335273 
    336 function comment_ID() { 
    337         echo get_comment_ID(); 
    338 
     274function wp_new_comment( $commentdata ) { 
     275        $commentdata = apply_filters('preprocess_comment', $commentdata); 
    339276 
    340 function get_comment_author() { 
    341         global $comment; 
    342         if ( empty($comment->comment_author) ) 
    343                 $author = __('Anonymous'); 
    344         else 
    345                 $author = $comment->comment_author; 
    346         return apply_filters('get_comment_author', $author); 
    347 
     277        $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; 
     278        $commentdata['user_ID']         = (int) $commentdata['user_ID']; 
    348279 
    349 function comment_author() { 
    350         $author = apply_filters('comment_author', get_comment_author() ); 
    351         echo $author; 
    352 
     280        $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; 
     281        $commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT']; 
    353282 
    354 function get_comment_author_email() { 
    355         global $comment; 
    356         return apply_filters('get_comment_author_email', $comment->comment_author_email); 
    357 
     283        $commentdata['comment_date']     = current_time('mysql'); 
     284        $commentdata['comment_date_gmt'] = current_time('mysql', 1); 
    358285 
    359 function comment_author_email() { 
    360         echo apply_filters('author_email', get_comment_author_email() ); 
    361 } 
    362286 
    363 function get_comment_author_link() { 
    364         global $comment; 
    365         $url    = get_comment_author_url(); 
    366         $author = get_comment_author(); 
     287        $commentdata = wp_filter_comment($commentdata); 
    367288 
    368         if ( empty( $url ) || 'http://' == $url ) 
    369                 $return = $author; 
    370         else 
    371                 $return = "<a href='$url' rel='external nofollow'>$author</a>"; 
    372         return apply_filters('get_comment_author_link', $return); 
    373 
     289        $commentdata['comment_approved'] = wp_allow_comment($commentdata); 
    374290 
    375 function comment_author_link() { 
    376         echo get_comment_author_link(); 
    377 
     291        $comment_ID = wp_insert_comment($commentdata); 
    378292 
    379 function get_comment_type() { 
    380         global $comment; 
     293        do_action('comment_post', $comment_ID, $commentdata['comment_approved']); 
    381294 
    382         if ( '' == $comment->comment_type ) 
    383                 $comment->comment_type = 'comment'; 
     295        if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching 
     296                if ( '0' == $commentdata['comment_approved'] ) 
     297                        wp_notify_moderator($comment_ID); 
    384298 
    385         return apply_filters('get_comment_type', $comment->comment_type); 
    386 
     299                $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment 
    387300 
    388 function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') { 
    389         $type = get_comment_type(); 
    390         switch( $type ) { 
    391                 case 'trackback' : 
    392                         echo $trackbacktxt; 
    393                         break; 
    394                 case 'pingback' : 
    395                         echo $pingbacktxt; 
    396                         break; 
    397                 default : 
    398                         echo $commenttxt; 
     301                if ( get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) 
     302                        wp_notify_postauthor($comment_ID, $commentdata['comment_type']); 
    399303        } 
    400 } 
    401304 
    402 function get_comment_author_url() { 
    403         global $comment; 
    404         return apply_filters('get_comment_author_url', $comment->comment_author_url); 
     305        return $comment_ID; 
    405306} 
    406307 
    407 function comment_author_url() { 
    408         echo apply_filters('comment_url', get_comment_author_url()); 
    409 
     308function wp_set_comment_status($comment_id, $comment_status) { 
     309    global $wpdb; 
    410310 
    411 function comment_author_email_link($linktext='', $before='', $after='') { 
    412         global $comment; 
    413         $email = apply_filters('comment_email', $comment->comment_author_email); 
    414         if ((!empty($email)) && ($email != '@')) { 
    415         $display = ($linktext != '') ? $linktext : $email; 
    416                 echo $before; 
    417                 echo "<a href='mailto:$email'>$display</a>"; 
    418                 echo $after; 
    419         } 
    420 
     311    switch($comment_status) { 
     312                case 'hold': 
     313                        $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1"; 
     314                break; 
     315                case 'approve': 
     316                        $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1"; 
     317                break; 
     318                case 'spam': 
     319                        $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1"; 
     320                break; 
     321                case 'delete': 
     322                        return wp_delete_comment($comment_id); 
     323                break; 
     324                default: 
     325                        return false; 
     326    } 
     327     
     328    if ($wpdb->query($query)) { 
     329                do_action('wp_set_comment_status', $comment_id, $comment_status); 
    421330 
    422 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) { 
    423         global $comment; 
    424         $url = get_comment_author_url(); 
    425         $display = ($linktext != '') ? $linktext : $url; 
    426         $return = "$before<a href='$url' rel='external'>$display</a>$after"; 
    427         return apply_filters('get_comment_author_url_link', $return); 
     331                $comment = get_comment($comment_id); 
     332                $comment_post_ID = $comment->comment_post_ID; 
     333                $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); 
     334                if( is_object( $c ) ) 
     335                        $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); 
     336                return true; 
     337    } else { 
     338                return false; 
     339    } 
    428340} 
    429341 
    430 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) { 
    431         echo get_comment_author_url_link( $linktext, $before, $after ); 
    432 
     342function wp_update_comment($commentarr) { 
     343        global $wpdb; 
    433344 
    434 function get_comment_author_IP() { 
    435         global $comment; 
    436         return apply_filters('get_comment_author_IP', $comment->comment_author_IP); 
    437 
     345        // First, get all of the original fields 
     346        $comment = get_comment($commentarr['comment_ID'], ARRAY_A); 
    438347 
    439 function comment_author_IP() { 
    440         echo get_comment_author_IP(); 
    441 
     348        // Escape data pulled from DB. 
     349        foreach ($comment as $key => $value) 
     350                $comment[$key] = $wpdb->escape($value); 
    442351 
    443 function get_comment_text() { 
    444         global $comment; 
    445         return apply_filters('get_comment_text', $comment->comment_content); 
    446 
     352        // Merge old and new fields with new fields overwriting old ones. 
     353        $commentarr = array_merge($comment, $commentarr); 
    447354 
    448 function comment_text() { 
    449         echo apply_filters('comment_text', get_comment_text() ); 
    450 
     355        $commentarr = wp_filter_comment( $commentarr ); 
    451356 
    452 function get_comment_excerpt() { 
    453         global $comment; 
    454         $comment_text = strip_tags($comment->comment_content); 
    455         $blah = explode(' ', $comment_text); 
    456         if (count($blah) > 20) { 
    457                 $k = 20; 
    458                 $use_dotdotdot = 1; 
    459         } else { 
    460                 $k = count($blah); 
    461                 $use_dotdotdot = 0; 
    462         } 
    463         $excerpt = ''; 
    464         for ($i=0; $i<$k; $i++) { 
    465                 $excerpt .= $blah[$i] . ' '; 
    466         } 
    467         $excerpt .= ($use_dotdotdot) ? '...' : ''; 
    468         return apply_filters('get_comment_excerpt', $excerpt); 
    469 
     357        // Now extract the merged array. 
     358        extract($commentarr); 
    470359 
    471 function comment_excerpt() { 
    472         echo apply_filters('comment_excerpt', get_comment_excerpt() ); 
    473 
     360        $comment_content = apply_filters('comment_save_pre', $comment_content); 
    474361 
    475 function get_comment_date( $d = '' ) { 
    476         global $comment; 
    477         if ( '' == $d ) 
    478                 $date = mysql2date( get_settings('date_format'), $comment->comment_date); 
    479         else 
    480                 $date = mysql2date($d, $comment->comment_date); 
    481         return apply_filters('get_comment_date', $date); 
    482 
     362        $result = $wpdb->query( 
     363                "UPDATE $wpdb->comments SET 
     364                        comment_content = '$comment_content', 
     365                        comment_author = '$comment_author', 
     366                        comment_author_email = '$comment_author_email', 
     367                        comment_approved = '$comment_approved', 
     368                        comment_author_url = '$comment_author_url', 
     369                        comment_date = '$comment_date' 
     370                WHERE comment_ID = $comment_ID" ); 
    483371 
    484 function comment_date( $d = '' ) { 
    485         echo get_comment_date( $d ); 
    486 
     372        $rval = $wpdb->rows_affected; 
    487373 
    488 function get_comment_time( $d = '', $gmt = false ) { 
    489         global $comment; 
    490         $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date; 
    491         if ( '' == $d ) 
    492                 $date = mysql2date(get_settings('time_format'), $comment_date); 
    493         else 
    494                 $date = mysql2date($d, $comment_date); 
    495         return apply_filters('get_comment_time', $date); 
    496 
     374        $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); 
     375        if( is_object( $c ) ) 
     376                $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); 
    497377 
    498 function comment_time( $d = '' ) { 
    499         echo get_comment_time($d); 
    500 
     378        do_action('edit_comment', $comment_ID); 
    501379 
    502 function get_trackback_url() { 
    503         global $id; 
    504         $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id; 
    505  
    506         if ( '' != get_settings('permalink_structure') ) 
    507                 $tb_url = trailingslashit(get_permalink()) . 'trackback/'; 
    508  
    509         return $tb_url; 
     380        return $rval; 
    510381} 
    511 function trackback_url( $display = true ) { 
    512         if ( $display) 
    513                 echo get_trackback_url(); 
    514         else 
    515                 return get_trackback_url(); 
    516 } 
    517382 
    518 function trackback_rdf($timezone = 0) { 
    519         global $id; 
    520         if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) { 
    521         echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  
    522             xmlns:dc="http://purl.org/dc/elements/1.1/" 
    523             xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> 
    524                 <rdf:Description rdf:about="'; 
    525         the_permalink(); 
    526         echo '"'."\n"; 
    527         echo '    dc:identifier="'; 
    528         the_permalink(); 
    529         echo '"'."\n"; 
    530         echo '    dc:title="'.str_replace('--', '&#x2d;&#x2d;', wptexturize(strip_tags(get_the_title()))).'"'."\n"; 
    531         echo '    trackback:ping="'.trackback_url(0).'"'." />\n"; 
    532         echo '</rdf:RDF>'; 
    533         } 
    534 } 
    535  
    536 function comments_open() { 
    537         global $post; 
    538         if ( 'open' == $post->comment_status ) 
    539                 return true; 
    540         else 
    541                 return false; 
    542 } 
    543  
    544 function pings_open() { 
    545         global $post; 
    546         if ( 'open' == $post->ping_status )  
    547                 return true; 
    548         else 
    549                 return false; 
    550 } 
    551  
    552 // Non-template functions 
    553  
    554 function get_lastcommentmodified($timezone = 'server') { 
    555         global $cache_lastcommentmodified, $pagenow, $wpdb; 
    556         $add_seconds_blog = get_settings('gmt_offset') * 3600; 
    557         $add_seconds_server = date('Z'); 
    558         $now = current_time('mysql', 1); 
    559         if ( !isset($cache_lastcommentmodified[$timezone]) ) { 
    560                 switch(strtolower($timezone)) { 
    561                         case 'gmt': 
    562                                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); 
    563                                 break; 
    564                         case 'blog': 
    565                                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1"); 
    566                                 break; 
    567                         case 'server': 
    568                                 $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"); 
    569                                 break; 
    570                 } 
    571                 $cache_lastcommentmodified[$timezone] = $lastcommentmodified; 
    572         } else { 
    573                 $lastcommentmodified = $cache_lastcommentmodified[$timezone]; 
    574         } 
    575         return $lastcommentmodified; 
    576 } 
    577  
    578 function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries 
    579         global $postc, $id, $commentdata, $wpdb; 
    580         if ($no_cache) { 
    581                 $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'"; 
    582                 if (false == $include_unapproved) { 
    583                     $query .= " AND comment_approved = '1'"; 
    584                 } 
    585                 $myrow = $wpdb->get_row($query, ARRAY_A); 
    586         } else { 
    587                 $myrow['comment_ID'] = $postc->comment_ID; 
    588                 $myrow['comment_post_ID'] = $postc->comment_post_ID; 
    589                 $myrow['comment_author'] = $postc->comment_author; 
    590                 $myrow['comment_author_email'] = $postc->comment_author_email; 
    591                 $myrow['comment_author_url'] = $postc->comment_author_url; 
    592                 $myrow['comment_author_IP'] = $postc->comment_author_IP; 
    593                 $myrow['comment_date'] = $postc->comment_date; 
    594                 $myrow['comment_content'] = $postc->comment_content; 
    595                 $myrow['comment_karma'] = $postc->comment_karma; 
    596                 $myrow['comment_approved'] = $postc->comment_approved; 
    597                 $myrow['comment_type'] = $postc->comment_type; 
    598         } 
    599         return $myrow; 
    600 } 
    601  
    602383function pingback($content, $post_ID) { 
    603384        global $wp_version, $wpdb; 
    604385        include_once (ABSPATH . WPINC . '/class-IXR.php'); 
     
    782563        return false; 
    783564} 
    784565 
    785 function wp_set_comment_status($comment_id, $comment_status) { 
    786     global $wpdb; 
    787  
    788     switch($comment_status) { 
    789                 case 'hold': 
    790                         $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1"; 
    791                 break; 
    792                 case 'approve': 
    793                         $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1"; 
    794                 break; 
    795                 case 'spam': 
    796                         $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1"; 
    797                 break; 
    798                 case 'delete': 
    799                         return wp_delete_comment($comment_id); 
    800                 break; 
    801                 default: 
    802                         return false; 
    803     } 
    804      
    805     if ($wpdb->query($query)) { 
    806                 do_action('wp_set_comment_status', $comment_id, $comment_status); 
    807  
    808                 $comment = get_comment($comment_id); 
    809                 $comment_post_ID = $comment->comment_post_ID; 
    810                 $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); 
    811                 if( is_object( $c ) ) 
    812                         $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); 
    813                 return true; 
    814     } else { 
    815                 return false; 
    816     } 
    817 } 
    818  
    819 function wp_get_comment_status($comment_id) { 
    820         global $wpdb; 
    821  
    822         $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1"); 
    823         if ($result == NULL) { 
    824                 return 'deleted'; 
    825         } else if ($result == '1') { 
    826                 return 'approved'; 
    827         } else if ($result == '0') { 
    828                 return 'unapproved'; 
    829         } else if ($result == 'spam') { 
    830                 return 'spam'; 
    831         } else { 
    832                 return false; 
    833         } 
    834 } 
    835  
    836 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { 
    837         global $wpdb; 
    838  
    839         if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual 
    840  
    841         if ( (count(explode('http:', $comment)) - 1) >= get_settings('comment_max_links') ) 
    842                 return false; // Check # of external links 
    843  
    844         $mod_keys = trim( get_settings('moderation_keys') ); 
    845         if ( !empty($mod_keys) ) { 
    846                 $words = explode("\n", $mod_keys ); 
    847  
    848                 foreach ($words as $word) { 
    849                         $word = trim($word); 
    850  
    851                         // Skip empty lines 
    852                         if (empty($word)) { continue; } 
    853  
    854                         // Do some escaping magic so that '#' chars in the  
    855                         // spam words don't break things: 
    856                         $word = preg_quote($word, '#'); 
    857  
    858                         $pattern = "#$word#i";  
    859                         if ( preg_match($pattern, $author) ) return false; 
    860                         if ( preg_match($pattern, $email) ) return false; 
    861                         if ( preg_match($pattern, $url) ) return false; 
    862                         if ( preg_match($pattern, $comment) ) return false; 
    863                         if ( preg_match($pattern, $user_ip) ) return false; 
    864                         if ( preg_match($pattern, $user_agent) ) return false; 
    865                 } 
    866         } 
    867  
    868         // Comment whitelisting: 
    869         if ( 1 == get_settings('comment_whitelist')) { 
    870                 if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll 
    871                         $uri = parse_url($url); 
    872                         $domain = $uri['host']; 
    873                         $uri = parse_url( get_option('home') ); 
    874                         $home_domain = $uri['host']; 
    875                         if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain ) 
    876                                 return true; 
    877                         else 
    878                                 return false; 
    879                 } elseif( $author != '' && $email != '' ) { 
    880