wp/wp-includes/comment.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Manages WordPress comments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
 * Check whether a comment passes internal checks to be allowed to add.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
 * If manual comment moderation is set in the administration, then all checks,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
 * regardless of their type and whitelist, will fail and the function will
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
 * return false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * If the number of links exceeds the amount in the administration, then the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * check fails. If any of the parameter contents match the blacklist of words,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * then the check fails.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
 * If the comment author was approved before, then the comment is automatically
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
 * whitelisted.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
 * If all checks pass, the function will return true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @since 1.2.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
 * @param string $author       Comment author name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
 * @param string $email        Comment author email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
 * @param string $url          Comment author URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
 * @param string $comment      Content of the comment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
 * @param string $user_ip      Comment author IP address.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
 * @param string $user_agent   Comment author User-Agent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
 * @param string $comment_type Comment type, either user-submitted comment,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
 *		                       trackback, or pingback.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
 * @return bool If all checks pass, true, otherwise false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	// If manual moderation is enabled, skip all checks and return false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	if ( 1 == get_option('comment_moderation') )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	/** This filter is documented in wp-includes/comment-template.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	$comment = apply_filters( 'comment_text', $comment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
	// Check for the number of external links if a max allowed number is set.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	if ( $max_links = get_option( 'comment_max_links' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		$num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
		 * Filter the maximum number of links allowed in a comment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
		 * @param int    $num_links The number of links allowed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
		 * @param string $url       Comment author's URL. Included in allowed links total.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
		$num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
		/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
		 * If the number of links in the comment exceeds the allowed amount,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
		 * fail the check by returning false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		if ( $num_links >= $max_links )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	$mod_keys = trim(get_option('moderation_keys'));
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
	// If moderation 'keys' (keywords) are set, process them.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	if ( !empty($mod_keys) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		$words = explode("\n", $mod_keys );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		foreach ( (array) $words as $word) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			$word = trim($word);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
			// Skip empty lines.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			if ( empty($word) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
			 * Do some escaping magic so that '#' (number of) characters in the spam
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
			 * words don't break things:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			$word = preg_quote($word, '#');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
			 * Check the comment fields for moderation keywords. If any are found,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
			 * fail the check for the given field by returning false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			$pattern = "#$word#i";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			if ( preg_match($pattern, $author) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			if ( preg_match($pattern, $email) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			if ( preg_match($pattern, $url) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			if ( preg_match($pattern, $comment) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
			if ( preg_match($pattern, $user_ip) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			if ( preg_match($pattern, $user_agent) ) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
	 * Check if the option to approve comments by previously-approved authors is enabled.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
	 * If it is enabled, check whether the comment author has a previously-approved comment,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
	 * as well as whether there are any moderation keywords (if set) present in the author
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
	 * email address. If both checks pass, return true. Otherwise, return false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	if ( 1 == get_option('comment_whitelist')) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
			// expected_slashed ($author, $email)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
			$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");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
			if ( ( 1 == $ok_to_comment ) &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
				( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
					return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * Retrieve the approved comments for post $post_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
 * @since 4.1.0 Refactored to leverage {@see WP_Comment_Query} over a direct query.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
 * @param  int   $post_id The ID of the post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
 * @param  array $args    Optional. See {@see WP_Comment_Query::query()} for information
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
 *                        on accepted arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
 * @return int|array $comments The approved comments, or number of comments if `$count`
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
 *                             argument is true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
function get_approved_comments( $post_id, $args = array() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
	if ( ! $post_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
		return array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
	$defaults = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
		'status'  => 1,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
		'post_id' => $post_id,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
		'order'   => 'ASC',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
	$r = wp_parse_args( $args, $defaults );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
	$query = new WP_Comment_Query;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   152
	return $query->query( $r );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
 * Retrieves comment data given a comment ID or comment object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
 * If an object is passed then the comment data will be cached and then returned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
 * after being passed through a filter. If the comment is empty, then the global
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 * comment variable will be used, if it is set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
 * @param object|string|int $comment Comment to retrieve.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
 * @return object|array|null Depends on $output value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
function get_comment(&$comment, $output = OBJECT) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	if ( empty($comment) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		if ( isset($GLOBALS['comment']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
			$_comment = & $GLOBALS['comment'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
			$_comment = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	} elseif ( is_object($comment) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		wp_cache_add($comment->comment_ID, $comment, 'comment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		$_comment = $comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
			$_comment = & $GLOBALS['comment'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
		} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			$_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
			if ( ! $_comment )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
				return null;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
			wp_cache_add($_comment->comment_ID, $_comment, 'comment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
	 * Fires after a comment is retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
	 * @param mixed $_comment Comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
	$_comment = apply_filters( 'get_comment', $_comment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		return $_comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		$__comment = get_object_vars($_comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		return $__comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		$__comment = array_values(get_object_vars($_comment));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		return $__comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		return $_comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * Retrieve a list of comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 * The comment list can be for the blog as a whole or for an individual post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
 * @since 2.7.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::parse_query()}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
 *                           for information on accepted arguments. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
 * @return int|array List of comments or number of found comments if `$count` argument is true.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
function get_comments( $args = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	$query = new WP_Comment_Query;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	return $query->query( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
 * WordPress Comment Query class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
 * See WP_Comment_Query::__construct() for accepted arguments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
class WP_Comment_Query {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   239
	 * SQL for database query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
	 * @since 4.0.1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
	 * @var string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	public $request;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   246
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   247
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	 * Metadata query container
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
	 * @var object WP_Meta_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	public $meta_query = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	 * Date query container
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
	 * @var object WP_Date_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	public $date_query = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
	 * Query vars set by the user.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
	 * @since 3.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
	public $query_vars;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   273
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	 * Default values for query vars.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
	public $query_var_defaults;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	 * List of comments located by the query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   285
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	 * @var array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
	public $comments;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
	 * Make private/protected methods readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
	 * @param callable $name      Method to call.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
	 * @param array    $arguments Arguments to pass when calling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
	 * @return mixed|bool Return value of the callback, false otherwise.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
	public function __call( $name, $arguments ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
		if ( 'get_search_sql' === $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
			return call_user_func_array( array( $this, $name ), $arguments );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   305
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   309
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
	 * Constructor.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
	 * Sets up the comment query, based on the query vars passed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
	 * @since  4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
	 * @param string|array $query {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
	 *     Optional. Array or query string of comment query parameters. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	 *     @type string       $author_email        Comment author email address. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
	 *     @type array        $author__in          Array of author IDs to include comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
	 *     @type array        $author__not_in      Array of author IDs to exclude comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
	 *     @type array        $comment__in         Array of comment IDs to include. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
	 *     @type array        $comment__not_in     Array of comment IDs to exclude. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   325
	 *     @type bool         $count               Whether to return a comment count (true) or array of comment
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   326
	 *                                             objects (false). Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
	 *     @type array        $date_query          Date query clauses to limit comments by. See WP_Date_Query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
	 *                                             Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
	 *     @type string       $fields              Comment fields to return. Accepts 'ids' for comment IDs only or
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	 *                                             empty for all fields. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	 *     @type int          $ID                  Currently unused.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
	 *     @type array        $include_unapproved  Array of IDs or email addresses of users whose unapproved comments
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
	 *                                             will be returned by the query regardless of `$status`. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	 *     @type int          $karma               Karma score to retrieve matching comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
	 *     @type string       $meta_key            Include comments with a matching comment meta key. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
	 *     @type string       $meta_value          Include comments with a matching comment meta value. Requires
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
	 *                                             `$meta_key` to be set. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
	 *     @type array        $meta_query          Meta query clauses to limit retrieved comments by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
	 *                                             See WP_Meta_Query. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	 *     @type int          $number              Maximum number of comments to retrieve. Default null (no limit).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
	 *     @type int          $offset              Number of comments to offset the query. Used to build LIMIT clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	 *                                             Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
	 *     @type string|array $orderby             Comment status or array of statuses. To use 'meta_value' or
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	 *                                             'meta_value_num', `$meta_key` must also be defined. To sort by
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	 *                                             a specific `$meta_query` clause, use that clause's array key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 *                                             Accepts 'comment_agent', 'comment_approved', 'comment_author',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
	 *                                             'comment_author_email', 'comment_author_IP',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
	 *                                             'comment_author_url', 'comment_content', 'comment_date',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	 *                                             'comment_date_gmt', 'comment_ID', 'comment_karma',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
	 *                                             'comment_parent', 'comment_post_ID', 'comment_type', 'user_id',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
	 *                                             'meta_value', 'meta_value_num', the value of $meta_key, and the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
	 *                                             array keys of `$meta_query`. Also accepts false, an empty array,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
	 *                                             or 'none' to disable `ORDER BY` clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
	 *                                             Default: 'comment_date_gmt'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
	 *     @type string       $order               How to order retrieved comments. Accepts 'ASC', 'DESC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
	 *                                             Default: 'DESC'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
	 *     @type int          $parent              Parent ID of comment to retrieve children of. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
	 *     @type array        $post_author__in     Array of author IDs to retrieve comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
	 *     @type array        $post_author__not_in Array of author IDs *not* to retrieve comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
	 *     @type int          $post_ID             Currently unused.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
	 *     @type int          $post_id             Limit results to those affiliated with a given post ID. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
	 *     @type array        $post__in            Array of post IDs to include affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
	 *     @type array        $post__not_in        Array of post IDs to exclude affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
	 *     @type int          $post_author         Comment author ID to limit results by. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
	 *     @type string       $post_status         Post status to retrieve affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
	 *     @type string       $post_type           Post type to retrieve affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
	 *     @type string       $post_name           Post name to retrieve affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
	 *     @type int          $post_parent         Post parent ID to retrieve affiliated comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	 *     @type string       $search              Search term(s) to retrieve matching comments for. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 *     @type string       $status              Comment status to limit results by. Accepts 'hold'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	 *                                             (`comment_status=0`), 'approve' (`comment_status=1`), 'all', or a
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	 *                                             custom comment status. Default 'all'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
	 *     @type string|array $type                Include comments of a given type, or array of types. Accepts
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
	 *                                             'comment', 'pings' (includes 'pingback' and 'trackback'), or any
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
	 *                                             custom type string. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	 *     @type array        $type__in            Include comments from a given array of comment types. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 *     @type int          $user_id             Include comments for a specific user ID. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 * @return WP_Comment_Query WP_Comment_Query instance.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
	public function __construct( $query = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   383
		$this->query_var_defaults = array(
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
			'author_email' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
			'author__in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
			'author__not_in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
			'include_unapproved' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
			'fields' => '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
			'ID' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
			'comment__in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
			'comment__not_in' => '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
			'karma' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			'number' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
			'offset' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
			'orderby' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
			'order' => 'DESC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
			'parent' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
			'post_author__in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
			'post_author__not_in' => '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			'post_ID' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
			'post_id' => 0,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
			'post__in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
			'post__not_in' => '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			'post_author' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
			'post_name' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
			'post_parent' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			'post_status' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
			'post_type' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   409
			'status' => 'all',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
			'type' => '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   411
			'type__in' => '',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   412
			'type__not_in' => '',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			'user_id' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
			'search' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
			'count' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
			'meta_key' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
			'meta_value' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
			'meta_query' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
			'date_query' => null, // See WP_Date_Query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
		if ( ! empty( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
			$this->query( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
	 * Parse arguments passed to the comment query with default query parameters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
	 * @since  4.2.0 Extracted from WP_Comment_Query::query().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
	 * @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
	public function parse_query( $query = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
		if ( empty( $query ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   438
			$query = $this->query_vars;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   440
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
		$this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   442
		do_action_ref_array( 'parse_comment_query', array( &$this ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   444
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
	 * Sets up the WordPress query for retrieving comments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   447
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   448
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   449
	 * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   450
	 *              'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
	 *              'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   452
	 *              arguments to $query_vars.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   453
	 * @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   454
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   455
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
	 * @param string|array $query Array or URL query string of parameters.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
	 * @return array List of comments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
	public function query( $query ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
		$this->query_vars = wp_parse_args( $query );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
		return $this->get_comments();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   463
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   464
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   465
	 * Get a list of comments matching the query vars.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   466
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   467
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   468
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   469
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   470
	 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   471
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
	 * @return array The list of comments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
	public function get_comments() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
		$groupby = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
		$this->parse_query();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
		// Parse meta query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		$this->meta_query = new WP_Meta_Query();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
		$this->meta_query->parse_query_vars( $this->query_vars );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   485
		if ( ! empty( $this->meta_query->queries ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
			$meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   487
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   488
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   489
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
		 * Fires before comments are retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   492
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   493
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   494
		 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
		do_action_ref_array( 'pre_get_comments', array( &$this ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   497
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   498
		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
		$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		$last_changed = wp_cache_get( 'last_changed', 'comment' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
		if ( ! $last_changed ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
			$last_changed = microtime();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			wp_cache_set( 'last_changed', $last_changed, 'comment' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
		$cache_key = "get_comments:$key:$last_changed";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
		if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   508
			$this->comments = $cache;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
			return $this->comments;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   510
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   511
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   512
		$where = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
		// Assemble clauses related to 'comment_approved'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
		$approved_clauses = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   516
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   517
		// 'status' accepts an array or a comma-separated string.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   518
		$status_clauses = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
		$statuses = $this->query_vars['status'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   520
		if ( ! is_array( $statuses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   521
			$statuses = preg_split( '/[\s,]+/', $statuses );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   523
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   524
		// 'any' overrides other statuses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   525
		if ( ! in_array( 'any', $statuses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   526
			foreach ( $statuses as $status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
				switch ( $status ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
					case 'hold' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   529
						$status_clauses[] = "comment_approved = '0'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   531
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
					case 'approve' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   533
						$status_clauses[] = "comment_approved = '1'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
					case 'all' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   537
					case '' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   538
						$status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   540
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
					default :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
						$status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   544
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   546
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
			if ( ! empty( $status_clauses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   548
				$approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   549
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   551
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   552
		// User IDs or emails whose unapproved comments are included, regardless of $status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   553
		if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   554
			$include_unapproved = $this->query_vars['include_unapproved'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   555
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
			// Accepts arrays or comma-separated strings.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   557
			if ( ! is_array( $include_unapproved ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   558
				$include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
			$unapproved_ids = $unapproved_emails = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
			foreach ( $include_unapproved as $unapproved_identifier ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   563
				// Numeric values are assumed to be user ids.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   564
				if ( is_numeric( $unapproved_identifier ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   565
					$approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   566
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   567
				// Otherwise we match against email addresses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   568
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   569
					$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   572
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   573
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
		// Collapse comment_approved clauses into a single OR-separated clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
		if ( ! empty( $approved_clauses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   576
			if ( 1 === count( $approved_clauses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   577
				$where[] = $approved_clauses[0];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   578
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   579
				$where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   580
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   583
		$order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   584
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   585
		// Disable ORDER BY with 'none', an empty array, or boolean false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
		if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
			$orderby = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
		} elseif ( ! empty( $this->query_vars['orderby'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
			$ordersby = is_array( $this->query_vars['orderby'] ) ?
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
				$this->query_vars['orderby'] :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
				preg_split( '/[,\s]/', $this->query_vars['orderby'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
			$orderby_array = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
			$found_orderby_comment_ID = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   595
			foreach ( $ordersby as $_key => $_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   596
				if ( ! $_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   597
					continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   598
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   599
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   600
				if ( is_int( $_key ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   601
					$_orderby = $_value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   602
					$_order = $order;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   603
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   604
					$_orderby = $_key;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   605
					$_order = $_value;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   606
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   607
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   608
				if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   609
					$found_orderby_comment_ID = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   610
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   611
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   612
				$parsed = $this->parse_orderby( $_orderby );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   613
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   614
				if ( ! $parsed ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   615
					continue;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   616
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   617
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   618
				$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   619
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   620
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   621
			// If no valid clauses were found, order by comment_date_gmt.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   622
			if ( empty( $orderby_array ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   623
				$orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   625
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   626
			// To ensure determinate sorting, always include a comment_ID clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   627
			if ( ! $found_orderby_comment_ID ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
				$comment_ID_order = '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   629
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
				// Inherit order from comment_date or comment_date_gmt, if available.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
				foreach ( $orderby_array as $orderby_clause ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   632
					if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   633
						$comment_ID_order = $match[1];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   634
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   637
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
				// If no date-related order is available, use the date from the first available clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   639
				if ( ! $comment_ID_order ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
					foreach ( $orderby_array as $orderby_clause ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
						if ( false !== strpos( 'ASC', $orderby_clause ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
							$comment_ID_order = 'ASC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
						} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   644
							$comment_ID_order = 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
						}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   648
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   649
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   650
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   651
				// Default to DESC.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   652
				if ( ! $comment_ID_order ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   653
					$comment_ID_order = 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   654
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   655
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   656
				$orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   659
			$orderby = implode( ', ', $orderby_array );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   661
			$orderby = "$wpdb->comments.comment_date_gmt $order";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   662
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   663
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   664
		$number = absint( $this->query_vars['number'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   665
		$offset = absint( $this->query_vars['offset'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   666
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   667
		if ( ! empty( $number ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   668
			if ( $offset ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
				$limits = 'LIMIT ' . $offset . ',' . $number;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   670
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
				$limits = 'LIMIT ' . $number;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   672
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
			$limits = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   677
		if ( $this->query_vars['count'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
			$fields = 'COUNT(*)';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   679
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
			switch ( strtolower( $this->query_vars['fields'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   681
				case 'ids':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
					$fields = "$wpdb->comments.comment_ID";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   683
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
				default:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
					$fields = "*";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
					break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
		$join = '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
		$post_id = absint( $this->query_vars['post_id'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
		if ( ! empty( $post_id ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
			$where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   696
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
		// Parse comment IDs for an IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
		if ( ! empty( $this->query_vars['comment__in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   699
			$where[] = 'comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
		// Parse comment IDs for a NOT IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
		if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
			$where[] = 'comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
		// Parse comment post IDs for an IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
		if ( ! empty( $this->query_vars['post__in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
			$where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   711
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   712
		// Parse comment post IDs for a NOT IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   713
		if ( ! empty( $this->query_vars['post__not_in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   714
			$where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
		if ( '' !== $this->query_vars['author_email'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
			$where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
		if ( '' !== $this->query_vars['karma'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
			$where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   723
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
		// Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
		$raw_types = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   727
			'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   728
			'NOT IN' => (array) $this->query_vars['type__not_in'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   729
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   730
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   731
		$comment_types = array();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   732
		foreach ( $raw_types as $operator => $_raw_types ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   733
			$_raw_types = array_unique( $_raw_types );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   734
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   735
			foreach ( $_raw_types as $type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   736
				switch ( $type ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   737
					// An empty translates to 'all', for backward compatibility
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   738
					case '':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   739
					case 'all' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   740
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   741
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   742
					case 'comment':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   743
					case 'comments':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   744
						$comment_types[ $operator ][] = "''";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   745
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   747
					case 'pings':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
						$comment_types[ $operator ][] = "'pingback'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   749
						$comment_types[ $operator ][] = "'trackback'";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   751
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   752
					default:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   753
						$comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   754
						break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   755
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   756
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   757
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   758
			if ( ! empty( $comment_types[ $operator ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   759
				$types_sql = implode( ', ', $comment_types[ $operator ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   760
				$where[] = "comment_type $operator ($types_sql)";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   761
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   763
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   764
		if ( '' !== $this->query_vars['parent'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   765
			$where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   766
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   767
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   768
		if ( is_array( $this->query_vars['user_id'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
			$where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   770
		} elseif ( '' !== $this->query_vars['user_id'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   771
			$where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   772
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   773
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   774
		if ( '' !== $this->query_vars['search'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   775
			$search_sql = $this->get_search_sql(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   776
				$this->query_vars['search'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   777
				array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   778
			);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   779
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   780
			// Strip leading 'AND'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   781
			$where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   782
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   783
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   784
		// If any post-related query vars are passed, join the posts table.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   785
		$join_posts_table = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   786
		$plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   787
		$post_fields = array_filter( $plucked );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   788
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		if ( ! empty( $post_fields ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   790
			$join_posts_table = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   791
			foreach ( $post_fields as $field_name => $field_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   792
				// $field_value may be an array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   793
				$esses = array_fill( 0, count( (array) $field_value ), '%s' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   794
				$where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   795
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   796
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   797
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   798
		// Comment author IDs for an IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   799
		if ( ! empty( $this->query_vars['author__in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   800
			$where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   801
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   802
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   803
		// Comment author IDs for a NOT IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   804
		if ( ! empty( $this->query_vars['author__not_in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   805
			$where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   806
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   807
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   808
		// Post author IDs for an IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   809
		if ( ! empty( $this->query_vars['post_author__in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   810
			$join_posts_table = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   811
			$where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   812
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   813
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   814
		// Post author IDs for a NOT IN clause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   815
		if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   816
			$join_posts_table = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   817
			$where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   818
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   819
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   820
		if ( $join_posts_table ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
			$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
		if ( ! empty( $meta_query_clauses ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
			$join .= $meta_query_clauses['join'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
			// Strip leading 'AND'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
			$where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   830
			if ( ! $this->query_vars['count'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   831
				$groupby = "{$wpdb->comments}.comment_ID";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   832
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   835
		$date_query = $this->query_vars['date_query'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
		if ( ! empty( $date_query ) && is_array( $date_query ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
			$date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   838
			$where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   841
		$where = implode( ' AND ', $where );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   842
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   843
		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   844
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   845
		 * Filter the comment query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   846
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   847
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   848
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   849
		 * @param array            $pieces A compacted array of comment query clauses.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   850
		 * @param WP_Comment_Query &$this  Current instance of WP_Comment_Query, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   851
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
		$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   853
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   854
		$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   855
		$join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   856
		$where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   857
		$orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   858
		$limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   859
		$groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   860
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   861
		if ( $where ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   862
			$where = 'WHERE ' . $where;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   863
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   864
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   865
		if ( $groupby ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
			$groupby = 'GROUP BY ' . $groupby;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   867
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   868
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   869
		if ( $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   870
			$orderby = "ORDER BY $orderby";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   871
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   872
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   873
		$this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   874
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   875
		if ( $this->query_vars['count'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
			return $wpdb->get_var( $this->request );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   877
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   878
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   879
		if ( 'ids' == $this->query_vars['fields'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   880
			$this->comments = $wpdb->get_col( $this->request );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   881
			return array_map( 'intval', $this->comments );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   882
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   883
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   884
		$results = $wpdb->get_results( $this->request );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   885
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   886
		 * Filter the comment query results.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   887
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   888
		 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   889
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   890
		 * @param array            $results  An array of comments.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   891
		 * @param WP_Comment_Query &$this    Current instance of WP_Comment_Query, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   892
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   893
		$comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
		wp_cache_add( $cache_key, $comments, 'comment' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   897
		$this->comments = $comments;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
		return $this->comments;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   901
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
	 * Used internally to generate an SQL string for searching across multiple columns
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
	 * @access protected
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
	 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
	 * @param string $string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
	 * @param array $cols
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   911
	protected function get_search_sql( $string, $cols ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   912
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   914
		$like = '%' . $wpdb->esc_like( $string ) . '%';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
		$searches = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
		foreach ( $cols as $col ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
			$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
		return ' AND (' . implode(' OR ', $searches) . ')';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   923
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   925
	 * Parse and sanitize 'orderby' keys passed to the comment query.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   928
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   929
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   930
	 * @global wpdb $wpdb WordPress database abstraction object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   932
	 * @param string $orderby Alias for the field to order by.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
	 * @return string|bool Value to used in the ORDER clause. False otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   934
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
	protected function parse_orderby( $orderby ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
		global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
		$allowed_keys = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
			'comment_agent',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   940
			'comment_approved',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   941
			'comment_author',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   942
			'comment_author_email',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
			'comment_author_IP',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   944
			'comment_author_url',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   945
			'comment_content',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
			'comment_date',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   947
			'comment_date_gmt',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
			'comment_ID',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
			'comment_karma',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
			'comment_parent',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   951
			'comment_post_ID',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   952
			'comment_type',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   953
			'user_id',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
		if ( ! empty( $this->query_vars['meta_key'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
			$allowed_keys[] = $this->query_vars['meta_key'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
			$allowed_keys[] = 'meta_value';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
			$allowed_keys[] = 'meta_value_num';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
		$meta_query_clauses = $this->meta_query->get_clauses();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
		if ( $meta_query_clauses ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
			$allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
		$parsed = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
		if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
			$parsed = "$wpdb->commentmeta.meta_value";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
		} else if ( $orderby == 'meta_value_num' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
			$parsed = "$wpdb->commentmeta.meta_value+0";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
		} else if ( in_array( $orderby, $allowed_keys ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
			if ( isset( $meta_query_clauses[ $orderby ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
				$meta_clause = $meta_query_clauses[ $orderby ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
				$parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
				$parsed = "$wpdb->comments.$orderby";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
		return $parsed;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   984
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
	 * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
	 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
	 * @access protected
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   990
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
	 * @param string $order The 'order' query variable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   992
	 * @return string The sanitized 'order' query variable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
	protected function parse_order( $order ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
		if ( ! is_string( $order ) || empty( $order ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   996
			return 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   999
		if ( 'ASC' === strtoupper( $order ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1000
			return 'ASC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1001
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1002
			return 'DESC';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1003
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1004
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
 * Retrieve all of the WordPress supported comment statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
 * Comments have a limited set of valid status values, this provides the comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
 * status values and descriptions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
 * @return array List of comment statuses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
function get_comment_statuses() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
	$status = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		'hold'		=> __('Unapproved'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
		/* translators: comment status  */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		'approve'	=> _x('Approved', 'adjective'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		/* translators: comment status */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
		'spam'		=> _x('Spam', 'adjective'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
	return $status;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
 * The date the last comment was modified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1033
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
 *		or 'server' locations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
 * @return string Last comment modified date.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
function get_lastcommentmodified($timezone = 'server') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	static $cache_lastcommentmodified = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
	if ( isset($cache_lastcommentmodified[$timezone]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
		return $cache_lastcommentmodified[$timezone];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
	$add_seconds_server = date('Z');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
	switch ( strtolower($timezone)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
		case 'gmt':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
			$lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
		case 'blog':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
			$lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
		case 'server':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			$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));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
	$cache_lastcommentmodified[$timezone] = $lastcommentmodified;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
	return $lastcommentmodified;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
 * The amount of comments in a post or total comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
 * A lot like {@link wp_count_comments()}, in that they both return comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
 * stats (albeit with different types). The {@link wp_count_comments()} actual
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
 * caches, but this function does not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1074
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1075
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
function get_comment_count( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
	$where = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
	if ( $post_id > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
		$where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
	$totals = (array) $wpdb->get_results("
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
		SELECT comment_approved, COUNT( * ) AS total
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
		FROM {$wpdb->comments}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		{$where}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
		GROUP BY comment_approved
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
	", ARRAY_A);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
	$comment_count = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
		"approved"              => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
		"awaiting_moderation"   => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
		"spam"                  => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
		"total_comments"        => 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
	foreach ( $totals as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
		switch ( $row['comment_approved'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
			case 'spam':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
				$comment_count['spam'] = $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
				$comment_count["total_comments"] += $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
			case 1:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
				$comment_count['approved'] = $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
				$comment_count['total_comments'] += $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
			case 0:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
				$comment_count['awaiting_moderation'] = $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
				$comment_count['total_comments'] += $row['total'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
	return $comment_count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
// Comment meta functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
 * Add meta data field to a comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1134
 * @link https://codex.wordpress.org/Function_Reference/add_comment_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
 * @param string $meta_key Metadata name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
 * @param mixed $meta_value Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
 * @param bool $unique Optional, default is false. Whether the same key should not be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
 * @return int|bool Meta ID on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
	return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
 * Remove metadata matching criteria from a comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
 * You can match based on the key, or key and value. Removing based on key and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
 * value, will keep from removing duplicate metadata with the same key. It also
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
 * allows removing all metadata matching key, if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
 * @link https://codex.wordpress.org/Function_Reference/delete_comment_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
 * @param int $comment_id comment ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
 * @param string $meta_key Metadata name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
 * @param mixed $meta_value Optional. Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
	return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
 * Retrieve comment meta field for a comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1169
 * @link https://codex.wordpress.org/Function_Reference/get_comment_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
 * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
 * @param bool $single Whether to return a single value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
 *  is true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
function get_comment_meta($comment_id, $key = '', $single = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
	return get_metadata('comment', $comment_id, $key, $single);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
 * Update comment meta field based on comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
 * Use the $prev_value parameter to differentiate between meta fields with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
 * same key and comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
 * If the meta field for the comment does not exist, it will be added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
 * @since 2.9.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1190
 * @link https://codex.wordpress.org/Function_Reference/update_comment_meta
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
 * @param string $meta_key Metadata key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
 * @param mixed $meta_value Metadata value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
 * @param mixed $prev_value Optional. Previous value to check before removing.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1196
 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
	return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
 * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
 * to recall previous comments by this commentator that are still held in moderation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
 * @param object $comment Comment object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
 * @param object $user Comment author's object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
function wp_set_comment_cookies($comment, $user) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
	if ( $user->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	 * Filter the lifetime of the comment cookie in seconds.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1220
	 * @param int $seconds Comment cookie lifetime. Default 30000000.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1221
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1222
	$comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1223
	$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1224
	setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1225
	setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
	setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
 * Sanitizes the cookies sent to the user already.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
 * Will only do anything if the cookies have already been created for the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
 * Mostly used after cookies had been sent to use elsewhere.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
 * @since 2.0.4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
function sanitize_comment_cookies() {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
	if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
		 * Filter the comment author's name cookie before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1241
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1242
		 * When this filter hook is evaluated in wp_filter_comment(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1243
		 * the comment author's name string is passed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1244
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1245
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1246
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1247
		 * @param string $author_cookie The comment author name cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1249
		$comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
		$comment_author = wp_unslash($comment_author);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
		$comment_author = esc_attr($comment_author);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1252
		$_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1255
	if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1256
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1257
		 * Filter the comment author's email cookie before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1258
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1259
		 * When this filter hook is evaluated in wp_filter_comment(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1260
		 * the comment author's email string is passed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1261
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1262
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1263
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1264
		 * @param string $author_email_cookie The comment author email cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1265
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1266
		$comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
		$comment_author_email = wp_unslash($comment_author_email);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
		$comment_author_email = esc_attr($comment_author_email);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
		$_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1272
	if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1273
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1274
		 * Filter the comment author's URL cookie before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1275
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1276
		 * When this filter hook is evaluated in wp_filter_comment(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1277
		 * the comment author's URL string is passed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1278
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1279
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1280
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1281
		 * @param string $author_url_cookie The comment author URL cookie.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1282
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1283
		$comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
		$comment_author_url = wp_unslash($comment_author_url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
		$_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
 * Validates whether this comment is allowed to be made.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1293
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1294
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
 * @param array $commentdata Contains information on the comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
 * @return mixed Signifies the approval status (0|1|'spam')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1299
function wp_allow_comment( $commentdata ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
	// Simple duplicate check
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1304
	$dupe = $wpdb->prepare(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1305
		"SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1306
		wp_unslash( $commentdata['comment_post_ID'] ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1307
		wp_unslash( $commentdata['comment_parent'] ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1308
		wp_unslash( $commentdata['comment_author'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1309
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1310
	if ( $commentdata['comment_author_email'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1311
		$dupe .= $wpdb->prepare(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1312
			"OR comment_author_email = %s ",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1313
			wp_unslash( $commentdata['comment_author_email'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1314
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1315
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1316
	$dupe .= $wpdb->prepare(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1317
		") AND comment_content = %s LIMIT 1",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1318
		wp_unslash( $commentdata['comment_content'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1319
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1320
	if ( $wpdb->get_var( $dupe ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1321
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1322
		 * Fires immediately after a duplicate comment is detected.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1323
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1324
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1325
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1326
		 * @param array $commentdata Comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1327
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
		do_action( 'comment_duplicate_trigger', $commentdata );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1329
		if ( defined( 'DOING_AJAX' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
			die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1331
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1332
		wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1335
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1336
	 * Fires immediately before a comment is marked approved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1337
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1338
	 * Allows checking for comment flooding.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1339
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1340
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1341
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1342
	 * @param string $comment_author_IP    Comment author's IP address.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1343
	 * @param string $comment_author_email Comment author's email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1344
	 * @param string $comment_date_gmt     GMT date the comment was posted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
	do_action(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
		'check_comment_flood',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
		$commentdata['comment_author_IP'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
		$commentdata['comment_author_email'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
		$commentdata['comment_date_gmt']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1352
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1353
	if ( ! empty( $commentdata['user_id'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1354
		$user = get_userdata( $commentdata['user_id'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1355
		$post_author = $wpdb->get_var( $wpdb->prepare(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1356
			"SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1357
			$commentdata['comment_post_ID']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1358
		) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1361
	if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
		// The author and the admins get respect.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
		$approved = 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1364
	} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
		// Everyone else's comments will be checked.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1366
		if ( check_comment(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1367
			$commentdata['comment_author'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1368
			$commentdata['comment_author_email'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1369
			$commentdata['comment_author_url'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1370
			$commentdata['comment_content'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1371
			$commentdata['comment_author_IP'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1372
			$commentdata['comment_agent'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1373
			$commentdata['comment_type']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1374
		) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
			$approved = 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1376
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
			$approved = 0;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1378
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1379
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1380
		if ( wp_blacklist_check(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1381
			$commentdata['comment_author'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1382
			$commentdata['comment_author_email'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1383
			$commentdata['comment_author_url'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1384
			$commentdata['comment_content'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1385
			$commentdata['comment_author_IP'],
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1386
			$commentdata['comment_agent']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1387
		) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
			$approved = 'spam';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1389
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1392
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1393
	 * Filter a comment's approval status before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1394
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1395
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1396
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1397
	 * @param bool|string $approved    The approval status. Accepts 1, 0, or 'spam'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1398
	 * @param array       $commentdata Comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1399
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
	$approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
	return $approved;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
 * Check whether comment flooding is occurring.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
 * Won't run, if current user can manage options, so to not block
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
 * administrators.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1411
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1412
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
 * @param string $ip Comment IP.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
 * @param string $email Comment author email address.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
 * @param string $date MySQL time string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
function check_comment_flood_db( $ip, $email, $date ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
	if ( current_user_can( 'manage_options' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
		return; // don't throttle admins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
	$hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
	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 ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		$time_lastcomment = mysql2date('U', $lasttime, false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
		$time_newcomment  = mysql2date('U', $date, false);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1426
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1427
		 * Filter the comment flood status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1428
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1429
		 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1430
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1431
		 * @param bool $bool             Whether a comment flood is occurring. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1432
		 * @param int  $time_lastcomment Timestamp of when the last comment was posted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1433
		 * @param int  $time_newcomment  Timestamp of when the new comment was posted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1434
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1435
		$flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
		if ( $flood_die ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1437
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1438
			 * Fires before the comment flood message is triggered.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1439
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1440
			 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1441
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1442
			 * @param int $time_lastcomment Timestamp of when the last comment was posted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1443
			 * @param int $time_newcomment  Timestamp of when the new comment was posted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1444
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1445
			do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
			if ( defined('DOING_AJAX') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
				die( __('You are posting comments too quickly. Slow down.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1450
			wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
 * Separates an array of comments into an array keyed by comment_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
 * @param array $comments Array of comments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
 * @return array Array of comments keyed by comment_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
function separate_comments(&$comments) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
	$comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
	$count = count($comments);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
	for ( $i = 0; $i < $count; $i++ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
		$type = $comments[$i]->comment_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
		if ( empty($type) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
			$type = 'comment';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
		$comments_by_type[$type][] = &$comments[$i];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
		if ( 'trackback' == $type || 'pingback' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
			$comments_by_type['pings'][] = &$comments[$i];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
	return $comments_by_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
 * Calculate the total number of comment pages.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
 * @since 2.7.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1482
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
 * @uses Walker_Comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
 * @param int $per_page Optional comments per page.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
 * @param boolean $threaded Optional control over flat or threaded comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
 * @return int Number of comment pages.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
	global $wp_query;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
	if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
		return $wp_query->max_num_comment_pages;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1496
	if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments )  )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
		$comments = $wp_query->comments;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
	if ( empty($comments) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
		return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
	if ( ! get_option( 'page_comments' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
		return 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
	if ( !isset($per_page) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
		$per_page = (int) get_query_var('comments_per_page');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
	if ( 0 === $per_page )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
		$per_page = (int) get_option('comments_per_page');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
	if ( 0 === $per_page )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
		return 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
	if ( !isset($threaded) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
		$threaded = get_option('thread_comments');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
	if ( $threaded ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
		$walker = new Walker_Comment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
		$count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
		$count = ceil( count( $comments ) / $per_page );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
	return $count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
 * Calculate what page number a comment will appear on for comment paging.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
 * @param int $comment_ID Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
 * @param array $args Optional args.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
 * @return int|null Comment page number or null on error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
function get_page_of_comment( $comment_ID, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
	if ( !$comment = get_comment( $comment_ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
	$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
	if ( '' === $args['per_page'] && get_option('page_comments') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
		$args['per_page'] = get_query_var('comments_per_page');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
	if ( empty($args['per_page']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
		$args['per_page'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
		$args['page'] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
	if ( $args['per_page'] < 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
		return 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
	if ( '' === $args['max_depth'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
		if ( get_option('thread_comments') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
			$args['max_depth'] = get_option('thread_comments_depth');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
			$args['max_depth'] = -1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
	// Find this comment's top level parent if threading is enabled
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
	if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
		return get_page_of_comment( $comment->comment_parent, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
	$allowedtypes = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
		'comment' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
		'pingback' => 'pingback',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
		'trackback' => 'trackback',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
	$comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
	// Count comments older than this one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
	$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 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
	// No older comments? Then it's page #1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
	if ( 0 == $oldercoms )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
		return 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
	// Divide comments older than this one by comments per page to get this comment's page number
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
	return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
 * Does comment contain blacklisted characters or words.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
 * @param string $author The author of the comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
 * @param string $email The email of the comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
 * @param string $url The url used in the comment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
 * @param string $comment The comment content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
 * @param string $user_ip The comment author IP address
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
 * @param string $user_agent The author's browser user agent
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
 * @return bool True if comment contains blacklisted content, false if comment does not
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1596
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1597
	 * Fires before the comment is tested for blacklisted characters or words.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1598
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1599
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1600
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1601
	 * @param string $author     Comment author.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1602
	 * @param string $email      Comment author's email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1603
	 * @param string $url        Comment author's URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1604
	 * @param string $comment    Comment content.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1605
	 * @param string $user_ip    Comment author's IP address.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1606
	 * @param string $user_agent Comment author's browser user agent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1607
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1608
	do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
	$mod_keys = trim( get_option('blacklist_keys') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
	if ( '' == $mod_keys )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
		return false; // If moderation keys are empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
	$words = explode("\n", $mod_keys );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
	foreach ( (array) $words as $word ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
		$word = trim($word);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
		// Skip empty lines
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
		if ( empty($word) ) { continue; }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
		// Do some escaping magic so that '#' chars in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
		// spam words don't break things:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
		$word = preg_quote($word, '#');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
		$pattern = "#$word#i";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
		if (
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
			   preg_match($pattern, $author)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
			|| preg_match($pattern, $email)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
			|| preg_match($pattern, $url)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
			|| preg_match($pattern, $comment)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
			|| preg_match($pattern, $user_ip)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
			|| preg_match($pattern, $user_agent)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
		 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
 * Retrieve total comments for blog or single post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
 * The properties of the returned object contain the 'moderated', 'approved',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
 * and spam comments for either the entire blog or single post. Those properties
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
 * contain the amount of comments that match the status. The 'total_comments'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
 * property contains the integer of total comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
 * The comment stats are cached and then retrieved, if they already exist in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
 * cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
 * @param int $post_id Optional. Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
 * @return object Comment stats.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
function wp_count_comments( $post_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1660
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1661
	 * Filter the comments count for a given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1662
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1663
	 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1664
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1665
	 * @param array $count   An empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1666
	 * @param int   $post_id The post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1667
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1668
	$stats = apply_filters( 'wp_count_comments', array(), $post_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
	if ( !empty($stats) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
		return $stats;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
	$count = wp_cache_get("comments-{$post_id}", 'counts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
	if ( false !== $count )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
		return $count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
	$where = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	if ( $post_id > 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
		$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
	$total = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
	foreach ( (array) $count as $row ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
		// Don't count post-trashed toward totals
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
		if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
			$total += $row['num_comments'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
		if ( isset( $approved[$row['comment_approved']] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
			$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
	$stats['total_comments'] = $total;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
	foreach ( $approved as $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
		if ( empty($stats[$key]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
			$stats[$key] = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
	$stats = (object) $stats;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
	wp_cache_set("comments-{$post_id}", $stats, 'counts');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
	return $stats;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
 * Trashes or deletes a comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
 * The comment is moved to trash instead of permanently deleted unless trash is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
 * disabled, item is already in the trash, or $force_delete is true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
 * The post comment count will be updated if the comment was approved and has a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
 * post ID available.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1715
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1716
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
 * @param int $comment_id Comment ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
function wp_delete_comment($comment_id, $force_delete = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
	if (!$comment = get_comment($comment_id))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
	if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
		return wp_trash_comment($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1730
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1731
	 * Fires immediately before a comment is deleted from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1732
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1733
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1734
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1735
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1736
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1737
	do_action( 'delete_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
	// Move children up a level.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
	$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
	if ( !empty($children) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
		$wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
		clean_comment_cache($children);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
	// Delete metadata
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
	foreach ( $meta_ids as $mid )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
		delete_metadata_by_mid( 'comment', $mid );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
	if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1753
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1754
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1755
	 * Fires immediately after a comment is deleted from the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1756
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1757
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1758
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1759
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1760
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1761
	do_action( 'deleted_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
	$post_id = $comment->comment_post_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
	if ( $post_id && $comment->comment_approved == 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
		wp_update_comment_count($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
	clean_comment_cache($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1769
	/** This action is documented in wp-includes/comment.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1770
	do_action( 'wp_set_comment_status', $comment_id, 'delete' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1771
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
	wp_transition_comment_status('delete', $comment->comment_approved, $comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
 * Moves a comment to the Trash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
 * If trash is disabled, comment is permanently deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1780
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1782
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
function wp_trash_comment($comment_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
	if ( !EMPTY_TRASH_DAYS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
		return wp_delete_comment($comment_id, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
	if ( !$comment = get_comment($comment_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1793
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1794
	 * Fires immediately before a comment is sent to the Trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1795
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1796
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1797
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1798
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1799
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1800
	do_action( 'trash_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
	if ( wp_set_comment_status($comment_id, 'trash') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
		add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
		add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1805
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1806
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1807
		 * Fires immediately after a comment is sent to Trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1808
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1809
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1810
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1811
		 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1812
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1813
		do_action( 'trashed_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
 * Removes a comment from the Trash
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1827
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
function wp_untrash_comment($comment_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
	if ( ! (int)$comment_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1832
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1833
	 * Fires immediately before a comment is restored from the Trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1834
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1835
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1836
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1837
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1838
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1839
	do_action( 'untrash_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
	$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
	if ( empty($status) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
		$status = '0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
	if ( wp_set_comment_status($comment_id, $status) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
		delete_comment_meta($comment_id, '_wp_trash_meta_time');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
		delete_comment_meta($comment_id, '_wp_trash_meta_status');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1848
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1849
		 * Fires immediately after a comment is restored from the Trash.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1850
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1851
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1852
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1853
		 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1854
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1855
		do_action( 'untrashed_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1856
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
 * Marks a comment as Spam
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
function wp_spam_comment($comment_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
	if ( !$comment = get_comment($comment_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1874
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1875
	 * Fires immediately before a comment is marked as Spam.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1876
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1877
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1878
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1879
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1880
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1881
	do_action( 'spam_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
	if ( wp_set_comment_status($comment_id, 'spam') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
		add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1885
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1886
		 * Fires immediately after a comment is marked as Spam.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1887
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1888
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1889
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1890
		 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1891
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1892
		do_action( 'spammed_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1894
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
 * Removes a comment from the Spam
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
 * @return bool True on success, false on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
function wp_unspam_comment($comment_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
	if ( ! (int)$comment_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1911
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1912
	 * Fires immediately before a comment is unmarked as Spam.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1913
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1914
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1915
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1916
	 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1917
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1918
	do_action( 'unspam_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
	$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
	if ( empty($status) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1922
		$status = '0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
	if ( wp_set_comment_status($comment_id, $status) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
		delete_comment_meta($comment_id, '_wp_trash_meta_status');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1926
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1927
		 * Fires immediately after a comment is unmarked as Spam.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1928
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1929
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1930
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1931
		 * @param int $comment_id The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1932
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1933
		do_action( 'unspammed_comment', $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1934
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1937
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
 * The status of a comment by ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1944
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
 * @param int $comment_id Comment ID
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1946
 * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
function wp_get_comment_status($comment_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
	$comment = get_comment($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
	if ( !$comment )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1953
	$approved = $comment->comment_approved;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1954
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1955
	if ( $approved == null )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1956
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1957
	elseif ( $approved == '1' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1958
		return 'approved';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1959
	elseif ( $approved == '0' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1960
		return 'unapproved';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1961
	elseif ( $approved == 'spam' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
		return 'spam';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1963
	elseif ( $approved == 'trash' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1964
		return 'trash';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
 * Call hooks for when a comment status transition occurs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1972
 * Calls hooks for comment status transitions. If the new comment status is not the same
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
 * as the previous comment status, then two hooks will be ran, the first is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
 * 'transition_comment_status' with new status, old status, and comment data. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
 * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
 * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
 * comment data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
 * The final action will run whether or not the comment statuses are the same. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
 * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
 * parameter and COMMENTTYPE is comment_type comment data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
 * @param string $new_status New comment status.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
 * @param string $old_status Previous comment status.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
 * @param object $comment Comment data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
function wp_transition_comment_status($new_status, $old_status, $comment) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1990
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1991
	 * Translate raw statuses to human readable formats for the hooks.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1992
	 * This is not a complete list of comment status, it's only the ones
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1993
	 * that need to be renamed
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1994
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
	$comment_statuses = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
		0         => 'unapproved',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
		'hold'    => 'unapproved', // wp_set_comment_status() uses "hold"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
		1         => 'approved',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
		'approve' => 'approved', // wp_set_comment_status() uses "approve"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
	if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
	if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
	// Call the hooks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
	if ( $new_status != $old_status ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2006
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2007
		 * Fires when the comment status is in transition.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2008
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2009
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2010
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2011
		 * @param int|string $new_status The new comment status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2012
		 * @param int|string $old_status The old comment status.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2013
		 * @param object     $comment    The comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2014
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2015
		do_action( 'transition_comment_status', $new_status, $old_status, $comment );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2016
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2017
		 * Fires when the comment status is in transition from one specific status to another.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2018
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2019
		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2020
		 * refer to the old and new comment statuses, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2021
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2022
		 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2023
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2024
		 * @param object $comment Comment object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2025
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2026
		do_action( "comment_{$old_status}_to_{$new_status}", $comment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2028
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2029
	 * Fires when the status of a specific comment type is in transition.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2030
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2031
	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2032
	 * refer to the new comment status, and the type of comment, respectively.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2033
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2034
	 * Typical comment types include an empty string (standard comment), 'pingback',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2035
	 * or 'trackback'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2036
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2037
	 * @since 2.7.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2038
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2039
	 * @param int $comment_ID The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2040
	 * @param obj $comment    Comment object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2041
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2042
	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
 * Get current commenter's name, email, and URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
 * Expects cookies content to already be sanitized. User of this function might
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
 * wish to recheck the returned array for validity.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2051
 * @see sanitize_comment_cookies() Use to sanitize cookies
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2052
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
 * @since 2.0.4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
 * @return array Comment author, email, url respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
function wp_get_current_commenter() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
	// Cookies should already be sanitized.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
	$comment_author = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
		$comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
	$comment_author_email = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
		$comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
	$comment_author_url = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
		$comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2072
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2073
	 * Filter the current commenter's name, email, and URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2074
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2075
	 * @since 3.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2076
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2077
	 * @param string $comment_author       Comment author's name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2078
	 * @param string $comment_author_email Comment author's email.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2079
	 * @param string $comment_author_url   Comment author's URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2080
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2081
	return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
 * Inserts a comment to the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2086
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
 * The available comment data key names are 'comment_author_IP', 'comment_date',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
 * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2091
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2092
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2093
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2094
 * @param array $commentdata Contains information on the comment.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2095
 * @return int|bool The new comment's ID on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2096
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2097
function wp_insert_comment( $commentdata ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2098
	global $wpdb;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2099
	$data = wp_unslash( $commentdata );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2100
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2101
	$comment_author       = ! isset( $data['comment_author'] )       ? '' : $data['comment_author'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2102
	$comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2103
	$comment_author_url   = ! isset( $data['comment_author_url'] )   ? '' : $data['comment_author_url'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2104
	$comment_author_IP    = ! isset( $data['comment_author_IP'] )    ? '' : $data['comment_author_IP'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2105
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2106
	$comment_date     = ! isset( $data['comment_date'] )     ? current_time( 'mysql' )            : $data['comment_date'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2107
	$comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2108
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2109
	$comment_post_ID  = ! isset( $data['comment_post_ID'] )  ? '' : $data['comment_post_ID'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2110
	$comment_content  = ! isset( $data['comment_content'] )  ? '' : $data['comment_content'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2111
	$comment_karma    = ! isset( $data['comment_karma'] )    ? 0  : $data['comment_karma'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2112
	$comment_approved = ! isset( $data['comment_approved'] ) ? 1  : $data['comment_approved'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2113
	$comment_agent    = ! isset( $data['comment_agent'] )    ? '' : $data['comment_agent'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2114
	$comment_type     = ! isset( $data['comment_type'] )     ? '' : $data['comment_type'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2115
	$comment_parent   = ! isset( $data['comment_parent'] )   ? 0  : $data['comment_parent'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2116
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2117
	$user_id  = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2118
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2119
	$compacted = 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' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2120
	if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2121
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2122
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2124
	$id = (int) $wpdb->insert_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2125
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2126
	if ( $comment_approved == 1 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2127
		wp_update_comment_count( $comment_post_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2128
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2129
	$comment = get_comment( $id );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2130
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2131
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2132
	 * Fires immediately after a comment is inserted into the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2133
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2134
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2135
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2136
	 * @param int $id      The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2137
	 * @param obj $comment Comment object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2138
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2139
	do_action( 'wp_insert_comment', $id, $comment );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2141
	wp_cache_set( 'last_changed', microtime(), 'comment' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2143
	return $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2144
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2146
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2147
 * Filters and sanitizes comment data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2148
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2149
 * Sets the comment data 'filtered' field to true when finished. This can be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2150
 * checked as to whether the comment should be filtered and to keep from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2151
 * filtering the same comment more than once.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2154
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2155
 * @param array $commentdata Contains information on the comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2156
 * @return array Parsed comment information.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2157
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
function wp_filter_comment($commentdata) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2159
	if ( isset( $commentdata['user_ID'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2160
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2161
		 * Filter the comment author's user id before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2162
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2163
		 * The first time this filter is evaluated, 'user_ID' is checked
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2164
		 * (for back-compat), followed by the standard 'user_id' value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2165
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2166
		 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2167
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2168
		 * @param int $user_ID The comment author's user ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2169
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2170
		$commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2171
	} elseif ( isset( $commentdata['user_id'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2172
		/** This filter is documented in wp-includes/comment.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2173
		$commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2174
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2175
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2176
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2177
	 * Filter the comment author's browser user agent before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2178
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2179
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2180
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2181
	 * @param int $comment_agent The comment author's browser user agent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2182
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2183
	$commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2184
	/** This filter is documented in wp-includes/comment.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2185
	$commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2186
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2187
	 * Filter the comment content before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2188
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2189
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2190
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2191
	 * @param int $comment_content The comment content.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2192
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2193
	$commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2194
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2195
	 * Filter the comment author's IP before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2196
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2197
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2198
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2199
	 * @param int $comment_author_ip The comment author's IP.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2200
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2201
	$commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2202
	/** This filter is documented in wp-includes/comment.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2203
	$commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2204
	/** This filter is documented in wp-includes/comment.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2205
	$commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2206
	$commentdata['filtered'] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2207
	return $commentdata;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2208
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2210
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2211
 * Whether a comment should be blocked because of comment flood.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2212
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2213
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2214
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2215
 * @param bool $block Whether plugin has already blocked comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2216
 * @param int $time_lastcomment Timestamp for last comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2217
 * @param int $time_newcomment Timestamp for new comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2218
 * @return bool Whether comment should be blocked.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2219
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2220
function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2221
	if ( $block ) // a plugin has already blocked... we'll let that decision stand
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2222
		return $block;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2223
	if ( ($time_newcomment - $time_lastcomment) < 15 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2224
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2225
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2226
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2228
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2229
 * Adds a new comment to the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2230
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2231
 * Filters new comment to ensure that the fields are sanitized and valid before
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2232
 * inserting comment into database. Calls 'comment_post' action with comment ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2233
 * and whether comment is approved by WordPress. Also has 'preprocess_comment'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2234
 * filter for processing the comment data before the function handles it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2235
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2236
 * We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2237
 * that it is properly set, such as in wp-config.php, for your environment.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2238
 * See {@link https://core.trac.wordpress.org/ticket/9235}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2239
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2240
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2241
 * @param array $commentdata Contains information on the comment.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2242
 * @return int|bool The ID of the comment on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2243
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2244
function wp_new_comment( $commentdata ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2245
	global $wpdb;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2246
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2247
	if ( isset( $commentdata['user_ID'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2248
		$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2249
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2250
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2251
	$prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2252
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2253
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2254
	 * Filter a comment's data before it is sanitized and inserted into the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2255
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2256
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2257
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2258
	 * @param array $commentdata Comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2259
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2260
	$commentdata = apply_filters( 'preprocess_comment', $commentdata );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2261
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2262
	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2263
	if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2264
		$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2265
	} elseif ( isset( $commentdata['user_id'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2266
		$commentdata['user_id'] = (int) $commentdata['user_id'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2267
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2268
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2269
	$commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2270
	$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2271
	$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2272
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2273
	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2274
	$commentdata['comment_agent']     = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2275
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2276
	if ( empty( $commentdata['comment_date'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2277
		$commentdata['comment_date'] = current_time('mysql');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2278
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2279
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2280
	if ( empty( $commentdata['comment_date_gmt'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2281
		$commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2282
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2283
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2284
	$commentdata = wp_filter_comment($commentdata);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2286
	$commentdata['comment_approved'] = wp_allow_comment($commentdata);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2288
	$comment_ID = wp_insert_comment($commentdata);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2289
	if ( ! $comment_ID ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2290
		$fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2291
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2292
		foreach( $fields as $field ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2293
			if ( isset( $commentdata[ $field ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2294
				$commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2295
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2296
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2297
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2298
		$commentdata = wp_filter_comment( $commentdata );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2299
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2300
		$commentdata['comment_approved'] = wp_allow_comment( $commentdata );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2301
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2302
		$comment_ID = wp_insert_comment( $commentdata );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2303
		if ( ! $comment_ID ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2304
			return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2305
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2306
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2307
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2308
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2309
	 * Fires immediately after a comment is inserted into the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2310
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2311
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2312
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2313
	 * @param int $comment_ID       The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2314
	 * @param int $comment_approved 1 (true) if the comment is approved, 0 (false) if not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2315
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2316
	do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2318
	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2319
		if ( '0' == $commentdata['comment_approved'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2320
			wp_notify_moderator( $comment_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2321
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2322
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2323
		// wp_notify_postauthor() checks if notifying the author of their own comment.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2324
		// By default, it won't, but filters can override this.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2325
		if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2326
			wp_notify_postauthor( $comment_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2327
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2330
	return $comment_ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2331
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2332
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2333
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2334
 * Sets the status of a comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2335
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2336
 * The 'wp_set_comment_status' action is called after the comment is handled.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2337
 * If the comment status is not in the list, then false is returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2338
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2339
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2340
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2341
 * @param int $comment_id Comment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2342
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2343
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2344
 * @return bool|WP_Error True on success, false or WP_Error on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2345
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2346
function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2347
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2349
	switch ( $comment_status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2350
		case 'hold':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2351
		case '0':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2352
			$status = '0';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2353
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2354
		case 'approve':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2355
		case '1':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2356
			$status = '1';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2357
			if ( get_option('comments_notify') ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2358
				wp_notify_postauthor( $comment_id );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2359
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2360
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2361
		case 'spam':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2362
			$status = 'spam';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2363
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2364
		case 'trash':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2365
			$status = 'trash';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2366
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2367
		default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2368
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2369
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2371
	$comment_old = clone get_comment($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2373
	if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2374
		if ( $wp_error )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2375
			return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2376
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2377
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2378
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2379
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2380
	clean_comment_cache($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2381
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2382
	$comment = get_comment($comment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2383
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2384
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2385
	 * Fires immediately before transitioning a comment's status from one to another
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2386
	 * in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2387
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2388
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2389
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2390
	 * @param int         $comment_id     Comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2391
	 * @param string|bool $comment_status Current comment status. Possible values include
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2392
	 *                                    'hold', 'approve', 'spam', 'trash', or false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2393
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2394
	do_action( 'wp_set_comment_status', $comment_id, $comment_status );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2395
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2396
	wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2397
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2398
	wp_update_comment_count($comment->comment_post_ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2399
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2400
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2401
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2402
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2403
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2404
 * Updates an existing comment in the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2405
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2406
 * Filters the comment and makes sure certain fields are valid before updating.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2407
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2408
 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2409
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2410
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2411
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2412
 * @param array $commentarr Contains information on the comment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2413
 * @return int Comment was updated if value is 1, or was not updated if value is 0.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2414
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2415
function wp_update_comment($commentarr) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2416
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2417
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2418
	// First, get all of the original fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2419
	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2420
	if ( empty( $comment ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2421
		return 0;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2422
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2423
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2424
	// Make sure that the comment post ID is valid (if specified).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2425
	if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2426
		return 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2427
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2428
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2429
	// Escape data pulled from DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2430
	$comment = wp_slash($comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2431
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2432
	$old_status = $comment['comment_approved'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2433
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2434
	// Merge old and new fields with new fields overwriting old ones.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2435
	$commentarr = array_merge($comment, $commentarr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2436
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2437
	$commentarr = wp_filter_comment( $commentarr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2438
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2439
	// Now extract the merged array.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2440
	$data = wp_unslash( $commentarr );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2441
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2442
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2443
	 * Filter the comment content before it is updated in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2444
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2445
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2446
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2447
	 * @param string $comment_content The comment data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2448
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2449
	$data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2450
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2451
	$data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2452
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2453
	if ( ! isset( $data['comment_approved'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2454
		$data['comment_approved'] = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2455
	} elseif ( 'hold' == $data['comment_approved'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2456
		$data['comment_approved'] = 0;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2457
	} elseif ( 'approve' == $data['comment_approved'] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2458
		$data['comment_approved'] = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2459
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2460
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2461
	$comment_ID = $data['comment_ID'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2462
	$comment_post_ID = $data['comment_post_ID'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2463
	$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' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2464
	$data = wp_array_slice_assoc( $data, $keys );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2465
	$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2466
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2467
	clean_comment_cache( $comment_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2468
	wp_update_comment_count( $comment_post_ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2469
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2470
	 * Fires immediately after a comment is updated in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2471
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2472
	 * The hook also fires immediately before comment status transition hooks are fired.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2473
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2474
	 * @since 1.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2475
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2476
	 * @param int $comment_ID The comment ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2477
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2478
	do_action( 'edit_comment', $comment_ID );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2479
	$comment = get_comment($comment_ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2480
	wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2481
	return $rval;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2482
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2483
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2484
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2485
 * Whether to defer comment counting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2486
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2487
 * When setting $defer to true, all post comment counts will not be updated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2488
 * until $defer is set to false. When $defer is set to false, then all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2489
 * previously deferred updated post comment counts will then be automatically
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2490
 * updated without having to call wp_update_comment_count() after.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2491
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2492
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2493
 * @staticvar bool $_defer
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2494
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2495
 * @param bool $defer
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2496
 * @return bool
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2497
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2498
function wp_defer_comment_counting($defer=null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2499
	static $_defer = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2501
	if ( is_bool($defer) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2502
		$_defer = $defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2503
		// flush any deferred counts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2504
		if ( !$defer )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2505
			wp_update_comment_count( null, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2506
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2507
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2508
	return $_defer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2509
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2510
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2511
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2512
 * Updates the comment count for post(s).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2513
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2514
 * When $do_deferred is false (is by default) and the comments have been set to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2515
 * be deferred, the post_id will be added to a queue, which will be updated at a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2516
 * later date and only updated once per post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2517
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2518
 * If the comments have not be set up to be deferred, then the post will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2519
 * updated. When $do_deferred is set to true, then all previous deferred post
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2520
 * IDs will be updated along with the current $post_id.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2521
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2522
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2523
 * @see wp_update_comment_count_now() For what could cause a false return value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2524
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2525
 * @param int $post_id Post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2526
 * @param bool $do_deferred Whether to process previously deferred post comment counts
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2527
 * @return bool|null True on success, false on failure
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2528
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2529
function wp_update_comment_count($post_id, $do_deferred=false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2530
	static $_deferred = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2531
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2532
	if ( $do_deferred ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2533
		$_deferred = array_unique($_deferred);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2534
		foreach ( $_deferred as $i => $_post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2535
			wp_update_comment_count_now($_post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2536
			unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2537
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2538
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2540
	if ( wp_defer_comment_counting() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2541
		$_deferred[] = $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2542
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2543
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2544
	elseif ( $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2545
		return wp_update_comment_count_now($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2546
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2547
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2548
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2549
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2550
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2551
 * Updates the comment count for the post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2552
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2553
 * @since 2.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2554
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2555
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2556
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2557
 * @param int $post_id Post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2558
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2559
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2560
function wp_update_comment_count_now($post_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2561
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2562
	$post_id = (int) $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2563
	if ( !$post_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2564
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2565
	if ( !$post = get_post($post_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2566
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2568
	$old = (int) $post->comment_count;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2569
	$new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2570
	$wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2571
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2572
	clean_post_cache( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2573
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2574
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2575
	 * Fires immediately after a post's comment count is updated in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2576
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2577
	 * @since 2.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2578
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2579
	 * @param int $post_id Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2580
	 * @param int $new     The new comment count.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2581
	 * @param int $old     The old comment count.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2582
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2583
	do_action( 'wp_update_comment_count', $post_id, $new, $old );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2584
	/** This action is documented in wp-includes/post.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2585
	do_action( 'edit_post', $post_id, $post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2586
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2587
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2588
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2590
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2591
// Ping and trackback functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2592
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2594
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2595
 * Finds a pingback server URI based on the given URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2596
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2597
 * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2598
 * a check for the x-pingback headers first and returns that, if available. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2599
 * check for the rel="pingback" has more overhead than just the header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2600
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2601
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2602
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2603
 * @param string $url URL to ping.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2604
 * @param int $deprecated Not Used.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2605
 * @return false|string False on failure, string containing URI on success.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2606
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2607
function discover_pingback_server_uri( $url, $deprecated = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2608
	if ( !empty( $deprecated ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2609
		_deprecated_argument( __FUNCTION__, '2.7' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2611
	$pingback_str_dquote = 'rel="pingback"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2612
	$pingback_str_squote = 'rel=\'pingback\'';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2614
	/** @todo Should use Filter Extension or custom preg_match instead. */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2615
	$parsed_url = parse_url($url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2616
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2617
	if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2618
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2619
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2620
	//Do not search for a pingback server on our own uploads
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2621
	$uploads_dir = wp_upload_dir();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2622
	if ( 0 === strpos($url, $uploads_dir['baseurl']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2623
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2625
	$response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2626
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2627
	if ( is_wp_error( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2628
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2629
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2630
	if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2631
		return wp_remote_retrieve_header( $response, 'x-pingback' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2633
	// Not an (x)html, sgml, or xml page, no use going further.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2634
	if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2635
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2637
	// Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2638
	$response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2639
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2640
	if ( is_wp_error( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2641
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2642
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2643
	$contents = wp_remote_retrieve_body( $response );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2644
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2645
	$pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2646
	$pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2647
	if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2648
		$quote = ($pingback_link_offset_dquote) ? '"' : '\'';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2649
		$pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2650
		$pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2651
		$pingback_href_start = $pingback_href_pos+6;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2652
		$pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2653
		$pingback_server_url_len = $pingback_href_end - $pingback_href_start;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2654
		$pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2656
		// We may find rel="pingback" but an incomplete pingback URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2657
		if ( $pingback_server_url_len > 0 ) { // We got it!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2658
			return $pingback_server_url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2659
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2660
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2661
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2662
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2663
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2664
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2665
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2666
 * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2667
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
 * @since 2.1.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2669
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2670
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2671
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2672
function do_all_pings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2673
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2674
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2675
	// Do pingbacks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2676
	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")) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2677
		delete_metadata_by_mid( 'post', $ping->meta_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2678
		pingback( $ping->post_content, $ping->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2679
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2681
	// Do Enclosures
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2682
	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")) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2683
		delete_metadata_by_mid( 'post', $enclosure->meta_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2684
		do_enclose( $enclosure->post_content, $enclosure->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2685
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2686
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2687
	// Do Trackbacks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2688
	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2689
	if ( is_array($trackbacks) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2690
		foreach ( $trackbacks as $trackback )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2691
			do_trackbacks($trackback);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2692
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2693
	//Do Update Services/Generic Pings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2694
	generic_ping();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2695
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2697
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2698
 * Perform trackbacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2699
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2700
 * @since 1.5.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2701
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2702
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2703
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2704
 * @param int $post_id Post ID to do trackbacks on.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2705
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2706
function do_trackbacks($post_id) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2707
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2709
	$post = get_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2710
	$to_ping = get_to_ping($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2711
	$pinged  = get_pung($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2712
	if ( empty($to_ping) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2713
		$wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2714
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2715
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2716
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2717
	if ( empty($post->post_excerpt) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2718
		/** This filter is documented in wp-includes/post-template.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2719
		$excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2720
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2721
		/** This filter is documented in wp-includes/post-template.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2722
		$excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2723
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2724
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2725
	$excerpt = str_replace(']]>', ']]&gt;', $excerpt);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2726
	$excerpt = wp_html_excerpt($excerpt, 252, '&#8230;');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2727
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2728
	/** This filter is documented in wp-includes/post-template.php */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2729
	$post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2730
	$post_title = strip_tags($post_title);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2731
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2732
	if ( $to_ping ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2733
		foreach ( (array) $to_ping as $tb_ping ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2734
			$tb_ping = trim($tb_ping);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2735
			if ( !in_array($tb_ping, $pinged) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2736
				trackback($tb_ping, $post_title, $excerpt, $post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2737
				$pinged[] = $tb_ping;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2738
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2739
				$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2740
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2741
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2742
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2743
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2744
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2745
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2746
 * Sends pings to all of the ping site services.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2747
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2748
 * @since 1.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2749
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2750
 * @param int $post_id Post ID.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2751
 * @return int Same as Post ID from parameter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2752
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2753
function generic_ping( $post_id = 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2754
	$services = get_option('ping_sites');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2755
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2756
	$services = explode("\n", $services);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2757
	foreach ( (array) $services as $service ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2758
		$service = trim($service);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2759
		if ( '' != $service )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2760
			weblog_ping($service);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2761
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2762
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2763
	return $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2764
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2765
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2766
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2767
 * Pings back the links found in a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2768
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2769
 * @since 0.71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2770
 * @uses $wp_version
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2771
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2772
 * @param string $content Post content to check for links.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2773
 * @param int $post_ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2774
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2775
function pingback($content, $post_ID) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2776
	global $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2777
	include_once(ABSPATH . WPINC . '/class-IXR.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2778
	include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2780
	// original code by Mort (http://mort.mine.nu:8080)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2781
	$post_links = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2782
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2783
	$pung = get_pung($post_ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2785
	// Step 1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2786
	// Parsing the post, external links (if any) are stored in the $post_links array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2787
	$post_links_temp = wp_extract_urls( $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2788
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2789
	// Step 2.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2790
	// Walking thru the links array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2791
	// first we get rid of links pointing to sites, not to specific files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2792
	// Example:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2793
	// http://dummy-weblog.org
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2794
	// http://dummy-weblog.org/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2795
	// http://dummy-weblog.org/post.php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2796
	// We don't wanna ping first and second types, even if they have a valid <link/>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2798
	foreach ( (array) $post_links_temp as $link_test ) :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2799
		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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2800
				&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2801
			if ( $test = @parse_url($link_test) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2802
				if ( isset($test['query']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2803
					$post_links[] = $link_test;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2804
				elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2805
					$post_links[] = $link_test;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2806
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2807
		endif;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2808
	endforeach;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2810
	$post_links = array_unique( $post_links );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2811
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2812
	 * Fires just before pinging back links found in a post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2813
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2814
	 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2815
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2816
	 * @param array &$post_links An array of post links to be checked, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2817
	 * @param array &$pung       Whether a link has already been pinged, passed by reference.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2818
	 * @param int   $post_ID     The post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2819
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2820
	do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2821
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2822
	foreach ( (array) $post_links as $pagelinkedto ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2823
		$pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2824
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2825
		if ( $pingback_server_url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2826
			@ set_time_limit( 60 );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2827
			// Now, the RPC call
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2828
			$pagelinkedfrom = get_permalink($post_ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2829
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2830
			// using a timeout of 3 seconds should be enough to cover slow servers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2831
			$client = new WP_HTTP_IXR_Client($pingback_server_url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2832
			$client->timeout = 3;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2833
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2834
			 * Filter the user agent sent when pinging-back a URL.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2835
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2836
			 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2837
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2838
			 * @param string $concat_useragent    The user agent concatenated with ' -- WordPress/'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2839
			 *                                    and the WordPress version.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2840
			 * @param string $useragent           The useragent.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2841
			 * @param string $pingback_server_url The server URL being linked to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2842
			 * @param string $pagelinkedto        URL of page linked to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2843
			 * @param string $pagelinkedfrom      URL of page linked from.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2844
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2845
			$client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2846
			// when set to true, this outputs debug messages by itself
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2847
			$client->debug = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2848
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2849
			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2850
				add_ping( $post_ID, $pagelinkedto );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2851
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2852
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2853
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2854
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2855
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2856
 * Check whether blog is public before returning sites.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2857
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2858
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2859
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2860
 * @param mixed $sites Will return if blog is public, will not return if not public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2861
 * @return mixed Empty string if blog is not public, returns $sites, if site is public.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2862
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2863
function privacy_ping_filter($sites) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2864
	if ( '0' != get_option('blog_public') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2865
		return $sites;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2866
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2867
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2868
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2870
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2871
 * Send a Trackback.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2872
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2873
 * Updates database when sending trackback to prevent duplicates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2874
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2875
 * @since 0.71
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2876
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  2877
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2878
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2879
 * @param string $trackback_url URL to send trackbacks.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2880
 * @param string $title Title of post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2881
 * @param string $excerpt Excerpt of post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2882
 * @param int $ID Post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2883
 * @return mixed Database query from update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2884
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2885
function trackback($trackback_url, $title, $excerpt, $ID) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2886
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2888
	if ( empty($trackback_url) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2889
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2890
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2891
	$options = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2892
	$options['timeout'] = 4;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2893
	$options['body'] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2894
		'title' => $title,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2895
		'url' => get_permalink($ID),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2896
		'blog_name' => get_option('blogname'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2897
		'excerpt' => $excerpt
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2898
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2899
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2900
	$response = wp_safe_remote_post( $trackback_url, $options );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2901
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2902
	if ( is_wp_error( $response ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2903
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2904
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2905
	$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2906
	return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2907
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2909
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2910
 * Send a pingback.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2911
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2912
 * @since 1.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2913
 * @uses $wp_version
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2914
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2915
 * @param string $server Host of blog to connect to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2916
 * @param string $path Path to send the ping.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2917
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2918
function weblog_ping($server = '', $path = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2919
	global $wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2920
	include_once(ABSPATH . WPINC . '/class-IXR.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2921
	include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2922
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2923
	// using a timeout of 3 seconds should be enough to cover slow servers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2924
	$client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2925
	$client->timeout = 3;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2926
	$client->useragent .= ' -- WordPress/'.$wp_version;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2927
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2928
	// when set to true, this outputs debug messages by itself
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2929
	$client->debug = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2930
	$home = trailingslashit( home_url() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2931
	if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2932
		$client->query('weblogUpdates.ping', get_option('blogname'), $home);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2933
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2934
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2935
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2936
 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2937
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2938
 * @since 3.5.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2939
 * @see wp_http_validate_url()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2940
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2941
 * @param string $source_uri
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2942
 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2943
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2944
function pingback_ping_source_uri( $source_uri ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2945
	return (string) wp_http_validate_url( $source_uri );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2946
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2947
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2948
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2949
 * Default filter attached to xmlrpc_pingback_error.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2950
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2951
 * Returns a generic pingback error code unless the error code is 48,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2952
 * which reports that the pingback is already registered.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2953
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2954
 * @since 3.5.1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2955
 * @link http://www.hixie.ch/specs/pingback/pingback#TOC3
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2956
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2957
 * @param IXR_Error $ixr_error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2958
 * @return IXR_Error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2959
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2960
function xmlrpc_pingback_error( $ixr_error ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2961
	if ( $ixr_error->code === 48 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2962
		return $ixr_error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2963
	return new IXR_Error( 0, '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2964
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2966
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2967
// Cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2968
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2969
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2970
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2971
 * Removes comment ID from the comment cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2972
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2973
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2974
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2975
 * @param int|array $ids Comment ID or array of comment IDs to remove from cache
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2976
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2977
function clean_comment_cache($ids) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2978
	foreach ( (array) $ids as $id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2979
		wp_cache_delete($id, 'comment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2980
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2981
	wp_cache_set( 'last_changed', microtime(), 'comment' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2982
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2983
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2984
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2985
 * Updates the comment cache of given comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2986
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2987
 * Will add the comments in $comments to the cache. If comment ID already exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2988
 * in the comment cache then it will not be updated. The comment is added to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2989
 * cache using the comment group with the key using the ID of the comments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2990
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2991
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2992
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2993
 * @param array $comments Array of comment row objects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2994
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2995
function update_comment_cache($comments) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2996
	foreach ( (array) $comments as $comment )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2997
		wp_cache_add($comment->comment_ID, $comment, 'comment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2998
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2999
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3000
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3001
// Internal
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3002
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3003
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3004
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3005
 * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3006
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3007
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3008
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3009
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3010
 * @param object $posts Post data object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3011
 * @param object $query Query object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3012
 * @return object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3013
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3014
function _close_comments_for_old_posts( $posts, $query ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3015
	if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3016
		return $posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3017
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3018
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3019
	 * Filter the list of post types to automatically close comments for.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3020
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3021
	 * @since 3.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3022
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3023
	 * @param array $post_types An array of registered post types. Default array with 'post'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3024
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3025
	$post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3026
	if ( ! in_array( $posts[0]->post_type, $post_types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3027
		return $posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3029
	$days_old = (int) get_option( 'close_comments_days_old' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3030
	if ( ! $days_old )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3031
		return $posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3032
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3033
	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3034
		$posts[0]->comment_status = 'closed';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3035
		$posts[0]->ping_status = 'closed';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3036
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3037
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3038
	return $posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3039
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3040
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3041
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3042
 * Close comments on an old post. Hooked to comments_open and pings_open.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3043
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3044
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3045
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3046
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3047
 * @param bool $open Comments open or closed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3048
 * @param int $post_id Post ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3049
 * @return bool $open
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3050
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3051
function _close_comments_for_old_post( $open, $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3052
	if ( ! $open )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3053
		return $open;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3054
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3055
	if ( !get_option('close_comments_for_old_posts') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3056
		return $open;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3057
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3058
	$days_old = (int) get_option('close_comments_days_old');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3059
	if ( !$days_old )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3060
		return $open;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3061
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3062
	$post = get_post($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3063
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  3064
	/** This filter is documented in wp-includes/comment.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3065
	$post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3066
	if ( ! in_array( $post->post_type, $post_types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3067
		return $open;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3068
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3069
	if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3070
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3071
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3072
	return $open;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  3073
}