3 * Manages WordPress comments
10 * Checks whether a comment passes internal checks to be allowed to add.
12 * If comment moderation is set in the administration, then all comments,
13 * regardless of their type and whitelist will be set to false. If the number of
14 * links exceeds the amount in the administration, then the check fails. If any
15 * of the parameter contents match the blacklist of words, then the check fails.
17 * If the number of links exceeds the amount in the administration, then the
18 * check fails. If any of the parameter contents match the blacklist of words,
19 * then the check fails.
21 * If the comment is a trackback and part of the blogroll, then the trackback is
22 * automatically whitelisted. If the comment author was approved before, then
23 * the comment is automatically whitelisted.
25 * If none of the checks fail, then the failback is to set the check to pass
31 * @param string $author Comment Author's name
32 * @param string $email Comment Author's email
33 * @param string $url Comment Author's URL
34 * @param string $comment Comment contents
35 * @param string $user_ip Comment Author's IP address
36 * @param string $user_agent Comment Author's User Agent
37 * @param string $comment_type Comment type, either user submitted comment,
38 * trackback, or pingback
39 * @return bool Whether the checks passed (true) and the comments should be
40 * displayed or set to moderated
42 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
45 if ( 1 == get_option('comment_moderation') )
46 return false; // If moderation is set to manual
48 if ( get_option('comment_max_links') && preg_match_all("/<[Aa][^>]*[Hh][Rr][Ee][Ff]=['\"]([^\"'>]+)[^>]*>/", apply_filters('comment_text',$comment), $out) >= get_option('comment_max_links') )
49 return false; // Check # of external links
51 $mod_keys = trim(get_option('moderation_keys'));
52 if ( !empty($mod_keys) ) {
53 $words = explode("\n", $mod_keys );
55 foreach ( (array) $words as $word) {
62 // Do some escaping magic so that '#' chars in the
63 // spam words don't break things:
64 $word = preg_quote($word, '#');
66 $pattern = "#$word#i";
67 if ( preg_match($pattern, $author) ) return false;
68 if ( preg_match($pattern, $email) ) return false;
69 if ( preg_match($pattern, $url) ) return false;
70 if ( preg_match($pattern, $comment) ) return false;
71 if ( preg_match($pattern, $user_ip) ) return false;
72 if ( preg_match($pattern, $user_agent) ) return false;
76 // Comment whitelisting:
77 if ( 1 == get_option('comment_whitelist')) {
78 if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
79 $uri = parse_url($url);
80 $domain = $uri['host'];
81 $uri = parse_url( get_option('home') );
82 $home_domain = $uri['host'];
83 if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain )
87 } elseif ( $author != '' && $email != '' ) {
88 // expected_slashed ($author, $email)
89 $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");
90 if ( ( 1 == $ok_to_comment ) &&
91 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
103 * Retrieve the approved comments for post $post_id.
108 * @param int $post_id The ID of the post
109 * @return array $comments The approved comments
111 function get_approved_comments($post_id) {
113 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));
117 * Retrieves comment data given a comment ID or comment object.
119 * If an object is passed then the comment data will be cached and then returned
120 * after being passed through a filter. If the comment is empty, then the global
121 * comment variable will be used, if it is set.
123 * If the comment is empty, then the global comment variable will be used, if it
129 * @param object|string|int $comment Comment to retrieve.
130 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
131 * @return object|array|null Depends on $output value.
133 function &get_comment(&$comment, $output = OBJECT) {
136 if ( empty($comment) ) {
137 if ( isset($GLOBALS['comment']) )
138 $_comment = & $GLOBALS['comment'];
141 } elseif ( is_object($comment) ) {
142 wp_cache_add($comment->comment_ID, $comment, 'comment');
143 $_comment = $comment;
145 if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
146 $_comment = & $GLOBALS['comment'];
147 } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
148 $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
149 wp_cache_add($_comment->comment_ID, $_comment, 'comment');
153 $_comment = apply_filters('get_comment', $_comment);
155 if ( $output == OBJECT ) {
157 } elseif ( $output == ARRAY_A ) {
158 $__comment = get_object_vars($_comment);
160 } elseif ( $output == ARRAY_N ) {
161 $__comment = array_values(get_object_vars($_comment));
169 * Retrieve a list of comments.
171 * The list of comment arguments are 'status', 'orderby', 'comment_date_gmt',
172 * 'order', 'number', 'offset', and 'post_id'.
177 * @param mixed $args Optional. Array or string of options to override defaults.
178 * @return array List of comments.
180 function get_comments( $args = '' ) {
183 $defaults = array('status' => '', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => '', 'offset' => '', 'post_id' => 0);
185 $args = wp_parse_args( $args, $defaults );
186 extract( $args, EXTR_SKIP );
188 // $args can be whatever, only use the args defined in defaults to compute the key
189 $key = md5( serialize( compact(array_keys($defaults)) ) );
190 $last_changed = wp_cache_get('last_changed', 'comment');
191 if ( !$last_changed ) {
192 $last_changed = time();
193 wp_cache_set('last_changed', $last_changed, 'comment');
195 $cache_key = "get_comments:$key:$last_changed";
197 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
201 $post_id = absint($post_id);
203 if ( 'hold' == $status )
204 $approved = "comment_approved = '0'";
205 elseif ( 'approve' == $status )
206 $approved = "comment_approved = '1'";
207 elseif ( 'spam' == $status )
208 $approved = "comment_approved = 'spam'";
210 $approved = "( comment_approved = '0' OR comment_approved = '1' )";
212 $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
214 $orderby = 'comment_date_gmt'; // Hard code for now
216 $number = absint($number);
217 $offset = absint($offset);
219 if ( !empty($number) ) {
221 $number = 'LIMIT ' . $offset . ',' . $number;
223 $number = 'LIMIT ' . $number;
229 if ( ! empty($post_id) )
230 $post_where = $wpdb->prepare( 'comment_post_ID = %d AND', $post_id );
234 $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
235 wp_cache_add( $cache_key, $comments, 'comment' );
241 * Retrieve all of the WordPress supported comment statuses.
243 * Comments have a limited set of valid status values, this provides the comment
244 * status values and descriptions.
250 * @return array List of comment statuses.
252 function get_comment_statuses( ) {
254 'hold' => __('Unapproved'),
255 'approve' => __('Approved'),
256 'spam' => _c('Spam|adjective'),
264 * The date the last comment was modified.
268 * @global array $cache_lastcommentmodified
270 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
271 * or 'server' locations.
272 * @return string Last comment modified date.
274 function get_lastcommentmodified($timezone = 'server') {
275 global $cache_lastcommentmodified, $wpdb;
277 if ( isset($cache_lastcommentmodified[$timezone]) )
278 return $cache_lastcommentmodified[$timezone];
280 $add_seconds_server = date('Z');
282 switch ( strtolower($timezone)) {
284 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
287 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
290 $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));
294 $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
296 return $lastcommentmodified;
300 * The amount of comments in a post or total comments.
302 * A lot like {@link wp_count_comments()}, in that they both return comment
303 * stats (albeit with different types). The {@link wp_count_comments()} actual
304 * caches, but this function does not.
309 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
310 * @return array The amount of spam, approved, awaiting moderation, and total comments.
312 function get_comment_count( $post_id = 0 ) {
315 $post_id = (int) $post_id;
318 if ( $post_id > 0 ) {
319 $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
322 $totals = (array) $wpdb->get_results("
323 SELECT comment_approved, COUNT( * ) AS total
324 FROM {$wpdb->comments}
326 GROUP BY comment_approved
329 $comment_count = array(
331 "awaiting_moderation" => 0,
333 "total_comments" => 0
336 foreach ( $totals as $row ) {
337 switch ( $row['comment_approved'] ) {
339 $comment_count['spam'] = $row['total'];
340 $comment_count["total_comments"] += $row['total'];
343 $comment_count['approved'] = $row['total'];
344 $comment_count['total_comments'] += $row['total'];
347 $comment_count['awaiting_moderation'] = $row['total'];
348 $comment_count['total_comments'] += $row['total'];
355 return $comment_count;
359 * Sanitizes the cookies sent to the user already.
361 * Will only do anything if the cookies have already been created for the user.
362 * Mostly used after cookies had been sent to use elsewhere.
366 function sanitize_comment_cookies() {
367 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
368 $comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
369 $comment_author = stripslashes($comment_author);
370 $comment_author = attribute_escape($comment_author);
371 $_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
374 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
375 $comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
376 $comment_author_email = stripslashes($comment_author_email);
377 $comment_author_email = attribute_escape($comment_author_email);
378 $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
381 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
382 $comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
383 $comment_author_url = stripslashes($comment_author_url);
384 $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
389 * Validates whether this comment is allowed to be made or not.
393 * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
394 * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
396 * @param array $commentdata Contains information on the comment
397 * @return mixed Signifies the approval status (0|1|'spam')
399 function wp_allow_comment($commentdata) {
401 extract($commentdata, EXTR_SKIP);
403 // Simple duplicate check
404 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
405 $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
406 if ( $comment_author_email )
407 $dupe .= "OR comment_author_email = '$comment_author_email' ";
408 $dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
409 if ( $wpdb->get_var($dupe) ) {
410 if ( defined('DOING_AJAX') )
411 die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
413 wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
416 do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
419 $userdata = get_userdata($user_id);
420 $user = new WP_User($user_id);
421 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
424 if ( isset($userdata) && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) {
425 // The author and the admins get respect.
428 // Everyone else's comments will be checked.
429 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
433 if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
437 $approved = apply_filters('pre_comment_approved', $approved);
442 * Check whether comment flooding is occurring.
444 * Won't run, if current user can manage options, so to not block
449 * @uses apply_filters() Calls 'comment_flood_filter' filter with first
450 * parameter false, last comment timestamp, new comment timestamp.
451 * @uses do_action() Calls 'comment_flood_trigger' action with parameters with
452 * last comment timestamp and new comment timestamp.
454 * @param string $ip Comment IP.
455 * @param string $email Comment author email address.
456 * @param string $date MySQL time string.
458 function check_comment_flood_db( $ip, $email, $date ) {
460 if ( current_user_can( 'manage_options' ) )
461 return; // don't throttle admins
462 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) ) ) {
463 $time_lastcomment = mysql2date('U', $lasttime);
464 $time_newcomment = mysql2date('U', $date);
465 $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
467 do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
469 if ( defined('DOING_AJAX') )
470 die( __('You are posting comments too quickly. Slow down.') );
472 wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
478 * Separates an array of comments into an array keyed by comment_type.
482 * @param array $comments Array of comments
483 * @return array Array of comments keyed by comment_type.
485 function &separate_comments(&$comments) {
486 $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
487 $count = count($comments);
488 for ( $i = 0; $i < $count; $i++ ) {
489 $type = $comments[$i]->comment_type;
492 $comments_by_type[$type][] = &$comments[$i];
493 if ( 'trackback' == $type || 'pingback' == $type )
494 $comments_by_type['pings'][] = &$comments[$i];
497 return $comments_by_type;
501 * Calculate the total number of comment pages.
504 * @uses get_query_var() Used to fill in the default for $per_page parameter.
505 * @uses get_option() Used to fill in defaults for parameters.
506 * @uses Walker_Comment
508 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
509 * @param int $per_page Optional comments per page.
510 * @param boolean $threaded Optional control over flat or threaded comments.
511 * @return int Number of comment pages.
513 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
516 if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
517 return $wp_query->max_num_comment_pages;
519 if ( !$comments || !is_array($comments) )
520 $comments = $wp_query->comments;
522 if ( empty($comments) )
525 if ( !isset($per_page) )
526 $per_page = (int) get_query_var('comments_per_page');
527 if ( 0 === $per_page )
528 $per_page = (int) get_option('comments_per_page');
529 if ( 0 === $per_page )
532 if ( !isset($threaded) )
533 $threaded = get_option('thread_comments');
536 $walker = new Walker_Comment;
537 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
539 $count = ceil( count( $comments ) / $per_page );
546 * Calculate what page number a comment will appear on for comment paging.
549 * @uses get_comment() Gets the full comment of the $comment_ID parameter.
550 * @uses get_option() Get various settings to control function and defaults.
551 * @uses get_page_of_comment() Used to loop up to top level comment.
553 * @param int $comment_ID Comment ID.
554 * @param array $args Optional args.
555 * @return int|null Comment page number or null on error.
557 function get_page_of_comment( $comment_ID, $args = array() ) {
560 if ( !$comment = get_comment( $comment_ID ) )
563 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
564 $args = wp_parse_args( $args, $defaults );
566 if ( '' === $args['per_page'] && get_option('page_comments') )
567 $args['per_page'] = get_query_var('comments_per_page');
568 if ( empty($args['per_page']) ) {
569 $args['per_page'] = 0;
572 if ( $args['per_page'] < 1 )
575 if ( '' === $args['max_depth'] ) {
576 if ( get_option('thread_comments') )
577 $args['max_depth'] = get_option('thread_comments_depth');
579 $args['max_depth'] = -1;
582 // Find this comment's top level parent if threading is enabled
583 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
584 return get_page_of_comment( $comment->comment_parent, $args );
586 $allowedtypes = array(
588 'pingback' => 'pingback',
589 'trackback' => 'trackback',
592 $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
594 // Count comments older than this one
595 $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 ) );
597 // No older comments? Then it's page #1.
598 if ( 0 == $oldercoms )
601 // Divide comments older than this one by comments per page to get this comment's page number
602 return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
606 * Does comment contain blacklisted characters or words.
609 * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters.
611 * @param string $author The author of the comment
612 * @param string $email The email of the comment
613 * @param string $url The url used in the comment
614 * @param string $comment The comment content
615 * @param string $user_ip The comment author IP address
616 * @param string $user_agent The author's browser user agent
617 * @return bool True if comment contains blacklisted content, false if comment does not
619 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
620 do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
622 if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
623 foreach ( (array) $chars[1] as $char ) {
624 // If it's an encoded char in the normal ASCII set, reject
626 continue; // Unless it's &
632 $mod_keys = trim( get_option('blacklist_keys') );
633 if ( '' == $mod_keys )
634 return false; // If moderation keys are empty
635 $words = explode("\n", $mod_keys );
637 foreach ( (array) $words as $word ) {
641 if ( empty($word) ) { continue; }
643 // Do some escaping magic so that '#' chars in the
644 // spam words don't break things:
645 $word = preg_quote($word, '#');
647 $pattern = "#$word#i";
649 preg_match($pattern, $author)
650 || preg_match($pattern, $email)
651 || preg_match($pattern, $url)
652 || preg_match($pattern, $comment)
653 || preg_match($pattern, $user_ip)
654 || preg_match($pattern, $user_agent)
662 * Retrieve total comments for blog or single post.
664 * The properties of the returned object contain the 'moderated', 'approved',
665 * and spam comments for either the entire blog or single post. Those properties
666 * contain the amount of comments that match the status. The 'total_comments'
667 * property contains the integer of total comments.
669 * The comment stats are cached and then retrieved, if they already exist in the
674 * @param int $post_id Optional. Post ID.
675 * @return object Comment stats.
677 function wp_count_comments( $post_id = 0 ) {
680 $post_id = (int) $post_id;
682 $stats = apply_filters('wp_count_comments', array(), $post_id);
683 if ( !empty($stats) )
686 $count = wp_cache_get("comments-{$post_id}", 'counts');
688 if ( false !== $count )
693 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
695 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
698 $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
699 $known_types = array_keys( $approved );
700 foreach( (array) $count as $row_num => $row ) {
701 $total += $row['num_comments'];
702 if ( in_array( $row['comment_approved'], $known_types ) )
703 $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
706 $stats['total_comments'] = $total;
707 foreach ( $approved as $key ) {
708 if ( empty($stats[$key]) )
712 $stats = (object) $stats;
713 wp_cache_set("comments-{$post_id}", $stats, 'counts');
719 * Removes comment ID and maybe updates post comment count.
721 * The post comment count will be updated if the comment was approved and has a
726 * @uses do_action() Calls 'delete_comment' hook on comment ID
727 * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
728 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
730 * @param int $comment_id Comment ID
731 * @return bool False if delete comment query failure, true on success.
733 function wp_delete_comment($comment_id) {
735 do_action('delete_comment', $comment_id);
737 $comment = get_comment($comment_id);
739 if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
742 $post_id = $comment->comment_post_ID;
743 if ( $post_id && $comment->comment_approved == 1 )
744 wp_update_comment_count($post_id);
746 clean_comment_cache($comment_id);
748 do_action('wp_set_comment_status', $comment_id, 'delete');
749 wp_transition_comment_status('delete', $comment->comment_approved, $comment);
754 * The status of a comment by ID.
758 * @param int $comment_id Comment ID
759 * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure.
761 function wp_get_comment_status($comment_id) {
762 $comment = get_comment($comment_id);
766 $approved = $comment->comment_approved;
768 if ( $approved == NULL )
770 elseif ( $approved == '1' )
772 elseif ( $approved == '0' )
774 elseif ( $approved == 'spam' )
781 * Call hooks for when a comment status transition occurs.
783 * Calls hooks for comment status transitions. If the new comment status is not the same
784 * as the previous comment status, then two hooks will be ran, the first is
785 * 'transition_comment_status' with new status, old status, and comment data. The
786 * next action called is 'comment_OLDSTATUS_to_NEWSTATUS' the NEWSTATUS is the
787 * $new_status parameter and the OLDSTATUS is $old_status parameter; it has the
790 * The final action will run whether or not the comment statuses are the same. The
791 * action is named 'comment_NEWSTATUS_COMMENTTYPE', NEWSTATUS is from the $new_status
792 * parameter and COMMENTTYPE is comment_type comment data.
796 * @param string $new_status New comment status.
797 * @param string $old_status Previous comment status.
798 * @param object $comment Comment data.
800 function wp_transition_comment_status($new_status, $old_status, $comment) {
801 // Translate raw statuses to human readable formats for the hooks
802 // This is not a complete list of comment status, it's only the ones that need to be renamed
803 $comment_statuses = array(
805 'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
807 'approve' => 'approved', // wp_set_comment_status() uses "approve"
809 if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
810 if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
813 if ( $new_status != $old_status ) {
814 do_action('transition_comment_status', $new_status, $old_status, $comment);
815 do_action("comment_${old_status}_to_$new_status", $comment);
817 do_action("comment_${new_status}_$comment->comment_type", $comment->comment_ID, $comment);
821 * Get current commenter's name, email, and URL.
823 * Expects cookies content to already be sanitized. User of this function might
824 * wish to recheck the returned array for validity.
826 * @see sanitize_comment_cookies() Use to sanitize cookies
830 * @return array Comment author, email, url respectively.
832 function wp_get_current_commenter() {
833 // Cookies should already be sanitized.
835 $comment_author = '';
836 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
837 $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
839 $comment_author_email = '';
840 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
841 $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
843 $comment_author_url = '';
844 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
845 $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
847 return compact('comment_author', 'comment_author_email', 'comment_author_url');
851 * Inserts a comment to the database.
853 * The available comment data key names are 'comment_author_IP', 'comment_date',
854 * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
859 * @param array $commentdata Contains information on the comment.
860 * @return int The new comment's ID.
862 function wp_insert_comment($commentdata) {
864 extract(stripslashes_deep($commentdata), EXTR_SKIP);
866 if ( ! isset($comment_author_IP) )
867 $comment_author_IP = '';
868 if ( ! isset($comment_date) )
869 $comment_date = current_time('mysql');
870 if ( ! isset($comment_date_gmt) )
871 $comment_date_gmt = get_gmt_from_date($comment_date);
872 if ( ! isset($comment_parent) )
874 if ( ! isset($comment_approved) )
875 $comment_approved = 1;
876 if ( ! isset($user_id) )
878 if ( ! isset($comment_type) )
881 $result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments
882 (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)
883 VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d)",
884 $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) );
886 $id = (int) $wpdb->insert_id;
888 if ( $comment_approved == 1)
889 wp_update_comment_count($comment_post_ID);
895 * Filters and sanitizes comment data.
897 * Sets the comment data 'filtered' field to true when finished. This can be
898 * checked as to whether the comment should be filtered and to keep from
899 * filtering the same comment more than once.
902 * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
903 * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
904 * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
905 * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
906 * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
907 * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
908 * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
910 * @param array $commentdata Contains information on the comment.
911 * @return array Parsed comment information.
913 function wp_filter_comment($commentdata) {
914 $commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
915 $commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
916 $commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
917 $commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
918 $commentdata['comment_author_IP'] = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
919 $commentdata['comment_author_url'] = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
920 $commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
921 $commentdata['filtered'] = true;
926 * Whether comment should be blocked because of comment flood.
930 * @param bool $block Whether plugin has already blocked comment.
931 * @param int $time_lastcomment Timestamp for last comment.
932 * @param int $time_newcomment Timestamp for new comment.
933 * @return bool Whether comment should be blocked.
935 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
936 if ( $block ) // a plugin has already blocked... we'll let that decision stand
938 if ( ($time_newcomment - $time_lastcomment) < 15 )
944 * Adds a new comment to the database.
946 * Filters new comment to ensure that the fields are sanitized and valid before
947 * inserting comment into database. Calls 'comment_post' action with comment ID
948 * and whether comment is approved by WordPress. Also has 'preprocess_comment'
949 * filter for processing the comment data before the function handles it.
952 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
953 * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
954 * @uses wp_filter_comment() Used to filter comment before adding comment.
955 * @uses wp_allow_comment() checks to see if comment is approved.
956 * @uses wp_insert_comment() Does the actual comment insertion to the database.
958 * @param array $commentdata Contains information on the comment.
959 * @return int The ID of the comment after adding.
961 function wp_new_comment( $commentdata ) {
962 $commentdata = apply_filters('preprocess_comment', $commentdata);
964 $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
965 $commentdata['user_ID'] = (int) $commentdata['user_ID'];
967 $commentdata['comment_parent'] = absint($commentdata['comment_parent']);
968 $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
969 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
971 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
972 $commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
974 $commentdata['comment_date'] = current_time('mysql');
975 $commentdata['comment_date_gmt'] = current_time('mysql', 1);
977 $commentdata = wp_filter_comment($commentdata);
979 $commentdata['comment_approved'] = wp_allow_comment($commentdata);
981 $comment_ID = wp_insert_comment($commentdata);
983 do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
985 if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
986 if ( '0' == $commentdata['comment_approved'] )
987 wp_notify_moderator($comment_ID);
989 $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
991 if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
992 wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
999 * Sets the status of a comment.
1001 * The 'wp_set_comment_status' action is called after the comment is handled and
1002 * will only be called, if the comment status is either 'hold', 'approve', or
1003 * 'spam'. If the comment status is not in the list, then false is returned and
1004 * if the status is 'delete', then the comment is deleted without calling the
1008 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1010 * @param int $comment_id Comment ID.
1011 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'.
1012 * @return bool False on failure or deletion and true on success.
1014 function wp_set_comment_status($comment_id, $comment_status) {
1017 switch ( $comment_status ) {
1019 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID = %d LIMIT 1", $comment_id);
1022 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID = %d LIMIT 1", $comment_id);
1023 if ( get_option('comments_notify') ) {
1024 $comment = get_comment($comment_id);
1025 wp_notify_postauthor($comment_id, $comment->comment_type);
1029 $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID = %d LIMIT 1", $comment_id);
1032 return wp_delete_comment($comment_id);
1038 if ( !$wpdb->query($query) )
1041 clean_comment_cache($comment_id);
1043 $comment = get_comment($comment_id);
1045 do_action('wp_set_comment_status', $comment_id, $comment_status);
1046 wp_transition_comment_status($comment_status, $comment->comment_approved, $comment);
1048 wp_update_comment_count($comment->comment_post_ID);
1054 * Updates an existing comment in the database.
1056 * Filters the comment and makes sure certain fields are valid before updating.
1060 * @uses wp_transition_comment_status() Passes new and old comment status along with $comment object
1062 * @param array $commentarr Contains information on the comment.
1063 * @return int Comment was updated if value is 1, or was not updated if value is 0.
1065 function wp_update_comment($commentarr) {
1068 // First, get all of the original fields
1069 $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
1071 // Escape data pulled from DB.
1072 foreach ( (array) $comment as $key => $value )
1073 $comment[$key] = $wpdb->escape($value);
1075 // Merge old and new fields with new fields overwriting old ones.
1076 $commentarr = array_merge($comment, $commentarr);
1078 $commentarr = wp_filter_comment( $commentarr );
1080 // Now extract the merged array.
1081 extract(stripslashes_deep($commentarr), EXTR_SKIP);
1083 $comment_content = apply_filters('comment_save_pre', $comment_content);
1085 $comment_date_gmt = get_gmt_from_date($comment_date);
1087 if ( !isset($comment_approved) )
1088 $comment_approved = 1;
1089 else if ( 'hold' == $comment_approved )
1090 $comment_approved = 0;
1091 else if ( 'approve' == $comment_approved )
1092 $comment_approved = 1;
1094 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->comments SET
1095 comment_content = %s,
1096 comment_author = %s,
1097 comment_author_email = %s,
1098 comment_approved = %s,
1099 comment_author_url = %s,
1101 comment_date_gmt = %s
1102 WHERE comment_ID = %d",
1105 $comment_author_email,
1107 $comment_author_url,
1112 $rval = $wpdb->rows_affected;
1114 clean_comment_cache($comment_ID);
1115 wp_update_comment_count($comment_post_ID);
1116 do_action('edit_comment', $comment_ID);
1117 $comment = get_comment($comment_ID);
1118 wp_transition_comment_status($comment_approved, $comment->comment_approved, $comment);
1123 * Whether to defer comment counting.
1125 * When setting $defer to true, all post comment counts will not be updated
1126 * until $defer is set to false. When $defer is set to false, then all
1127 * previously deferred updated post comment counts will then be automatically
1128 * updated without having to call wp_update_comment_count() after.
1131 * @staticvar bool $_defer
1133 * @param bool $defer
1136 function wp_defer_comment_counting($defer=null) {
1137 static $_defer = false;
1139 if ( is_bool($defer) ) {
1141 // flush any deferred counts
1143 wp_update_comment_count( null, true );
1150 * Updates the comment count for post(s).
1152 * When $do_deferred is false (is by default) and the comments have been set to
1153 * be deferred, the post_id will be added to a queue, which will be updated at a
1154 * later date and only updated once per post ID.
1156 * If the comments have not be set up to be deferred, then the post will be
1157 * updated. When $do_deferred is set to true, then all previous deferred post
1158 * IDs will be updated along with the current $post_id.
1161 * @see wp_update_comment_count_now() For what could cause a false return value
1163 * @param int $post_id Post ID
1164 * @param bool $do_deferred Whether to process previously deferred post comment counts
1165 * @return bool True on success, false on failure
1167 function wp_update_comment_count($post_id, $do_deferred=false) {
1168 static $_deferred = array();
1170 if ( $do_deferred ) {
1171 $_deferred = array_unique($_deferred);
1172 foreach ( $_deferred as $i => $_post_id ) {
1173 wp_update_comment_count_now($_post_id);
1174 unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
1178 if ( wp_defer_comment_counting() ) {
1179 $_deferred[] = $post_id;
1182 elseif ( $post_id ) {
1183 return wp_update_comment_count_now($post_id);
1189 * Updates the comment count for the post.
1193 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
1194 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
1196 * @param int $post_id Post ID
1197 * @return bool False on '0' $post_id or if post with ID does not exist. True on success.
1199 function wp_update_comment_count_now($post_id) {
1201 $post_id = (int) $post_id;
1204 if ( !$post = get_post($post_id) )
1207 $old = (int) $post->comment_count;
1208 $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
1209 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $new, $post_id) );
1211 if ( 'page' == $post->post_type )
1212 clean_page_cache( $post_id );
1214 clean_post_cache( $post_id );
1216 do_action('wp_update_comment_count', $post_id, $new, $old);
1217 do_action('edit_post', $post_id, $post);
1223 // Ping and trackback functions.
1227 * Finds a pingback server URI based on the given URL.
1229 * Checks the HTML for the rel="pingback" link and x-pingback headers. It does
1230 * a check for the x-pingback headers first and returns that, if available. The
1231 * check for the rel="pingback" has more overhead than just the header.
1235 * @param string $url URL to ping.
1236 * @param int $deprecated Not Used.
1237 * @return bool|string False on failure, string containing URI on success.
1239 function discover_pingback_server_uri($url, $deprecated = 2048) {
1241 $pingback_str_dquote = 'rel="pingback"';
1242 $pingback_str_squote = 'rel=\'pingback\'';
1244 /** @todo Should use Filter Extension or custom preg_match instead. */
1245 $parsed_url = parse_url($url);
1247 if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
1250 $response = wp_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.1' ) );
1252 if ( is_wp_error( $response ) )
1255 if ( isset( $response['headers']['x-pingback'] ) )
1256 return $response['headers']['x-pingback'];
1258 // Not an (x)html, sgml, or xml page, no use going further.
1259 if ( isset( $response['headers']['content-type'] ) && preg_match('#(image|audio|video|model)/#is', $response['headers']['content-type']) )
1262 $contents = $response['body'];
1264 $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
1265 $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
1266 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
1267 $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
1268 $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
1269 $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
1270 $pingback_href_start = $pingback_href_pos+6;
1271 $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
1272 $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
1273 $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
1275 // We may find rel="pingback" but an incomplete pingback URL
1276 if ( $pingback_server_url_len > 0 ) { // We got it!
1277 return $pingback_server_url;
1285 * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
1290 function do_all_pings() {
1294 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")) {
1295 $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1296 pingback($ping->post_content, $ping->ID);
1300 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")) {
1301 $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
1302 do_enclose($enclosure->post_content, $enclosure->ID);
1306 $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
1307 if ( is_array($trackbacks) )
1308 foreach ( $trackbacks as $trackback )
1309 do_trackbacks($trackback);
1311 //Do Update Services/Generic Pings
1316 * Perform trackbacks.
1321 * @param int $post_id Post ID to do trackbacks on.
1323 function do_trackbacks($post_id) {
1326 $post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) );
1327 $to_ping = get_to_ping($post_id);
1328 $pinged = get_pung($post_id);
1329 if ( empty($to_ping) ) {
1330 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = %d", $post_id) );
1334 if ( empty($post->post_excerpt) )
1335 $excerpt = apply_filters('the_content', $post->post_content);
1337 $excerpt = apply_filters('the_excerpt', $post->post_excerpt);
1338 $excerpt = str_replace(']]>', ']]>', $excerpt);
1339 $excerpt = wp_html_excerpt($excerpt, 252) . '...';
1341 $post_title = apply_filters('the_title', $post->post_title);
1342 $post_title = strip_tags($post_title);
1345 foreach ( (array) $to_ping as $tb_ping ) {
1346 $tb_ping = trim($tb_ping);
1347 if ( !in_array($tb_ping, $pinged) ) {
1348 trackback($tb_ping, $post_title, $excerpt, $post_id);
1349 $pinged[] = $tb_ping;
1351 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = %d", $post_id) );
1358 * Sends pings to all of the ping site services.
1362 * @param int $post_id Post ID. Not actually used.
1363 * @return int Same as Post ID from parameter
1365 function generic_ping($post_id = 0) {
1366 $services = get_option('ping_sites');
1368 $services = explode("\n", $services);
1369 foreach ( (array) $services as $service ) {
1370 $service = trim($service);
1371 if ( '' != $service )
1372 weblog_ping($service);
1379 * Pings back the links found in a post.
1385 * @param string $content Post content to check for links.
1386 * @param int $post_ID Post ID.
1388 function pingback($content, $post_ID) {
1390 include_once(ABSPATH . WPINC . '/class-IXR.php');
1392 // original code by Mort (http://mort.mine.nu:8080)
1393 $post_links = array();
1395 $pung = get_pung($post_ID);
1399 $gunk = '/#~:.?+=&%@!\-';
1401 $any = $ltrs . $gunk . $punc;
1404 // Parsing the post, external links (if any) are stored in the $post_links array
1405 // This regexp comes straight from phpfreaks.com
1406 // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
1407 preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
1410 // Walking thru the links array
1411 // first we get rid of links pointing to sites, not to specific files
1413 // http://dummy-weblog.org
1414 // http://dummy-weblog.org/
1415 // http://dummy-weblog.org/post.php
1416 // We don't wanna ping first and second types, even if they have a valid <link/>
1418 foreach ( (array) $post_links_temp[0] as $link_test ) :
1419 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
1420 && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
1421 if ( $test = @parse_url($link_test) ) {
1422 if ( isset($test['query']) )
1423 $post_links[] = $link_test;
1424 elseif ( ($test['path'] != '/') && ($test['path'] != '') )
1425 $post_links[] = $link_test;
1430 do_action_ref_array('pre_ping', array(&$post_links, &$pung));
1432 foreach ( (array) $post_links as $pagelinkedto ) {
1433 $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
1435 if ( $pingback_server_url ) {
1436 @ set_time_limit( 60 );
1437 // Now, the RPC call
1438 $pagelinkedfrom = get_permalink($post_ID);
1440 // using a timeout of 3 seconds should be enough to cover slow servers
1441 $client = new IXR_Client($pingback_server_url);
1442 $client->timeout = 3;
1443 $client->useragent .= ' -- WordPress/' . $wp_version;
1445 // when set to true, this outputs debug messages by itself
1446 $client->debug = false;
1448 if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
1449 add_ping( $post_ID, $pagelinkedto );
1455 * Check whether blog is public before returning sites.
1459 * @param mixed $sites Will return if blog is public, will not return if not public.
1460 * @return mixed Empty string if blog is not public, returns $sites, if site is public.
1462 function privacy_ping_filter($sites) {
1463 if ( '0' != get_option('blog_public') )
1472 * Updates database when sending trackback to prevent duplicates.
1477 * @param string $trackback_url URL to send trackbacks.
1478 * @param string $title Title of post.
1479 * @param string $excerpt Excerpt of post.
1480 * @param int $ID Post ID.
1481 * @return mixed Database query from update.
1483 function trackback($trackback_url, $title, $excerpt, $ID) {
1486 if ( empty($trackback_url) )
1490 $options['timeout'] = 4;
1491 $options['body'] = array(
1493 'url' => get_permalink($ID),
1494 'blog_name' => get_option('blogname'),
1495 'excerpt' => $excerpt
1498 $response = wp_remote_post($trackback_url, $options);
1500 if ( is_wp_error( $response ) )
1503 $tb_url = addslashes( $trackback_url );
1504 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
1505 return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) );
1515 * @param string $server Host of blog to connect to.
1516 * @param string $path Path to send the ping.
1518 function weblog_ping($server = '', $path = '') {
1520 include_once(ABSPATH . WPINC . '/class-IXR.php');
1522 // using a timeout of 3 seconds should be enough to cover slow servers
1523 $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1524 $client->timeout = 3;
1525 $client->useragent .= ' -- WordPress/'.$wp_version;
1527 // when set to true, this outputs debug messages by itself
1528 $client->debug = false;
1529 $home = trailingslashit( get_option('home') );
1530 if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
1531 $client->query('weblogUpdates.ping', get_option('blogname'), $home);
1539 * Removes comment ID from the comment cache.
1542 * @package WordPress
1545 * @param int $id Comment ID to remove from cache
1547 function clean_comment_cache($id) {
1548 wp_cache_delete($id, 'comment');
1552 * Updates the comment cache of given comments.
1554 * Will add the comments in $comments to the cache. If comment ID already exists
1555 * in the comment cache then it will not be updated. The comment is added to the
1556 * cache using the comment group with the key using the ID of the comments.
1559 * @package WordPress
1562 * @param array $comments Array of comment row objects
1564 function update_comment_cache($comments) {
1565 foreach ( (array) $comments as $comment )
1566 wp_cache_add($comment->comment_ID, $comment, 'comment');
1574 * Close comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
1579 * @param object $posts Post data object.
1582 function _close_comments_for_old_posts( $posts ) {
1583 if ( empty($posts) || !is_singular() || !get_option('close_comments_for_old_posts') )
1586 $days_old = (int) get_option('close_comments_days_old');
1590 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) ) {
1591 $posts[0]->comment_status = 'closed';
1592 $posts[0]->ping_status = 'closed';
1599 * Close comments on an old post. Hooked to comments_open and pings_open.
1604 * @param bool $open Comments open or closed
1605 * @param int $post_id Post ID
1606 * @return bool $open
1608 function _close_comments_for_old_post( $open, $post_id ) {
1612 if ( !get_option('close_comments_for_old_posts') )
1615 $days_old = (int) get_option('close_comments_days_old');
1619 $post = get_post($post_id);
1621 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) )