3 * Manages WordPress comments
9 * Checks whether a comment passes internal checks to be allowed to add.
11 * If comment moderation is set in the administration, then all comments,
12 * regardless of their type and whitelist will be set to false.
14 * If the number of links exceeds the amount in the administration, then the
17 * If any of the parameter contents match the blacklist of words, then the check
20 * If the comment is a trackback and part of the blogroll, then the trackback is
21 * automatically whitelisted. If the comment author was approved before, then
22 * the comment is automatically whitelisted.
24 * If none of the checks fail, then the failback is to set the check to pass
30 * @param string $author Comment Author's name
31 * @param string $email Comment Author's email
32 * @param string $url Comment Author's URL
33 * @param string $comment Comment contents
34 * @param string $user_ip Comment Author's IP address
35 * @param string $user_agent Comment Author's User Agent
36 * @param string $comment_type Comment type, either user submitted comment,
37 * trackback, or pingback
38 * @return bool Whether the checks passed (true) and the comments should be
39 * displayed or set to moderated
41 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
44 if ( 1 == get_option('comment_moderation') )
45 return false; // If moderation is set to manual
47 if ( preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $comment, $out) >= get_option('comment_max_links') )
48 return false; // Check # of external links
50 $mod_keys = trim(get_option('moderation_keys'));
51 if ( !empty($mod_keys) ) {
52 $words = explode("\n", $mod_keys );
54 foreach ($words as $word) {
61 // Do some escaping magic so that '#' chars in the
62 // spam words don't break things:
63 $word = preg_quote($word, '#');
65 $pattern = "#$word#i";
66 if ( preg_match($pattern, $author) ) return false;
67 if ( preg_match($pattern, $email) ) return false;
68 if ( preg_match($pattern, $url) ) return false;
69 if ( preg_match($pattern, $comment) ) return false;
70 if ( preg_match($pattern, $user_ip) ) return false;
71 if ( preg_match($pattern, $user_agent) ) return false;
75 // Comment whitelisting:
76 if ( 1 == get_option('comment_whitelist')) {
77 if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
78 $uri = parse_url($url);
79 $domain = $uri['host'];
80 $uri = parse_url( get_option('home') );
81 $home_domain = $uri['host'];
82 if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain )
86 } elseif ( $author != '' && $email != '' ) {
87 // expected_slashed ($author, $email)
88 $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");
89 if ( ( 1 == $ok_to_comment ) &&
90 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
102 * Retrieve the approved comments for post $post_id.
107 * @param int $post_id The ID of the post
108 * @return array $comments The approved comments
110 function get_approved_comments($post_id) {
112 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));
116 * Retrieves comment data given a comment ID or comment object.
118 * If an object is passed then the comment data will be cached and then returned
119 * after being passed through a filter.
121 * If the comment is empty, then the global comment variable will be used, if it
127 * @param object|string|int $comment Comment to retrieve.
128 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants
129 * @return object|array|null Depends on $output value.
131 function &get_comment(&$comment, $output = OBJECT) {
134 if ( empty($comment) ) {
135 if ( isset($GLOBALS['comment']) )
136 $_comment = & $GLOBALS['comment'];
139 } elseif ( is_object($comment) ) {
140 wp_cache_add($comment->comment_ID, $comment, 'comment');
141 $_comment = $comment;
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 wp_cache_add($_comment->comment_ID, $_comment, 'comment');
151 $_comment = apply_filters('get_comment', $_comment);
153 if ( $output == OBJECT ) {
155 } elseif ( $output == ARRAY_A ) {
156 return get_object_vars($_comment);
157 } elseif ( $output == ARRAY_N ) {
158 return array_values(get_object_vars($_comment));
165 * Retrieve an array of comment data about comment $comment_ID.
167 * get_comment() technically does the same thing as this function. This function
168 * also appears to reference variables and then not use them or not update them
169 * when needed. It is advised to switch to get_comment(), since this function
170 * might be deprecated in favor of using get_comment().
172 * @deprecated Use get_comment()
176 * @uses $postc Comment cache, might not be used any more
178 * @uses $wpdb Database Object
180 * @param int $comment_ID The ID of the comment
181 * @param int $no_cache Whether to use the cache or not (casted to bool)
182 * @param bool $include_unapproved Whether to include unapproved comments or not
183 * @return array The comment data
185 function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) {
186 global $postc, $wpdb;
188 $query = $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d", $comment_ID);
189 if ( false == $include_unapproved )
190 $query .= " AND comment_approved = '1'";
191 $myrow = $wpdb->get_row($query, ARRAY_A);
193 $myrow['comment_ID'] = $postc->comment_ID;
194 $myrow['comment_post_ID'] = $postc->comment_post_ID;
195 $myrow['comment_author'] = $postc->comment_author;
196 $myrow['comment_author_email'] = $postc->comment_author_email;
197 $myrow['comment_author_url'] = $postc->comment_author_url;
198 $myrow['comment_author_IP'] = $postc->comment_author_IP;
199 $myrow['comment_date'] = $postc->comment_date;
200 $myrow['comment_content'] = $postc->comment_content;
201 $myrow['comment_karma'] = $postc->comment_karma;
202 $myrow['comment_approved'] = $postc->comment_approved;
203 $myrow['comment_type'] = $postc->comment_type;
209 * The date the last comment was modified.
211 * {@internal Missing Long Description}}
215 * @global array $cache_lastcommentmodified
217 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
218 * or 'server' locations
219 * @return string Last comment modified date
221 function get_lastcommentmodified($timezone = 'server') {
222 global $cache_lastcommentmodified, $wpdb;
224 if ( isset($cache_lastcommentmodified[$timezone]) )
225 return $cache_lastcommentmodified[$timezone];
227 $add_seconds_server = date('Z');
229 switch ( strtolower($timezone)) {
231 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
234 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
237 $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));
241 $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
243 return $lastcommentmodified;
247 * The amount of comments in a post or total comments.
249 * {@internal Missing Long Description}}
254 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide
255 * @return array The amount of spam, approved, awaiting moderation, and total
257 function get_comment_count( $post_id = 0 ) {
260 $post_id = (int) $post_id;
263 if ( $post_id > 0 ) {
264 $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
267 $totals = (array) $wpdb->get_results("
268 SELECT comment_approved, COUNT( * ) AS total
269 FROM {$wpdb->comments}
271 GROUP BY comment_approved
274 $comment_count = array(
276 "awaiting_moderation" => 0,
278 "total_comments" => 0
281 foreach ( $totals as $row ) {
282 switch ( $row['comment_approved'] ) {
284 $comment_count['spam'] = $row['total'];
285 $comment_count["total_comments"] += $row['total'];
288 $comment_count['approved'] = $row['total'];
289 $comment_count['total_comments'] += $row['total'];
292 $comment_count['awaiting_moderation'] = $row['total'];
293 $comment_count['total_comments'] += $row['total'];
300 return $comment_count;
304 * Sanitizes the cookies sent to the user already.
306 * Will only do anything if the cookies have already been created for the user.
307 * Mostly used after cookies had been sent to use elsewhere.
311 function sanitize_comment_cookies() {
312 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
313 $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
314 $comment_author = stripslashes($comment_author);
315 $comment_author = attribute_escape($comment_author);
316 $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
319 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
320 $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
321 $comment_author_email = stripslashes($comment_author_email);
322 $comment_author_email = attribute_escape($comment_author_email);
323 $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
326 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
327 $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
328 $comment_author_url = stripslashes($comment_author_url);
329 $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
334 * Validates whether this comment is allowed to be made or not.
336 * {@internal Missing Long Description}}
340 * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
341 * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
343 * @param array $commentdata Contains information on the comment
344 * @return mixed Signifies the approval status (0|1|'spam')
346 function wp_allow_comment($commentdata) {
348 extract($commentdata, EXTR_SKIP);
350 // Simple duplicate check
351 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
352 $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
353 if ( $comment_author_email )
354 $dupe .= "OR comment_author_email = '$comment_author_email' ";
355 $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
356 if ( $wpdb->get_var($dupe) )
357 wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
359 do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
362 $userdata = get_userdata($user_id);
363 $user = new WP_User($user_id);
364 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
367 if ( $userdata && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) {
368 // The author and the admins get respect.
371 // Everyone else's comments will be checked.
372 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
376 if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
380 $approved = apply_filters('pre_comment_approved', $approved);
385 * {@internal Missing Short Description}}
387 * {@internal Missing Long Description}}
391 * @uses apply_filters() {@internal Missing Description}}
392 * @uses do_action() {@internal Missing Description}}
394 * @param string $ip {@internal Missing Description}}
395 * @param string $email {@internal Missing Description}}
396 * @param unknown_type $date {@internal Missing Description}}
398 function check_comment_flood_db( $ip, $email, $date ) {
400 if ( current_user_can( 'manage_options' ) )
401 return; // don't throttle admins
402 if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = %s OR comment_author_email = %s ORDER BY comment_date DESC LIMIT 1", $ip, $email) ) ) {
403 $time_lastcomment = mysql2date('U', $lasttime);
404 $time_newcomment = mysql2date('U', $date);
405 $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
407 do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
408 wp_die( __('You are posting comments too quickly. Slow down.') );
414 * Does comment contain blacklisted characters or words.
416 * {@internal Missing Long Description}}
419 * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters
421 * @param string $author The author of the comment
422 * @param string $email The email of the comment
423 * @param string $url The url used in the comment
424 * @param string $comment The comment content
425 * @param string $user_ip The comment author IP address
426 * @param string $user_agent The author's browser user agent
427 * @return bool True if comment contains blacklisted content, false if comment does not
429 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
430 do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
432 if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
433 foreach ( (array) $chars[1] as $char ) {
434 // If it's an encoded char in the normal ASCII set, reject
436 continue; // Unless it's &
442 $mod_keys = trim( get_option('blacklist_keys') );
443 if ( '' == $mod_keys )
444 return false; // If moderation keys are empty
445 $words = explode("\n", $mod_keys );
447 foreach ( (array) $words as $word ) {
451 if ( empty($word) ) { continue; }
453 // Do some escaping magic so that '#' chars in the
454 // spam words don't break things:
455 $word = preg_quote($word, '#');
457 $pattern = "#$word#i";
459 preg_match($pattern, $author)
460 || preg_match($pattern, $email)
461 || preg_match($pattern, $url)
462 || preg_match($pattern, $comment)
463 || preg_match($pattern, $user_ip)
464 || preg_match($pattern, $user_agent)
472 * {@internal Missing Short Description}}
474 * {@internal Missing Long Description}}
476 * @param unknown_type $post_id
479 function wp_count_comments( $post_id = 0 ) {
482 $post_id = (int) $post_id;
484 $count = wp_cache_get("comments-{$post_id}", 'counts');
486 if ( false !== $count )
491 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
493 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
497 $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
498 foreach( (array) $count as $row_num => $row ) {
499 $total += $row['num_comments'];
500 $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
503 $stats['total_comments'] = $total;
504 foreach ( $approved as $key ) {
505 if ( empty($stats[$key]) )
509 $stats = (object) $stats;
510 wp_cache_set("comments-{$post_id}", $stats, 'counts');
516 * Removes comment ID and maybe updates post comment count.
518 * The post comment count will be updated if the comment was approved and has a
523 * @uses do_action() Calls 'delete_comment' hook on comment ID
524 * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
526 * @param int $comment_id Comment ID
527 * @return bool False if delete comment query failure, true on success
529 function wp_delete_comment($comment_id) {
531 do_action('delete_comment', $comment_id);
533 $comment = get_comment($comment_id);
535 if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
538 $post_id = $comment->comment_post_ID;
539 if ( $post_id && $comment->comment_approved == 1 )
540 wp_update_comment_count($post_id);
542 clean_comment_cache($comment_id);
544 do_action('wp_set_comment_status', $comment_id, 'delete');
549 * The status of a comment by ID.
553 * @param int $comment_id Comment ID
554 * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure
556 function wp_get_comment_status($comment_id) {
557 $comment = get_comment($comment_id);
561 $approved = $comment->comment_approved;
563 if ( $approved == NULL )
565 elseif ( $approved == '1' )
567 elseif ( $approved == '0' )
569 elseif ( $approved == 'spam' )
576 * Get current commenter's name, email, and URL.
578 * Expects cookies content to already be sanitized. User of this function
579 * might wish to recheck the returned array for validity.
581 * @see sanitize_comment_cookies() Use to sanitize cookies
585 * @return array Comment author, email, url respectively
587 function wp_get_current_commenter() {
588 // Cookies should already be sanitized.
590 $comment_author = '';
591 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
592 $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
594 $comment_author_email = '';
595 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
596 $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
598 $comment_author_url = '';
599 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
600 $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
602 return compact('comment_author', 'comment_author_email', 'comment_author_url');
606 * Inserts a comment to the database.
608 * {@internal Missing Long Description}}
613 * @param array $commentdata Contains information on the comment
614 * @return int The new comment's id
616 function wp_insert_comment($commentdata) {
618 extract(stripslashes_deep($commentdata), EXTR_SKIP);
620 if ( ! isset($comment_author_IP) )
621 $comment_author_IP = '';
622 if ( ! isset($comment_date) )
623 $comment_date = current_time('mysql');
624 if ( ! isset($comment_date_gmt) )
625 $comment_date_gmt = get_gmt_from_date($comment_date);
626 if ( ! isset($comment_parent) )
628 if ( ! isset($comment_approved) )
629 $comment_approved = 1;
630 if ( ! isset($user_id) )
633 $result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments
634 (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id)
635 VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d)",
636 $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id) );
638 $id = (int) $wpdb->insert_id;
640 if ( $comment_approved == 1)
641 wp_update_comment_count($comment_post_ID);
647 * Parses and returns comment information.
649 * Sets the comment data 'filtered' field to true when finished. This can be
650 * checked as to whether the comment should be filtered and to keep from
651 * filtering the same comment more than once.
654 * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
655 * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
656 * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
657 * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
658 * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
659 * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
660 * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
662 * @param array $commentdata Contains information on the comment
663 * @return array Parsed comment information
665 function wp_filter_comment($commentdata) {
666 $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
667 $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
668 $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
669 $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
670 $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
671 $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
672 $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
673 $commentdata['filtered'] = true;
678 * {@internal Missing Short Description}}
680 * {@internal Missing Long Description}}
684 * @param unknown_type $block {@internal Missing Description}}
685 * @param unknown_type $time_lastcomment {@internal Missing Description}}
686 * @param unknown_type $time_newcomment {@internal Missing Description}}
687 * @return unknown {@internal Missing Description}}
689 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
690 if ( $block ) // a plugin has already blocked... we'll let that decision stand
692 if ( ($time_newcomment - $time_lastcomment) < 15 )
698 * Parses and adds a new comment to the database.
700 * {@internal Missing Long Description}}
703 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
704 * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
705 * @uses wp_filter_comment() Used to filter comment before adding comment
706 * @uses wp_allow_comment() checks to see if comment is approved.
707 * @uses wp_insert_comment() Does the actual comment insertion to the database
709 * @param array $commentdata Contains information on the comment
710 * @return int The ID of the comment after adding.
712 function wp_new_comment( $commentdata ) {
713 $commentdata = apply_filters('preprocess_comment', $commentdata);
715 $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
716 $commentdata['user_ID'] = (int) $commentdata['user_ID'];
718 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
719 $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
721 $commentdata['comment_date'] = current_time('mysql');
722 $commentdata['comment_date_gmt'] = current_time('mysql', 1);
724 $commentdata = wp_filter_comment($commentdata);
726 $commentdata['comment_approved'] = wp_allow_comment($commentdata);
728 $comment_ID = wp_insert_comment($commentdata);
730 do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
732 if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
733 if ( '0' == $commentdata['comment_approved'] )
734 wp_notify_moderator($comment_ID);
736 $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
738 if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
739 wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
746 * Sets the status of comment ID.
748 * {@internal Missing Long Description}}
752 * @param int $comment_id Comment ID
753 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'
754 * @return bool False on failure or deletion and true on success.
756 function wp_set_comment_status($comment_id, $comment_status) {
759 switch ( $comment_status ) {
761 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID = %d LIMIT 1", $comment_id);
764 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID = %d LIMIT 1", $comment_id);
765 if ( get_option('comments_notify') ) {
766 $comment = get_comment($comment_id);
767 wp_notify_postauthor($comment_id, $comment->comment_type);
771 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID = %d LIMIT 1", $comment_id);
774 return wp_delete_comment($comment_id);
780 if ( !$wpdb->query($query) )
783 clean_comment_cache($comment_id);
785 do_action('wp_set_comment_status', $comment_id, $comment_status);
786 $comment = get_comment($comment_id);
787 wp_update_comment_count($comment->comment_post_ID);
793 * Parses and updates an existing comment in the database.
795 * {@internal Missing Long Description}}
800 * @param array $commentarr Contains information on the comment
801 * @return int Comment was updated if value is 1, or was not updated if value is 0.
803 function wp_update_comment($commentarr) {
806 // First, get all of the original fields
807 $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
809 // Escape data pulled from DB.
810 foreach ( (array) $comment as $key => $value )
811 $comment[$key] = $wpdb->escape($value);
813 // Merge old and new fields with new fields overwriting old ones.
814 $commentarr = array_merge($comment, $commentarr);
816 $commentarr = wp_filter_comment( $commentarr );
818 // Now extract the merged array.
819 extract(stripslashes_deep($commentarr), EXTR_SKIP);
821 $comment_content = apply_filters('comment_save_pre', $comment_content);
823 $comment_date_gmt = get_gmt_from_date($comment_date);
825 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->comments SET
826 comment_content = %s,
828 comment_author_email = %s,
829 comment_approved = %s,
830 comment_author_url = %s,
832 comment_date_gmt = %s
833 WHERE comment_ID = %d",
836 $comment_author_email,
843 $rval = $wpdb->rows_affected;
845 clean_comment_cache($comment_ID);
846 wp_update_comment_count($comment_post_ID);
847 do_action('edit_comment', $comment_ID);
852 * Whether to defer comment counting.
854 * When setting $defer to true, all post comment counts will not be updated
855 * until $defer is set to false. When $defer is set to false, then all
856 * previously deferred updated post comment counts will then be automatically
857 * updated without having to call wp_update_comment_count() after.
860 * @staticvar bool $_defer
865 function wp_defer_comment_counting($defer=null) {
866 static $_defer = false;
868 if ( is_bool($defer) ) {
870 // flush any deferred counts
872 wp_update_comment_count( null, true );
879 * Updates the comment count for post(s).
881 * When $do_deferred is false (is by default) and the comments have been set to
882 * be deferred, the post_id will be added to a queue, which will be updated at a
883 * later date and only updated once per post ID.
885 * If the comments have not be set up to be deferred, then the post will be
886 * updated. When $do_deferred is set to true, then all previous deferred post
887 * IDs will be updated along with the current $post_id.
890 * @see wp_update_comment_count_now() For what could cause a false return value
892 * @param int $post_id Post ID
893 * @param bool $do_deferred Whether to process previously deferred post comment counts
894 * @return bool True on success, false on failure
896 function wp_update_comment_count($post_id, $do_deferred=false) {
897 static $_deferred = array();
899 if ( $do_deferred ) {
900 $_deferred = array_unique($_deferred);
901 foreach ( $_deferred as $i => $_post_id ) {
902 wp_update_comment_count_now($_post_id);
903 unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
907 if ( wp_defer_comment_counting() ) {
908 $_deferred[] = $post_id;
911 elseif ( $post_id ) {
912 return wp_update_comment_count_now($post_id);
918 * Updates the comment count for the post.
922 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
923 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
925 * @param int $post_id Post ID
926 * @return bool False on '0' $post_id or if post with ID does not exist. True on success.
928 function wp_update_comment_count_now($post_id) {
930 $post_id = (int) $post_id;
933 if ( !$post = get_post($post_id) )
936 $old = (int) $post->comment_count;
937 $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
938 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $new, $post_id) );
940 if ( 'page' == $post->post_type )
941 clean_page_cache( $post_id );
943 clean_post_cache( $post_id );
945 do_action('wp_update_comment_count', $post_id, $new, $old);
946 do_action('edit_post', $post_id, $post);
952 // Ping and trackback functions.
956 * Finds a pingback server URI based on the given URL.
958 * {@internal Missing Long Description}}
963 * @param string $url URL to ping
964 * @param int $timeout_bytes Number of bytes to timeout at. Prevents big file downloads, default is 2048.
965 * @return bool|string False on failure, string containing URI on success.
967 function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
973 $pingback_str_dquote = 'rel="pingback"';
974 $pingback_str_squote = 'rel=\'pingback\'';
975 $x_pingback_str = 'x-pingback: ';
977 extract(parse_url($url), EXTR_SKIP);
979 if ( !isset($host) ) // Not an URL. This should never happen.
982 $path = ( !isset($path) ) ? '/' : $path;
983 $path .= ( isset($query) ) ? '?' . $query : '';
984 $port = ( isset($port) ) ? $port : 80;
986 // Try to connect to the server at $host
987 $fp = @fsockopen($host, $port, $errno, $errstr, 2);
988 if ( !$fp ) // Couldn't open a connection to $host
991 // Send the GET request
992 $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
994 fputs($fp, $request);
996 // Let's check for an X-Pingback header first
997 while ( !feof($fp) ) {
998 $line = fgets($fp, 512);
999 if ( trim($line) == '' )
1001 $headers .= trim($line)."\n";
1002 $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
1003 if ( $x_pingback_header_offset ) {
1005 preg_match('#x-pingback: (.+)#is', $headers, $matches);
1006 $pingback_server_url = trim($matches[1]);
1007 return $pingback_server_url;
1009 if ( strpos(strtolower($headers), 'content-type: ') ) {
1010 preg_match('#content-type: (.+)#is', $headers, $matches);
1011 $content_type = trim($matches[1]);
1015 if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
1018 while ( !feof($fp) ) {
1019 $line = fgets($fp, 1024);
1020 $contents .= trim($line);
1021 $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
1022 $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
1023 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
1024 $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
1025 $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
1026 $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
1027 $pingback_href_start = $pingback_href_pos+6;
1028 $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
1029 $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
1030 $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
1031 // We may find rel="pingback" but an incomplete pingback URL
1032 if ( $pingback_server_url_len > 0 ) { // We got it!
1034 return $pingback_server_url;
1037 $byte_count += strlen($line);
1038 if ( $byte_count > $timeout_bytes ) {
1039 // It's no use going further, there probably isn't any pingback
1040 // server to find in this file. (Prevents loading large files.)
1046 // We didn't find anything.
1052 * {@internal Missing Short Description}}
1054 * {@internal Missing Long Description}}
1059 function do_all_pings() {
1063 while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
1064 $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1065 pingback($ping->post_content, $ping->ID);
1069 while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
1070 $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
1071 do_enclose($enclosure->post_content, $enclosure->ID);
1075 $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
1076 if ( is_array($trackbacks) )
1077 foreach ( $trackbacks as $trackback )
1078 do_trackbacks($trackback);
1080 //Do Update Services/Generic Pings
1085 * {@internal Missing Short Description}}
1087 * {@internal Missing Long Description}}
1092 * @param int $post_id Post ID to do trackbacks on
1094 function do_trackbacks($post_id) {
1097 $post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) );
1098 $to_ping = get_to_ping($post_id);
1099 $pinged = get_pung($post_id);
1100 if ( empty($to_ping) ) {
1101 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = %d", $post_id) );
1105 if ( empty($post->post_excerpt) )
1106 $excerpt = apply_filters('the_content', $post->post_content);
1108 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
1109 $excerpt = str_replace(']]>', ']]>', $excerpt);
1110 $excerpt = wp_html_excerpt($excerpt, 252) . '...';
1112 $post_title = apply_filters('the_title', $post->post_title);
1113 $post_title = strip_tags($post_title);
1116 foreach ( (array) $to_ping as $tb_ping ) {
1117 $tb_ping = trim($tb_ping);
1118 if ( !in_array($tb_ping, $pinged) ) {
1119 trackback($tb_ping, $post_title, $excerpt, $post_id);
1120 $pinged[] = $tb_ping;
1122 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = %d", $post_id) );
1129 * {@internal Missing Short Description}}
1131 * {@internal Missing Long Description}}
1135 * @param int $post_id Post ID. Not actually used.
1136 * @return int Same as Post ID from parameter
1138 function generic_ping($post_id = 0) {
1139 $services = get_option('ping_sites');
1141 $services = explode("\n", $services);
1142 foreach ( (array) $services as $service ) {
1143 $service = trim($service);
1144 if ( '' != $service )
1145 weblog_ping($service);
1152 * Pings back the links found in a post.
1154 * {@internal Missing Long Description}}
1160 * @param string $content {@internal Missing Description}}
1161 * @param int $post_ID {@internal Missing Description}}
1163 function pingback($content, $post_ID) {
1165 include_once(ABSPATH . WPINC . '/class-IXR.php');
1167 // original code by Mort (http://mort.mine.nu:8080)
1168 $post_links = array();
1170 $pung = get_pung($post_ID);
1174 $gunk = '/#~:.?+=&%@!\-';
1176 $any = $ltrs . $gunk . $punc;
1179 // Parsing the post, external links (if any) are stored in the $post_links array
1180 // This regexp comes straight from phpfreaks.com
1181 // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
1182 preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
1185 // Walking thru the links array
1186 // first we get rid of links pointing to sites, not to specific files
1188 // http://dummy-weblog.org
1189 // http://dummy-weblog.org/
1190 // http://dummy-weblog.org/post.php
1191 // We don't wanna ping first and second types, even if they have a valid <link/>
1193 foreach ( $post_links_temp[0] as $link_test ) :
1194 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
1195 && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
1196 $test = parse_url($link_test);
1197 if ( isset($test['query']) )
1198 $post_links[] = $link_test;
1199 elseif ( ($test['path'] != '/') && ($test['path'] != '') )
1200 $post_links[] = $link_test;
1204 do_action_ref_array('pre_ping', array(&$post_links, &$pung));
1206 foreach ( (array) $post_links as $pagelinkedto ) {
1207 $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
1209 if ( $pingback_server_url ) {
1210 @ set_time_limit( 60 );
1211 // Now, the RPC call
1212 $pagelinkedfrom = get_permalink($post_ID);
1214 // using a timeout of 3 seconds should be enough to cover slow servers
1215 $client = new IXR_Client($pingback_server_url);
1216 $client->timeout = 3;
1217 $client->useragent .= ' -- WordPress/' . $wp_version;
1219 // when set to true, this outputs debug messages by itself
1220 $client->debug = false;
1222 if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
1223 add_ping( $post_ID, $pagelinkedto );
1229 * {@internal Missing Short Description}}
1231 * {@internal Missing Long Description}}
1235 * @param unknown_type $sites {@internal Missing Description}}
1236 * @return unknown {@internal Missing Description}}
1238 function privacy_ping_filter($sites) {
1239 if ( '0' != get_option('blog_public') )
1248 * Updates database when sending trackback to prevent duplicates.
1252 * @uses $wp_version WordPress version
1254 * @param string $trackback_url URL to send trackbacks.
1255 * @param string $title Title of post
1256 * @param string $excerpt Excerpt of post
1257 * @param int $ID Post ID
1258 * @return mixed Database query from update
1260 function trackback($trackback_url, $title, $excerpt, $ID) {
1261 global $wpdb, $wp_version;
1263 if ( empty($trackback_url) )
1266 $title = urlencode($title);
1267 $excerpt = urlencode($excerpt);
1268 $blog_name = urlencode(get_option('blogname'));
1269 $tb_url = $trackback_url;
1270 $url = urlencode(get_permalink($ID));
1271 $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
1272 $trackback_url = parse_url($trackback_url);
1273 $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
1274 $http_request .= 'Host: '.$trackback_url['host']."\r\n";
1275 $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
1276 $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
1277 $http_request .= "User-Agent: WordPress/" . $wp_version;
1278 $http_request .= "\r\n\r\n";
1279 $http_request .= $query_string;
1280 if ( '' == $trackback_url['port'] )
1281 $trackback_url['port'] = 80;
1282 $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
1283 @fputs($fs, $http_request);
1286 $tb_url = addslashes( $tb_url );
1287 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
1288 return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) );
1298 * @param string $server Host of blog to connect to.
1299 * @param string $path Path to send the ping.
1301 function weblog_ping($server = '', $path = '') {
1303 include_once(ABSPATH . WPINC . '/class-IXR.php');
1305 // using a timeout of 3 seconds should be enough to cover slow servers
1306 $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1307 $client->timeout = 3;
1308 $client->useragent .= ' -- WordPress/'.$wp_version;
1310 // when set to true, this outputs debug messages by itself
1311 $client->debug = false;
1312 $home = trailingslashit( get_option('home') );
1313 if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
1314 $client->query('weblogUpdates.ping', get_option('blogname'), $home);
1322 * Removes comment ID from the comment cache.
1325 * @package WordPress
1328 * @param int $id Comment ID to remove from cache
1330 function clean_comment_cache($id) {
1331 wp_cache_delete($id, 'comment');
1335 * Updates the comment cache of given comments.
1337 * Will add the comments in $comments to the cache. If comment ID already
1338 * exists in the comment cache then it will not be updated.
1340 * The comment is added to the cache using the comment group with the key
1341 * using the ID of the comments.
1345 * @param array $comments Array of comment row objects
1347 function update_comment_cache($comments) {
1348 foreach ( (array) $comments as $comment )
1349 wp_cache_add($comment->comment_ID, $comment, 'comment');