3 * Manages WordPress comments
10 * Checks whether a comment passes internal checks to be allowed to add.
12 * If comment moderation is set in the administration, then all comments,
13 * regardless of their type and whitelist will be set to false. If the number of
14 * links exceeds the amount in the administration, then the check fails. If any
15 * of the parameter contents match the blacklist of words, then the check fails.
17 * If the number of links exceeds the amount in the administration, then the
18 * check fails. If any of the parameter contents match the blacklist of words,
19 * then the check fails.
21 * If the comment author was approved before, then the comment is
22 * automatically whitelisted.
24 * If none of the checks fail, then the failback is to set the check to pass
30 * @param string $author Comment Author's name
31 * @param string $email Comment Author's email
32 * @param string $url Comment Author's URL
33 * @param string $comment Comment contents
34 * @param string $user_ip Comment Author's IP address
35 * @param string $user_agent Comment Author's User Agent
36 * @param string $comment_type Comment type, either user submitted comment,
37 * trackback, or pingback
38 * @return bool Whether the checks passed (true) and the comments should be
39 * displayed or set to moderated
41 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
44 if ( 1 == get_option('comment_moderation') )
45 return false; // If moderation is set to manual
47 /** This filter is documented in wp-includes/comment-template.php */
48 $comment = apply_filters( 'comment_text', $comment );
50 // Check # of external links
51 if ( $max_links = get_option( 'comment_max_links' ) ) {
52 $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
54 * Filter the maximum number of links allowed in a comment.
58 * @param int $num_links The number of links allowed.
59 * @param string $url Comment author's URL. Included in allowed links total.
61 $num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
62 if ( $num_links >= $max_links )
66 $mod_keys = trim(get_option('moderation_keys'));
67 if ( !empty($mod_keys) ) {
68 $words = explode("\n", $mod_keys );
70 foreach ( (array) $words as $word) {
77 // Do some escaping magic so that '#' chars in the
78 // spam words don't break things:
79 $word = preg_quote($word, '#');
81 $pattern = "#$word#i";
82 if ( preg_match($pattern, $author) ) return false;
83 if ( preg_match($pattern, $email) ) return false;
84 if ( preg_match($pattern, $url) ) return false;
85 if ( preg_match($pattern, $comment) ) return false;
86 if ( preg_match($pattern, $user_ip) ) return false;
87 if ( preg_match($pattern, $user_agent) ) return false;
91 // Comment whitelisting:
92 if ( 1 == get_option('comment_whitelist')) {
93 if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
94 // expected_slashed ($author, $email)
95 $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");
96 if ( ( 1 == $ok_to_comment ) &&
97 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
109 * Retrieve the approved comments for post $post_id.
114 * @param int $post_id The ID of the post
115 * @return array $comments The approved comments
117 function get_approved_comments($post_id) {
119 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));
123 * Retrieves comment data given a comment ID or comment object.
125 * If an object is passed then the comment data will be cached and then returned
126 * after being passed through a filter. If the comment is empty, then the global
127 * comment variable will be used, if it is set.
132 * @param object|string|int $comment Comment to retrieve.
133 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
134 * @return object|array|null Depends on $output value.
136 function get_comment(&$comment, $output = OBJECT) {
140 if ( empty($comment) ) {
141 if ( isset($GLOBALS['comment']) )
142 $_comment = & $GLOBALS['comment'];
145 } elseif ( is_object($comment) ) {
146 wp_cache_add($comment->comment_ID, $comment, 'comment');
147 $_comment = $comment;
149 if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
150 $_comment = & $GLOBALS['comment'];
151 } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
152 $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
155 wp_cache_add($_comment->comment_ID, $_comment, 'comment');
160 * Fires after a comment is retrieved.
164 * @param mixed $_comment Comment data.
166 $_comment = apply_filters( 'get_comment', $_comment );
168 if ( $output == OBJECT ) {
170 } elseif ( $output == ARRAY_A ) {
171 $__comment = get_object_vars($_comment);
173 } elseif ( $output == ARRAY_N ) {
174 $__comment = array_values(get_object_vars($_comment));
182 * Retrieve a list of comments.
184 * The comment list can be for the blog as a whole or for an individual post.
186 * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
187 * 'order', 'number', 'offset', and 'post_id'.
192 * @param mixed $args Optional. Array or string of options to override defaults.
193 * @return array List of comments.
195 function get_comments( $args = '' ) {
196 $query = new WP_Comment_Query;
197 return $query->query( $args );
201 * WordPress Comment Query class.
205 class WP_Comment_Query {
207 * Metadata query container
211 * @var object WP_Meta_Query
213 var $meta_query = false;
216 * Date query container
220 * @var object WP_Date_Query
222 var $date_query = false;
229 * @param string|array $query_vars
232 function query( $query_vars ) {
236 'author_email' => '',
259 'date_query' => null, // See WP_Date_Query
264 $this->query_vars = wp_parse_args( $query_vars, $defaults );
267 $this->meta_query = new WP_Meta_Query();
268 $this->meta_query->parse_query_vars( $this->query_vars );
271 * Fires before comments are retrieved.
275 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
277 do_action_ref_array( 'pre_get_comments', array( &$this ) );
278 extract( $this->query_vars, EXTR_SKIP );
280 // $args can be whatever, only use the args defined in defaults to compute the key
281 $key = md5( serialize( compact(array_keys($defaults)) ) );
282 $last_changed = wp_cache_get( 'last_changed', 'comment' );
283 if ( ! $last_changed ) {
284 $last_changed = microtime();
285 wp_cache_set( 'last_changed', $last_changed, 'comment' );
287 $cache_key = "get_comments:$key:$last_changed";
289 if ( $cache = wp_cache_get( $cache_key, 'comment' ) )
292 $post_id = absint($post_id);
294 if ( 'hold' == $status )
295 $approved = "comment_approved = '0'";
296 elseif ( 'approve' == $status )
297 $approved = "comment_approved = '1'";
298 elseif ( ! empty( $status ) && 'all' != $status )
299 $approved = $wpdb->prepare( "comment_approved = %s", $status );
301 $approved = "( comment_approved = '0' OR comment_approved = '1' )";
303 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
305 if ( ! empty( $orderby ) ) {
306 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
307 $allowed_keys = array(
311 'comment_author_email',
313 'comment_author_url',
324 if ( ! empty( $this->query_vars['meta_key'] ) ) {
325 $allowed_keys[] = $this->query_vars['meta_key'];
326 $allowed_keys[] = 'meta_value';
327 $allowed_keys[] = 'meta_value_num';
329 $ordersby = array_intersect( $ordersby, $allowed_keys );
330 foreach ( $ordersby as $key => $value ) {
331 if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) {
332 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
333 } elseif ( $value == 'meta_value_num' ) {
334 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
337 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
339 $orderby = 'comment_date_gmt';
342 $number = absint($number);
343 $offset = absint($offset);
345 if ( !empty($number) ) {
347 $limits = 'LIMIT ' . $offset . ',' . $number;
349 $limits = 'LIMIT ' . $number;
355 $fields = 'COUNT(*)';
362 if ( ! empty($post_id) )
363 $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
364 if ( '' !== $author_email )
365 $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
367 $where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
368 if ( 'comment' == $type ) {
369 $where .= " AND comment_type = ''";
370 } elseif( 'pings' == $type ) {
371 $where .= ' AND comment_type IN ("pingback", "trackback")';
372 } elseif ( ! empty( $type ) ) {
373 $where .= $wpdb->prepare( ' AND comment_type = %s', $type );
375 if ( '' !== $parent )
376 $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
377 if ( '' !== $user_id )
378 $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
379 if ( '' !== $search )
380 $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
382 $post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
383 if ( ! empty( $post_fields ) ) {
384 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
385 foreach( $post_fields as $field_name => $field_value )
386 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
389 if ( ! empty( $this->meta_query->queries ) ) {
390 $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
391 $join .= $clauses['join'];
392 $where .= $clauses['where'];
393 $groupby = "{$wpdb->comments}.comment_ID";
396 if ( ! empty( $date_query ) && is_array( $date_query ) ) {
397 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
398 $where .= $date_query_object->get_sql();
401 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
403 * Filter the comment query clauses.
407 * @param array $pieces A compacted array of comment query clauses.
408 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
410 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
411 foreach ( $pieces as $piece )
412 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
415 $groupby = 'GROUP BY ' . $groupby;
417 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
420 return $wpdb->get_var( $query );
422 $comments = $wpdb->get_results( $query );
424 * Filter the comment query results.
428 * @param array $comments An array of comments.
429 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
431 $comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
433 wp_cache_add( $cache_key, $comments, 'comment' );
439 * Used internally to generate an SQL string for searching across multiple columns
444 * @param string $string
448 function get_search_sql( $string, $cols ) {
449 $string = esc_sql( like_escape( $string ) );
452 foreach ( $cols as $col )
453 $searches[] = "$col LIKE '%$string%'";
455 return ' AND (' . implode(' OR ', $searches) . ')';
460 * Retrieve all of the WordPress supported comment statuses.
462 * Comments have a limited set of valid status values, this provides the comment
463 * status values and descriptions.
469 * @return array List of comment statuses.
471 function get_comment_statuses() {
473 'hold' => __('Unapproved'),
474 /* translators: comment status */
475 'approve' => _x('Approved', 'adjective'),
476 /* translators: comment status */
477 'spam' => _x('Spam', 'adjective'),
484 * The date the last comment was modified.
489 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
490 * or 'server' locations.
491 * @return string Last comment modified date.
493 function get_lastcommentmodified($timezone = 'server') {
495 static $cache_lastcommentmodified = array();
497 if ( isset($cache_lastcommentmodified[$timezone]) )
498 return $cache_lastcommentmodified[$timezone];
500 $add_seconds_server = date('Z');
502 switch ( strtolower($timezone)) {
504 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
507 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
510 $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));
514 $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
516 return $lastcommentmodified;
520 * The amount of comments in a post or total comments.
522 * A lot like {@link wp_count_comments()}, in that they both return comment
523 * stats (albeit with different types). The {@link wp_count_comments()} actual
524 * caches, but this function does not.
529 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
530 * @return array The amount of spam, approved, awaiting moderation, and total comments.
532 function get_comment_count( $post_id = 0 ) {
535 $post_id = (int) $post_id;
538 if ( $post_id > 0 ) {
539 $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
542 $totals = (array) $wpdb->get_results("
543 SELECT comment_approved, COUNT( * ) AS total
544 FROM {$wpdb->comments}
546 GROUP BY comment_approved
549 $comment_count = array(
551 "awaiting_moderation" => 0,
553 "total_comments" => 0
556 foreach ( $totals as $row ) {
557 switch ( $row['comment_approved'] ) {
559 $comment_count['spam'] = $row['total'];
560 $comment_count["total_comments"] += $row['total'];
563 $comment_count['approved'] = $row['total'];
564 $comment_count['total_comments'] += $row['total'];
567 $comment_count['awaiting_moderation'] = $row['total'];
568 $comment_count['total_comments'] += $row['total'];
575 return $comment_count;
579 // Comment meta functions
583 * Add meta data field to a comment.
587 * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
589 * @param int $comment_id Comment ID.
590 * @param string $meta_key Metadata name.
591 * @param mixed $meta_value Metadata value.
592 * @param bool $unique Optional, default is false. Whether the same key should not be added.
593 * @return int|bool Meta ID on success, false on failure.
595 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
596 return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
600 * Remove metadata matching criteria from a comment.
602 * You can match based on the key, or key and value. Removing based on key and
603 * value, will keep from removing duplicate metadata with the same key. It also
604 * allows removing all metadata matching key, if needed.
607 * @uses delete_metadata
608 * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
610 * @param int $comment_id comment ID
611 * @param string $meta_key Metadata name.
612 * @param mixed $meta_value Optional. Metadata value.
613 * @return bool True on success, false on failure.
615 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
616 return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
620 * Retrieve comment meta field for a comment.
624 * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
626 * @param int $comment_id Comment ID.
627 * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
628 * @param bool $single Whether to return a single value.
629 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
632 function get_comment_meta($comment_id, $key = '', $single = false) {
633 return get_metadata('comment', $comment_id, $key, $single);
637 * Update comment meta field based on comment ID.
639 * Use the $prev_value parameter to differentiate between meta fields with the
640 * same key and comment ID.
642 * If the meta field for the comment does not exist, it will be added.
645 * @uses update_metadata
646 * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
648 * @param int $comment_id Comment ID.
649 * @param string $meta_key Metadata key.
650 * @param mixed $meta_value Metadata value.
651 * @param mixed $prev_value Optional. Previous value to check before removing.
652 * @return bool True on success, false on failure.
654 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
655 return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
659 * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
660 * to recall previous comments by this commentator that are still held in moderation.
662 * @param object $comment Comment object.
663 * @param object $user Comment author's object.
667 function wp_set_comment_cookies($comment, $user) {
668 if ( $user->exists() )
672 * Filter the lifetime of the comment cookie in seconds.
676 * @param int $seconds Comment cookie lifetime. Default 30000000.
678 $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
679 setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
680 setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
681 setcookie('comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
685 * Sanitizes the cookies sent to the user already.
687 * Will only do anything if the cookies have already been created for the user.
688 * Mostly used after cookies had been sent to use elsewhere.
692 function sanitize_comment_cookies() {
693 if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
695 * Filter the comment author's name cookie before it is set.
697 * When this filter hook is evaluated in wp_filter_comment(),
698 * the comment author's name string is passed.
702 * @param string $author_cookie The comment author name cookie.
704 $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
705 $comment_author = wp_unslash($comment_author);
706 $comment_author = esc_attr($comment_author);
707 $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
710 if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
712 * Filter the comment author's email cookie before it is set.
714 * When this filter hook is evaluated in wp_filter_comment(),
715 * the comment author's email string is passed.
719 * @param string $author_email_cookie The comment author email cookie.
721 $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
722 $comment_author_email = wp_unslash($comment_author_email);
723 $comment_author_email = esc_attr($comment_author_email);
724 $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
727 if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
729 * Filter the comment author's URL cookie before it is set.
731 * When this filter hook is evaluated in wp_filter_comment(),
732 * the comment author's URL string is passed.
736 * @param string $author_url_cookie The comment author URL cookie.
738 $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
739 $comment_author_url = wp_unslash($comment_author_url);
740 $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
745 * Validates whether this comment is allowed to be made.
749 * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
750 * @uses apply_filters() Calls 'comment_duplicate_trigger' hook on commentdata.
751 * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
753 * @param array $commentdata Contains information on the comment
754 * @return mixed Signifies the approval status (0|1|'spam')
756 function wp_allow_comment($commentdata) {
758 extract($commentdata, EXTR_SKIP);
760 // Simple duplicate check
761 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
762 $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( $comment_post_ID ), wp_unslash( $comment_parent ), wp_unslash( $comment_author ) );
763 if ( $comment_author_email )
764 $dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $comment_author_email ) );
765 $dupe .= $wpdb->prepare( ") AND comment_content = %s LIMIT 1", wp_unslash( $comment_content ) );
766 if ( $wpdb->get_var($dupe) ) {
768 * Fires immediately after a duplicate comment is detected.
772 * @param array $commentdata Comment data.
774 do_action( 'comment_duplicate_trigger', $commentdata );
775 if ( defined('DOING_AJAX') )
776 die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
778 wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
782 * Fires immediately before a comment is marked approved.
784 * Allows checking for comment flooding.
788 * @param string $comment_author_IP Comment author's IP address.
789 * @param string $comment_author_email Comment author's email.
790 * @param string $comment_date_gmt GMT date the comment was posted.
792 do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
794 if ( ! empty( $user_id ) ) {
795 $user = get_userdata( $user_id );
796 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
799 if ( isset( $user ) && ( $user_id == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
800 // The author and the admins get respect.
803 // Everyone else's comments will be checked.
804 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
808 if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
813 * Filter a comment's approval status before it is set.
817 * @param bool|string $approved The approval status. Accepts 1, 0, or 'spam'.
818 * @param array $commentdata Comment data.
820 $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
825 * Check whether comment flooding is occurring.
827 * Won't run, if current user can manage options, so to not block
832 * @uses apply_filters() Calls 'comment_flood_filter' filter with first
833 * parameter false, last comment timestamp, new comment timestamp.
834 * @uses do_action() Calls 'comment_flood_trigger' action with parameters with
835 * last comment timestamp and new comment timestamp.
837 * @param string $ip Comment IP.
838 * @param string $email Comment author email address.
839 * @param string $date MySQL time string.
841 function check_comment_flood_db( $ip, $email, $date ) {
843 if ( current_user_can( 'manage_options' ) )
844 return; // don't throttle admins
845 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
846 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 ) ) ) {
847 $time_lastcomment = mysql2date('U', $lasttime, false);
848 $time_newcomment = mysql2date('U', $date, false);
850 * Filter the comment flood status.
854 * @param bool $bool Whether a comment flood is occurring. Default false.
855 * @param int $time_lastcomment Timestamp of when the last comment was posted.
856 * @param int $time_newcomment Timestamp of when the new comment was posted.
858 $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
861 * Fires before the comment flood message is triggered.
865 * @param int $time_lastcomment Timestamp of when the last comment was posted.
866 * @param int $time_newcomment Timestamp of when the new comment was posted.
868 do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
870 if ( defined('DOING_AJAX') )
871 die( __('You are posting comments too quickly. Slow down.') );
873 wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
879 * Separates an array of comments into an array keyed by comment_type.
883 * @param array $comments Array of comments
884 * @return array Array of comments keyed by comment_type.
886 function separate_comments(&$comments) {
887 $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
888 $count = count($comments);
889 for ( $i = 0; $i < $count; $i++ ) {
890 $type = $comments[$i]->comment_type;
893 $comments_by_type[$type][] = &$comments[$i];
894 if ( 'trackback' == $type || 'pingback' == $type )
895 $comments_by_type['pings'][] = &$comments[$i];
898 return $comments_by_type;
902 * Calculate the total number of comment pages.
905 * @uses get_query_var() Used to fill in the default for $per_page parameter.
906 * @uses get_option() Used to fill in defaults for parameters.
907 * @uses Walker_Comment
909 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
910 * @param int $per_page Optional comments per page.
911 * @param boolean $threaded Optional control over flat or threaded comments.
912 * @return int Number of comment pages.
914 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
917 if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
918 return $wp_query->max_num_comment_pages;
920 if ( !$comments || !is_array($comments) )
921 $comments = $wp_query->comments;
923 if ( empty($comments) )
926 if ( ! get_option( 'page_comments' ) )
929 if ( !isset($per_page) )
930 $per_page = (int) get_query_var('comments_per_page');
931 if ( 0 === $per_page )
932 $per_page = (int) get_option('comments_per_page');
933 if ( 0 === $per_page )
936 if ( !isset($threaded) )
937 $threaded = get_option('thread_comments');
940 $walker = new Walker_Comment;
941 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
943 $count = ceil( count( $comments ) / $per_page );
950 * Calculate what page number a comment will appear on for comment paging.
953 * @uses get_comment() Gets the full comment of the $comment_ID parameter.
954 * @uses get_option() Get various settings to control function and defaults.
955 * @uses get_page_of_comment() Used to loop up to top level comment.
957 * @param int $comment_ID Comment ID.
958 * @param array $args Optional args.
959 * @return int|null Comment page number or null on error.
961 function get_page_of_comment( $comment_ID, $args = array() ) {
964 if ( !$comment = get_comment( $comment_ID ) )
967 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
968 $args = wp_parse_args( $args, $defaults );
970 if ( '' === $args['per_page'] && get_option('page_comments') )
971 $args['per_page'] = get_query_var('comments_per_page');
972 if ( empty($args['per_page']) ) {
973 $args['per_page'] = 0;
976 if ( $args['per_page'] < 1 )
979 if ( '' === $args['max_depth'] ) {
980 if ( get_option('thread_comments') )
981 $args['max_depth'] = get_option('thread_comments_depth');
983 $args['max_depth'] = -1;
986 // Find this comment's top level parent if threading is enabled
987 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
988 return get_page_of_comment( $comment->comment_parent, $args );
990 $allowedtypes = array(
992 'pingback' => 'pingback',
993 'trackback' => 'trackback',
996 $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
998 // Count comments older than this one
999 $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
1001 // No older comments? Then it's page #1.
1002 if ( 0 == $oldercoms )
1005 // Divide comments older than this one by comments per page to get this comment's page number
1006 return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
1010 * Does comment contain blacklisted characters or words.
1013 * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters.
1015 * @param string $author The author of the comment
1016 * @param string $email The email of the comment
1017 * @param string $url The url used in the comment
1018 * @param string $comment The comment content
1019 * @param string $user_ip The comment author IP address
1020 * @param string $user_agent The author's browser user agent
1021 * @return bool True if comment contains blacklisted content, false if comment does not
1023 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
1025 * Fires before the comment is tested for blacklisted characters or words.
1029 * @param string $author Comment author.
1030 * @param string $email Comment author's email.
1031 * @param string $url Comment author's URL.
1032 * @param string $comment Comment content.
1033 * @param string $user_ip Comment author's IP address.
1034 * @param string $user_agent Comment author's browser user agent.
1036 do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
1038 $mod_keys = trim( get_option('blacklist_keys') );
1039 if ( '' == $mod_keys )
1040 return false; // If moderation keys are empty
1041 $words = explode("\n", $mod_keys );
1043 foreach ( (array) $words as $word ) {
1044 $word = trim($word);
1047 if ( empty($word) ) { continue; }
1049 // Do some escaping magic so that '#' chars in the
1050 // spam words don't break things:
1051 $word = preg_quote($word, '#');
1053 $pattern = "#$word#i";
1055 preg_match($pattern, $author)
1056 || preg_match($pattern, $email)
1057 || preg_match($pattern, $url)
1058 || preg_match($pattern, $comment)
1059 || preg_match($pattern, $user_ip)
1060 || preg_match($pattern, $user_agent)
1068 * Retrieve total comments for blog or single post.
1070 * The properties of the returned object contain the 'moderated', 'approved',
1071 * and spam comments for either the entire blog or single post. Those properties
1072 * contain the amount of comments that match the status. The 'total_comments'
1073 * property contains the integer of total comments.
1075 * The comment stats are cached and then retrieved, if they already exist in the
1080 * @param int $post_id Optional. Post ID.
1081 * @return object Comment stats.
1083 function wp_count_comments( $post_id = 0 ) {
1086 $post_id = (int) $post_id;
1089 * Filter the comments count for a given post.
1093 * @param array $count An empty array.
1094 * @param int $post_id The post ID.
1096 $stats = apply_filters( 'wp_count_comments', array(), $post_id );
1097 if ( !empty($stats) )
1100 $count = wp_cache_get("comments-{$post_id}", 'counts');
1102 if ( false !== $count )
1107 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
1109 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
1112 $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
1113 foreach ( (array) $count as $row ) {
1114 // Don't count post-trashed toward totals
1115 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
1116 $total += $row['num_comments'];
1117 if ( isset( $approved[$row['comment_approved']] ) )
1118 $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
1121 $stats['total_comments'] = $total;
1122 foreach ( $approved as $key ) {
1123 if ( empty($stats[$key]) )
1127 $stats = (object) $stats;
1128 wp_cache_set("comments-{$post_id}", $stats, 'counts');
1134 * Trashes or deletes a comment.
1136 * The comment is moved to trash instead of permanently deleted unless trash is
1137 * disabled, item is already in the trash, or $force_delete is true.
1139 * The post comment count will be updated if the comment was approved and has a
1140 * post ID available.
1144 * @uses do_action() Calls 'delete_comment' hook on comment ID
1145 * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success
1146 * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
1147 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1149 * @param int $comment_id Comment ID
1150 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
1151 * @return bool True on success, false on failure.
1153 function wp_delete_comment($comment_id, $force_delete = false) {
1155 if (!$comment = get_comment($comment_id))
1158 if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
1159 return wp_trash_comment($comment_id);
1162 * Fires immediately before a comment is deleted from the database.
1166 * @param int $comment_id The comment ID.
1168 do_action( 'delete_comment', $comment_id );
1170 // Move children up a level.
1171 $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
1172 if ( !empty($children) ) {
1173 $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
1174 clean_comment_cache($children);
1178 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1179 foreach ( $meta_ids as $mid )
1180 delete_metadata_by_mid( 'comment', $mid );
1182 if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1186 * Fires immediately after a comment is deleted from the database.
1190 * @param int $comment_id The comment ID.
1192 do_action( 'deleted_comment', $comment_id );
1194 $post_id = $comment->comment_post_ID;
1195 if ( $post_id && $comment->comment_approved == 1 )
1196 wp_update_comment_count($post_id);
1198 clean_comment_cache($comment_id);
1201 * Fires immediately before changing the comment's status to 'delete'.
1205 * @param int $comment_id The comment ID.
1206 * @param string $status The new 'delete' comment status.
1208 do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1209 wp_transition_comment_status('delete', $comment->comment_approved, $comment);
1214 * Moves a comment to the Trash
1216 * If trash is disabled, comment is permanently deleted.
1219 * @uses do_action() on 'trash_comment' before trashing
1220 * @uses do_action() on 'trashed_comment' after trashing
1221 * @uses wp_delete_comment() if trash is disabled
1223 * @param int $comment_id Comment ID.
1224 * @return bool True on success, false on failure.
1226 function wp_trash_comment($comment_id) {
1227 if ( !EMPTY_TRASH_DAYS )
1228 return wp_delete_comment($comment_id, true);
1230 if ( !$comment = get_comment($comment_id) )
1234 * Fires immediately before a comment is sent to the Trash.
1238 * @param int $comment_id The comment ID.
1240 do_action( 'trash_comment', $comment_id );
1242 if ( wp_set_comment_status($comment_id, 'trash') ) {
1243 add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1244 add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1247 * Fires immediately after a comment is sent to Trash.
1251 * @param int $comment_id The comment ID.
1253 do_action( 'trashed_comment', $comment_id );
1261 * Removes a comment from the Trash
1264 * @uses do_action() on 'untrash_comment' before untrashing
1265 * @uses do_action() on 'untrashed_comment' after untrashing
1267 * @param int $comment_id Comment ID.
1268 * @return bool True on success, false on failure.
1270 function wp_untrash_comment($comment_id) {
1271 if ( ! (int)$comment_id )
1275 * Fires immediately before a comment is restored from the Trash.
1279 * @param int $comment_id The comment ID.
1281 do_action( 'untrash_comment', $comment_id );
1283 $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1284 if ( empty($status) )
1287 if ( wp_set_comment_status($comment_id, $status) ) {
1288 delete_comment_meta($comment_id, '_wp_trash_meta_time');
1289 delete_comment_meta($comment_id, '_wp_trash_meta_status');
1291 * Fires immediately after a comment is restored from the Trash.
1295 * @param int $comment_id The comment ID.
1297 do_action( 'untrashed_comment', $comment_id );
1305 * Marks a comment as Spam
1308 * @uses do_action() on 'spam_comment' before spamming
1309 * @uses do_action() on 'spammed_comment' after spamming
1311 * @param int $comment_id Comment ID.
1312 * @return bool True on success, false on failure.
1314 function wp_spam_comment($comment_id) {
1315 if ( !$comment = get_comment($comment_id) )
1319 * Fires immediately before a comment is marked as Spam.
1323 * @param int $comment_id The comment ID.
1325 do_action( 'spam_comment', $comment_id );
1327 if ( wp_set_comment_status($comment_id, 'spam') ) {
1328 add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1330 * Fires immediately after a comment is marked as Spam.
1334 * @param int $comment_id The comment ID.
1336 do_action( 'spammed_comment', $comment_id );
1344 * Removes a comment from the Spam
1347 * @uses do_action() on 'unspam_comment' before unspamming
1348 * @uses do_action() on 'unspammed_comment' after unspamming
1350 * @param int $comment_id Comment ID.
1351 * @return bool True on success, false on failure.
1353 function wp_unspam_comment($comment_id) {
1354 if ( ! (int)$comment_id )
1358 * Fires immediately before a comment is unmarked as Spam.
1362 * @param int $comment_id The comment ID.
1364 do_action( 'unspam_comment', $comment_id );
1366 $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1367 if ( empty($status) )
1370 if ( wp_set_comment_status($comment_id, $status) ) {
1371 delete_comment_meta($comment_id, '_wp_trash_meta_status');
1373 * Fires immediately after a comment is unmarked as Spam.
1377 * @param int $comment_id The comment ID.
1379 do_action( 'unspammed_comment', $comment_id );
1387 * The status of a comment by ID.
1391 * @param int $comment_id Comment ID
1392 * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
1394 function wp_get_comment_status($comment_id) {
1395 $comment = get_comment($comment_id);
1399 $approved = $comment->comment_approved;
1401 if ( $approved == null )
1403 elseif ( $approved == '1' )
1405 elseif ( $approved == '0' )
1406 return 'unapproved';
1407 elseif ( $approved == 'spam' )
1409 elseif ( $approved == 'trash' )
1416 * Call hooks for when a comment status transition occurs.
1418 * Calls hooks for comment status transitions. If the new comment status is not the same
1419 * as the previous comment status, then two hooks will be ran, the first is
1420 * 'transition_comment_status' with new status, old status, and comment data. The
1421 * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
1422 * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
1425 * The final action will run whether or not the comment statuses are the same. The
1426 * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status
1427 * parameter and COMMENTTYPE is comment_type comment data.
1431 * @param string $new_status New comment status.
1432 * @param string $old_status Previous comment status.
1433 * @param object $comment Comment data.
1435 function wp_transition_comment_status($new_status, $old_status, $comment) {
1437 * Translate raw statuses to human readable formats for the hooks.
1438 * This is not a complete list of comment status, it's only the ones
1439 * that need to be renamed
1441 $comment_statuses = array(
1443 'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
1445 'approve' => 'approved', // wp_set_comment_status() uses "approve"
1447 if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
1448 if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
1451 if ( $new_status != $old_status ) {
1453 * Fires when the comment status is in transition.
1457 * @param int|string $new_status The new comment status.
1458 * @param int|string $old_status The old comment status.
1459 * @param object $comment The comment data.
1461 do_action( 'transition_comment_status', $new_status, $old_status, $comment );
1463 * Fires when the comment status is in transition from one specific status to another.
1465 * The dynamic portions of the hook name, $old_status, and $new_status,
1466 * refer to the old and new comment statuses, respectively.
1470 * @param object $comment Comment object.
1472 do_action( "comment_{$old_status}_to_{$new_status}", $comment );
1475 * Fires when the status of a specific comment type is in transition.
1477 * The dynamic portions of the hook name, $new_status, and $comment->comment_type,
1478 * refer to the new comment status, and the type of comment, respectively.
1480 * Typical comment types include an empty string (standard comment), 'pingback',
1485 * @param int $comment_ID The comment ID.
1486 * @param obj $comment Comment object.
1488 do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
1492 * Get current commenter's name, email, and URL.
1494 * Expects cookies content to already be sanitized. User of this function might
1495 * wish to recheck the returned array for validity.
1497 * @see sanitize_comment_cookies() Use to sanitize cookies
1501 * @return array Comment author, email, url respectively.
1503 function wp_get_current_commenter() {
1504 // Cookies should already be sanitized.
1506 $comment_author = '';
1507 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
1508 $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
1510 $comment_author_email = '';
1511 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
1512 $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
1514 $comment_author_url = '';
1515 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
1516 $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
1519 * Filter the current commenter's name, email, and URL.
1523 * @param string $comment_author Comment author's name.
1524 * @param string $comment_author_email Comment author's email.
1525 * @param string $comment_author_url Comment author's URL.
1527 return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
1531 * Inserts a comment to the database.
1533 * The available comment data key names are 'comment_author_IP', 'comment_date',
1534 * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
1539 * @param array $commentdata Contains information on the comment.
1540 * @return int The new comment's ID.
1542 function wp_insert_comment($commentdata) {
1544 extract(wp_unslash($commentdata), EXTR_SKIP);
1546 if ( ! isset($comment_author_IP) )
1547 $comment_author_IP = '';
1548 if ( ! isset($comment_date) )
1549 $comment_date = current_time('mysql');
1550 if ( ! isset($comment_date_gmt) )
1551 $comment_date_gmt = get_gmt_from_date($comment_date);
1552 if ( ! isset($comment_parent) )
1553 $comment_parent = 0;
1554 if ( ! isset($comment_approved) )
1555 $comment_approved = 1;
1556 if ( ! isset($comment_karma) )
1558 if ( ! isset($user_id) )
1560 if ( ! isset($comment_type) )
1563 $data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
1564 $wpdb->insert($wpdb->comments, $data);
1566 $id = (int) $wpdb->insert_id;
1568 if ( $comment_approved == 1 )
1569 wp_update_comment_count($comment_post_ID);
1571 $comment = get_comment($id);
1574 * Fires immediately after a comment is inserted into the database.
1578 * @param int $id The comment ID.
1579 * @param obj $comment Comment object.
1581 do_action( 'wp_insert_comment', $id, $comment );
1583 wp_cache_set( 'last_changed', microtime(), 'comment' );
1589 * Filters and sanitizes comment data.
1591 * Sets the comment data 'filtered' field to true when finished. This can be
1592 * checked as to whether the comment should be filtered and to keep from
1593 * filtering the same comment more than once.
1597 * @param array $commentdata Contains information on the comment.
1598 * @return array Parsed comment information.
1600 function wp_filter_comment($commentdata) {
1601 if ( isset( $commentdata['user_ID'] ) ) {
1603 * Filter the comment author's user id before it is set.
1605 * The first time this filter is evaluated, 'user_ID' is checked
1606 * (for back-compat), followed by the standard 'user_id' value.
1610 * @param int $user_ID The comment author's user ID.
1612 $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
1613 } elseif ( isset( $commentdata['user_id'] ) ) {
1614 /** This filter is documented in wp-includes/comment.php */
1615 $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
1619 * Filter the comment author's browser user agent before it is set.
1623 * @param int $comment_agent The comment author's browser user agent.
1625 $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
1626 /** This filter is documented in wp-includes/comment.php */
1627 $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
1629 * Filter the comment content before it is set.
1633 * @param int $comment_content The comment content.
1635 $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
1637 * Filter the comment author's IP before it is set.
1641 * @param int $comment_author_ip The comment author's IP.
1643 $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
1644 /** This filter is documented in wp-includes/comment.php */
1645 $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
1646 /** This filter is documented in wp-includes/comment.php */
1647 $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
1648 $commentdata['filtered'] = true;
1649 return $commentdata;
1653 * Whether a comment should be blocked because of comment flood.
1657 * @param bool $block Whether plugin has already blocked comment.
1658 * @param int $time_lastcomment Timestamp for last comment.
1659 * @param int $time_newcomment Timestamp for new comment.
1660 * @return bool Whether comment should be blocked.
1662 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
1663 if ( $block ) // a plugin has already blocked... we'll let that decision stand
1665 if ( ($time_newcomment - $time_lastcomment) < 15 )
1671 * Adds a new comment to the database.
1673 * Filters new comment to ensure that the fields are sanitized and valid before
1674 * inserting comment into database. Calls 'comment_post' action with comment ID
1675 * and whether comment is approved by WordPress. Also has 'preprocess_comment'
1676 * filter for processing the comment data before the function handles it.
1678 * We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure
1679 * that it is properly set, such as in wp-config.php, for your environment.
1680 * See {@link http://core.trac.wordpress.org/ticket/9235}
1683 * @param array $commentdata Contains information on the comment.
1684 * @return int The ID of the comment after adding.
1686 function wp_new_comment( $commentdata ) {
1688 * Filter a comment's data before it is sanitized and inserted into the database.
1692 * @param array $commentdata Comment data.
1694 $commentdata = apply_filters( 'preprocess_comment', $commentdata );
1696 $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
1697 if ( isset($commentdata['user_ID']) )
1698 $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1699 elseif ( isset($commentdata['user_id']) )
1700 $commentdata['user_id'] = (int) $commentdata['user_id'];
1702 $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
1703 $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
1704 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
1706 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
1707 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
1709 $commentdata['comment_date'] = current_time('mysql');
1710 $commentdata['comment_date_gmt'] = current_time('mysql', 1);
1712 $commentdata = wp_filter_comment($commentdata);
1714 $commentdata['comment_approved'] = wp_allow_comment($commentdata);
1716 $comment_ID = wp_insert_comment($commentdata);
1719 * Fires immediately after a comment is inserted into the database.
1723 * @param int $comment_ID The comment ID.
1724 * @param int $comment_approved 1 (true) if the comment is approved, 0 (false) if not.
1726 do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
1728 if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
1729 if ( '0' == $commentdata['comment_approved'] ) {
1730 wp_notify_moderator( $comment_ID );
1733 // wp_notify_postauthor() checks if notifying the author of their own comment.
1734 // By default, it won't, but filters can override this.
1735 if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
1736 wp_notify_postauthor( $comment_ID );
1744 * Sets the status of a comment.
1746 * The 'wp_set_comment_status' action is called after the comment is handled.
1747 * If the comment status is not in the list, then false is returned.
1750 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1752 * @param int $comment_id Comment ID.
1753 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
1754 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
1755 * @return bool|WP_Error True on success, false or WP_Error on failure.
1757 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
1761 switch ( $comment_status ) {
1769 if ( get_option('comments_notify') ) {
1770 wp_notify_postauthor( $comment_id );
1783 $comment_old = clone get_comment($comment_id);
1785 if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
1787 return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
1792 clean_comment_cache($comment_id);
1794 $comment = get_comment($comment_id);
1797 * Fires after a comment status has been updated in the database.
1799 * The hook also fires immediately before comment status transition hooks are fired.
1803 * @param int $comment_id The comment ID.
1804 * @param string|bool $comment_status The comment status. Possible values include 'hold',
1805 * 'approve', 'spam', 'trash', or false.
1807 do_action( 'wp_set_comment_status', $comment_id, $comment_status );
1808 wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
1810 wp_update_comment_count($comment->comment_post_ID);
1816 * Updates an existing comment in the database.
1818 * Filters the comment and makes sure certain fields are valid before updating.
1822 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1824 * @param array $commentarr Contains information on the comment.
1825 * @return int Comment was updated if value is 1, or was not updated if value is 0.
1827 function wp_update_comment($commentarr) {
1830 // First, get all of the original fields
1831 $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
1832 if ( empty( $comment ) )
1835 // Escape data pulled from DB.
1836 $comment = wp_slash($comment);
1838 $old_status = $comment['comment_approved'];
1840 // Merge old and new fields with new fields overwriting old ones.
1841 $commentarr = array_merge($comment, $commentarr);
1843 $commentarr = wp_filter_comment( $commentarr );
1845 // Now extract the merged array.
1846 extract(wp_unslash($commentarr), EXTR_SKIP);
1849 * Filter the comment content before it is updated in the database.
1853 * @param string $comment_content The comment data.
1855 $comment_content = apply_filters( 'comment_save_pre', $comment_content );
1857 $comment_date_gmt = get_gmt_from_date($comment_date);
1859 if ( !isset($comment_approved) )
1860 $comment_approved = 1;
1861 else if ( 'hold' == $comment_approved )
1862 $comment_approved = 0;
1863 else if ( 'approve' == $comment_approved )
1864 $comment_approved = 1;
1866 $data = compact( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
1867 $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
1869 clean_comment_cache($comment_ID);
1870 wp_update_comment_count($comment_post_ID);
1872 * Fires immediately after a comment is updated in the database.
1874 * The hook also fires immediately before comment status transition hooks are fired.
1878 * @param int $comment_ID The comment ID.
1880 do_action( 'edit_comment', $comment_ID );
1881 $comment = get_comment($comment_ID);
1882 wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
1887 * Whether to defer comment counting.
1889 * When setting $defer to true, all post comment counts will not be updated
1890 * until $defer is set to false. When $defer is set to false, then all
1891 * previously deferred updated post comment counts will then be automatically
1892 * updated without having to call wp_update_comment_count() after.
1895 * @staticvar bool $_defer
1897 * @param bool $defer
1900 function wp_defer_comment_counting($defer=null) {
1901 static $_defer = false;
1903 if ( is_bool($defer) ) {
1905 // flush any deferred counts
1907 wp_update_comment_count( null, true );
1914 * Updates the comment count for post(s).
1916 * When $do_deferred is false (is by default) and the comments have been set to
1917 * be deferred, the post_id will be added to a queue, which will be updated at a
1918 * later date and only updated once per post ID.
1920 * If the comments have not be set up to be deferred, then the post will be
1921 * updated. When $do_deferred is set to true, then all previous deferred post
1922 * IDs will be updated along with the current $post_id.
1925 * @see wp_update_comment_count_now() For what could cause a false return value
1927 * @param int $post_id Post ID
1928 * @param bool $do_deferred Whether to process previously deferred post comment counts
1929 * @return bool True on success, false on failure
1931 function wp_update_comment_count($post_id, $do_deferred=false) {
1932 static $_deferred = array();
1934 if ( $do_deferred ) {
1935 $_deferred = array_unique($_deferred);
1936 foreach ( $_deferred as $i => $_post_id ) {
1937 wp_update_comment_count_now($_post_id);
1938 unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
1942 if ( wp_defer_comment_counting() ) {
1943 $_deferred[] = $post_id;
1946 elseif ( $post_id ) {
1947 return wp_update_comment_count_now($post_id);
1953 * Updates the comment count for the post.
1957 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
1958 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
1960 * @param int $post_id Post ID
1961 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
1963 function wp_update_comment_count_now($post_id) {
1965 $post_id = (int) $post_id;
1968 if ( !$post = get_post($post_id) )
1971 $old = (int) $post->comment_count;
1972 $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
1973 $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
1975 clean_post_cache( $post );
1978 * Fires immediately after a post's comment count is updated in the database.
1982 * @param int $post_id Post ID.
1983 * @param int $new The new comment count.
1984 * @param int $old The old comment count.
1986 do_action( 'wp_update_comment_count', $post_id, $new, $old );
1987 /** This action is documented in wp-includes/post.php */
1988 do_action( 'edit_post', $post_id, $post );
1994 // Ping and trackback functions.
1998 * Finds a pingback server URI based on the given URL.
2000 * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
2001 * a check for the x-pingback headers first and returns that, if available. The
2002 * check for the rel="pingback" has more overhead than just the header.
2006 * @param string $url URL to ping.
2007 * @param int $deprecated Not Used.
2008 * @return bool|string False on failure, string containing URI on success.
2010 function discover_pingback_server_uri( $url, $deprecated = '' ) {
2011 if ( !empty( $deprecated ) )
2012 _deprecated_argument( __FUNCTION__, '2.7' );
2014 $pingback_str_dquote = 'rel="pingback"';
2015 $pingback_str_squote = 'rel=\'pingback\'';
2017 /** @todo Should use Filter Extension or custom preg_match instead. */
2018 $parsed_url = parse_url($url);
2020 if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
2023 //Do not search for a pingback server on our own uploads
2024 $uploads_dir = wp_upload_dir();
2025 if ( 0 === strpos($url, $uploads_dir['baseurl']) )
2028 $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2030 if ( is_wp_error( $response ) )
2033 if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
2034 return wp_remote_retrieve_header( $response, 'x-pingback' );
2036 // Not an (x)html, sgml, or xml page, no use going further.
2037 if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
2040 // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
2041 $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2043 if ( is_wp_error( $response ) )
2046 $contents = wp_remote_retrieve_body( $response );
2048 $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
2049 $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
2050 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
2051 $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
2052 $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
2053 $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
2054 $pingback_href_start = $pingback_href_pos+6;
2055 $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
2056 $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
2057 $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
2059 // We may find rel="pingback" but an incomplete pingback URL
2060 if ( $pingback_server_url_len > 0 ) { // We got it!
2061 return $pingback_server_url;
2069 * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
2074 function do_all_pings() {
2078 while ($ping = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
2079 delete_metadata_by_mid( 'post', $ping->meta_id );
2080 pingback( $ping->post_content, $ping->ID );
2084 while ($enclosure = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
2085 delete_metadata_by_mid( 'post', $enclosure->meta_id );
2086 do_enclose( $enclosure->post_content, $enclosure->ID );
2090 $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
2091 if ( is_array($trackbacks) )
2092 foreach ( $trackbacks as $trackback )
2093 do_trackbacks($trackback);
2095 //Do Update Services/Generic Pings
2100 * Perform trackbacks.
2105 * @param int $post_id Post ID to do trackbacks on.
2107 function do_trackbacks($post_id) {
2110 $post = get_post( $post_id );
2111 $to_ping = get_to_ping($post_id);
2112 $pinged = get_pung($post_id);
2113 if ( empty($to_ping) ) {
2114 $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
2118 if ( empty($post->post_excerpt) ) {
2119 /** This filter is documented in wp-admin/post-template.php */
2120 $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
2122 /** This filter is documented in wp-admin/post-template.php */
2123 $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
2126 $excerpt = str_replace(']]>', ']]>', $excerpt);
2127 $excerpt = wp_html_excerpt($excerpt, 252, '…');
2129 /** This filter is documented in wp-includes/post-template.php */
2130 $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
2131 $post_title = strip_tags($post_title);
2134 foreach ( (array) $to_ping as $tb_ping ) {
2135 $tb_ping = trim($tb_ping);
2136 if ( !in_array($tb_ping, $pinged) ) {
2137 trackback($tb_ping, $post_title, $excerpt, $post_id);
2138 $pinged[] = $tb_ping;
2140 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
2147 * Sends pings to all of the ping site services.
2151 * @param int $post_id Post ID. Not actually used.
2152 * @return int Same as Post ID from parameter
2154 function generic_ping($post_id = 0) {
2155 $services = get_option('ping_sites');
2157 $services = explode("\n", $services);
2158 foreach ( (array) $services as $service ) {
2159 $service = trim($service);
2160 if ( '' != $service )
2161 weblog_ping($service);
2168 * Pings back the links found in a post.
2174 * @param string $content Post content to check for links.
2175 * @param int $post_ID Post ID.
2177 function pingback($content, $post_ID) {
2179 include_once(ABSPATH . WPINC . '/class-IXR.php');
2180 include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2182 // original code by Mort (http://mort.mine.nu:8080)
2183 $post_links = array();
2185 $pung = get_pung($post_ID);
2188 // Parsing the post, external links (if any) are stored in the $post_links array
2189 $post_links_temp = wp_extract_urls( $content );
2192 // Walking thru the links array
2193 // first we get rid of links pointing to sites, not to specific files
2195 // http://dummy-weblog.org
2196 // http://dummy-weblog.org/
2197 // http://dummy-weblog.org/post.php
2198 // We don't wanna ping first and second types, even if they have a valid <link/>
2200 foreach ( (array) $post_links_temp as $link_test ) :
2201 if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
2202 && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
2203 if ( $test = @parse_url($link_test) ) {
2204 if ( isset($test['query']) )
2205 $post_links[] = $link_test;
2206 elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
2207 $post_links[] = $link_test;
2212 $post_links = array_unique( $post_links );
2214 * Fires just before pinging back links found in a post.
2218 * @param array &$post_links An array of post links to be checked, passed by reference.
2219 * @param array &$pung Whether a link has already been pinged, passed by reference.
2220 * @param int $post_ID The post ID.
2222 do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
2224 foreach ( (array) $post_links as $pagelinkedto ) {
2225 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
2227 if ( $pingback_server_url ) {
2228 @ set_time_limit( 60 );
2229 // Now, the RPC call
2230 $pagelinkedfrom = get_permalink($post_ID);
2232 // using a timeout of 3 seconds should be enough to cover slow servers
2233 $client = new WP_HTTP_IXR_Client($pingback_server_url);
2234 $client->timeout = 3;
2236 * Filter the user agent sent when pinging-back a URL.
2240 * @param string $concat_useragent The user agent concatenated with ' -- WordPress/'
2241 * and the WordPress version.
2242 * @param string $useragent The useragent.
2243 * @param string $pingback_server_url The server URL being linked to.
2244 * @param string $pagelinkedto URL of page linked to.
2245 * @param string $pagelinkedfrom URL of page linked from.
2247 $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
2248 // when set to true, this outputs debug messages by itself
2249 $client->debug = false;
2251 if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
2252 add_ping( $post_ID, $pagelinkedto );
2258 * Check whether blog is public before returning sites.
2262 * @param mixed $sites Will return if blog is public, will not return if not public.
2263 * @return mixed Empty string if blog is not public, returns $sites, if site is public.
2265 function privacy_ping_filter($sites) {
2266 if ( '0' != get_option('blog_public') )
2275 * Updates database when sending trackback to prevent duplicates.
2280 * @param string $trackback_url URL to send trackbacks.
2281 * @param string $title Title of post.
2282 * @param string $excerpt Excerpt of post.
2283 * @param int $ID Post ID.
2284 * @return mixed Database query from update.
2286 function trackback($trackback_url, $title, $excerpt, $ID) {
2289 if ( empty($trackback_url) )
2293 $options['timeout'] = 4;
2294 $options['body'] = array(
2296 'url' => get_permalink($ID),
2297 'blog_name' => get_option('blogname'),
2298 'excerpt' => $excerpt
2301 $response = wp_safe_remote_post( $trackback_url, $options );
2303 if ( is_wp_error( $response ) )
2306 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
2307 return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
2317 * @param string $server Host of blog to connect to.
2318 * @param string $path Path to send the ping.
2320 function weblog_ping($server = '', $path = '') {
2322 include_once(ABSPATH . WPINC . '/class-IXR.php');
2323 include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2325 // using a timeout of 3 seconds should be enough to cover slow servers
2326 $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
2327 $client->timeout = 3;
2328 $client->useragent .= ' -- WordPress/'.$wp_version;
2330 // when set to true, this outputs debug messages by itself
2331 $client->debug = false;
2332 $home = trailingslashit( home_url() );
2333 if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
2334 $client->query('weblogUpdates.ping', get_option('blogname'), $home);
2338 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI
2341 * @see wp_http_validate_url()
2343 * @param string $source_uri
2346 function pingback_ping_source_uri( $source_uri ) {
2347 return (string) wp_http_validate_url( $source_uri );
2351 * Default filter attached to xmlrpc_pingback_error.
2353 * Returns a generic pingback error code unless the error code is 48,
2354 * which reports that the pingback is already registered.
2357 * @link http://www.hixie.ch/specs/pingback/pingback#TOC3
2359 * @param IXR_Error $ixr_error
2362 function xmlrpc_pingback_error( $ixr_error ) {
2363 if ( $ixr_error->code === 48 )
2365 return new IXR_Error( 0, '' );
2373 * Removes comment ID from the comment cache.
2376 * @package WordPress
2379 * @param int|array $ids Comment ID or array of comment IDs to remove from cache
2381 function clean_comment_cache($ids) {
2382 foreach ( (array) $ids as $id )
2383 wp_cache_delete($id, 'comment');
2385 wp_cache_set( 'last_changed', microtime(), 'comment' );
2389 * Updates the comment cache of given comments.
2391 * Will add the comments in $comments to the cache. If comment ID already exists
2392 * in the comment cache then it will not be updated. The comment is added to the
2393 * cache using the comment group with the key using the ID of the comments.
2396 * @package WordPress
2399 * @param array $comments Array of comment row objects
2401 function update_comment_cache($comments) {
2402 foreach ( (array) $comments as $comment )
2403 wp_cache_add($comment->comment_ID, $comment, 'comment');
2411 * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
2416 * @param object $posts Post data object.
2417 * @param object $query Query object.
2420 function _close_comments_for_old_posts( $posts, $query ) {
2421 if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
2425 * Filter the list of post types to automatically close comments for.
2429 * @param array $post_types An array of registered post types. Default array with 'post'.
2431 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2432 if ( ! in_array( $posts[0]->post_type, $post_types ) )
2435 $days_old = (int) get_option( 'close_comments_days_old' );
2439 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
2440 $posts[0]->comment_status = 'closed';
2441 $posts[0]->ping_status = 'closed';
2448 * Close comments on an old post. Hooked to comments_open and pings_open.
2453 * @param bool $open Comments open or closed
2454 * @param int $post_id Post ID
2455 * @return bool $open
2457 function _close_comments_for_old_post( $open, $post_id ) {
2461 if ( !get_option('close_comments_for_old_posts') )
2464 $days_old = (int) get_option('close_comments_days_old');
2468 $post = get_post($post_id);
2470 /** This filter is documented in wp-includes/comment.php */
2471 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2472 if ( ! in_array( $post->post_type, $post_types ) )
2475 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )