WordPressを読む 45-1 /blog/wp-includes/comment.php 1
2015/01/11
目次
/blog/wp-includes/comment.php 1
関数 check_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 | <?php /** * Manages WordPress comments * * @package WordPress * @subpackage Comment */ /** * Checks whether a comment passes internal checks to be allowed to add. * * If comment moderation is set in the administration, then all comments, * regardless of their type and whitelist will be set to false. If the number of * links exceeds the amount in the administration, then the check fails. If any * of the parameter contents match the blacklist of words, then the check fails. * * If the number of links exceeds the amount in the administration, then the * check fails. If any of the parameter contents match the blacklist of words, * then the check fails. * * If the comment author was approved before, then the comment is * automatically whitelisted. * * If none of the checks fail, then the failback is to set the check to pass * (return true). * * @since 1.2.0 * @uses $wpdb * * @param string $author Comment Author's name * @param string $email Comment Author's email * @param string $url Comment Author's URL * @param string $comment Comment contents * @param string $user_ip Comment Author's IP address * @param string $user_agent Comment Author's User Agent * @param string $comment_type Comment type, either user submitted comment, * trackback, or pingback * @return bool Whether the checks passed (true) and the comments should be * displayed or set to moderated */ function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { global $wpdb; if ( 1 == get_option('comment_moderation') ) return false; // If moderation is set to manual /** This filter is documented in wp-includes/comment-template.php */ $comment = apply_filters( 'comment_text', $comment ); // Check # of external links if ( $max_links = get_option( 'comment_max_links' ) ) { $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out ); /** * Filter the maximum number of links allowed in a comment. * * @since 3.0.0 * * @param int $num_links The number of links allowed. * @param string $url Comment author's URL. Included in allowed links total. */ $num_links = apply_filters( 'comment_max_links_url', $num_links, $url ); if ( $num_links >= $max_links ) return false; } $mod_keys = trim(get_option('moderation_keys')); if ( !empty($mod_keys) ) { $words = explode("\n", $mod_keys ); foreach ( (array) $words as $word) { $word = trim($word); // Skip empty lines if ( empty($word) ) continue; // Do some escaping magic so that '#' chars in the // spam words don't break things: $word = preg_quote($word, '#'); $pattern = "#$word#i"; if ( preg_match($pattern, $author) ) return false; if ( preg_match($pattern, $email) ) return false; if ( preg_match($pattern, $url) ) return false; if ( preg_match($pattern, $comment) ) return false; if ( preg_match($pattern, $user_ip) ) return false; if ( preg_match($pattern, $user_agent) ) return false; } } // Comment whitelisting: if ( 1 == get_option('comment_whitelist')) { if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) { // expected_slashed ($author, $email) $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"); if ( ( 1 == $ok_to_comment ) && ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) ) return true; else return false; } else { return false; } } return true; } |
関数 get_approved_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Retrieve the approved comments for post $post_id. * * @since 2.0.0 * @uses $wpdb * * @param int $post_id The ID of the post * @return array $comments The approved comments */ function get_approved_comments($post_id) { global $wpdb; return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id)); } |
関数 get_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 | /** * Retrieves comment data given a comment ID or comment object. * * If an object is passed then the comment data will be cached and then returned * after being passed through a filter. If the comment is empty, then the global * comment variable will be used, if it is set. * * @since 2.0.0 * @uses $wpdb * * @param object|string|int $comment Comment to retrieve. * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants. * @return object|array|null Depends on $output value. */ function get_comment(&$comment, $output = OBJECT) { global $wpdb; if ( empty($comment) ) { if ( isset($GLOBALS['comment']) ) $_comment = & $GLOBALS['comment']; else $_comment = null; } elseif ( is_object($comment) ) { wp_cache_add($comment->comment_ID, $comment, 'comment'); $_comment = $comment; } else { if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) { $_comment = & $GLOBALS['comment']; } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) { $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment)); if ( ! $_comment ) return null; wp_cache_add($_comment->comment_ID, $_comment, 'comment'); } } /** * Fires after a comment is retrieved. * * @since 2.3.0 * * @param mixed $_comment Comment data. */ $_comment = apply_filters( 'get_comment', $_comment ); if ( $output == OBJECT ) { return $_comment; } elseif ( $output == ARRAY_A ) { $__comment = get_object_vars($_comment); return $__comment; } elseif ( $output == ARRAY_N ) { $__comment = array_values(get_object_vars($_comment)); return $__comment; } else { return $_comment; } } |
関数 get_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Retrieve a list of comments. * * The comment list can be for the blog as a whole or for an individual post. * * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt', * 'order', 'number', 'offset', and 'post_id'. * * @since 2.7.0 * @uses $wpdb * * @param mixed $args Optional. Array or string of options to override defaults. * @return array List of comments. */ function get_comments( $args = '' ) { $query = new WP_Comment_Query; return $query->query( $args ); } |
クラス WP_Comment_Query::クラス変数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * WordPress Comment Query class. * * @since 3.1.0 */ class WP_Comment_Query { /** * Metadata query container * * @since 3.5.0 * @access public * @var object WP_Meta_Query */ public $meta_query = false; /** * Date query container * * @since 3.7.0 * @access public * @var object WP_Date_Query */ public $date_query = false; |
クラス WP_Comment_Query::__call()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Make private/protected methods readable for backwards compatibility. * * @since 4.0.0 * @access public * * @param callable $name Method to call. * @param array $arguments Arguments to pass when calling. * @return mixed|bool Return value of the callback, false otherwise. */ public function __call( $name, $arguments ) { return call_user_func_array( array( $this, $name ), $arguments ); } |
クラス WP_Comment_Query::query()
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | /** * Execute the query * * @since 3.1.0 * * @param string|array $query_vars * @return int|array */ public function query( $query_vars ) { global $wpdb; $defaults = array( 'author_email' => '', 'fields' => '', 'ID' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_ID' => '', 'post_id' => 0, 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => '', 'type' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null, // See WP_Date_Query ); $groupby = ''; $this->query_vars = wp_parse_args( $query_vars, $defaults ); // Parse meta query $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $this->query_vars ); /** * Fires before comments are retrieved. * * @since 3.1.0 * * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. */ do_action_ref_array( 'pre_get_comments', array( &$this ) ); // $args can be whatever, only use the args defined in defaults to compute the key $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) ) ); $last_changed = wp_cache_get( 'last_changed', 'comment' ); if ( ! $last_changed ) { $last_changed = microtime(); wp_cache_set( 'last_changed', $last_changed, 'comment' ); } $cache_key = "get_comments:$key:$last_changed"; if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { return $cache; } $status = $this->query_vars['status']; if ( 'hold' == $status ) { $approved = "comment_approved = '0'"; } elseif ( 'approve' == $status ) { $approved = "comment_approved = '1'"; } elseif ( ! empty( $status ) && 'all' != $status ) { $approved = $wpdb->prepare( "comment_approved = %s", $status ); } else { $approved = "( comment_approved = '0' OR comment_approved = '1' )"; } $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; if ( ! empty( $this->query_vars['orderby'] ) ) { $ordersby = is_array( $this->query_vars['orderby'] ) ? $this->query_vars['orderby'] : preg_split( '/[,\s]/', $this->query_vars['orderby'] ); $allowed_keys = array( 'comment_agent', 'comment_approved', 'comment_author', 'comment_author_email', 'comment_author_IP', 'comment_author_url', 'comment_content', 'comment_date', 'comment_date_gmt', 'comment_ID', 'comment_karma', 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id', ); if ( ! empty( $this->query_vars['meta_key'] ) ) { $allowed_keys[] = $this->query_vars['meta_key']; $allowed_keys[] = 'meta_value'; $allowed_keys[] = 'meta_value_num'; } $ordersby = array_intersect( $ordersby, $allowed_keys ); foreach ( $ordersby as $key => $value ) { if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) { $ordersby[ $key ] = "$wpdb->commentmeta.meta_value"; } elseif ( $value == 'meta_value_num' ) { $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0"; } } $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); } else { $orderby = 'comment_date_gmt'; } $number = absint( $this->query_vars['number'] ); $offset = absint( $this->query_vars['offset'] ); if ( ! empty( $number ) ) { if ( $offset ) { $limits = 'LIMIT ' . $offset . ',' . $number; } else { $limits = 'LIMIT ' . $number; } } else { $limits = ''; } if ( $this->query_vars['count'] ) { $fields = 'COUNT(*)'; } else { switch ( strtolower( $this->query_vars['fields'] ) ) { case 'ids': $fields = "$wpdb->comments.comment_ID"; break; default: $fields = "*"; break; } } $join = ''; $where = $approved; $post_id = absint( $this->query_vars['post_id'] ); if ( ! empty( $post_id ) ) { $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id ); } if ( '' !== $this->query_vars['author_email'] ) { $where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] ); } if ( '' !== $this->query_vars['karma'] ) { $where .= $wpdb->prepare( ' AND comment_karma = %d', $this->query_vars['karma'] ); } if ( 'comment' == $this->query_vars['type'] ) { $where .= " AND comment_type = ''"; } elseif( 'pings' == $this->query_vars['type'] ) { $where .= ' AND comment_type IN ("pingback", "trackback")'; } elseif ( ! empty( $this->query_vars['type'] ) ) { $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] ); } if ( '' !== $this->query_vars['parent'] ) { $where .= $wpdb->prepare( ' AND comment_parent = %d', $this->query_vars['parent'] ); } if ( is_array( $this->query_vars['user_id'] ) ) { $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; } elseif ( '' !== $this->query_vars['user_id'] ) { $where .= $wpdb->prepare( ' AND user_id = %d', $this->query_vars['user_id'] ); } if ( '' !== $this->query_vars['search'] ) { $where .= $this->get_search_sql( $this->query_vars['search'], array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) ); } $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) ); $post_fields = array_filter( $plucked ); if ( ! empty( $post_fields ) ) { $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; foreach( $post_fields as $field_name => $field_value ) $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); } if ( ! empty( $this->meta_query->queries ) ) { $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); $join .= $clauses['join']; $where .= $clauses['where']; $groupby = "{$wpdb->comments}.comment_ID"; } $date_query = $this->query_vars['date_query']; if ( ! empty( $date_query ) && is_array( $date_query ) ) { $date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); $where .= $date_query_object->get_sql(); } $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' ); /** * Filter the comment query clauses. * * @since 3.1.0 * * @param array $pieces A compacted array of comment query clauses. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. */ $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; if ( $groupby ) { $groupby = 'GROUP BY ' . $groupby; } $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; if ( $this->query_vars['count'] ) { return $wpdb->get_var( $query ); } if ( 'ids' == $this->query_vars['fields'] ) { $this->comments = $wpdb->get_col( $query ); return array_map( 'intval', $this->comments ); } $results = $wpdb->get_results( $query ); /** * Filter the comment query results. * * @since 3.1.0 * * @param array $results An array of comments. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. */ $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); wp_cache_add( $cache_key, $comments, 'comment' ); return $comments; } |
クラス WP_Comment_Query::クラス定義終了