WordPressを読む 45-2 /blog/wp-includes/comment.php 2
2015/01/11
目次
- 1 /blog/wp-includes/comment.php 2
- 2 WP_Comment_Query::get_search_sql()
- 3 関数 get_comment_statuses()
- 4 関数 get_lastcommentmodified()
- 5 関数 get_comment_count()
- 6 関数 add_comment_meta()
- 7 関数 delete_comment_meta()
- 8 関数 get_comment_meta()
- 9 関数 update_comment_meta()
- 10 関数 wp_set_comment_cookies()
- 11 関数 sanitize_comment_cookies()
- 12 関数 wp_allow_comment()
- 13 関数 check_comment_flood_db()
- 14 関数 separate_comments()
/blog/wp-includes/comment.php 2
WP_Comment_Query::get_search_sql()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Used internally to generate an SQL string for searching across multiple columns * * @access protected * @since 3.1.0 * * @param string $string * @param array $cols * @return string */ protected function get_search_sql( $string, $cols ) { global $wpdb; $like = '%' . $wpdb->esc_like( $string ) . '%'; $searches = array(); foreach ( $cols as $col ) { $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); } return ' AND (' . implode(' OR ', $searches) . ')'; } } |
WP_Comment_Query::クラス定義終了
関数 get_comment_statuses()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Retrieve all of the WordPress supported comment statuses. * * Comments have a limited set of valid status values, this provides the comment * status values and descriptions. * * @since 2.7.0 * * @return array List of comment statuses. */ function get_comment_statuses() { $status = array( 'hold' => __('Unapproved'), /* translators: comment status */ 'approve' => _x('Approved', 'adjective'), /* translators: comment status */ 'spam' => _x('Spam', 'adjective'), ); return $status; } |
関数 get_lastcommentmodified()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * The date the last comment was modified. * * @since 1.5.0 * @uses $wpdb * * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', * or 'server' locations. * @return string Last comment modified date. */ function get_lastcommentmodified($timezone = 'server') { global $wpdb; static $cache_lastcommentmodified = array(); if ( isset($cache_lastcommentmodified[$timezone]) ) return $cache_lastcommentmodified[$timezone]; $add_seconds_server = date('Z'); switch ( strtolower($timezone)) { case 'gmt': $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1"); break; case 'blog': $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1"); break; case 'server': $lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server)); break; } $cache_lastcommentmodified[$timezone] = $lastcommentmodified; return $lastcommentmodified; } |
関数 get_comment_count()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /** * The amount of comments in a post or total comments. * * A lot like {@link wp_count_comments()}, in that they both return comment * stats (albeit with different types). The {@link wp_count_comments()} actual * caches, but this function does not. * * @since 2.0.0 * @uses $wpdb * * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide. * @return array The amount of spam, approved, awaiting moderation, and total comments. */ function get_comment_count( $post_id = 0 ) { global $wpdb; $post_id = (int) $post_id; $where = ''; if ( $post_id > 0 ) { $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id); } $totals = (array) $wpdb->get_results(" SELECT comment_approved, COUNT( * ) AS total FROM {$wpdb->comments} {$where} GROUP BY comment_approved ", ARRAY_A); $comment_count = array( "approved" => 0, "awaiting_moderation" => 0, "spam" => 0, "total_comments" => 0 ); foreach ( $totals as $row ) { switch ( $row['comment_approved'] ) { case 'spam': $comment_count['spam'] = $row['total']; $comment_count["total_comments"] += $row['total']; break; case 1: $comment_count['approved'] = $row['total']; $comment_count['total_comments'] += $row['total']; break; case 0: $comment_count['awaiting_moderation'] = $row['total']; $comment_count['total_comments'] += $row['total']; break; default: break; } } return $comment_count; } |
関数 add_comment_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // // Comment meta functions // /** * Add meta data field to a comment. * * @since 2.9.0 * @uses add_metadata * @link http://codex.wordpress.org/Function_Reference/add_comment_meta * * @param int $comment_id Comment ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Metadata value. * @param bool $unique Optional, default is false. Whether the same key should not be added. * @return int|bool Meta ID on success, false on failure. */ function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) { return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique); } |
http://www.next-beta.net/blog/7130
関数 delete_comment_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Remove metadata matching criteria from a comment. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching key, if needed. * * @since 2.9.0 * @uses delete_metadata * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta * * @param int $comment_id comment ID * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. * @return bool True on success, false on failure. */ function delete_comment_meta($comment_id, $meta_key, $meta_value = '') { return delete_metadata('comment', $comment_id, $meta_key, $meta_value); } |
関数 get_comment_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Retrieve comment meta field for a comment. * * @since 2.9.0 * @uses get_metadata * @link http://codex.wordpress.org/Function_Reference/get_comment_meta * * @param int $comment_id Comment ID. * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. * @param bool $single Whether to return a single value. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single * is true. */ function get_comment_meta($comment_id, $key = '', $single = false) { return get_metadata('comment', $comment_id, $key, $single); } |
関数 update_comment_meta()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Update comment meta field based on comment ID. * * Use the $prev_value parameter to differentiate between meta fields with the * same key and comment ID. * * If the meta field for the comment does not exist, it will be added. * * @since 2.9.0 * @uses update_metadata * @link http://codex.wordpress.org/Function_Reference/update_comment_meta * * @param int $comment_id Comment ID. * @param string $meta_key Metadata key. * @param mixed $meta_value Metadata value. * @param mixed $prev_value Optional. Previous value to check before removing. * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. */ function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') { return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * Sets the cookies used to store an unauthenticated commentator's identity. Typically used * to recall previous comments by this commentator that are still held in moderation. * * @param object $comment Comment object. * @param object $user Comment author's object. * * @since 3.4.0 */ function wp_set_comment_cookies($comment, $user) { if ( $user->exists() ) return; /** * Filter the lifetime of the comment cookie in seconds. * * @since 2.8.0 * * @param int $seconds Comment cookie lifetime. Default 30000000. */ $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /** * Sanitizes the cookies sent to the user already. * * Will only do anything if the cookies have already been created for the user. * Mostly used after cookies had been sent to use elsewhere. * * @since 2.0.4 */ function sanitize_comment_cookies() { if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) { /** * Filter the comment author's name cookie before it is set. * * When this filter hook is evaluated in wp_filter_comment(), * the comment author's name string is passed. * * @since 1.5.0 * * @param string $author_cookie The comment author name cookie. */ $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] ); $comment_author = wp_unslash($comment_author); $comment_author = esc_attr($comment_author); $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author; } if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) { /** * Filter the comment author's email cookie before it is set. * * When this filter hook is evaluated in wp_filter_comment(), * the comment author's email string is passed. * * @since 1.5.0 * * @param string $author_email_cookie The comment author email cookie. */ $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] ); $comment_author_email = wp_unslash($comment_author_email); $comment_author_email = esc_attr($comment_author_email); $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email; } if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) { /** * Filter the comment author's URL cookie before it is set. * * When this filter hook is evaluated in wp_filter_comment(), * the comment author's URL string is passed. * * @since 1.5.0 * * @param string $author_url_cookie The comment author URL cookie. */ $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] ); $comment_author_url = wp_unslash($comment_author_url); $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url; } } |
関数 wp_allow_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | /** * Validates whether this comment is allowed to be made. * * @since 2.0.0 * @uses $wpdb * * @param array $commentdata Contains information on the comment * @return mixed Signifies the approval status (0|1|'spam') */ function wp_allow_comment( $commentdata ) { global $wpdb; // Simple duplicate check // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) $dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", wp_unslash( $commentdata['comment_post_ID'] ), wp_unslash( $commentdata['comment_parent'] ), wp_unslash( $commentdata['comment_author'] ) ); if ( $commentdata['comment_author_email'] ) { $dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $commentdata['comment_author_email'] ) ); } $dupe .= $wpdb->prepare( ") AND comment_content = %s LIMIT 1", wp_unslash( $commentdata['comment_content'] ) ); if ( $wpdb->get_var( $dupe ) ) { /** * Fires immediately after a duplicate comment is detected. * * @since 3.0.0 * * @param array $commentdata Comment data. */ do_action( 'comment_duplicate_trigger', $commentdata ); if ( defined( 'DOING_AJAX' ) ) { die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); } wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); } /** * Fires immediately before a comment is marked approved. * * Allows checking for comment flooding. * * @since 2.3.0 * * @param string $comment_author_IP Comment author's IP address. * @param string $comment_author_email Comment author's email. * @param string $comment_date_gmt GMT date the comment was posted. */ do_action( 'check_comment_flood', $commentdata['comment_author_IP'], $commentdata['comment_author_email'], $commentdata['comment_date_gmt'] ); if ( ! empty( $commentdata['user_id'] ) ) { $user = get_userdata( $commentdata['user_id'] ); $post_author = $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $commentdata['comment_post_ID'] ) ); } if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { // The author and the admins get respect. $approved = 1; } else { // Everyone else's comments will be checked. if ( check_comment( $commentdata['comment_author'], $commentdata['comment_author_email'], $commentdata['comment_author_url'], $commentdata['comment_content'], $commentdata['comment_author_IP'], $commentdata['comment_agent'], $commentdata['comment_type'] ) ) { $approved = 1; } else { $approved = 0; } if ( wp_blacklist_check( $commentdata['comment_author'], $commentdata['comment_author_email'], $commentdata['comment_author_url'], $commentdata['comment_content'], $commentdata['comment_author_IP'], $commentdata['comment_agent'] ) ) { $approved = 'spam'; } } /** * Filter a comment's approval status before it is set. * * @since 2.1.0 * * @param bool|string $approved The approval status. Accepts 1, 0, or 'spam'. * @param array $commentdata Comment data. */ $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata ); return $approved; } |
関数 check_comment_flood_db()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** * Check whether comment flooding is occurring. * * Won't run, if current user can manage options, so to not block * administrators. * * @since 2.3.0 * @uses $wpdb * * @param string $ip Comment IP. * @param string $email Comment author email address. * @param string $date MySQL time string. */ function check_comment_flood_db( $ip, $email, $date ) { global $wpdb; if ( current_user_can( 'manage_options' ) ) return; // don't throttle admins $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) { $time_lastcomment = mysql2date('U', $lasttime, false); $time_newcomment = mysql2date('U', $date, false); /** * Filter the comment flood status. * * @since 2.1.0 * * @param bool $bool Whether a comment flood is occurring. Default false. * @param int $time_lastcomment Timestamp of when the last comment was posted. * @param int $time_newcomment Timestamp of when the new comment was posted. */ $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment ); if ( $flood_die ) { /** * Fires before the comment flood message is triggered. * * @since 1.5.0 * * @param int $time_lastcomment Timestamp of when the last comment was posted. * @param int $time_newcomment Timestamp of when the new comment was posted. */ do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); if ( defined('DOING_AJAX') ) die( __('You are posting comments too quickly. Slow down.') ); wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) ); } } } |
関数 separate_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Separates an array of comments into an array keyed by comment_type. * * @since 2.7.0 * * @param array $comments Array of comments * @return array Array of comments keyed by comment_type. */ function separate_comments(&$comments) { $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array()); $count = count($comments); for ( $i = 0; $i < $count; $i++ ) { $type = $comments[$i]->comment_type; if ( empty($type) ) $type = 'comment'; $comments_by_type[$type][] = &$comments[$i]; if ( 'trackback' == $type || 'pingback' == $type ) $comments_by_type['pings'][] = &$comments[$i]; } return $comments_by_type; } |