📄 comment-functions.php
字号:
<?php// Template functionsfunction comments_template( $file = '/comments.php' ) { global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity; if ( ! (is_single() || is_page() || $withcomments) ) return; $req = get_settings('require_name_email'); $commenter = wp_get_current_commenter(); extract($commenter); // TODO: Use API instead of SELECTs. if ( empty($comment_author) ) { $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date"); } else { $author_db = $wpdb->escape($comment_author); $email_db = $wpdb->escape($comment_author_email); $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"); } define('COMMENTS_TEMPLATE', true); $include = apply_filters('comments_template', TEMPLATEPATH . $file ); if ( file_exists( $include ) ) require( $include ); else require( ABSPATH . 'wp-content/themes/default/comments.php');}function wp_new_comment( $commentdata ) { $commentdata = apply_filters('preprocess_comment', $commentdata); $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID']; $commentdata['user_ID'] = (int) $commentdata['user_ID']; $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT']; $commentdata['comment_date'] = current_time('mysql'); $commentdata['comment_date_gmt'] = current_time('mysql', 1); $commentdata = wp_filter_comment($commentdata); $commentdata['comment_approved'] = wp_allow_comment($commentdata); $comment_ID = wp_insert_comment($commentdata); do_action('comment_post', $comment_ID, $commentdata['comment_approved']); if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching if ( '0' == $commentdata['comment_approved'] ) wp_notify_moderator($comment_ID); $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment if ( get_settings('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) wp_notify_postauthor($comment_ID, $commentdata['comment_type']); } return $comment_ID;}function wp_insert_comment($commentdata) { global $wpdb; extract($commentdata); if ( ! isset($comment_author_IP) ) $comment_author_IP = $_SERVER['REMOTE_ADDR']; if ( ! isset($comment_date) ) $comment_date = current_time('mysql'); if ( ! isset($comment_date_gmt) ) $comment_date_gmt = gmdate('Y-m-d H:i:s', strtotime($comment_date) ); if ( ! isset($comment_parent) ) $comment_parent = 0; if ( ! isset($comment_approved) ) $comment_approved = 1; $result = $wpdb->query("INSERT INTO $wpdb->comments (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) VALUES ('$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') "); $id = $wpdb->insert_id; if ( $comment_approved == 1) { $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'"); $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$comment_post_ID'" ); } return $id;}function wp_filter_comment($commentdata) { $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']); $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']); $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']); $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']); $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']); $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']); $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']); $commentdata['filtered'] = true; return $commentdata;}function wp_allow_comment($commentdata) { global $wpdb; extract($commentdata); $comment_user_domain = apply_filters('pre_comment_user_domain', gethostbyaddr($comment_author_IP) ); // Simple duplicate check $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' "; $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; if ( $wpdb->get_var($dupe) ) die( __('Duplicate comment detected; it looks as though you\'ve already said that!') ); // Simple flood-protection if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$comment_author_IP' OR comment_author_email = '$comment_author_email' ORDER BY comment_date DESC LIMIT 1") ) { $time_lastcomment = mysql2date('U', $lasttime); $time_newcomment = mysql2date('U', $comment_date_gmt); if ( ($time_newcomment - $time_lastcomment) < 15 ) { do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment); die( __('Sorry, you can only post a new comment once every 15 seconds. Slow down cowboy.') ); } } if ( $user_id ) { $userdata = get_userdata($user_id); $user = new WP_User($user_id); $post_author = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = '$comment_post_ID' LIMIT 1"); } // The author and the admins get respect. if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) { $approved = 1; } // Everyone else's comments will be checked. else { if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) ) $approved = 1; else $approved = 0; if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) ) $approved = 'spam'; } $approved = apply_filters('pre_comment_approved', $approved); return $approved;}function wp_update_comment($commentarr) { global $wpdb; // First, get all of the original fields $comment = get_comment($commentarr['comment_ID'], ARRAY_A); // Escape data pulled from DB. foreach ($comment as $key => $value) $comment[$key] = $wpdb->escape($value); // Merge old and new fields with new fields overwriting old ones. $commentarr = array_merge($comment, $commentarr); $commentarr = wp_filter_comment( $commentarr ); // Now extract the merged array. extract($commentarr); $comment_content = apply_filters('comment_save_pre', $comment_content); $result = $wpdb->query( "UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author', comment_author_email = '$comment_author_email', comment_approved = '$comment_approved', comment_author_url = '$comment_author_url', comment_date = '$comment_date' WHERE comment_ID = $comment_ID" ); $rval = $wpdb->rows_affected; $c = $wpdb->get_row( "SELECT count(*) as c FROM {$wpdb->comments} WHERE comment_post_ID = '$comment_post_ID' AND comment_approved = '1'" ); if( is_object( $c ) ) $wpdb->query( "UPDATE $wpdb->posts SET comment_count = '$c->c' WHERE ID = '$comment_post_ID'" ); do_action('edit_comment', $comment_ID); return $rval;}function wp_delete_comment($comment_id) { global $wpdb; do_action('delete_comment', $comment_id); $comment = get_comment($comment_id); if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") ) return false; $post_id = $comment->comment_post_ID; if ( $post_id && $comment->comment_approved == 1 ) { $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'"); $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$post_id'" ); } do_action('wp_set_comment_status', $comment_id, 'delete'); return true;}function clean_url( $url ) { if ('' == $url) return $url; $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $url); $url = str_replace(';//', '://', $url); $url = (!strstr($url, '://')) ? 'http://'.$url : $url; $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url); return $url;}function get_comments_number( $post_id = 0 ) { global $wpdb, $comment_count_cache, $id; $post_id = (int) $post_id; if ( !$post_id ) $post_id = $id; if ( !isset($comment_count_cache[$post_id]) ) $comment_count_cache[$id] = $wpdb->get_var("SELECT comment_count FROM $wpdb->posts WHERE ID = '$post_id'"); return apply_filters('get_comments_number', $comment_count_cache[$post_id]);}function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) { global $id, $comment; $number = get_comments_number( $id ); if ($number == 0) { $blah = $zero; } elseif ($number == 1) { $blah = $one; } elseif ($number > 1) { $blah = str_replace('%', $number, $more); } echo apply_filters('comments_number', $blah);}function get_comments_link() { return get_permalink() . '#comments';}function get_comment_link() { global $comment; return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;}function comments_link( $file = '', $echo = true ) { echo get_comments_link();}function comments_popup_script($width=400, $height=400, $file='') { global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript; if (empty ($file)) { $wpcommentspopupfile = ''; // Use the index. } else { $wpcommentspopupfile = $file; } $wpcommentsjavascript = 1; $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"; echo $javascript;}function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') { global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb; global $comment_count_cache; if (! is_single() && ! is_page()) { if ( !isset($comment_count_cache[$id]) ) $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';"); $number = $comment_count_cache[$id]; if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) { echo $none; return; } else { if (!empty($post->post_password)) { // if there's a password if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie echo(__('Enter your password to view comments')); return; } } echo '<a href="'; if ($wpcommentsjavascript) { if ( empty($wpcommentspopupfile) ) $home = get_settings('home'); else $home = get_settings('siteurl'); echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id; echo '" onclick="wpopen(this.href); return false"'; } else { // if comments_popup_script() is not in the template, display simple comment link if ( 0 == $number ) echo get_permalink() . '#respond'; else comments_link(); echo '"';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -