X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/48ab98cb1779cf2088c1351ac3dd3d0da6fb31d3..c81aba3d563f7459dc79140e4c5be67bcf506b92:/wp-includes/comment.php
diff --git a/wp-includes/comment.php b/wp-includes/comment.php
index a4cc7e2b..9586b8b7 100644
--- a/wp-includes/comment.php
+++ b/wp-includes/comment.php
@@ -1,55 +1,55 @@
]*href/i', $comment, $out );
+
/**
* Filter the maximum number of links allowed in a comment.
*
@@ -59,25 +59,38 @@ function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $
* @param string $url Comment author's URL. Included in allowed links total.
*/
$num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
+
+ /*
+ * If the number of links in the comment exceeds the allowed amount,
+ * fail the check by returning false.
+ */
if ( $num_links >= $max_links )
return false;
}
$mod_keys = trim(get_option('moderation_keys'));
+
+ // If moderation 'keys' (keywords) are set, process them.
if ( !empty($mod_keys) ) {
$words = explode("\n", $mod_keys );
foreach ( (array) $words as $word) {
$word = trim($word);
- // Skip empty lines
+ // Skip empty lines.
if ( empty($word) )
continue;
- // Do some escaping magic so that '#' chars in the
- // spam words don't break things:
+ /*
+ * Do some escaping magic so that '#' (number of) characters in the spam
+ * words don't break things:
+ */
$word = preg_quote($word, '#');
+ /*
+ * Check the comment fields for moderation keywords. If any are found,
+ * fail the check for the given field by returning false.
+ */
$pattern = "#$word#i";
if ( preg_match($pattern, $author) ) return false;
if ( preg_match($pattern, $email) ) return false;
@@ -88,7 +101,13 @@ function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $
}
}
- // Comment whitelisting:
+ /*
+ * Check if the option to approve comments by previously-approved authors is enabled.
+ *
+ * If it is enabled, check whether the comment author has a previously-approved comment,
+ * as well as whether there are any moderation keywords (if set) present in the author
+ * email address. If both checks pass, return true. Otherwise, return false.
+ */
if ( 1 == get_option('comment_whitelist')) {
if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
// expected_slashed ($author, $email)
@@ -109,14 +128,28 @@ function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $
* Retrieve the approved comments for post $post_id.
*
* @since 2.0.0
- * @uses $wpdb
+ * @since 4.1.0 Refactored to leverage {@see WP_Comment_Query} over a direct query.
*
- * @param int $post_id The ID of the post
- * @return array $comments The approved comments
+ * @param int $post_id The ID of the post.
+ * @param array $args Optional. See {@see WP_Comment_Query::query()} for information
+ * on accepted arguments.
+ * @return int|array $comments The approved comments, or number of comments if `$count`
+ * argument is true.
*/
-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));
+function get_approved_comments( $post_id, $args = array() ) {
+ if ( ! $post_id ) {
+ return array();
+ }
+
+ $defaults = array(
+ 'status' => 1,
+ 'post_id' => $post_id,
+ 'order' => 'ASC',
+ );
+ $r = wp_parse_args( $args, $defaults );
+
+ $query = new WP_Comment_Query;
+ return $query->query( $r );
}
/**
@@ -127,32 +160,28 @@ function get_approved_comments($post_id) {
* comment variable will be used, if it is set.
*
* @since 2.0.0
- * @uses $wpdb
*
- * @param object|string|int $comment Comment to retrieve.
+ * @global WP_Comment $comment
+ *
+ * @param WP_Comment|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.
+ * @return WP_Comment|array|null Depends on $output value.
*/
-function get_comment(&$comment, $output = OBJECT) {
- global $wpdb;
+function get_comment( &$comment = null, $output = OBJECT ) {
+ if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
+ $comment = $GLOBALS['comment'];
+ }
- 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');
+ if ( $comment instanceof WP_Comment ) {
$_comment = $comment;
+ } elseif ( is_object( $comment ) ) {
+ $_comment = new WP_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');
- }
+ $_comment = WP_Comment::get_instance( $comment );
+ }
+
+ if ( ! $_comment ) {
+ return null;
}
/**
@@ -167,14 +196,11 @@ function get_comment(&$comment, $output = OBJECT) {
if ( $output == OBJECT ) {
return $_comment;
} elseif ( $output == ARRAY_A ) {
- $__comment = get_object_vars($_comment);
- return $__comment;
+ return $_comment->to_array();
} elseif ( $output == ARRAY_N ) {
- $__comment = array_values(get_object_vars($_comment));
- return $__comment;
- } else {
- return $_comment;
+ return array_values( $_comment->to_array() );
}
+ return $_comment;
}
/**
@@ -182,341 +208,17 @@ function get_comment(&$comment, $output = OBJECT) {
*
* 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.
+ * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::parse_query()}
+ * for information on accepted arguments. Default empty.
+ * @return int|array List of comments or number of found comments if `$count` argument is true.
*/
function get_comments( $args = '' ) {
$query = new WP_Comment_Query;
return $query->query( $args );
}
-/**
- * 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;
-
- /**
- * 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 );
- }
-
- /**
- * 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;
- }
-
- /**
- * 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) . ')';
- }
-}
-
/**
* Retrieve all of the WordPress supported comment statuses.
*
@@ -534,16 +236,63 @@ function get_comment_statuses() {
'approve' => _x('Approved', 'adjective'),
/* translators: comment status */
'spam' => _x('Spam', 'adjective'),
+ /* translators: comment status */
+ 'trash' => _x('Trash', 'adjective'),
);
return $status;
}
+/**
+ * Gets the default comment status for a post type.
+ *
+ * @since 4.3.0
+ *
+ * @param string $post_type Optional. Post type. Default 'post'.
+ * @param string $comment_type Optional. Comment type. Default 'comment'.
+ * @return string Expected return value is 'open' or 'closed'.
+ */
+function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
+ switch ( $comment_type ) {
+ case 'pingback' :
+ case 'trackback' :
+ $supports = 'trackbacks';
+ $option = 'ping';
+ break;
+ default :
+ $supports = 'comments';
+ $option = 'comment';
+ }
+
+ // Set the status.
+ if ( 'page' === $post_type ) {
+ $status = 'closed';
+ } elseif ( post_type_supports( $post_type, $supports ) ) {
+ $status = get_option( "default_{$option}_status" );
+ } else {
+ $status = 'closed';
+ }
+
+ /**
+ * Filter the default comment status for the given post type.
+ *
+ * @since 4.3.0
+ *
+ * @param string $status Default status for the given post type,
+ * either 'open' or 'closed'.
+ * @param string $post_type Post type. Default is `post`.
+ * @param string $comment_type Type of comment. Default is `comment`.
+ */
+ return apply_filters( 'get_default_comment_status' , $status, $post_type, $comment_type );
+}
+
/**
* The date the last comment was modified.
*
* @since 1.5.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @staticvar array $cache_lastcommentmodified
*
* @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
* or 'server' locations.
@@ -583,7 +332,8 @@ function get_lastcommentmodified($timezone = 'server') {
* caches, but this function does not.
*
* @since 2.0.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @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.
@@ -606,25 +356,36 @@ function get_comment_count( $post_id = 0 ) {
", ARRAY_A);
$comment_count = array(
- "approved" => 0,
- "awaiting_moderation" => 0,
- "spam" => 0,
- "total_comments" => 0
+ 'approved' => 0,
+ 'awaiting_moderation' => 0,
+ 'spam' => 0,
+ 'trash' => 0,
+ 'post-trashed' => 0,
+ 'total_comments' => 0,
+ 'all' => 0,
);
foreach ( $totals as $row ) {
switch ( $row['comment_approved'] ) {
+ case 'trash':
+ $comment_count['trash'] = $row['total'];
+ break;
+ case 'post-trashed':
+ $comment_count['post-trashed'] = $row['total'];
+ break;
case 'spam':
$comment_count['spam'] = $row['total'];
- $comment_count["total_comments"] += $row['total'];
+ $comment_count['total_comments'] += $row['total'];
break;
- case 1:
+ case '1':
$comment_count['approved'] = $row['total'];
$comment_count['total_comments'] += $row['total'];
+ $comment_count['all'] += $row['total'];
break;
- case 0:
+ case '0':
$comment_count['awaiting_moderation'] = $row['total'];
$comment_count['total_comments'] += $row['total'];
+ $comment_count['all'] += $row['total'];
break;
default:
break;
@@ -642,8 +403,7 @@ function get_comment_count( $post_id = 0 ) {
* Add meta data field to a comment.
*
* @since 2.9.0
- * @uses add_metadata
- * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
+ * @link https://codex.wordpress.org/Function_Reference/add_comment_meta
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata name.
@@ -663,8 +423,7 @@ function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false)
* 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
+ * @link https://codex.wordpress.org/Function_Reference/delete_comment_meta
*
* @param int $comment_id comment ID
* @param string $meta_key Metadata name.
@@ -679,8 +438,7 @@ function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
* Retrieve comment meta field for a comment.
*
* @since 2.9.0
- * @uses get_metadata
- * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
+ * @link https://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.
@@ -701,8 +459,7 @@ function get_comment_meta($comment_id, $key = '', $single = false) {
* 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
+ * @link https://codex.wordpress.org/Function_Reference/update_comment_meta
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata key.
@@ -718,8 +475,8 @@ function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value =
* 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.
+ * @param WP_Comment $comment Comment object.
+ * @param object $user Comment author's object.
*
* @since 3.4.0
*/
@@ -805,10 +562,11 @@ function sanitize_comment_cookies() {
* Validates whether this comment is allowed to be made.
*
* @since 2.0.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentdata Contains information on the comment
- * @return mixed Signifies the approval status (0|1|'spam')
+ * @return int|string Signifies the approval status (0|1|'spam')
*/
function wp_allow_comment( $commentdata ) {
global $wpdb;
@@ -831,7 +589,22 @@ function wp_allow_comment( $commentdata ) {
") AND comment_content = %s LIMIT 1",
wp_unslash( $commentdata['comment_content'] )
);
- if ( $wpdb->get_var( $dupe ) ) {
+
+ $dupe_id = $wpdb->get_var( $dupe );
+
+ /**
+ * Filters the ID, if any, of the duplicate comment found when creating a new comment.
+ *
+ * Return an empty value from this filter to allow what WP considers a duplicate comment.
+ *
+ * @since 4.4.0
+ *
+ * @param int $dupe_id ID of the comment identified as a duplicate.
+ * @param array $commentdata Data for the comment being created.
+ */
+ $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata );
+
+ if ( $dupe_id ) {
/**
* Fires immediately after a duplicate comment is detected.
*
@@ -843,7 +616,7 @@ function wp_allow_comment( $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!') );
+ wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );
}
/**
@@ -899,7 +672,7 @@ function wp_allow_comment( $commentdata ) {
$commentdata['comment_author_IP'],
$commentdata['comment_agent']
) ) {
- $approved = 'spam';
+ $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
}
}
@@ -922,7 +695,8 @@ function wp_allow_comment( $commentdata ) {
* administrators.
*
* @since 2.3.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $ip Comment IP.
* @param string $email Comment author email address.
@@ -930,10 +704,28 @@ function wp_allow_comment( $commentdata ) {
*/
function check_comment_flood_db( $ip, $email, $date ) {
global $wpdb;
- if ( current_user_can( 'manage_options' ) )
- return; // don't throttle admins
+ // don't throttle admins or moderators
+ if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
+ return;
+ }
$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 ) ) ) {
+
+ if ( is_user_logged_in() ) {
+ $user = get_current_user_id();
+ $check_column = '`user_id`';
+ } else {
+ $user = $ip;
+ $check_column = '`comment_author_IP`';
+ }
+
+ $sql = $wpdb->prepare(
+ "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1",
+ $hour_ago,
+ $user,
+ $email
+ );
+ $lasttime = $wpdb->get_var( $sql );
+ if ( $lasttime ) {
$time_lastcomment = mysql2date('U', $lasttime, false);
$time_newcomment = mysql2date('U', $date, false);
/**
@@ -960,7 +752,7 @@ function check_comment_flood_db( $ip, $email, $date ) {
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) );
+ wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
}
}
}
@@ -995,9 +787,11 @@ function separate_comments(&$comments) {
*
* @uses Walker_Comment
*
- * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
- * @param int $per_page Optional comments per page.
- * @param boolean $threaded Optional control over flat or threaded comments.
+ * @global WP_Query $wp_query
+ *
+ * @param array $comments Optional array of WP_Comment objects. Defaults to $wp_query->comments
+ * @param int $per_page Optional comments per page.
+ * @param bool $threaded Optional control over flat or threaded comments.
* @return int Number of comment pages.
*/
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
@@ -1012,8 +806,9 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded
if ( empty($comments) )
return 0;
- if ( ! get_option( 'page_comments' ) )
+ if ( ! get_option( 'page_comments' ) ) {
return 1;
+ }
if ( !isset($per_page) )
$per_page = (int) get_query_var('comments_per_page');
@@ -1039,60 +834,120 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded
* Calculate what page number a comment will appear on for comment paging.
*
* @since 2.7.0
- * @uses get_comment() Gets the full comment of the $comment_ID parameter.
- * @uses get_option() Get various settings to control function and defaults.
- * @uses get_page_of_comment() Used to loop up to top level comment.
*
- * @param int $comment_ID Comment ID.
- * @param array $args Optional args.
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int $comment_ID Comment ID.
+ * @param array $args {
+ * Array of optional arguments.
+ * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment',
+ * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
+ * Default is 'all'.
+ * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the
+ * 'comments_per_page' option.
+ * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
+ * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
+ * } *
* @return int|null Comment page number or null on error.
*/
function get_page_of_comment( $comment_ID, $args = array() ) {
global $wpdb;
+ $page = null;
+
if ( !$comment = get_comment( $comment_ID ) )
return;
$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
$args = wp_parse_args( $args, $defaults );
+ $original_args = $args;
+
+ // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
+ if ( get_option( 'page_comments' ) ) {
+ if ( '' === $args['per_page'] ) {
+ $args['per_page'] = get_query_var( 'comments_per_page' );
+ }
+
+ if ( '' === $args['per_page'] ) {
+ $args['per_page'] = get_option( 'comments_per_page' );
+ }
+ }
- if ( '' === $args['per_page'] && get_option('page_comments') )
- $args['per_page'] = get_query_var('comments_per_page');
if ( empty($args['per_page']) ) {
$args['per_page'] = 0;
$args['page'] = 0;
}
- if ( $args['per_page'] < 1 )
- return 1;
- if ( '' === $args['max_depth'] ) {
- if ( get_option('thread_comments') )
- $args['max_depth'] = get_option('thread_comments_depth');
- else
- $args['max_depth'] = -1;
+ if ( $args['per_page'] < 1 ) {
+ $page = 1;
}
- // Find this comment's top level parent if threading is enabled
- if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
- return get_page_of_comment( $comment->comment_parent, $args );
+ if ( null === $page ) {
+ if ( '' === $args['max_depth'] ) {
+ if ( get_option('thread_comments') )
+ $args['max_depth'] = get_option('thread_comments_depth');
+ else
+ $args['max_depth'] = -1;
+ }
- $allowedtypes = array(
- 'comment' => '',
- 'pingback' => 'pingback',
- 'trackback' => 'trackback',
- );
+ // Find this comment's top level parent if threading is enabled
+ if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
+ return get_page_of_comment( $comment->comment_parent, $args );
+
+ $comment_args = array(
+ 'type' => $args['type'],
+ 'post_id' => $comment->comment_post_ID,
+ 'fields' => 'ids',
+ 'count' => true,
+ 'status' => 'approve',
+ 'parent' => 0,
+ 'date_query' => array(
+ array(
+ 'column' => "$wpdb->comments.comment_date_gmt",
+ 'before' => $comment->comment_date_gmt,
+ )
+ ),
+ );
- $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
+ $comment_query = new WP_Comment_Query();
+ $older_comment_count = $comment_query->query( $comment_args );
- // Count comments older than this one
- $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 ) );
+ // No older comments? Then it's page #1.
+ if ( 0 == $older_comment_count ) {
+ $page = 1;
- // No older comments? Then it's page #1.
- if ( 0 == $oldercoms )
- return 1;
+ // Divide comments older than this one by comments per page to get this comment's page number
+ } else {
+ $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
+ }
+ }
- // Divide comments older than this one by comments per page to get this comment's page number
- return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
+ /**
+ * Filters the calculated page on which a comment appears.
+ *
+ * @since 4.4.0
+ *
+ * @param int $page Comment page.
+ * @param array $args {
+ * Arguments used to calculate pagination. These include arguments auto-detected by the function,
+ * based on query vars, system settings, etc. For pristine arguments passed to the function,
+ * see `$original_args`.
+ *
+ * @type string $type Type of comments to count.
+ * @type int $page Calculated current page.
+ * @type int $per_page Calculated number of comments per page.
+ * @type int $max_depth Maximum comment threading depth allowed.
+ * }
+ * @param array $original_args {
+ * Array of arguments passed to the function. Some or all of these may not be set.
+ *
+ * @type string $type Type of comments to count.
+ * @type int $page Current comment page.
+ * @type int $per_page Number of comments per page.
+ * @type int $max_depth Maximum comment threading depth allowed.
+ * }
+ */
+ return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args );
}
/**
@@ -1166,11 +1021,9 @@ function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_age
* @since 2.5.0
*
* @param int $post_id Optional. Post ID.
- * @return object Comment stats.
+ * @return object|array Comment stats.
*/
function wp_count_comments( $post_id = 0 ) {
- global $wpdb;
-
$post_id = (int) $post_id;
/**
@@ -1181,41 +1034,24 @@ function wp_count_comments( $post_id = 0 ) {
* @param array $count An empty array.
* @param int $post_id The post ID.
*/
- $stats = apply_filters( 'wp_count_comments', array(), $post_id );
- if ( !empty($stats) )
- return $stats;
-
- $count = wp_cache_get("comments-{$post_id}", 'counts');
+ $filtered = apply_filters( 'wp_count_comments', array(), $post_id );
+ if ( ! empty( $filtered ) ) {
+ return $filtered;
+ }
- if ( false !== $count )
+ $count = wp_cache_get( "comments-{$post_id}", 'counts' );
+ if ( false !== $count ) {
return $count;
-
- $where = '';
- if ( $post_id > 0 )
- $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
-
- $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
-
- $total = 0;
- $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
- foreach ( (array) $count as $row ) {
- // Don't count post-trashed toward totals
- if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
- $total += $row['num_comments'];
- if ( isset( $approved[$row['comment_approved']] ) )
- $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
}
- $stats['total_comments'] = $total;
- foreach ( $approved as $key ) {
- if ( empty($stats[$key]) )
- $stats[$key] = 0;
- }
+ $stats = get_comment_count( $post_id );
+ $stats['moderated'] = $stats['awaiting_moderation'];
+ unset( $stats['awaiting_moderation'] );
- $stats = (object) $stats;
- wp_cache_set("comments-{$post_id}", $stats, 'counts');
+ $stats_object = (object) $stats;
+ wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
- return $stats;
+ return $stats_object;
}
/**
@@ -1228,11 +1064,11 @@ function wp_count_comments( $post_id = 0 ) {
* post ID available.
*
* @since 2.0.0
- * @uses $wpdb
- * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
*
- * @param int $comment_id Comment ID
- * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
+ * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
* @return bool True on success, false on failure.
*/
function wp_delete_comment($comment_id, $force_delete = false) {
@@ -1240,7 +1076,7 @@ function wp_delete_comment($comment_id, $force_delete = false) {
if (!$comment = get_comment($comment_id))
return false;
- if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
+ if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) )
return wp_trash_comment($comment_id);
/**
@@ -1250,21 +1086,21 @@ function wp_delete_comment($comment_id, $force_delete = false) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'delete_comment', $comment_id );
+ do_action( 'delete_comment', $comment->comment_ID );
// Move children up a level.
- $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
+ $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) );
if ( !empty($children) ) {
- $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
+ $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID));
clean_comment_cache($children);
}
// Delete metadata
- $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
+ $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
foreach ( $meta_ids as $mid )
delete_metadata_by_mid( 'comment', $mid );
- if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
+ if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) )
return false;
/**
@@ -1274,16 +1110,16 @@ function wp_delete_comment($comment_id, $force_delete = false) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'deleted_comment', $comment_id );
+ do_action( 'deleted_comment', $comment->comment_ID );
$post_id = $comment->comment_post_ID;
if ( $post_id && $comment->comment_approved == 1 )
wp_update_comment_count($post_id);
- clean_comment_cache($comment_id);
+ clean_comment_cache( $comment->comment_ID );
/** This action is documented in wp-includes/comment.php */
- do_action( 'wp_set_comment_status', $comment_id, 'delete' );
+ do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' );
wp_transition_comment_status('delete', $comment->comment_approved, $comment);
return true;
@@ -1296,9 +1132,7 @@ function wp_delete_comment($comment_id, $force_delete = false) {
*
* @since 2.9.0
*
- * @uses wp_delete_comment() if trash is disabled
- *
- * @param int $comment_id Comment ID.
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @return bool True on success, false on failure.
*/
function wp_trash_comment($comment_id) {
@@ -1315,11 +1149,13 @@ function wp_trash_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'trash_comment', $comment_id );
+ do_action( 'trash_comment', $comment->comment_ID );
- if ( wp_set_comment_status($comment_id, 'trash') ) {
- add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
- add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
+ if ( wp_set_comment_status( $comment, 'trash' ) ) {
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
+ add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
+ add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
/**
* Fires immediately after a comment is sent to Trash.
@@ -1328,7 +1164,7 @@ function wp_trash_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'trashed_comment', $comment_id );
+ do_action( 'trashed_comment', $comment->comment_ID );
return true;
}
@@ -1340,12 +1176,14 @@ function wp_trash_comment($comment_id) {
*
* @since 2.9.0
*
- * @param int $comment_id Comment ID.
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @return bool True on success, false on failure.
*/
function wp_untrash_comment($comment_id) {
- if ( ! (int)$comment_id )
+ $comment = get_comment( $comment_id );
+ if ( ! $comment ) {
return false;
+ }
/**
* Fires immediately before a comment is restored from the Trash.
@@ -1354,15 +1192,15 @@ function wp_untrash_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'untrash_comment', $comment_id );
+ do_action( 'untrash_comment', $comment->comment_ID );
- $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
+ $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
if ( empty($status) )
$status = '0';
- if ( wp_set_comment_status($comment_id, $status) ) {
- delete_comment_meta($comment_id, '_wp_trash_meta_time');
- delete_comment_meta($comment_id, '_wp_trash_meta_status');
+ if ( wp_set_comment_status( $comment, $status ) ) {
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
/**
* Fires immediately after a comment is restored from the Trash.
*
@@ -1370,7 +1208,7 @@ function wp_untrash_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'untrashed_comment', $comment_id );
+ do_action( 'untrashed_comment', $comment->comment_ID );
return true;
}
@@ -1382,12 +1220,14 @@ function wp_untrash_comment($comment_id) {
*
* @since 2.9.0
*
- * @param int $comment_id Comment ID.
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @return bool True on success, false on failure.
*/
-function wp_spam_comment($comment_id) {
- if ( !$comment = get_comment($comment_id) )
+function wp_spam_comment( $comment_id ) {
+ $comment = get_comment( $comment_id );
+ if ( ! $comment ) {
return false;
+ }
/**
* Fires immediately before a comment is marked as Spam.
@@ -1396,10 +1236,13 @@ function wp_spam_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'spam_comment', $comment_id );
+ do_action( 'spam_comment', $comment->comment_ID );
- if ( wp_set_comment_status($comment_id, 'spam') ) {
- add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
+ if ( wp_set_comment_status( $comment, 'spam' ) ) {
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
+ add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
+ add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );
/**
* Fires immediately after a comment is marked as Spam.
*
@@ -1407,7 +1250,7 @@ function wp_spam_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'spammed_comment', $comment_id );
+ do_action( 'spammed_comment', $comment->comment_ID );
return true;
}
@@ -1419,12 +1262,14 @@ function wp_spam_comment($comment_id) {
*
* @since 2.9.0
*
- * @param int $comment_id Comment ID.
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @return bool True on success, false on failure.
*/
-function wp_unspam_comment($comment_id) {
- if ( ! (int)$comment_id )
+function wp_unspam_comment( $comment_id ) {
+ $comment = get_comment( $comment_id );
+ if ( ! $comment ) {
return false;
+ }
/**
* Fires immediately before a comment is unmarked as Spam.
@@ -1433,14 +1278,15 @@ function wp_unspam_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'unspam_comment', $comment_id );
+ do_action( 'unspam_comment', $comment->comment_ID );
- $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
+ $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true );
if ( empty($status) )
$status = '0';
- if ( wp_set_comment_status($comment_id, $status) ) {
- delete_comment_meta($comment_id, '_wp_trash_meta_status');
+ if ( wp_set_comment_status( $comment, $status ) ) {
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
+ delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
/**
* Fires immediately after a comment is unmarked as Spam.
*
@@ -1448,7 +1294,7 @@ function wp_unspam_comment($comment_id) {
*
* @param int $comment_id The comment ID.
*/
- do_action( 'unspammed_comment', $comment_id );
+ do_action( 'unspammed_comment', $comment->comment_ID );
return true;
}
@@ -1460,8 +1306,8 @@ function wp_unspam_comment($comment_id) {
*
* @since 1.0.0
*
- * @param int $comment_id Comment ID
- * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object
+ * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
*/
function wp_get_comment_status($comment_id) {
$comment = get_comment($comment_id);
@@ -1534,19 +1380,19 @@ function wp_transition_comment_status($new_status, $old_status, $comment) {
/**
* Fires when the comment status is in transition from one specific status to another.
*
- * The dynamic portions of the hook name, $old_status, and $new_status,
+ * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
* refer to the old and new comment statuses, respectively.
*
* @since 2.7.0
*
- * @param object $comment Comment object.
+ * @param WP_Comment $comment Comment object.
*/
do_action( "comment_{$old_status}_to_{$new_status}", $comment );
}
/**
* Fires when the status of a specific comment type is in transition.
*
- * The dynamic portions of the hook name, $new_status, and $comment->comment_type,
+ * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
* refer to the new comment status, and the type of comment, respectively.
*
* Typical comment types include an empty string (standard comment), 'pingback',
@@ -1554,8 +1400,8 @@ function wp_transition_comment_status($new_status, $old_status, $comment) {
*
* @since 2.7.0
*
- * @param int $comment_ID The comment ID.
- * @param obj $comment Comment object.
+ * @param int $comment_ID The comment ID.
+ * @param WP_Comment $comment Comment object.
*/
do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
}
@@ -1592,24 +1438,51 @@ function wp_get_current_commenter() {
*
* @since 3.1.0
*
- * @param string $comment_author Comment author's name.
- * @param string $comment_author_email Comment author's email.
- * @param string $comment_author_url Comment author's URL.
+ * @param array $comment_author_data {
+ * An array of current commenter variables.
+ *
+ * @type string $comment_author The name of the author of the comment. Default empty.
+ * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
+ * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
+ * }
*/
return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
}
/**
- * Inserts a comment to the database.
- *
- * The available comment data key names are 'comment_author_IP', 'comment_date',
- * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
+ * Inserts a comment into the database.
*
* @since 2.0.0
- * @uses $wpdb
- *
- * @param array $commentdata Contains information on the comment.
- * @return int|bool The new comment's ID on success, false on failure.
+ * @since 4.4.0 Introduced `$comment_meta` argument.
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $commentdata {
+ * Array of arguments for inserting a new comment.
+ *
+ * @type string $comment_agent The HTTP user agent of the `$comment_author` when
+ * the comment was submitted. Default empty.
+ * @type int|string $comment_approved Whether the comment has been approved. Default 1.
+ * @type string $comment_author The name of the author of the comment. Default empty.
+ * @type string $comment_author_email The email address of the `$comment_author`. Default empty.
+ * @type string $comment_author_IP The IP address of the `$comment_author`. Default empty.
+ * @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
+ * @type string $comment_content The content of the comment. Default empty.
+ * @type string $comment_date The date the comment was submitted. To set the date
+ * manually, `$comment_date_gmt` must also be specified.
+ * Default is the current time.
+ * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
+ * Default is `$comment_date` in the site's GMT timezone.
+ * @type int $comment_karma The karma of the comment. Default 0.
+ * @type int $comment_parent ID of this comment's parent, if any. Default 0.
+ * @type int $comment_post_ID ID of the post that relates to the comment, if any.
+ * Default empty.
+ * @type string $comment_type Comment type. Default empty.
+ * @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the
+ * new comment.
+ * @type int $user_id ID of the user who submitted the comment. Default 0.
+ * }
+ * @return int|false The new comment's ID on success, false on failure.
*/
function wp_insert_comment( $commentdata ) {
global $wpdb;
@@ -1645,13 +1518,20 @@ function wp_insert_comment( $commentdata ) {
}
$comment = get_comment( $id );
+ // If metadata is provided, store it.
+ if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
+ foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
+ add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
+ }
+ }
+
/**
* Fires immediately after a comment is inserted into the database.
*
* @since 2.8.0
*
- * @param int $id The comment ID.
- * @param obj $comment Comment object.
+ * @param int $id The comment ID.
+ * @param WP_Comment $comment Comment object.
*/
do_action( 'wp_insert_comment', $id, $comment );
@@ -1752,13 +1632,38 @@ function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment)
*
* We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure
* that it is properly set, such as in wp-config.php, for your environment.
- * See {@link http://core.trac.wordpress.org/ticket/9235}
+ * See {@link https://core.trac.wordpress.org/ticket/9235}
*
* @since 1.5.0
- * @param array $commentdata Contains information on the comment.
- * @return int|bool The ID of the comment on success, false on failure.
+ * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
+ *
+ * @see wp_insert_comment()
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $commentdata {
+ * Comment data.
+ *
+ * @type string $comment_author The name of the comment author.
+ * @type string $comment_author_email The comment author email address.
+ * @type string $comment_author_url The comment author URL.
+ * @type string $comment_content The content of the comment.
+ * @type string $comment_date The date the comment was submitted. Default is the current time.
+ * @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
+ * Default is `$comment_date` in the GMT timezone.
+ * @type int $comment_parent The ID of this comment's parent, if any. Default 0.
+ * @type int $comment_post_ID The ID of the post that relates to the comment.
+ * @type int $user_id The ID of the user who submitted the comment. Default 0.
+ * @type int $user_ID Kept for backward-compatibility. Use `$user_id` instead.
+ * @type string $comment_agent Comment author user agent. Default is the value of 'HTTP_USER_AGENT'
+ * in the `$_SERVER` superglobal sent in the original request.
+ * @type string $comment_author_IP Comment author IP address in IPv4 format. Default is the value of
+ * 'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request.
+ * }
+ * @return int|false The ID of the comment on success, false on failure.
*/
function wp_new_comment( $commentdata ) {
+ global $wpdb;
+
if ( isset( $commentdata['user_ID'] ) ) {
$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
}
@@ -1785,11 +1690,23 @@ function wp_new_comment( $commentdata ) {
$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
- $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
- $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
+ if ( ! isset( $commentdata['comment_author_IP'] ) ) {
+ $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
+ }
+ $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
+
+ if ( ! isset( $commentdata['comment_agent'] ) ) {
+ $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: '';
+ }
+ $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
- $commentdata['comment_date'] = current_time('mysql');
- $commentdata['comment_date_gmt'] = current_time('mysql', 1);
+ if ( empty( $commentdata['comment_date'] ) ) {
+ $commentdata['comment_date'] = current_time('mysql');
+ }
+
+ if ( empty( $commentdata['comment_date_gmt'] ) ) {
+ $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
+ }
$commentdata = wp_filter_comment($commentdata);
@@ -1797,7 +1714,22 @@ function wp_new_comment( $commentdata ) {
$comment_ID = wp_insert_comment($commentdata);
if ( ! $comment_ID ) {
- return false;
+ $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
+
+ foreach ( $fields as $field ) {
+ if ( isset( $commentdata[ $field ] ) ) {
+ $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
+ }
+ }
+
+ $commentdata = wp_filter_comment( $commentdata );
+
+ $commentdata['comment_approved'] = wp_allow_comment( $commentdata );
+
+ $comment_ID = wp_insert_comment( $commentdata );
+ if ( ! $comment_ID ) {
+ return false;
+ }
}
/**
@@ -1805,24 +1737,79 @@ function wp_new_comment( $commentdata ) {
*
* @since 1.2.0
*
- * @param int $comment_ID The comment ID.
- * @param int $comment_approved 1 (true) if the comment is approved, 0 (false) if not.
+ * @param int $comment_ID The comment ID.
+ * @param int|string $comment_approved 1 if the comment is approved, 0 if not, 'spam' if spam.
*/
do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
- if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
- if ( '0' == $commentdata['comment_approved'] ) {
- wp_notify_moderator( $comment_ID );
- }
+ return $comment_ID;
+}
- // wp_notify_postauthor() checks if notifying the author of their own comment.
- // By default, it won't, but filters can override this.
- if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
- wp_notify_postauthor( $comment_ID );
- }
+/**
+ * Send a comment moderation notification to the comment moderator.
+ *
+ * @since 4.4.0
+ *
+ * @param int $comment_ID ID of the comment.
+ * @return bool True on success, false on failure.
+ */
+function wp_new_comment_notify_moderator( $comment_ID ) {
+ $comment = get_comment( $comment_ID );
+
+ // Only send notifications for pending comments.
+ $maybe_notify = ( '0' == $comment->comment_approved );
+
+ /** This filter is documented in wp-includes/comment.php */
+ $maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_ID );
+
+ if ( ! $maybe_notify ) {
+ return false;
}
- return $comment_ID;
+ return wp_notify_moderator( $comment_ID );
+}
+
+/**
+ * Send a notification of a new comment to the post author.
+ *
+ * @since 4.4.0
+ *
+ * Uses the {@see 'notify_post_author'} filter to determine whether the post author
+ * should be notified when a new comment is added, overriding site setting.
+ *
+ * @param int $comment_ID Comment ID.
+ * @return bool True on success, false on failure.
+ */
+function wp_new_comment_notify_postauthor( $comment_ID ) {
+ $comment = get_comment( $comment_ID );
+
+ $maybe_notify = get_option( 'comments_notify' );
+
+ /**
+ * Filter whether to send the post author new comment notification emails,
+ * overriding the site setting.
+ *
+ * @since 4.4.0
+ *
+ * @param bool $maybe_notify Whether to notify the post author about the new comment.
+ * @param int $comment_ID The ID of the comment for the notification.
+ */
+ $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_ID );
+
+ /*
+ * wp_notify_postauthor() checks if notifying the author of their own comment.
+ * By default, it won't, but filters can override this.
+ */
+ if ( ! $maybe_notify ) {
+ return false;
+ }
+
+ // Only send notifications for approved comments.
+ if ( ! isset( $comment->comment_approved ) || '1' != $comment->comment_approved ) {
+ return false;
+ }
+
+ return wp_notify_postauthor( $comment_ID );
}
/**
@@ -1832,11 +1819,12 @@ function wp_new_comment( $commentdata ) {
* If the comment status is not in the list, then false is returned.
*
* @since 1.0.0
- * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
*
- * @param int $comment_id Comment ID.
- * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
- * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
+ * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
+ * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
* @return bool|WP_Error True on success, false or WP_Error on failure.
*/
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
@@ -1850,9 +1838,7 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
case 'approve':
case '1':
$status = '1';
- if ( get_option('comments_notify') ) {
- wp_notify_postauthor( $comment_id );
- }
+ add_action( 'wp_set_comment_status', 'wp_new_comment_notify_postauthor' );
break;
case 'spam':
$status = 'spam';
@@ -1866,16 +1852,16 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
$comment_old = clone get_comment($comment_id);
- if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
+ if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array( 'comment_ID' => $comment_old->comment_ID ) ) ) {
if ( $wp_error )
return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
else
return false;
}
- clean_comment_cache($comment_id);
+ clean_comment_cache( $comment_old->comment_ID );
- $comment = get_comment($comment_id);
+ $comment = get_comment( $comment_old->comment_ID );
/**
* Fires immediately before transitioning a comment's status from one to another
@@ -1887,7 +1873,7 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
* @param string|bool $comment_status Current comment status. Possible values include
* 'hold', 'approve', 'spam', 'trash', or false.
*/
- do_action( 'wp_set_comment_status', $comment_id, $comment_status );
+ do_action( 'wp_set_comment_status', $comment->comment_ID, $comment_status );
wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
@@ -1902,8 +1888,8 @@ function wp_set_comment_status($comment_id, $comment_status, $wp_error = false)
* Filters the comment and makes sure certain fields are valid before updating.
*
* @since 2.0.0
- * @uses $wpdb
- * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentarr Contains information on the comment.
* @return int Comment was updated if value is 1, or was not updated if value is 0.
@@ -1916,6 +1902,12 @@ function wp_update_comment($commentarr) {
if ( empty( $comment ) ) {
return 0;
}
+
+ // Make sure that the comment post ID is valid (if specified).
+ if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
+ return 0;
+ }
+
// Escape data pulled from DB.
$comment = wp_slash($comment);
@@ -1942,15 +1934,15 @@ function wp_update_comment($commentarr) {
if ( ! isset( $data['comment_approved'] ) ) {
$data['comment_approved'] = 1;
- } else if ( 'hold' == $data['comment_approved'] ) {
+ } elseif ( 'hold' == $data['comment_approved'] ) {
$data['comment_approved'] = 0;
- } else if ( 'approve' == $data['comment_approved'] ) {
+ } elseif ( 'approve' == $data['comment_approved'] ) {
$data['comment_approved'] = 1;
}
$comment_ID = $data['comment_ID'];
$comment_post_ID = $data['comment_post_ID'];
- $keys = array( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
+ $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' );
$data = wp_array_slice_assoc( $data, $keys );
$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
@@ -1983,7 +1975,7 @@ function wp_update_comment($commentarr) {
* @staticvar bool $_defer
*
* @param bool $defer
- * @return unknown
+ * @return bool
*/
function wp_defer_comment_counting($defer=null) {
static $_defer = false;
@@ -2012,9 +2004,11 @@ function wp_defer_comment_counting($defer=null) {
* @since 2.1.0
* @see wp_update_comment_count_now() For what could cause a false return value
*
+ * @staticvar array $_deferred
+ *
* @param int $post_id Post ID
* @param bool $do_deferred Whether to process previously deferred post comment counts
- * @return bool True on success, false on failure
+ * @return bool|void True on success, false on failure
*/
function wp_update_comment_count($post_id, $do_deferred=false) {
static $_deferred = array();
@@ -2041,7 +2035,8 @@ function wp_update_comment_count($post_id, $do_deferred=false) {
* Updates the comment count for the post.
*
* @since 2.5.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_id Post ID
* @return bool True on success, false on '0' $post_id or if post with ID does not exist.
@@ -2051,6 +2046,10 @@ function wp_update_comment_count_now($post_id) {
$post_id = (int) $post_id;
if ( !$post_id )
return false;
+
+ wp_cache_delete( 'comments-0', 'counts' );
+ wp_cache_delete( "comments-{$post_id}", 'counts' );
+
if ( !$post = get_post($post_id) )
return false;
@@ -2091,7 +2090,7 @@ function wp_update_comment_count_now($post_id) {
*
* @param string $url URL to ping.
* @param int $deprecated Not Used.
- * @return bool|string False on failure, string containing URI on success.
+ * @return false|string False on failure, string containing URI on success.
*/
function discover_pingback_server_uri( $url, $deprecated = '' ) {
if ( !empty( $deprecated ) )
@@ -2155,7 +2154,8 @@ function discover_pingback_server_uri( $url, $deprecated = '' ) {
* Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
*
* @since 2.1.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*/
function do_all_pings() {
global $wpdb;
@@ -2186,7 +2186,8 @@ function do_all_pings() {
* Perform trackbacks.
*
* @since 1.5.0
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_id Post ID to do trackbacks on.
*/
@@ -2254,8 +2255,8 @@ function generic_ping( $post_id = 0 ) {
* Pings back the links found in a post.
*
* @since 0.71
- * @uses $wp_version
- * @uses IXR_Client
+ *
+ * @global string $wp_version
*
* @param string $content Post content to check for links.
* @param int $post_ID Post ID.
@@ -2361,13 +2362,14 @@ function privacy_ping_filter($sites) {
* Updates database when sending trackback to prevent duplicates.
*
* @since 0.71
- * @uses $wpdb
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $trackback_url URL to send trackbacks.
* @param string $title Title of post.
* @param string $excerpt Excerpt of post.
* @param int $ID Post ID.
- * @return mixed Database query from update.
+ * @return int|false|void Database query from update.
*/
function trackback($trackback_url, $title, $excerpt, $ID) {
global $wpdb;
@@ -2376,7 +2378,7 @@ function trackback($trackback_url, $title, $excerpt, $ID) {
return;
$options = array();
- $options['timeout'] = 4;
+ $options['timeout'] = 10;
$options['body'] = array(
'title' => $title,
'url' => get_permalink($ID),
@@ -2397,8 +2399,8 @@ function trackback($trackback_url, $title, $excerpt, $ID) {
* Send a pingback.
*
* @since 1.2.0
- * @uses $wp_version
- * @uses IXR_Client
+ *
+ * @global string $wp_version
*
* @param string $server Host of blog to connect to.
* @param string $path Path to send the ping.
@@ -2463,8 +2465,9 @@ function xmlrpc_pingback_error( $ixr_error ) {
* @param int|array $ids Comment ID or array of comment IDs to remove from cache
*/
function clean_comment_cache($ids) {
- foreach ( (array) $ids as $id )
- wp_cache_delete($id, 'comment');
+ foreach ( (array) $ids as $id ) {
+ wp_cache_delete( $id, 'comment' );
+ }
wp_cache_set( 'last_changed', microtime(), 'comment' );
}
@@ -2477,12 +2480,46 @@ function clean_comment_cache($ids) {
* cache using the comment group with the key using the ID of the comments.
*
* @since 2.3.0
+ * @since 4.4.0 Introduced the `$update_meta_cache` parameter.
*
- * @param array $comments Array of comment row objects
+ * @param array $comments Array of comment row objects
+ * @param bool $update_meta_cache Whether to update commentmeta cache. Default true.
*/
-function update_comment_cache($comments) {
+function update_comment_cache( $comments, $update_meta_cache = true ) {
foreach ( (array) $comments as $comment )
wp_cache_add($comment->comment_ID, $comment, 'comment');
+
+ if ( $update_meta_cache ) {
+ // Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
+ $comment_ids = array();
+ foreach ( $comments as $comment ) {
+ $comment_ids[] = $comment->comment_ID;
+ }
+ update_meta_cache( 'comment', $comment_ids );
+ }
+}
+
+/**
+ * Adds any comments from the given IDs to the cache that do not already exist in cache.
+ *
+ * @since 4.4.0
+ * @access private
+ *
+ * @see update_comment_cache()
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $comment_ids Array of comment IDs.
+ * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true.
+ */
+function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
+ global $wpdb;
+
+ $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' );
+ if ( !empty( $non_cached_ids ) ) {
+ $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
+
+ update_comment_cache( $fresh_comments, $update_meta_cache );
+ }
}
//
@@ -2495,9 +2532,9 @@ function update_comment_cache($comments) {
* @access private
* @since 2.7.0
*
- * @param object $posts Post data object.
- * @param object $query Query object.
- * @return object
+ * @param WP_Post $posts Post data object.
+ * @param WP_Query $query Query object.
+ * @return array
*/
function _close_comments_for_old_posts( $posts, $query ) {
if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
@@ -2554,8 +2591,210 @@ function _close_comments_for_old_post( $open, $post_id ) {
if ( ! in_array( $post->post_type, $post_types ) )
return $open;
+ // Undated drafts should not show up as comments closed.
+ if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
+ return $open;
+ }
+
if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
return false;
return $open;
}
+
+/**
+ * Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form.
+ *
+ * This function expects unslashed data, as opposed to functions such as `wp_new_comment()` which
+ * expect slashed data.
+ *
+ * @since 4.4.0
+ *
+ * @param array $comment_data {
+ * Comment data.
+ *
+ * @type string|int $comment_post_ID The ID of the post that relates to the comment.
+ * @type string $author The name of the comment author.
+ * @type string $email The comment author email address.
+ * @type string $url The comment author URL.
+ * @type string $comment The content of the comment.
+ * @type string|int $comment_parent The ID of this comment's parent, if any. Default 0.
+ * @type string $_wp_unfiltered_html_comment The nonce value for allowing unfiltered HTML.
+ * }
+ * @return WP_Comment|WP_Error A WP_Comment object on success, a WP_Error object on failure.
+ */
+function wp_handle_comment_submission( $comment_data ) {
+
+ $comment_post_ID = $comment_parent = 0;
+ $comment_author = $comment_author_email = $comment_author_url = $comment_content = $_wp_unfiltered_html_comment = null;
+
+ if ( isset( $comment_data['comment_post_ID'] ) ) {
+ $comment_post_ID = (int) $comment_data['comment_post_ID'];
+ }
+ if ( isset( $comment_data['author'] ) && is_string( $comment_data['author'] ) ) {
+ $comment_author = trim( strip_tags( $comment_data['author'] ) );
+ }
+ if ( isset( $comment_data['email'] ) && is_string( $comment_data['email'] ) ) {
+ $comment_author_email = trim( $comment_data['email'] );
+ }
+ if ( isset( $comment_data['url'] ) && is_string( $comment_data['url'] ) ) {
+ $comment_author_url = trim( $comment_data['url'] );
+ }
+ if ( isset( $comment_data['comment'] ) && is_string( $comment_data['comment'] ) ) {
+ $comment_content = trim( $comment_data['comment'] );
+ }
+ if ( isset( $comment_data['comment_parent'] ) ) {
+ $comment_parent = absint( $comment_data['comment_parent'] );
+ }
+ if ( isset( $comment_data['_wp_unfiltered_html_comment'] ) && is_string( $comment_data['_wp_unfiltered_html_comment'] ) ) {
+ $_wp_unfiltered_html_comment = trim( $comment_data['_wp_unfiltered_html_comment'] );
+ }
+
+ $post = get_post( $comment_post_ID );
+
+ if ( empty( $post->comment_status ) ) {
+
+ /**
+ * Fires when a comment is attempted on a post that does not exist.
+ *
+ * @since 1.5.0
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'comment_id_not_found', $comment_post_ID );
+
+ return new WP_Error( 'comment_id_not_found' );
+
+ }
+
+ // get_post_status() will get the parent status for attachments.
+ $status = get_post_status( $post );
+
+ if ( ( 'private' == $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) {
+ return new WP_Error( 'comment_id_not_found' );
+ }
+
+ $status_obj = get_post_status_object( $status );
+
+ if ( ! comments_open( $comment_post_ID ) ) {
+
+ /**
+ * Fires when a comment is attempted on a post that has comments closed.
+ *
+ * @since 1.5.0
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'comment_closed', $comment_post_ID );
+
+ return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 );
+
+ } elseif ( 'trash' == $status ) {
+
+ /**
+ * Fires when a comment is attempted on a trashed post.
+ *
+ * @since 2.9.0
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'comment_on_trash', $comment_post_ID );
+
+ return new WP_Error( 'comment_on_trash' );
+
+ } elseif ( ! $status_obj->public && ! $status_obj->private ) {
+
+ /**
+ * Fires when a comment is attempted on a post in draft mode.
+ *
+ * @since 1.5.1
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'comment_on_draft', $comment_post_ID );
+
+ return new WP_Error( 'comment_on_draft' );
+
+ } elseif ( post_password_required( $comment_post_ID ) ) {
+
+ /**
+ * Fires when a comment is attempted on a password-protected post.
+ *
+ * @since 2.9.0
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'comment_on_password_protected', $comment_post_ID );
+
+ return new WP_Error( 'comment_on_password_protected' );
+
+ } else {
+
+ /**
+ * Fires before a comment is posted.
+ *
+ * @since 2.8.0
+ *
+ * @param int $comment_post_ID Post ID.
+ */
+ do_action( 'pre_comment_on_post', $comment_post_ID );
+
+ }
+
+ // If the user is logged in
+ $user = wp_get_current_user();
+ if ( $user->exists() ) {
+ if ( empty( $user->display_name ) ) {
+ $user->display_name=$user->user_login;
+ }
+ $comment_author = $user->display_name;
+ $comment_author_email = $user->user_email;
+ $comment_author_url = $user->user_url;
+ $user_ID = $user->ID;
+ if ( current_user_can( 'unfiltered_html' ) ) {
+ if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] )
+ || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID )
+ ) {
+ kses_remove_filters(); // start with a clean slate
+ kses_init_filters(); // set up the filters
+ }
+ }
+ } else {
+ if ( get_option( 'comment_registration' ) ) {
+ return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to post a comment.' ), 403 );
+ }
+ }
+
+ $comment_type = '';
+
+ if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
+ if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
+ return new WP_Error( 'require_name_email', __( 'ERROR: please fill the required fields (name, email).' ), 200 );
+ } elseif ( ! is_email( $comment_author_email ) ) {
+ return new WP_Error( 'require_valid_email', __( 'ERROR: please enter a valid email address.' ), 200 );
+ }
+ }
+
+ if ( '' == $comment_content ) {
+ return new WP_Error( 'require_valid_comment', __( 'ERROR: please type a comment.' ), 200 );
+ }
+
+ $commentdata = compact(
+ 'comment_post_ID',
+ 'comment_author',
+ 'comment_author_email',
+ 'comment_author_url',
+ 'comment_content',
+ 'comment_type',
+ 'comment_parent',
+ 'user_ID'
+ );
+
+ $comment_id = wp_new_comment( wp_slash( $commentdata ) );
+ if ( ! $comment_id ) {
+ return new WP_Error( 'comment_save_error', __( 'ERROR: The comment could not be saved. Please try again later.' ), 500 );
+ }
+
+ return get_comment( $comment_id );
+
+}