]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/comment.php
WordPress 3.4.1-scripts
[autoinstalls/wordpress.git] / wp-includes / comment.php
1 <?php
2 /**
3  * Manages WordPress comments
4  *
5  * @package WordPress
6  * @subpackage Comment
7  */
8
9 /**
10  * Checks whether a comment passes internal checks to be allowed to add.
11  *
12  * If comment moderation is set in the administration, then all comments,
13  * regardless of their type and whitelist will be set to false. If the number of
14  * links exceeds the amount in the administration, then the check fails. If any
15  * of the parameter contents match the blacklist of words, then the check fails.
16  *
17  * If the number of links exceeds the amount in the administration, then the
18  * check fails. If any of the parameter contents match the blacklist of words,
19  * then the check fails.
20  *
21  * If the comment author was approved before, then the comment is
22  * automatically whitelisted.
23  *
24  * If none of the checks fail, then the failback is to set the check to pass
25  * (return true).
26  *
27  * @since 1.2.0
28  * @uses $wpdb
29  *
30  * @param string $author Comment Author's name
31  * @param string $email Comment Author's email
32  * @param string $url Comment Author's URL
33  * @param string $comment Comment contents
34  * @param string $user_ip Comment Author's IP address
35  * @param string $user_agent Comment Author's User Agent
36  * @param string $comment_type Comment type, either user submitted comment,
37  *              trackback, or pingback
38  * @return bool Whether the checks passed (true) and the comments should be
39  *              displayed or set to moderated
40  */
41 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
42         global $wpdb;
43
44         if ( 1 == get_option('comment_moderation') )
45                 return false; // If moderation is set to manual
46
47         $comment = apply_filters( 'comment_text', $comment );
48
49         // Check # of external links
50         if ( $max_links = get_option( 'comment_max_links' ) ) {
51                 $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
52                 $num_links = apply_filters( 'comment_max_links_url', $num_links, $url ); // provide for counting of $url as a link
53                 if ( $num_links >= $max_links )
54                         return false;
55         }
56
57         $mod_keys = trim(get_option('moderation_keys'));
58         if ( !empty($mod_keys) ) {
59                 $words = explode("\n", $mod_keys );
60
61                 foreach ( (array) $words as $word) {
62                         $word = trim($word);
63
64                         // Skip empty lines
65                         if ( empty($word) )
66                                 continue;
67
68                         // Do some escaping magic so that '#' chars in the
69                         // spam words don't break things:
70                         $word = preg_quote($word, '#');
71
72                         $pattern = "#$word#i";
73                         if ( preg_match($pattern, $author) ) return false;
74                         if ( preg_match($pattern, $email) ) return false;
75                         if ( preg_match($pattern, $url) ) return false;
76                         if ( preg_match($pattern, $comment) ) return false;
77                         if ( preg_match($pattern, $user_ip) ) return false;
78                         if ( preg_match($pattern, $user_agent) ) return false;
79                 }
80         }
81
82         // Comment whitelisting:
83         if ( 1 == get_option('comment_whitelist')) {
84                 if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
85                         // expected_slashed ($author, $email)
86                         $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");
87                         if ( ( 1 == $ok_to_comment ) &&
88                                 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
89                                         return true;
90                         else
91                                 return false;
92                 } else {
93                         return false;
94                 }
95         }
96         return true;
97 }
98
99 /**
100  * Retrieve the approved comments for post $post_id.
101  *
102  * @since 2.0.0
103  * @uses $wpdb
104  *
105  * @param int $post_id The ID of the post
106  * @return array $comments The approved comments
107  */
108 function get_approved_comments($post_id) {
109         global $wpdb;
110         return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
111 }
112
113 /**
114  * Retrieves comment data given a comment ID or comment object.
115  *
116  * If an object is passed then the comment data will be cached and then returned
117  * after being passed through a filter. If the comment is empty, then the global
118  * comment variable will be used, if it is set.
119  *
120  * If the comment is empty, then the global comment variable will be used, if it
121  * is set.
122  *
123  * @since 2.0.0
124  * @uses $wpdb
125  *
126  * @param object|string|int $comment Comment to retrieve.
127  * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
128  * @return object|array|null Depends on $output value.
129  */
130 function &get_comment(&$comment, $output = OBJECT) {
131         global $wpdb;
132         $null = null;
133
134         if ( empty($comment) ) {
135                 if ( isset($GLOBALS['comment']) )
136                         $_comment = & $GLOBALS['comment'];
137                 else
138                         $_comment = null;
139         } elseif ( is_object($comment) ) {
140                 wp_cache_add($comment->comment_ID, $comment, 'comment');
141                 $_comment = $comment;
142         } else {
143                 if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
144                         $_comment = & $GLOBALS['comment'];
145                 } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
146                         $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
147                         if ( ! $_comment )
148                                 return $null;
149                         wp_cache_add($_comment->comment_ID, $_comment, 'comment');
150                 }
151         }
152
153         $_comment = apply_filters('get_comment', $_comment);
154
155         if ( $output == OBJECT ) {
156                 return $_comment;
157         } elseif ( $output == ARRAY_A ) {
158                 $__comment = get_object_vars($_comment);
159                 return $__comment;
160         } elseif ( $output == ARRAY_N ) {
161                 $__comment = array_values(get_object_vars($_comment));
162                 return $__comment;
163         } else {
164                 return $_comment;
165         }
166 }
167
168 /**
169  * Retrieve a list of comments.
170  *
171  * The comment list can be for the blog as a whole or for an individual post.
172  *
173  * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
174  * 'order', 'number', 'offset', and 'post_id'.
175  *
176  * @since 2.7.0
177  * @uses $wpdb
178  *
179  * @param mixed $args Optional. Array or string of options to override defaults.
180  * @return array List of comments.
181  */
182 function get_comments( $args = '' ) {
183         $query = new WP_Comment_Query;
184         return $query->query( $args );
185 }
186
187 /**
188  * WordPress Comment Query class.
189  *
190  * @since 3.1.0
191  */
192 class WP_Comment_Query {
193
194         /**
195          * Execute the query
196          *
197          * @since 3.1.0
198          *
199          * @param string|array $query_vars
200          * @return int|array
201          */
202         function query( $query_vars ) {
203                 global $wpdb;
204
205                 $defaults = array(
206                         'author_email' => '',
207                         'ID' => '',
208                         'karma' => '',
209                         'number' => '',
210                         'offset' => '',
211                         'orderby' => '',
212                         'order' => 'DESC',
213                         'parent' => '',
214                         'post_ID' => '',
215                         'post_id' => 0,
216                         'post_author' => '',
217                         'post_name' => '',
218                         'post_parent' => '',
219                         'post_status' => '',
220                         'post_type' => '',
221                         'status' => '',
222                         'type' => '',
223                         'user_id' => '',
224                         'search' => '',
225                         'count' => false
226                 );
227
228                 $this->query_vars = wp_parse_args( $query_vars, $defaults );
229                 do_action_ref_array( 'pre_get_comments', array( &$this ) );
230                 extract( $this->query_vars, EXTR_SKIP );
231
232                 // $args can be whatever, only use the args defined in defaults to compute the key
233                 $key = md5( serialize( compact(array_keys($defaults)) )  );
234                 $last_changed = wp_cache_get('last_changed', 'comment');
235                 if ( !$last_changed ) {
236                         $last_changed = time();
237                         wp_cache_set('last_changed', $last_changed, 'comment');
238                 }
239                 $cache_key = "get_comments:$key:$last_changed";
240
241                 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
242                         return $cache;
243                 }
244
245                 $post_id = absint($post_id);
246
247                 if ( 'hold' == $status )
248                         $approved = "comment_approved = '0'";
249                 elseif ( 'approve' == $status )
250                         $approved = "comment_approved = '1'";
251                 elseif ( 'spam' == $status )
252                         $approved = "comment_approved = 'spam'";
253                 elseif ( 'trash' == $status )
254                         $approved = "comment_approved = 'trash'";
255                 else
256                         $approved = "( comment_approved = '0' OR comment_approved = '1' )";
257
258                 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
259
260                 if ( ! empty( $orderby ) ) {
261                         $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
262                         $ordersby = array_intersect(
263                                 $ordersby,
264                                 array(
265                                         'comment_agent',
266                                         'comment_approved',
267                                         'comment_author',
268                                         'comment_author_email',
269                                         'comment_author_IP',
270                                         'comment_author_url',
271                                         'comment_content',
272                                         'comment_date',
273                                         'comment_date_gmt',
274                                         'comment_ID',
275                                         'comment_karma',
276                                         'comment_parent',
277                                         'comment_post_ID',
278                                         'comment_type',
279                                         'user_id',
280                                 )
281                         );
282                         $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
283                 } else {
284                         $orderby = 'comment_date_gmt';
285                 }
286
287                 $number = absint($number);
288                 $offset = absint($offset);
289
290                 if ( !empty($number) ) {
291                         if ( $offset )
292                                 $limits = 'LIMIT ' . $offset . ',' . $number;
293                         else
294                                 $limits = 'LIMIT ' . $number;
295                 } else {
296                         $limits = '';
297                 }
298
299                 if ( $count )
300                         $fields = 'COUNT(*)';
301                 else
302                         $fields = '*';
303
304                 $join = '';
305                 $where = $approved;
306
307                 if ( ! empty($post_id) )
308                         $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
309                 if ( '' !== $author_email )
310                         $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
311                 if ( '' !== $karma )
312                         $where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
313                 if ( 'comment' == $type ) {
314                         $where .= " AND comment_type = ''";
315                 } elseif( 'pings' == $type ) {
316                         $where .= ' AND comment_type IN ("pingback", "trackback")';
317                 } elseif ( ! empty( $type ) ) {
318                         $where .= $wpdb->prepare( ' AND comment_type = %s', $type );
319                 }
320                 if ( '' !== $parent )
321                         $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
322                 if ( '' !== $user_id )
323                         $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
324                 if ( '' !== $search )
325                         $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
326
327                 $post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
328                 if ( ! empty( $post_fields ) ) {
329                         $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
330                         foreach( $post_fields as $field_name => $field_value )
331                                 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
332                 }
333
334                 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
335                 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
336                 foreach ( $pieces as $piece )
337                         $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
338
339                 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits";
340
341                 if ( $count )
342                         return $wpdb->get_var( $query );
343
344                 $comments = $wpdb->get_results( $query );
345                 $comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
346
347                 wp_cache_add( $cache_key, $comments, 'comment' );
348
349                 return $comments;
350         }
351
352         /*
353          * Used internally to generate an SQL string for searching across multiple columns
354          *
355          * @access protected
356          * @since 3.1.0
357          *
358          * @param string $string
359          * @param array $cols
360          * @return string
361          */
362         function get_search_sql( $string, $cols ) {
363                 $string = esc_sql( like_escape( $string ) );
364
365                 $searches = array();
366                 foreach ( $cols as $col )
367                         $searches[] = "$col LIKE '%$string%'";
368
369                 return ' AND (' . implode(' OR ', $searches) . ')';
370         }
371 }
372
373 /**
374  * Retrieve all of the WordPress supported comment statuses.
375  *
376  * Comments have a limited set of valid status values, this provides the comment
377  * status values and descriptions.
378  *
379  * @package WordPress
380  * @subpackage Post
381  * @since 2.7.0
382  *
383  * @return array List of comment statuses.
384  */
385 function get_comment_statuses( ) {
386         $status = array(
387                 'hold'          => __('Unapproved'),
388                 /* translators: comment status  */
389                 'approve'       => _x('Approved', 'adjective'),
390                 /* translators: comment status */
391                 'spam'          => _x('Spam', 'adjective'),
392         );
393
394         return $status;
395 }
396
397 /**
398  * The date the last comment was modified.
399  *
400  * @since 1.5.0
401  * @uses $wpdb
402  * @global array $cache_lastcommentmodified
403  *
404  * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
405  *              or 'server' locations.
406  * @return string Last comment modified date.
407  */
408 function get_lastcommentmodified($timezone = 'server') {
409         global $cache_lastcommentmodified, $wpdb;
410
411         if ( isset($cache_lastcommentmodified[$timezone]) )
412                 return $cache_lastcommentmodified[$timezone];
413
414         $add_seconds_server = date('Z');
415
416         switch ( strtolower($timezone)) {
417                 case 'gmt':
418                         $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
419                         break;
420                 case 'blog':
421                         $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
422                         break;
423                 case 'server':
424                         $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));
425                         break;
426         }
427
428         $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
429
430         return $lastcommentmodified;
431 }
432
433 /**
434  * The amount of comments in a post or total comments.
435  *
436  * A lot like {@link wp_count_comments()}, in that they both return comment
437  * stats (albeit with different types). The {@link wp_count_comments()} actual
438  * caches, but this function does not.
439  *
440  * @since 2.0.0
441  * @uses $wpdb
442  *
443  * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
444  * @return array The amount of spam, approved, awaiting moderation, and total comments.
445  */
446 function get_comment_count( $post_id = 0 ) {
447         global $wpdb;
448
449         $post_id = (int) $post_id;
450
451         $where = '';
452         if ( $post_id > 0 ) {
453                 $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
454         }
455
456         $totals = (array) $wpdb->get_results("
457                 SELECT comment_approved, COUNT( * ) AS total
458                 FROM {$wpdb->comments}
459                 {$where}
460                 GROUP BY comment_approved
461         ", ARRAY_A);
462
463         $comment_count = array(
464                 "approved"              => 0,
465                 "awaiting_moderation"   => 0,
466                 "spam"                  => 0,
467                 "total_comments"        => 0
468         );
469
470         foreach ( $totals as $row ) {
471                 switch ( $row['comment_approved'] ) {
472                         case 'spam':
473                                 $comment_count['spam'] = $row['total'];
474                                 $comment_count["total_comments"] += $row['total'];
475                                 break;
476                         case 1:
477                                 $comment_count['approved'] = $row['total'];
478                                 $comment_count['total_comments'] += $row['total'];
479                                 break;
480                         case 0:
481                                 $comment_count['awaiting_moderation'] = $row['total'];
482                                 $comment_count['total_comments'] += $row['total'];
483                                 break;
484                         default:
485                                 break;
486                 }
487         }
488
489         return $comment_count;
490 }
491
492 //
493 // Comment meta functions
494 //
495
496 /**
497  * Add meta data field to a comment.
498  *
499  * @since 2.9.0
500  * @uses add_metadata
501  * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
502  *
503  * @param int $comment_id Comment ID.
504  * @param string $meta_key Metadata name.
505  * @param mixed $meta_value Metadata value.
506  * @param bool $unique Optional, default is false. Whether the same key should not be added.
507  * @return bool False for failure. True for success.
508  */
509 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
510         return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
511 }
512
513 /**
514  * Remove metadata matching criteria from a comment.
515  *
516  * You can match based on the key, or key and value. Removing based on key and
517  * value, will keep from removing duplicate metadata with the same key. It also
518  * allows removing all metadata matching key, if needed.
519  *
520  * @since 2.9.0
521  * @uses delete_metadata
522  * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
523  *
524  * @param int $comment_id comment ID
525  * @param string $meta_key Metadata name.
526  * @param mixed $meta_value Optional. Metadata value.
527  * @return bool False for failure. True for success.
528  */
529 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
530         return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
531 }
532
533 /**
534  * Retrieve comment meta field for a comment.
535  *
536  * @since 2.9.0
537  * @uses get_metadata
538  * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
539  *
540  * @param int $comment_id Comment ID.
541  * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
542  * @param bool $single Whether to return a single value.
543  * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
544  *  is true.
545  */
546 function get_comment_meta($comment_id, $key = '', $single = false) {
547         return get_metadata('comment', $comment_id, $key, $single);
548 }
549
550 /**
551  * Update comment meta field based on comment ID.
552  *
553  * Use the $prev_value parameter to differentiate between meta fields with the
554  * same key and comment ID.
555  *
556  * If the meta field for the comment does not exist, it will be added.
557  *
558  * @since 2.9.0
559  * @uses update_metadata
560  * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
561  *
562  * @param int $comment_id Comment ID.
563  * @param string $meta_key Metadata key.
564  * @param mixed $meta_value Metadata value.
565  * @param mixed $prev_value Optional. Previous value to check before removing.
566  * @return bool False on failure, true if success.
567  */
568 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
569         return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
570 }
571
572 /**
573  * Sets the cookies used to store an unauthenticated commentator's identity. Typically used
574  * to recall previous comments by this commentator that are still held in moderation.
575  *
576  * @param object $comment Comment object.
577  * @param object $user Comment author's object.
578  *
579  * @since 3.4.0
580  */
581 function wp_set_comment_cookies($comment, $user) {
582         if ( $user->exists() )
583                 return;
584
585         $comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000);
586         setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
587         setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
588         setcookie('comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
589 }
590
591 /**
592  * Sanitizes the cookies sent to the user already.
593  *
594  * Will only do anything if the cookies have already been created for the user.
595  * Mostly used after cookies had been sent to use elsewhere.
596  *
597  * @since 2.0.4
598  */
599 function sanitize_comment_cookies() {
600         if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
601                 $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
602                 $comment_author = stripslashes($comment_author);
603                 $comment_author = esc_attr($comment_author);
604                 $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
605         }
606
607         if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
608                 $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
609                 $comment_author_email = stripslashes($comment_author_email);
610                 $comment_author_email = esc_attr($comment_author_email);
611                 $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
612         }
613
614         if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
615                 $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
616                 $comment_author_url = stripslashes($comment_author_url);
617                 $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
618         }
619 }
620
621 /**
622  * Validates whether this comment is allowed to be made.
623  *
624  * @since 2.0.0
625  * @uses $wpdb
626  * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
627  * @uses apply_filters() Calls 'comment_duplicate_trigger' hook on commentdata.
628  * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
629  *
630  * @param array $commentdata Contains information on the comment
631  * @return mixed Signifies the approval status (0|1|'spam')
632  */
633 function wp_allow_comment($commentdata) {
634         global $wpdb;
635         extract($commentdata, EXTR_SKIP);
636
637         // Simple duplicate check
638         // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
639         $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' ";
640         if ( $comment_author_email )
641                 $dupe .= "OR comment_author_email = '$comment_author_email' ";
642         $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
643         if ( $wpdb->get_var($dupe) ) {
644                 do_action( 'comment_duplicate_trigger', $commentdata );
645                 if ( defined('DOING_AJAX') )
646                         die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
647
648                 wp_die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
649         }
650
651         do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
652
653         if ( isset($user_id) && $user_id) {
654                 $userdata = get_userdata($user_id);
655                 $user = new WP_User($user_id);
656                 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
657         }
658
659         if ( isset($userdata) && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) {
660                 // The author and the admins get respect.
661                 $approved = 1;
662          } else {
663                 // Everyone else's comments will be checked.
664                 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
665                         $approved = 1;
666                 else
667                         $approved = 0;
668                 if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
669                         $approved = 'spam';
670         }
671
672         $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
673         return $approved;
674 }
675
676 /**
677  * Check whether comment flooding is occurring.
678  *
679  * Won't run, if current user can manage options, so to not block
680  * administrators.
681  *
682  * @since 2.3.0
683  * @uses $wpdb
684  * @uses apply_filters() Calls 'comment_flood_filter' filter with first
685  *              parameter false, last comment timestamp, new comment timestamp.
686  * @uses do_action() Calls 'comment_flood_trigger' action with parameters with
687  *              last comment timestamp and new comment timestamp.
688  *
689  * @param string $ip Comment IP.
690  * @param string $email Comment author email address.
691  * @param string $date MySQL time string.
692  */
693 function check_comment_flood_db( $ip, $email, $date ) {
694         global $wpdb;
695         if ( current_user_can( 'manage_options' ) )
696                 return; // don't throttle admins
697         $hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
698         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 ) ) ) {
699                 $time_lastcomment = mysql2date('U', $lasttime, false);
700                 $time_newcomment  = mysql2date('U', $date, false);
701                 $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
702                 if ( $flood_die ) {
703                         do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
704
705                         if ( defined('DOING_AJAX') )
706                                 die( __('You are posting comments too quickly. Slow down.') );
707
708                         wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
709                 }
710         }
711 }
712
713 /**
714  * Separates an array of comments into an array keyed by comment_type.
715  *
716  * @since 2.7.0
717  *
718  * @param array $comments Array of comments
719  * @return array Array of comments keyed by comment_type.
720  */
721 function &separate_comments(&$comments) {
722         $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
723         $count = count($comments);
724         for ( $i = 0; $i < $count; $i++ ) {
725                 $type = $comments[$i]->comment_type;
726                 if ( empty($type) )
727                         $type = 'comment';
728                 $comments_by_type[$type][] = &$comments[$i];
729                 if ( 'trackback' == $type || 'pingback' == $type )
730                         $comments_by_type['pings'][] = &$comments[$i];
731         }
732
733         return $comments_by_type;
734 }
735
736 /**
737  * Calculate the total number of comment pages.
738  *
739  * @since 2.7.0
740  * @uses get_query_var() Used to fill in the default for $per_page parameter.
741  * @uses get_option() Used to fill in defaults for parameters.
742  * @uses Walker_Comment
743  *
744  * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
745  * @param int $per_page Optional comments per page.
746  * @param boolean $threaded Optional control over flat or threaded comments.
747  * @return int Number of comment pages.
748  */
749 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
750         global $wp_query;
751
752         if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
753                 return $wp_query->max_num_comment_pages;
754
755         if ( !$comments || !is_array($comments) )
756                 $comments = $wp_query->comments;
757
758         if ( empty($comments) )
759                 return 0;
760
761         if ( !isset($per_page) )
762                 $per_page = (int) get_query_var('comments_per_page');
763         if ( 0 === $per_page )
764                 $per_page = (int) get_option('comments_per_page');
765         if ( 0 === $per_page )
766                 return 1;
767
768         if ( !isset($threaded) )
769                 $threaded = get_option('thread_comments');
770
771         if ( $threaded ) {
772                 $walker = new Walker_Comment;
773                 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
774         } else {
775                 $count = ceil( count( $comments ) / $per_page );
776         }
777
778         return $count;
779 }
780
781 /**
782  * Calculate what page number a comment will appear on for comment paging.
783  *
784  * @since 2.7.0
785  * @uses get_comment() Gets the full comment of the $comment_ID parameter.
786  * @uses get_option() Get various settings to control function and defaults.
787  * @uses get_page_of_comment() Used to loop up to top level comment.
788  *
789  * @param int $comment_ID Comment ID.
790  * @param array $args Optional args.
791  * @return int|null Comment page number or null on error.
792  */
793 function get_page_of_comment( $comment_ID, $args = array() ) {
794         global $wpdb;
795
796         if ( !$comment = get_comment( $comment_ID ) )
797                 return;
798
799         $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
800         $args = wp_parse_args( $args, $defaults );
801
802         if ( '' === $args['per_page'] && get_option('page_comments') )
803                 $args['per_page'] = get_query_var('comments_per_page');
804         if ( empty($args['per_page']) ) {
805                 $args['per_page'] = 0;
806                 $args['page'] = 0;
807         }
808         if ( $args['per_page'] < 1 )
809                 return 1;
810
811         if ( '' === $args['max_depth'] ) {
812                 if ( get_option('thread_comments') )
813                         $args['max_depth'] = get_option('thread_comments_depth');
814                 else
815                         $args['max_depth'] = -1;
816         }
817
818         // Find this comment's top level parent if threading is enabled
819         if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
820                 return get_page_of_comment( $comment->comment_parent, $args );
821
822         $allowedtypes = array(
823                 'comment' => '',
824                 'pingback' => 'pingback',
825                 'trackback' => 'trackback',
826         );
827
828         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
829
830         // Count comments older than this one
831         $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 ) );
832
833         // No older comments? Then it's page #1.
834         if ( 0 == $oldercoms )
835                 return 1;
836
837         // Divide comments older than this one by comments per page to get this comment's page number
838         return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
839 }
840
841 /**
842  * Does comment contain blacklisted characters or words.
843  *
844  * @since 1.5.0
845  * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters.
846  *
847  * @param string $author The author of the comment
848  * @param string $email The email of the comment
849  * @param string $url The url used in the comment
850  * @param string $comment The comment content
851  * @param string $user_ip The comment author IP address
852  * @param string $user_agent The author's browser user agent
853  * @return bool True if comment contains blacklisted content, false if comment does not
854  */
855 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
856         do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
857
858         $mod_keys = trim( get_option('blacklist_keys') );
859         if ( '' == $mod_keys )
860                 return false; // If moderation keys are empty
861         $words = explode("\n", $mod_keys );
862
863         foreach ( (array) $words as $word ) {
864                 $word = trim($word);
865
866                 // Skip empty lines
867                 if ( empty($word) ) { continue; }
868
869                 // Do some escaping magic so that '#' chars in the
870                 // spam words don't break things:
871                 $word = preg_quote($word, '#');
872
873                 $pattern = "#$word#i";
874                 if (
875                            preg_match($pattern, $author)
876                         || preg_match($pattern, $email)
877                         || preg_match($pattern, $url)
878                         || preg_match($pattern, $comment)
879                         || preg_match($pattern, $user_ip)
880                         || preg_match($pattern, $user_agent)
881                  )
882                         return true;
883         }
884         return false;
885 }
886
887 /**
888  * Retrieve total comments for blog or single post.
889  *
890  * The properties of the returned object contain the 'moderated', 'approved',
891  * and spam comments for either the entire blog or single post. Those properties
892  * contain the amount of comments that match the status. The 'total_comments'
893  * property contains the integer of total comments.
894  *
895  * The comment stats are cached and then retrieved, if they already exist in the
896  * cache.
897  *
898  * @since 2.5.0
899  *
900  * @param int $post_id Optional. Post ID.
901  * @return object Comment stats.
902  */
903 function wp_count_comments( $post_id = 0 ) {
904         global $wpdb;
905
906         $post_id = (int) $post_id;
907
908         $stats = apply_filters('wp_count_comments', array(), $post_id);
909         if ( !empty($stats) )
910                 return $stats;
911
912         $count = wp_cache_get("comments-{$post_id}", 'counts');
913
914         if ( false !== $count )
915                 return $count;
916
917         $where = '';
918         if ( $post_id > 0 )
919                 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
920
921         $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
922
923         $total = 0;
924         $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
925         foreach ( (array) $count as $row ) {
926                 // Don't count post-trashed toward totals
927                 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
928                         $total += $row['num_comments'];
929                 if ( isset( $approved[$row['comment_approved']] ) )
930                         $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
931         }
932
933         $stats['total_comments'] = $total;
934         foreach ( $approved as $key ) {
935                 if ( empty($stats[$key]) )
936                         $stats[$key] = 0;
937         }
938
939         $stats = (object) $stats;
940         wp_cache_set("comments-{$post_id}", $stats, 'counts');
941
942         return $stats;
943 }
944
945 /**
946  * Trashes or deletes a comment.
947  *
948  * The comment is moved to trash instead of permanently deleted unless trash is
949  * disabled, item is already in the trash, or $force_delete is true.
950  *
951  * The post comment count will be updated if the comment was approved and has a
952  * post ID available.
953  *
954  * @since 2.0.0
955  * @uses $wpdb
956  * @uses do_action() Calls 'delete_comment' hook on comment ID
957  * @uses do_action() Calls 'deleted_comment' hook on comment ID after deletion, on success
958  * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
959  * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
960  *
961  * @param int $comment_id Comment ID
962  * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
963  * @return bool False if delete comment query failure, true on success.
964  */
965 function wp_delete_comment($comment_id, $force_delete = false) {
966         global $wpdb;
967         if (!$comment = get_comment($comment_id))
968                 return false;
969
970         if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
971                 return wp_trash_comment($comment_id);
972
973         do_action('delete_comment', $comment_id);
974
975         // Move children up a level.
976         $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
977         if ( !empty($children) ) {
978                 $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
979                 clean_comment_cache($children);
980         }
981
982         // Delete metadata
983         $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
984         foreach ( $meta_ids as $mid )
985                 delete_metadata_by_mid( 'comment', $mid );
986
987         if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
988                 return false;
989         do_action('deleted_comment', $comment_id);
990
991         $post_id = $comment->comment_post_ID;
992         if ( $post_id && $comment->comment_approved == 1 )
993                 wp_update_comment_count($post_id);
994
995         clean_comment_cache($comment_id);
996
997         do_action('wp_set_comment_status', $comment_id, 'delete');
998         wp_transition_comment_status('delete', $comment->comment_approved, $comment);
999         return true;
1000 }
1001
1002 /**
1003  * Moves a comment to the Trash
1004  *
1005  * If trash is disabled, comment is permanently deleted.
1006  *
1007  * @since 2.9.0
1008  * @uses do_action() on 'trash_comment' before trashing
1009  * @uses do_action() on 'trashed_comment' after trashing
1010  * @uses wp_delete_comment() if trash is disabled
1011  *
1012  * @param int $comment_id Comment ID.
1013  * @return mixed False on failure
1014  */
1015 function wp_trash_comment($comment_id) {
1016         if ( !EMPTY_TRASH_DAYS )
1017                 return wp_delete_comment($comment_id, true);
1018
1019         if ( !$comment = get_comment($comment_id) )
1020                 return false;
1021
1022         do_action('trash_comment', $comment_id);
1023
1024         if ( wp_set_comment_status($comment_id, 'trash') ) {
1025                 add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1026                 add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1027                 do_action('trashed_comment', $comment_id);
1028                 return true;
1029         }
1030
1031         return false;
1032 }
1033
1034 /**
1035  * Removes a comment from the Trash
1036  *
1037  * @since 2.9.0
1038  * @uses do_action() on 'untrash_comment' before untrashing
1039  * @uses do_action() on 'untrashed_comment' after untrashing
1040  *
1041  * @param int $comment_id Comment ID.
1042  * @return mixed False on failure
1043  */
1044 function wp_untrash_comment($comment_id) {
1045         if ( ! (int)$comment_id )
1046                 return false;
1047
1048         do_action('untrash_comment', $comment_id);
1049
1050         $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1051         if ( empty($status) )
1052                 $status = '0';
1053
1054         if ( wp_set_comment_status($comment_id, $status) ) {
1055                 delete_comment_meta($comment_id, '_wp_trash_meta_time');
1056                 delete_comment_meta($comment_id, '_wp_trash_meta_status');
1057                 do_action('untrashed_comment', $comment_id);
1058                 return true;
1059         }
1060
1061         return false;
1062 }
1063
1064 /**
1065  * Marks a comment as Spam
1066  *
1067  * @since 2.9.0
1068  * @uses do_action() on 'spam_comment' before spamming
1069  * @uses do_action() on 'spammed_comment' after spamming
1070  *
1071  * @param int $comment_id Comment ID.
1072  * @return mixed False on failure
1073  */
1074 function wp_spam_comment($comment_id) {
1075         if ( !$comment = get_comment($comment_id) )
1076                 return false;
1077
1078         do_action('spam_comment', $comment_id);
1079
1080         if ( wp_set_comment_status($comment_id, 'spam') ) {
1081                 add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1082                 do_action('spammed_comment', $comment_id);
1083                 return true;
1084         }
1085
1086         return false;
1087 }
1088
1089 /**
1090  * Removes a comment from the Spam
1091  *
1092  * @since 2.9.0
1093  * @uses do_action() on 'unspam_comment' before unspamming
1094  * @uses do_action() on 'unspammed_comment' after unspamming
1095  *
1096  * @param int $comment_id Comment ID.
1097  * @return mixed False on failure
1098  */
1099 function wp_unspam_comment($comment_id) {
1100         if ( ! (int)$comment_id )
1101                 return false;
1102
1103         do_action('unspam_comment', $comment_id);
1104
1105         $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1106         if ( empty($status) )
1107                 $status = '0';
1108
1109         if ( wp_set_comment_status($comment_id, $status) ) {
1110                 delete_comment_meta($comment_id, '_wp_trash_meta_status');
1111                 do_action('unspammed_comment', $comment_id);
1112                 return true;
1113         }
1114
1115         return false;
1116 }
1117
1118 /**
1119  * The status of a comment by ID.
1120  *
1121  * @since 1.0.0
1122  *
1123  * @param int $comment_id Comment ID
1124  * @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
1125  */
1126 function wp_get_comment_status($comment_id) {
1127         $comment = get_comment($comment_id);
1128         if ( !$comment )
1129                 return false;
1130
1131         $approved = $comment->comment_approved;
1132
1133         if ( $approved == null )
1134                 return false;
1135         elseif ( $approved == '1' )
1136                 return 'approved';
1137         elseif ( $approved == '0' )
1138                 return 'unapproved';
1139         elseif ( $approved == 'spam' )
1140                 return 'spam';
1141         elseif ( $approved == 'trash' )
1142                 return 'trash';
1143         else
1144                 return false;
1145 }
1146
1147 /**
1148  * Call hooks for when a comment status transition occurs.
1149  *
1150  * Calls hooks for comment status transitions. If the new comment status is not the same
1151  * as the previous comment status, then two hooks will be ran, the first is
1152  * 'transition_comment_status' with new status, old status, and comment data. The
1153  * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
1154  * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
1155  * comment data.
1156  *
1157  * The final action will run whether or not the comment statuses are the same. The
1158  * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status
1159  * parameter and COMMENTTYPE is comment_type comment data.
1160  *
1161  * @since 2.7.0
1162  *
1163  * @param string $new_status New comment status.
1164  * @param string $old_status Previous comment status.
1165  * @param object $comment Comment data.
1166  */
1167 function wp_transition_comment_status($new_status, $old_status, $comment) {
1168         // Translate raw statuses to human readable formats for the hooks
1169         // This is not a complete list of comment status, it's only the ones that need to be renamed
1170         $comment_statuses = array(
1171                 0         => 'unapproved',
1172                 'hold'    => 'unapproved', // wp_set_comment_status() uses "hold"
1173                 1         => 'approved',
1174                 'approve' => 'approved', // wp_set_comment_status() uses "approve"
1175         );
1176         if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
1177         if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
1178
1179         // Call the hooks
1180         if ( $new_status != $old_status ) {
1181                 do_action('transition_comment_status', $new_status, $old_status, $comment);
1182                 do_action("comment_{$old_status}_to_{$new_status}", $comment);
1183         }
1184         do_action("comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment);
1185 }
1186
1187 /**
1188  * Get current commenter's name, email, and URL.
1189  *
1190  * Expects cookies content to already be sanitized. User of this function might
1191  * wish to recheck the returned array for validity.
1192  *
1193  * @see sanitize_comment_cookies() Use to sanitize cookies
1194  *
1195  * @since 2.0.4
1196  *
1197  * @return array Comment author, email, url respectively.
1198  */
1199 function wp_get_current_commenter() {
1200         // Cookies should already be sanitized.
1201
1202         $comment_author = '';
1203         if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
1204                 $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
1205
1206         $comment_author_email = '';
1207         if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
1208                 $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
1209
1210         $comment_author_url = '';
1211         if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
1212                 $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
1213
1214         return apply_filters('wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url'));
1215 }
1216
1217 /**
1218  * Inserts a comment to the database.
1219  *
1220  * The available comment data key names are 'comment_author_IP', 'comment_date',
1221  * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
1222  *
1223  * @since 2.0.0
1224  * @uses $wpdb
1225  *
1226  * @param array $commentdata Contains information on the comment.
1227  * @return int The new comment's ID.
1228  */
1229 function wp_insert_comment($commentdata) {
1230         global $wpdb;
1231         extract(stripslashes_deep($commentdata), EXTR_SKIP);
1232
1233         if ( ! isset($comment_author_IP) )
1234                 $comment_author_IP = '';
1235         if ( ! isset($comment_date) )
1236                 $comment_date = current_time('mysql');
1237         if ( ! isset($comment_date_gmt) )
1238                 $comment_date_gmt = get_gmt_from_date($comment_date);
1239         if ( ! isset($comment_parent) )
1240                 $comment_parent = 0;
1241         if ( ! isset($comment_approved) )
1242                 $comment_approved = 1;
1243         if ( ! isset($comment_karma) )
1244                 $comment_karma = 0;
1245         if ( ! isset($user_id) )
1246                 $user_id = 0;
1247         if ( ! isset($comment_type) )
1248                 $comment_type = '';
1249
1250         $data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
1251         $wpdb->insert($wpdb->comments, $data);
1252
1253         $id = (int) $wpdb->insert_id;
1254
1255         if ( $comment_approved == 1 )
1256                 wp_update_comment_count($comment_post_ID);
1257
1258         $comment = get_comment($id);
1259         do_action('wp_insert_comment', $id, $comment);
1260
1261         return $id;
1262 }
1263
1264 /**
1265  * Filters and sanitizes comment data.
1266  *
1267  * Sets the comment data 'filtered' field to true when finished. This can be
1268  * checked as to whether the comment should be filtered and to keep from
1269  * filtering the same comment more than once.
1270  *
1271  * @since 2.0.0
1272  * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
1273  * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
1274  * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
1275  * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
1276  * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
1277  * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
1278  * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
1279  *
1280  * @param array $commentdata Contains information on the comment.
1281  * @return array Parsed comment information.
1282  */
1283 function wp_filter_comment($commentdata) {
1284         if ( isset($commentdata['user_ID']) )
1285                 $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
1286         elseif ( isset($commentdata['user_id']) )
1287                 $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_id']);
1288         $commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
1289         $commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
1290         $commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
1291         $commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
1292         $commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
1293         $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
1294         $commentdata['filtered'] = true;
1295         return $commentdata;
1296 }
1297
1298 /**
1299  * Whether comment should be blocked because of comment flood.
1300  *
1301  * @since 2.1.0
1302  *
1303  * @param bool $block Whether plugin has already blocked comment.
1304  * @param int $time_lastcomment Timestamp for last comment.
1305  * @param int $time_newcomment Timestamp for new comment.
1306  * @return bool Whether comment should be blocked.
1307  */
1308 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
1309         if ( $block ) // a plugin has already blocked... we'll let that decision stand
1310                 return $block;
1311         if ( ($time_newcomment - $time_lastcomment) < 15 )
1312                 return true;
1313         return false;
1314 }
1315
1316 /**
1317  * Adds a new comment to the database.
1318  *
1319  * Filters new comment to ensure that the fields are sanitized and valid before
1320  * inserting comment into database. Calls 'comment_post' action with comment ID
1321  * and whether comment is approved by WordPress. Also has 'preprocess_comment'
1322  * filter for processing the comment data before the function handles it.
1323  *
1324  * We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure
1325  * that it is properly set, such as in wp-config.php, for your environment.
1326  * See {@link http://core.trac.wordpress.org/ticket/9235}
1327  *
1328  * @since 1.5.0
1329  * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
1330  * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
1331  * @uses wp_filter_comment() Used to filter comment before adding comment.
1332  * @uses wp_allow_comment() checks to see if comment is approved.
1333  * @uses wp_insert_comment() Does the actual comment insertion to the database.
1334  *
1335  * @param array $commentdata Contains information on the comment.
1336  * @return int The ID of the comment after adding.
1337  */
1338 function wp_new_comment( $commentdata ) {
1339         $commentdata = apply_filters('preprocess_comment', $commentdata);
1340
1341         $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
1342         if ( isset($commentdata['user_ID']) )
1343                 $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1344         elseif ( isset($commentdata['user_id']) )
1345                 $commentdata['user_id'] = (int) $commentdata['user_id'];
1346
1347         $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
1348         $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
1349         $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
1350
1351         $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
1352         $commentdata['comment_agent']     = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
1353
1354         $commentdata['comment_date']     = current_time('mysql');
1355         $commentdata['comment_date_gmt'] = current_time('mysql', 1);
1356
1357         $commentdata = wp_filter_comment($commentdata);
1358
1359         $commentdata['comment_approved'] = wp_allow_comment($commentdata);
1360
1361         $comment_ID = wp_insert_comment($commentdata);
1362
1363         do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
1364
1365         if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
1366                 if ( '0' == $commentdata['comment_approved'] )
1367                         wp_notify_moderator($comment_ID);
1368
1369                 $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
1370
1371                 if ( get_option('comments_notify') && $commentdata['comment_approved'] && ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) )
1372                         wp_notify_postauthor($comment_ID, isset( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '' );
1373         }
1374
1375         return $comment_ID;
1376 }
1377
1378 /**
1379  * Sets the status of a comment.
1380  *
1381  * The 'wp_set_comment_status' action is called after the comment is handled.
1382  * If the comment status is not in the list, then false is returned.
1383  *
1384  * @since 1.0.0
1385  * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1386  *
1387  * @param int $comment_id Comment ID.
1388  * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
1389  * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
1390  * @return bool False on failure or deletion and true on success.
1391  */
1392 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
1393         global $wpdb;
1394
1395         $status = '0';
1396         switch ( $comment_status ) {
1397                 case 'hold':
1398                 case '0':
1399                         $status = '0';
1400                         break;
1401                 case 'approve':
1402                 case '1':
1403                         $status = '1';
1404                         if ( get_option('comments_notify') ) {
1405                                 $comment = get_comment($comment_id);
1406                                 wp_notify_postauthor($comment_id, $comment->comment_type);
1407                         }
1408                         break;
1409                 case 'spam':
1410                         $status = 'spam';
1411                         break;
1412                 case 'trash':
1413                         $status = 'trash';
1414                         break;
1415                 default:
1416                         return false;
1417         }
1418
1419         $comment_old = clone get_comment($comment_id);
1420
1421         if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
1422                 if ( $wp_error )
1423                         return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
1424                 else
1425                         return false;
1426         }
1427
1428         clean_comment_cache($comment_id);
1429
1430         $comment = get_comment($comment_id);
1431
1432         do_action('wp_set_comment_status', $comment_id, $comment_status);
1433         wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
1434
1435         wp_update_comment_count($comment->comment_post_ID);
1436
1437         return true;
1438 }
1439
1440 /**
1441  * Updates an existing comment in the database.
1442  *
1443  * Filters the comment and makes sure certain fields are valid before updating.
1444  *
1445  * @since 2.0.0
1446  * @uses $wpdb
1447  * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1448  *
1449  * @param array $commentarr Contains information on the comment.
1450  * @return int Comment was updated if value is 1, or was not updated if value is 0.
1451  */
1452 function wp_update_comment($commentarr) {
1453         global $wpdb;
1454
1455         // First, get all of the original fields
1456         $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
1457
1458         // Escape data pulled from DB.
1459         $comment = esc_sql($comment);
1460
1461         $old_status = $comment['comment_approved'];
1462
1463         // Merge old and new fields with new fields overwriting old ones.
1464         $commentarr = array_merge($comment, $commentarr);
1465
1466         $commentarr = wp_filter_comment( $commentarr );
1467
1468         // Now extract the merged array.
1469         extract(stripslashes_deep($commentarr), EXTR_SKIP);
1470
1471         $comment_content = apply_filters('comment_save_pre', $comment_content);
1472
1473         $comment_date_gmt = get_gmt_from_date($comment_date);
1474
1475         if ( !isset($comment_approved) )
1476                 $comment_approved = 1;
1477         else if ( 'hold' == $comment_approved )
1478                 $comment_approved = 0;
1479         else if ( 'approve' == $comment_approved )
1480                 $comment_approved = 1;
1481
1482         $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt');
1483         $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
1484
1485         clean_comment_cache($comment_ID);
1486         wp_update_comment_count($comment_post_ID);
1487         do_action('edit_comment', $comment_ID);
1488         $comment = get_comment($comment_ID);
1489         wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
1490         return $rval;
1491 }
1492
1493 /**
1494  * Whether to defer comment counting.
1495  *
1496  * When setting $defer to true, all post comment counts will not be updated
1497  * until $defer is set to false. When $defer is set to false, then all
1498  * previously deferred updated post comment counts will then be automatically
1499  * updated without having to call wp_update_comment_count() after.
1500  *
1501  * @since 2.5.0
1502  * @staticvar bool $_defer
1503  *
1504  * @param bool $defer
1505  * @return unknown
1506  */
1507 function wp_defer_comment_counting($defer=null) {
1508         static $_defer = false;
1509
1510         if ( is_bool($defer) ) {
1511                 $_defer = $defer;
1512                 // flush any deferred counts
1513                 if ( !$defer )
1514                         wp_update_comment_count( null, true );
1515         }
1516
1517         return $_defer;
1518 }
1519
1520 /**
1521  * Updates the comment count for post(s).
1522  *
1523  * When $do_deferred is false (is by default) and the comments have been set to
1524  * be deferred, the post_id will be added to a queue, which will be updated at a
1525  * later date and only updated once per post ID.
1526  *
1527  * If the comments have not be set up to be deferred, then the post will be
1528  * updated. When $do_deferred is set to true, then all previous deferred post
1529  * IDs will be updated along with the current $post_id.
1530  *
1531  * @since 2.1.0
1532  * @see wp_update_comment_count_now() For what could cause a false return value
1533  *
1534  * @param int $post_id Post ID
1535  * @param bool $do_deferred Whether to process previously deferred post comment counts
1536  * @return bool True on success, false on failure
1537  */
1538 function wp_update_comment_count($post_id, $do_deferred=false) {
1539         static $_deferred = array();
1540
1541         if ( $do_deferred ) {
1542                 $_deferred = array_unique($_deferred);
1543                 foreach ( $_deferred as $i => $_post_id ) {
1544                         wp_update_comment_count_now($_post_id);
1545                         unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
1546                 }
1547         }
1548
1549         if ( wp_defer_comment_counting() ) {
1550                 $_deferred[] = $post_id;
1551                 return true;
1552         }
1553         elseif ( $post_id ) {
1554                 return wp_update_comment_count_now($post_id);
1555         }
1556
1557 }
1558
1559 /**
1560  * Updates the comment count for the post.
1561  *
1562  * @since 2.5.0
1563  * @uses $wpdb
1564  * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
1565  * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
1566  *
1567  * @param int $post_id Post ID
1568  * @return bool False on '0' $post_id or if post with ID does not exist. True on success.
1569  */
1570 function wp_update_comment_count_now($post_id) {
1571         global $wpdb;
1572         $post_id = (int) $post_id;
1573         if ( !$post_id )
1574                 return false;
1575         if ( !$post = get_post($post_id) )
1576                 return false;
1577
1578         $old = (int) $post->comment_count;
1579         $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
1580         $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
1581
1582         clean_post_cache( $post );
1583
1584         do_action('wp_update_comment_count', $post_id, $new, $old);
1585         do_action('edit_post', $post_id, $post);
1586
1587         return true;
1588 }
1589
1590 //
1591 // Ping and trackback functions.
1592 //
1593
1594 /**
1595  * Finds a pingback server URI based on the given URL.
1596  *
1597  * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
1598  * a check for the x-pingback headers first and returns that, if available. The
1599  * check for the rel="pingback" has more overhead than just the header.
1600  *
1601  * @since 1.5.0
1602  *
1603  * @param string $url URL to ping.
1604  * @param int $deprecated Not Used.
1605  * @return bool|string False on failure, string containing URI on success.
1606  */
1607 function discover_pingback_server_uri( $url, $deprecated = '' ) {
1608         if ( !empty( $deprecated ) )
1609                 _deprecated_argument( __FUNCTION__, '2.7' );
1610
1611         $pingback_str_dquote = 'rel="pingback"';
1612         $pingback_str_squote = 'rel=\'pingback\'';
1613
1614         /** @todo Should use Filter Extension or custom preg_match instead. */
1615         $parsed_url = parse_url($url);
1616
1617         if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
1618                 return false;
1619
1620         //Do not search for a pingback server on our own uploads
1621         $uploads_dir = wp_upload_dir();
1622         if ( 0 === strpos($url, $uploads_dir['baseurl']) )
1623                 return false;
1624
1625         $response = wp_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
1626
1627         if ( is_wp_error( $response ) )
1628                 return false;
1629
1630         if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
1631                 return wp_remote_retrieve_header( $response, 'x-pingback' );
1632
1633         // Not an (x)html, sgml, or xml page, no use going further.
1634         if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
1635                 return false;
1636
1637         // Now do a GET since we're going to look in the html headers (and we're sure its not a binary file)
1638         $response = wp_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
1639
1640         if ( is_wp_error( $response ) )
1641                 return false;
1642
1643         $contents = wp_remote_retrieve_body( $response );
1644
1645         $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
1646         $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
1647         if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
1648                 $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
1649                 $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
1650                 $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
1651                 $pingback_href_start = $pingback_href_pos+6;
1652                 $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
1653                 $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
1654                 $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
1655
1656                 // We may find rel="pingback" but an incomplete pingback URL
1657                 if ( $pingback_server_url_len > 0 ) { // We got it!
1658                         return $pingback_server_url;
1659                 }
1660         }
1661
1662         return false;
1663 }
1664
1665 /**
1666  * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
1667  *
1668  * @since 2.1.0
1669  * @uses $wpdb
1670  */
1671 function do_all_pings() {
1672         global $wpdb;
1673
1674         // Do pingbacks
1675         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")) {
1676                 delete_metadata_by_mid( 'post', $ping->meta_id );
1677                 pingback( $ping->post_content, $ping->ID );
1678         }
1679
1680         // Do Enclosures
1681         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")) {
1682                 delete_metadata_by_mid( 'post', $enclosure->meta_id );
1683                 do_enclose( $enclosure->post_content, $enclosure->ID );
1684         }
1685
1686         // Do Trackbacks
1687         $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
1688         if ( is_array($trackbacks) )
1689                 foreach ( $trackbacks as $trackback )
1690                         do_trackbacks($trackback);
1691
1692         //Do Update Services/Generic Pings
1693         generic_ping();
1694 }
1695
1696 /**
1697  * Perform trackbacks.
1698  *
1699  * @since 1.5.0
1700  * @uses $wpdb
1701  *
1702  * @param int $post_id Post ID to do trackbacks on.
1703  */
1704 function do_trackbacks($post_id) {
1705         global $wpdb;
1706
1707         $post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) );
1708         $to_ping = get_to_ping($post_id);
1709         $pinged  = get_pung($post_id);
1710         if ( empty($to_ping) ) {
1711                 $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
1712                 return;
1713         }
1714
1715         if ( empty($post->post_excerpt) )
1716                 $excerpt = apply_filters('the_content', $post->post_content);
1717         else
1718                 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
1719         $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
1720         $excerpt = wp_html_excerpt($excerpt, 252) . '...';
1721
1722         $post_title = apply_filters('the_title', $post->post_title, $post->ID);
1723         $post_title = strip_tags($post_title);
1724
1725         if ( $to_ping ) {
1726                 foreach ( (array) $to_ping as $tb_ping ) {
1727                         $tb_ping = trim($tb_ping);
1728                         if ( !in_array($tb_ping, $pinged) ) {
1729                                 trackback($tb_ping, $post_title, $excerpt, $post_id);
1730                                 $pinged[] = $tb_ping;
1731                         } else {
1732                                 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
1733                         }
1734                 }
1735         }
1736 }
1737
1738 /**
1739  * Sends pings to all of the ping site services.
1740  *
1741  * @since 1.2.0
1742  *
1743  * @param int $post_id Post ID. Not actually used.
1744  * @return int Same as Post ID from parameter
1745  */
1746 function generic_ping($post_id = 0) {
1747         $services = get_option('ping_sites');
1748
1749         $services = explode("\n", $services);
1750         foreach ( (array) $services as $service ) {
1751                 $service = trim($service);
1752                 if ( '' != $service )
1753                         weblog_ping($service);
1754         }
1755
1756         return $post_id;
1757 }
1758
1759 /**
1760  * Pings back the links found in a post.
1761  *
1762  * @since 0.71
1763  * @uses $wp_version
1764  * @uses IXR_Client
1765  *
1766  * @param string $content Post content to check for links.
1767  * @param int $post_ID Post ID.
1768  */
1769 function pingback($content, $post_ID) {
1770         global $wp_version;
1771         include_once(ABSPATH . WPINC . '/class-IXR.php');
1772         include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
1773
1774         // original code by Mort (http://mort.mine.nu:8080)
1775         $post_links = array();
1776
1777         $pung = get_pung($post_ID);
1778
1779         // Variables
1780         $ltrs = '\w';
1781         $gunk = '/#~:.?+=&%@!\-';
1782         $punc = '.:?\-';
1783         $any = $ltrs . $gunk . $punc;
1784
1785         // Step 1
1786         // Parsing the post, external links (if any) are stored in the $post_links array
1787         // This regexp comes straight from phpfreaks.com
1788         // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
1789         preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
1790
1791         // Step 2.
1792         // Walking thru the links array
1793         // first we get rid of links pointing to sites, not to specific files
1794         // Example:
1795         // http://dummy-weblog.org
1796         // http://dummy-weblog.org/
1797         // http://dummy-weblog.org/post.php
1798         // We don't wanna ping first and second types, even if they have a valid <link/>
1799
1800         foreach ( (array) $post_links_temp[0] as $link_test ) :
1801                 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
1802                                 && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
1803                         if ( $test = @parse_url($link_test) ) {
1804                                 if ( isset($test['query']) )
1805                                         $post_links[] = $link_test;
1806                                 elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
1807                                         $post_links[] = $link_test;
1808                         }
1809                 endif;
1810         endforeach;
1811
1812         do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
1813
1814         foreach ( (array) $post_links as $pagelinkedto ) {
1815                 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
1816
1817                 if ( $pingback_server_url ) {
1818                         @ set_time_limit( 60 );
1819                          // Now, the RPC call
1820                         $pagelinkedfrom = get_permalink($post_ID);
1821
1822                         // using a timeout of 3 seconds should be enough to cover slow servers
1823                         $client = new WP_HTTP_IXR_Client($pingback_server_url);
1824                         $client->timeout = 3;
1825                         $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom);
1826                         // when set to true, this outputs debug messages by itself
1827                         $client->debug = false;
1828
1829                         if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
1830                                 add_ping( $post_ID, $pagelinkedto );
1831                 }
1832         }
1833 }
1834
1835 /**
1836  * Check whether blog is public before returning sites.
1837  *
1838  * @since 2.1.0
1839  *
1840  * @param mixed $sites Will return if blog is public, will not return if not public.
1841  * @return mixed Empty string if blog is not public, returns $sites, if site is public.
1842  */
1843 function privacy_ping_filter($sites) {
1844         if ( '0' != get_option('blog_public') )
1845                 return $sites;
1846         else
1847                 return '';
1848 }
1849
1850 /**
1851  * Send a Trackback.
1852  *
1853  * Updates database when sending trackback to prevent duplicates.
1854  *
1855  * @since 0.71
1856  * @uses $wpdb
1857  *
1858  * @param string $trackback_url URL to send trackbacks.
1859  * @param string $title Title of post.
1860  * @param string $excerpt Excerpt of post.
1861  * @param int $ID Post ID.
1862  * @return mixed Database query from update.
1863  */
1864 function trackback($trackback_url, $title, $excerpt, $ID) {
1865         global $wpdb;
1866
1867         if ( empty($trackback_url) )
1868                 return;
1869
1870         $options = array();
1871         $options['timeout'] = 4;
1872         $options['body'] = array(
1873                 'title' => $title,
1874                 'url' => get_permalink($ID),
1875                 'blog_name' => get_option('blogname'),
1876                 'excerpt' => $excerpt
1877         );
1878
1879         $response = wp_remote_post($trackback_url, $options);
1880
1881         if ( is_wp_error( $response ) )
1882                 return;
1883
1884         $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
1885         return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
1886 }
1887
1888 /**
1889  * Send a pingback.
1890  *
1891  * @since 1.2.0
1892  * @uses $wp_version
1893  * @uses IXR_Client
1894  *
1895  * @param string $server Host of blog to connect to.
1896  * @param string $path Path to send the ping.
1897  */
1898 function weblog_ping($server = '', $path = '') {
1899         global $wp_version;
1900         include_once(ABSPATH . WPINC . '/class-IXR.php');
1901         include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
1902
1903         // using a timeout of 3 seconds should be enough to cover slow servers
1904         $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1905         $client->timeout = 3;
1906         $client->useragent .= ' -- WordPress/'.$wp_version;
1907
1908         // when set to true, this outputs debug messages by itself
1909         $client->debug = false;
1910         $home = trailingslashit( home_url() );
1911         if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
1912                 $client->query('weblogUpdates.ping', get_option('blogname'), $home);
1913 }
1914
1915 //
1916 // Cache
1917 //
1918
1919 /**
1920  * Removes comment ID from the comment cache.
1921  *
1922  * @since 2.3.0
1923  * @package WordPress
1924  * @subpackage Cache
1925  *
1926  * @param int|array $ids Comment ID or array of comment IDs to remove from cache
1927  */
1928 function clean_comment_cache($ids) {
1929         foreach ( (array) $ids as $id )
1930                 wp_cache_delete($id, 'comment');
1931
1932         wp_cache_set('last_changed', time(), 'comment');
1933 }
1934
1935 /**
1936  * Updates the comment cache of given comments.
1937  *
1938  * Will add the comments in $comments to the cache. If comment ID already exists
1939  * in the comment cache then it will not be updated. The comment is added to the
1940  * cache using the comment group with the key using the ID of the comments.
1941  *
1942  * @since 2.3.0
1943  * @package WordPress
1944  * @subpackage Cache
1945  *
1946  * @param array $comments Array of comment row objects
1947  */
1948 function update_comment_cache($comments) {
1949         foreach ( (array) $comments as $comment )
1950                 wp_cache_add($comment->comment_ID, $comment, 'comment');
1951 }
1952
1953 //
1954 // Internal
1955 //
1956
1957 /**
1958  * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
1959  *
1960  * @access private
1961  * @since 2.7.0
1962  *
1963  * @param object $posts Post data object.
1964  * @param object $query Query object.
1965  * @return object
1966  */
1967 function _close_comments_for_old_posts( $posts, $query ) {
1968         if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
1969                 return $posts;
1970
1971         $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
1972         if ( ! in_array( $posts[0]->post_type, $post_types ) )
1973                 return $posts;
1974
1975         $days_old = (int) get_option( 'close_comments_days_old' );
1976         if ( ! $days_old )
1977                 return $posts;
1978
1979         if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) ) {
1980                 $posts[0]->comment_status = 'closed';
1981                 $posts[0]->ping_status = 'closed';
1982         }
1983
1984         return $posts;
1985 }
1986
1987 /**
1988  * Close comments on an old post. Hooked to comments_open and pings_open.
1989  *
1990  * @access private
1991  * @since 2.7.0
1992  *
1993  * @param bool $open Comments open or closed
1994  * @param int $post_id Post ID
1995  * @return bool $open
1996  */
1997 function _close_comments_for_old_post( $open, $post_id ) {
1998         if ( ! $open )
1999                 return $open;
2000
2001         if ( !get_option('close_comments_for_old_posts') )
2002                 return $open;
2003
2004         $days_old = (int) get_option('close_comments_days_old');
2005         if ( !$days_old )
2006                 return $open;
2007
2008         $post = get_post($post_id);
2009
2010         $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2011         if ( ! in_array( $post->post_type, $post_types ) )
2012                 return $open;
2013
2014         if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) )
2015                 return false;
2016
2017         return $open;
2018 }