PageRenderTime 18ms CodeModel.GetById 100ms app.highlight 164ms RepoModel.GetById 128ms app.codeStats 1ms

/tags/2.5/wp-includes/comment.php

#
PHP | 1295 lines | 675 code | 159 blank | 461 comment | 158 complexity | 77306d13a3d4569c88459c10b643920b MD5 | raw file
   1<?php
   2/**
   3 * Manages WordPress comments
   4 *
   5 * @package WordPress
   6 */
   7
   8/**
   9 * check_comment() - Checks whether a comment passes internal checks to be allowed to add
  10 *
  11 * {@internal Missing Long Description}}
  12 *
  13 * @since 1.2
  14 * @uses $wpdb
  15 *
  16 * @param string $author {@internal Missing Description }}
  17 * @param string $email {@internal Missing Description }}
  18 * @param string $url {@internal Missing Description }}
  19 * @param string $comment {@internal Missing Description }}
  20 * @param string $user_ip {@internal Missing Description }}
  21 * @param string $user_agent {@internal Missing Description }}
  22 * @param string $comment_type {@internal Missing Description }}
  23 * @return bool {@internal Missing Description }}
  24 */
  25function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  26	global $wpdb;
  27
  28	if ( 1 == get_option('comment_moderation') )
  29		return false; // If moderation is set to manual
  30
  31	if ( preg_match_all("|(href\t*?=\t*?['\"]?)?(https?:)?//|i", $comment, $out) >= get_option('comment_max_links') )
  32		return false; // Check # of external links
  33
  34	$mod_keys = trim(get_option('moderation_keys'));
  35	if ( !empty($mod_keys) ) {
  36		$words = explode("\n", $mod_keys );
  37
  38		foreach ($words as $word) {
  39			$word = trim($word);
  40
  41			// Skip empty lines
  42			if ( empty($word) )
  43				continue;
  44
  45			// Do some escaping magic so that '#' chars in the
  46			// spam words don't break things:
  47			$word = preg_quote($word, '#');
  48
  49			$pattern = "#$word#i";
  50			if ( preg_match($pattern, $author) ) return false;
  51			if ( preg_match($pattern, $email) ) return false;
  52			if ( preg_match($pattern, $url) ) return false;
  53			if ( preg_match($pattern, $comment) ) return false;
  54			if ( preg_match($pattern, $user_ip) ) return false;
  55			if ( preg_match($pattern, $user_agent) ) return false;
  56		}
  57	}
  58
  59	// Comment whitelisting:
  60	if ( 1 == get_option('comment_whitelist')) {
  61		if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  62			$uri = parse_url($url);
  63			$domain = $uri['host'];
  64			$uri = parse_url( get_option('home') );
  65			$home_domain = $uri['host'];
  66			if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain )
  67				return true;
  68			else
  69				return false;
  70		} elseif ( $author != '' && $email != '' ) {
  71			// expected_slashed ($author, $email)
  72			$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");
  73			if ( ( 1 == $ok_to_comment ) &&
  74				( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
  75					return true;
  76			else
  77				return false;
  78		} else {
  79			return false;
  80		}
  81	}
  82	return true;
  83}
  84
  85/**
  86 * get_approved_comments() - Returns the approved comments for post $post_id
  87 *
  88 * @since 2.0
  89 * @uses $wpdb
  90 *
  91 * @param int $post_id The ID of the post
  92 * @return array $comments The approved comments
  93 */
  94function get_approved_comments($post_id) {
  95	global $wpdb;
  96	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));
  97}
  98
  99/**
 100 * get_comment() - Retrieves comment data given a comment ID or comment object.
 101 *
 102 * {@internal Missing Long Description}}
 103 *
 104 * @since 2.0
 105 * @uses $wpdb
 106 *
 107 * @param object|string|int $comment {@internal Missing Description}}
 108 * @param string $output OBJECT or ARRAY_A or ARRAY_N constants
 109 * @return object|array|null Depends on $output value.
 110 */
 111function &get_comment(&$comment, $output = OBJECT) {
 112	global $wpdb;
 113
 114	if ( empty($comment) ) {
 115		if ( isset($GLOBALS['comment']) )
 116			$_comment = & $GLOBALS['comment'];
 117		else
 118			$_comment = null;
 119	} elseif ( is_object($comment) ) {
 120		wp_cache_add($comment->comment_ID, $comment, 'comment');
 121		$_comment = $comment;
 122	} else {
 123		if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
 124			$_comment = & $GLOBALS['comment'];
 125		} elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
 126			$_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
 127			wp_cache_add($_comment->comment_ID, $_comment, 'comment');
 128		}
 129	}
 130
 131	$_comment = apply_filters('get_comment', $_comment);
 132
 133	if ( $output == OBJECT ) {
 134		return $_comment;
 135	} elseif ( $output == ARRAY_A ) {
 136		return get_object_vars($_comment);
 137	} elseif ( $output == ARRAY_N ) {
 138		return array_values(get_object_vars($_comment));
 139	} else {
 140		return $_comment;
 141	}
 142}
 143
 144/**
 145 * get_commentdata() - Returns an array of comment data about comment $comment_ID
 146 *
 147 * get_comment() technically does the same thing as this function. This function also
 148 * appears to reference variables and then not use them or not update them when needed.
 149 * It is advised to switch to get_comment(), since this function might be deprecated in
 150 * favor of using get_comment().
 151 *
 152 * @deprecated Use get_comment()
 153 * @see get_comment()
 154 * @since 0.71
 155 *
 156 * @uses $postc Comment cache, might not be used any more
 157 * @uses $id
 158 * @uses $wpdb Database Object
 159 *
 160 * @param int $comment_ID The ID of the comment
 161 * @param int $no_cache Whether to use the cache or not (casted to bool)
 162 * @param bool $include_unapproved Whether to include unapproved comments or not
 163 * @return array The comment data
 164 */
 165function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
 166	global $postc, $wpdb;
 167	if ( $no_cache ) {
 168		$query = $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d", $comment_ID);
 169		if ( false == $include_unapproved )
 170			$query .= " AND comment_approved = '1'";
 171		$myrow = $wpdb->get_row($query, ARRAY_A);
 172	} else {
 173		$myrow['comment_ID']           = $postc->comment_ID;
 174		$myrow['comment_post_ID']      = $postc->comment_post_ID;
 175		$myrow['comment_author']       = $postc->comment_author;
 176		$myrow['comment_author_email'] = $postc->comment_author_email;
 177		$myrow['comment_author_url']   = $postc->comment_author_url;
 178		$myrow['comment_author_IP']    = $postc->comment_author_IP;
 179		$myrow['comment_date']         = $postc->comment_date;
 180		$myrow['comment_content']      = $postc->comment_content;
 181		$myrow['comment_karma']        = $postc->comment_karma;
 182		$myrow['comment_approved']     = $postc->comment_approved;
 183		$myrow['comment_type']         = $postc->comment_type;
 184	}
 185	return $myrow;
 186}
 187
 188/**
 189 * get_lastcommentmodified() - The date the last comment was modified
 190 *
 191 * {@internal Missing Long Description}}
 192 *
 193 * @since 1.5.0
 194 * @uses $wpdb
 195 * @global array $cache_lastcommentmodified
 196 *
 197 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', or 'server' locations
 198 * @return string Last comment modified date
 199 */
 200function get_lastcommentmodified($timezone = 'server') {
 201	global $cache_lastcommentmodified, $wpdb;
 202
 203	if ( isset($cache_lastcommentmodified[$timezone]) )
 204		return $cache_lastcommentmodified[$timezone];
 205
 206	$add_seconds_server = date('Z');
 207
 208	switch ( strtolower($timezone)) {
 209		case 'gmt':
 210			$lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
 211			break;
 212		case 'blog':
 213			$lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
 214			break;
 215		case 'server':
 216			$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));
 217			break;
 218	}
 219
 220	$cache_lastcommentmodified[$timezone] = $lastcommentmodified;
 221
 222	return $lastcommentmodified;
 223}
 224
 225/**
 226 * get_comment_count() - The amount of comments in a post or total comments
 227 *
 228 * {@internal Missing Long Description}}
 229 *
 230 * @since 2.0.0
 231 * @uses $wpdb
 232 *
 233 * @param int $post_id Optional. Comment amount in post if > 0, else total com
 234ments blog wide
 235 * @return array The amount of spam, approved, awaiting moderation, and total
 236 */
 237function get_comment_count( $post_id = 0 ) {
 238	global $wpdb;
 239
 240	$post_id = (int) $post_id;
 241
 242	$where = '';
 243	if ( $post_id > 0 ) {
 244		$where = "WHERE comment_post_ID = {$post_id}";
 245	}
 246
 247	$totals = (array) $wpdb->get_results("
 248		SELECT comment_approved, COUNT( * ) AS total
 249		FROM {$wpdb->comments}
 250		{$where}
 251		GROUP BY comment_approved
 252	", ARRAY_A);
 253
 254	$comment_count = array(
 255		"approved"              => 0,
 256		"awaiting_moderation"   => 0,
 257		"spam"                  => 0,
 258		"total_comments"        => 0
 259	);
 260
 261	foreach ( $totals as $row ) {
 262		switch ( $row['comment_approved'] ) {
 263			case 'spam':
 264				$comment_count['spam'] = $row['total'];
 265				$comment_count["total_comments"] += $row['total'];
 266				break;
 267			case 1:
 268				$comment_count['approved'] = $row['total'];
 269				$comment_count['total_comments'] += $row['total'];
 270				break;
 271			case 0:
 272				$comment_count['awaiting_moderation'] = $row['total'];
 273				$comment_count['total_comments'] += $row['total'];
 274				break;
 275			default:
 276				break;
 277		}
 278	}
 279
 280	return $comment_count;
 281}
 282
 283/**
 284 * sanitize_comment_cookies() - {@internal Missing Short Description}}
 285 *
 286 * {@internal Missing Long Description}}
 287 *
 288 * @since 2.0.4
 289 *
 290 */
 291function sanitize_comment_cookies() {
 292	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) {
 293		$comment_author = apply_filters('pre_comment_author_name', $_COOKIE['comment_author_'.COOKIEHASH]);
 294		$comment_author = stripslashes($comment_author);
 295		$comment_author = attribute_escape($comment_author);
 296		$_COOKIE['comment_author_'.COOKIEHASH] = $comment_author;
 297	}
 298
 299	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) {
 300		$comment_author_email = apply_filters('pre_comment_author_email', $_COOKIE['comment_author_email_'.COOKIEHASH]);
 301		$comment_author_email = stripslashes($comment_author_email);
 302		$comment_author_email = attribute_escape($comment_author_email);
 303		$_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
 304	}
 305
 306	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) {
 307		$comment_author_url = apply_filters('pre_comment_author_url', $_COOKIE['comment_author_url_'.COOKIEHASH]);
 308		$comment_author_url = stripslashes($comment_author_url);
 309		$_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
 310	}
 311}
 312
 313/**
 314 * wp_allow_comment() - Validates whether this comment is allowed to be made or not
 315 *
 316 * {@internal Missing Long Description}}
 317 *
 318 * @since 2.0.0
 319 * @uses $wpdb
 320 * @uses apply_filters() Calls 'pre_comment_approved' hook on the type of comment
 321 * @uses do_action() Calls 'check_comment_flood' hook on $comment_author_IP, $comment_author_email, and $comment_date_gmt
 322 *
 323 * @param array $commentdata Contains information on the comment
 324 * @return mixed Signifies the approval status (0|1|'spam')
 325 */
 326function wp_allow_comment($commentdata) {
 327	global $wpdb;
 328	extract($commentdata, EXTR_SKIP);
 329
 330	// Simple duplicate check
 331	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
 332	$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
 333	if ( $comment_author_email )
 334		$dupe .= "OR comment_author_email = '$comment_author_email' ";
 335	$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
 336	if ( $wpdb->get_var($dupe) )
 337		wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
 338
 339	do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
 340
 341	if ( $user_id ) {
 342		$userdata = get_userdata($user_id);
 343		$user = new WP_User($user_id);
 344		$post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
 345	}
 346
 347	if ( $userdata && ( $user_id == $post_author || $user->has_cap('level_9') ) ) {
 348		// The author and the admins get respect.
 349		$approved = 1;
 350	 } else {
 351		// Everyone else's comments will be checked.
 352		if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
 353			$approved = 1;
 354		else
 355			$approved = 0;
 356		if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
 357			$approved = 'spam';
 358	}
 359
 360	$approved = apply_filters('pre_comment_approved', $approved);
 361	return $approved;
 362}
 363
 364/**
 365 * check_comment_flood_db() - {@internal Missing Short Description}}
 366 *
 367 * {@internal Missing Long Description}}
 368 *
 369 * @since 2.3.0
 370 * @uses $wpdb
 371 * @uses apply_filters() {@internal Missing Description}}
 372 * @uses do_action() {@internal Missing Description}}
 373 *
 374 * @param string $ip {@internal Missing Description}}
 375 * @param string $email {@internal Missing Description}}
 376 * @param unknown_type $date {@internal Missing Description}}
 377 */
 378function check_comment_flood_db( $ip, $email, $date ) {
 379	global $wpdb;
 380	if ( current_user_can( 'manage_options' ) )
 381		return; // don't throttle admins
 382	if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$ip' OR comment_author_email = '$email' ORDER BY comment_date DESC LIMIT 1") ) {
 383		$time_lastcomment = mysql2date('U', $lasttime);
 384		$time_newcomment  = mysql2date('U', $date);
 385		$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
 386		if ( $flood_die ) {
 387			do_action('comment_flood_trigger', $time_lastcomment, $time_newcomment);
 388			wp_die( __('You are posting comments too quickly.  Slow down.') );
 389		}
 390	}
 391}
 392
 393/**
 394 * wp_blacklist_check() - Does comment contain blacklisted characters or words
 395 *
 396 * {@internal Missing Long Description}}
 397 *
 398 * @since 1.5.0
 399 * @uses do_action() Calls 'wp_blacklist_check' hook for all parameters
 400 *
 401 * @param string $author The author of the comment
 402 * @param string $email The email of the comment
 403 * @param string $url The url used in the comment
 404 * @param string $comment The comment content
 405 * @param string $user_ip The comment author IP address
 406 * @param string $user_agent The author's browser user agent
 407 * @return bool True if comment contains blacklisted content, false if comment does not
 408 */
 409function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
 410	do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
 411
 412	if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
 413		foreach ( (array) $chars[1] as $char ) {
 414			// If it's an encoded char in the normal ASCII set, reject
 415			if ( 38 == $char )
 416				continue; // Unless it's &
 417			if ( $char < 128 )
 418				return true;
 419		}
 420	}
 421
 422	$mod_keys = trim( get_option('blacklist_keys') );
 423	if ( '' == $mod_keys )
 424		return false; // If moderation keys are empty
 425	$words = explode("\n", $mod_keys );
 426
 427	foreach ( (array) $words as $word ) {
 428		$word = trim($word);
 429
 430		// Skip empty lines
 431		if ( empty($word) ) { continue; }
 432
 433		// Do some escaping magic so that '#' chars in the
 434		// spam words don't break things:
 435		$word = preg_quote($word, '#');
 436
 437		$pattern = "#$word#i";
 438		if (
 439			   preg_match($pattern, $author)
 440			|| preg_match($pattern, $email)
 441			|| preg_match($pattern, $url)
 442			|| preg_match($pattern, $comment)
 443			|| preg_match($pattern, $user_ip)
 444			|| preg_match($pattern, $user_agent)
 445		 )
 446			return true;
 447	}
 448	return false;
 449}
 450
 451function wp_count_comments() {
 452	global $wpdb;
 453
 454	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} GROUP BY comment_approved", ARRAY_A );
 455
 456	$stats = array( );
 457	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
 458	foreach( (array) $count as $row_num => $row ) {
 459		$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
 460	}
 461
 462	foreach ( $approved as $key ) {
 463		if ( empty($stats[$key]) )
 464			$stats[$key] = 0;
 465	}
 466
 467	return (object) $stats;
 468}
 469
 470/**
 471 * wp_delete_comment() - Removes comment ID and maybe updates post comment count
 472 *
 473 * The post comment count will be updated if the comment was approved and has a post
 474 * ID available.
 475 *
 476 * @since 2.0.0
 477 * @uses $wpdb
 478 * @uses do_action() Calls 'delete_comment' hook on comment ID
 479 * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
 480 *
 481 * @param int $comment_id Comment ID
 482 * @return bool False if delete comment query failure, true on success
 483 */
 484function wp_delete_comment($comment_id) {
 485	global $wpdb;
 486	do_action('delete_comment', $comment_id);
 487
 488	$comment = get_comment($comment_id);
 489
 490	if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
 491		return false;
 492
 493	$post_id = $comment->comment_post_ID;
 494	if ( $post_id && $comment->comment_approved == 1 )
 495		wp_update_comment_count($post_id);
 496
 497	clean_comment_cache($comment_id);
 498
 499	do_action('wp_set_comment_status', $comment_id, 'delete');
 500	return true;
 501}
 502
 503/**
 504 * wp_get_comment_status() - The status of a comment by ID
 505 *
 506 * @since 1.0.0
 507 *
 508 * @param int $comment_id Comment ID
 509 * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure
 510 */
 511function wp_get_comment_status($comment_id) {
 512	$comment = get_comment($comment_id);
 513	if ( !$comment )
 514		return false;
 515
 516	$approved = $comment->comment_approved;
 517
 518	if ( $approved == NULL )
 519		return 'deleted';
 520	elseif ( $approved == '1' )
 521		return 'approved';
 522	elseif ( $approved == '0' )
 523		return 'unapproved';
 524	elseif ( $approved == 'spam' )
 525		return 'spam';
 526	else
 527		return false;
 528}
 529
 530/**
 531 * wp_get_current_commenter() - Get current commenter's name, email, and URL
 532 *
 533 * Expects cookies content to already be sanitized. User of this function
 534 * might wish to recheck the returned array for validity.
 535 *
 536 * @see sanitize_comment_cookies() Use to sanitize cookies
 537 *
 538 * @since 2.0.4
 539 *
 540 * @return array Comment author, email, url respectively
 541 */
 542function wp_get_current_commenter() {
 543	// Cookies should already be sanitized.
 544
 545	$comment_author = '';
 546	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
 547		$comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
 548
 549	$comment_author_email = '';
 550	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
 551		$comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
 552
 553	$comment_author_url = '';
 554	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
 555		$comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
 556
 557	return compact('comment_author', 'comment_author_email', 'comment_author_url');
 558}
 559
 560/**
 561 * wp_insert_comment() - Inserts a comment to the database
 562 *
 563 * {@internal Missing Long Description}}
 564 *
 565 * @since 2.0.0
 566 * @uses $wpdb
 567 *
 568 * @param array $commentdata Contains information on the comment
 569 * @return int The new comment's id
 570 */
 571function wp_insert_comment($commentdata) {
 572	global $wpdb;
 573	extract($commentdata, EXTR_SKIP);
 574
 575	if ( ! isset($comment_author_IP) )
 576		$comment_author_IP = '';
 577	if ( ! isset($comment_date) )
 578		$comment_date = current_time('mysql');
 579	if ( ! isset($comment_date_gmt) )
 580		$comment_date_gmt = get_gmt_from_date($comment_date);
 581	if ( ! isset($comment_parent) )
 582		$comment_parent = 0;
 583	if ( ! isset($comment_approved) )
 584		$comment_approved = 1;
 585	if ( ! isset($user_id) )
 586		$user_id = 0;
 587
 588	$result = $wpdb->query("INSERT INTO $wpdb->comments
 589	(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)
 590	VALUES
 591	('$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')
 592	");
 593
 594	$id = (int) $wpdb->insert_id;
 595
 596	if ( $comment_approved == 1)
 597		wp_update_comment_count($comment_post_ID);
 598
 599	return $id;
 600}
 601
 602/**
 603 * wp_filter_comment() - Parses and returns comment information
 604 *
 605 * Sets the comment data 'filtered' field to true when finished. This
 606 * can be checked as to whether the comment should be filtered and to
 607 * keep from filtering the same comment more than once.
 608 *
 609 * @since 2.0.0
 610 * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
 611 * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
 612 * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
 613 * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
 614 * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
 615 * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
 616 * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
 617 *
 618 * @param array $commentdata Contains information on the comment
 619 * @return array Parsed comment information
 620 */
 621function wp_filter_comment($commentdata) {
 622	$commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']);
 623	$commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
 624	$commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
 625	$commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
 626	$commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
 627	$commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
 628	$commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
 629	$commentdata['filtered'] = true;
 630	return $commentdata;
 631}
 632
 633/**
 634 * wp_throttle_comment_flood() - {@internal Missing Short Description}}
 635 *
 636 * {@internal Missing Long Description}}
 637 *
 638 * @since 2.1.0
 639 *
 640 * @param unknown_type $block {@internal Missing Description}}
 641 * @param unknown_type $time_lastcomment {@internal Missing Description}}
 642 * @param unknown_type $time_newcomment {@internal Missing Description}}
 643 * @return unknown {@internal Missing Description}}
 644 */
 645function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
 646	if ( $block ) // a plugin has already blocked... we'll let that decision stand
 647		return $block;
 648	if ( ($time_newcomment - $time_lastcomment) < 15 )
 649		return true;
 650	return false;
 651}
 652
 653/**
 654 * wp_new_comment() - Parses and adds a new comment to the database
 655 *
 656 * {@internal Missing Long Description}}
 657 *
 658 * @since 1.5.0
 659 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
 660 * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
 661 * @uses wp_filter_comment() Used to filter comment before adding comment
 662 * @uses wp_allow_comment() checks to see if comment is approved.
 663 * @uses wp_insert_comment() Does the actual comment insertion to the database
 664 *
 665 * @param array $commentdata Contains information on the comment
 666 * @return int The ID of the comment after adding.
 667 */
 668function wp_new_comment( $commentdata ) {
 669	$commentdata = apply_filters('preprocess_comment', $commentdata);
 670
 671	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
 672	$commentdata['user_ID']         = (int) $commentdata['user_ID'];
 673
 674	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
 675	$commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT'];
 676
 677	$commentdata['comment_date']     = current_time('mysql');
 678	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
 679
 680	$commentdata = wp_filter_comment($commentdata);
 681
 682	$commentdata['comment_approved'] = wp_allow_comment($commentdata);
 683
 684	$comment_ID = wp_insert_comment($commentdata);
 685
 686	do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
 687
 688	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
 689		if ( '0' == $commentdata['comment_approved'] )
 690			wp_notify_moderator($comment_ID);
 691
 692		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
 693
 694		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
 695			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
 696	}
 697
 698	return $comment_ID;
 699}
 700
 701/**
 702 * wp_set_comment_status() - Sets the status of comment ID
 703 *
 704 * {@internal Missing Long Description}}
 705 *
 706 * @since 1.0.0
 707 *
 708 * @param int $comment_id Comment ID
 709 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'
 710 * @return bool False on failure or deletion and true on success.
 711 */
 712function wp_set_comment_status($comment_id, $comment_status) {
 713	global $wpdb;
 714
 715	switch ( $comment_status ) {
 716		case 'hold':
 717			$query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
 718			break;
 719		case 'approve':
 720			$query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
 721			break;
 722		case 'spam':
 723			$query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
 724			break;
 725		case 'delete':
 726			return wp_delete_comment($comment_id);
 727			break;
 728		default:
 729			return false;
 730	}
 731
 732	if ( !$wpdb->query($query) )
 733		return false;
 734
 735	clean_comment_cache($comment_id);
 736
 737	do_action('wp_set_comment_status', $comment_id, $comment_status);
 738	$comment = get_comment($comment_id);
 739	wp_update_comment_count($comment->comment_post_ID);
 740
 741	return true;
 742}
 743
 744/**
 745 * wp_update_comment() - Parses and updates an existing comment in the database
 746 *
 747 * {@internal Missing Long Description}}
 748 *
 749 * @since 2.0.0
 750 * @uses $wpdb
 751 *
 752 * @param array $commentarr Contains information on the comment
 753 * @return int Comment was updated if value is 1, or was not updated if value is 0.
 754 */
 755function wp_update_comment($commentarr) {
 756	global $wpdb;
 757
 758	// First, get all of the original fields
 759	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
 760
 761	// Escape data pulled from DB.
 762	foreach ( (array) $comment as $key => $value )
 763		$comment[$key] = $wpdb->escape($value);
 764
 765	// Merge old and new fields with new fields overwriting old ones.
 766	$commentarr = array_merge($comment, $commentarr);
 767
 768	$commentarr = wp_filter_comment( $commentarr );
 769
 770	// Now extract the merged array.
 771	extract($commentarr, EXTR_SKIP);
 772
 773	$comment_content = apply_filters('comment_save_pre', $comment_content);
 774
 775	$comment_date_gmt = get_gmt_from_date($comment_date);
 776
 777	$wpdb->query(
 778		"UPDATE $wpdb->comments SET
 779			comment_content      = '$comment_content',
 780			comment_author       = '$comment_author',
 781			comment_author_email = '$comment_author_email',
 782			comment_approved     = '$comment_approved',
 783			comment_author_url   = '$comment_author_url',
 784			comment_date         = '$comment_date',
 785			comment_date_gmt     = '$comment_date_gmt'
 786		WHERE comment_ID = $comment_ID" );
 787
 788	$rval = $wpdb->rows_affected;
 789
 790	clean_comment_cache($comment_ID);
 791	wp_update_comment_count($comment_post_ID);
 792	do_action('edit_comment', $comment_ID);
 793	return $rval;
 794}
 795
 796/**
 797 * wp_defer_comment_counting() - Whether to defer comment counting
 798 *
 799 * When setting $defer to true, all post comment counts will not be updated
 800 * until $defer is set to false. When $defer is set to false, then all
 801 * previously deferred updated post comment counts will then be automatically
 802 * updated without having to call wp_update_comment_count() after.
 803 *
 804 * @since 2.5
 805 * @staticvar bool $_defer
 806 *
 807 * @param bool $defer
 808 * @return unknown
 809 */
 810function wp_defer_comment_counting($defer=null) {
 811	static $_defer = false;
 812
 813	if ( is_bool($defer) ) {
 814		$_defer = $defer;
 815		// flush any deferred counts
 816		if ( !$defer )
 817			wp_update_comment_count( null, true );
 818	}
 819
 820	return $_defer;
 821}
 822
 823/**
 824 * wp_update_comment_count() - Updates the comment count for post(s)
 825 *
 826 * When $do_deferred is false (is by default) and the comments have been
 827 * set to be deferred, the post_id will be added to a queue, which will
 828 * be updated at a later date and only updated once per post ID.
 829 *
 830 * If the comments have not be set up to be deferred, then the post will
 831 * be updated. When $do_deferred is set to true, then all previous deferred
 832 * post IDs will be updated along with the current $post_id.
 833 *
 834 * @since 2.1.0
 835 * @see wp_update_comment_count_now() For what could cause a false return value
 836 *
 837 * @param int $post_id Post ID
 838 * @param bool $do_deferred Whether to process previously deferred post comment counts
 839 * @return bool True on success, false on failure
 840 */
 841function wp_update_comment_count($post_id, $do_deferred=false) {
 842	static $_deferred = array();
 843
 844	if ( $do_deferred ) {
 845		$_deferred = array_unique($_deferred);
 846		foreach ( $_deferred as $i => $_post_id ) {
 847			wp_update_comment_count_now($_post_id);
 848			unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
 849		}
 850	}
 851
 852	if ( wp_defer_comment_counting() ) {
 853		$_deferred[] = $post_id;
 854		return true;
 855	}
 856	elseif ( $post_id ) {
 857		return wp_update_comment_count_now($post_id);
 858	}
 859
 860}
 861
 862/**
 863 * wp_update_comment_count_now() - Updates the comment count for the post
 864 *
 865 * @since 2.5
 866 * @uses $wpdb
 867 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
 868 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
 869 *
 870 * @param int $post_id Post ID
 871 * @return bool False on '0' $post_id or if post with ID does not exist. True on success.
 872 */
 873function wp_update_comment_count_now($post_id) {
 874	global $wpdb;
 875	$post_id = (int) $post_id;
 876	if ( !$post_id )
 877		return false;
 878	if ( !$post = get_post($post_id) )
 879		return false;
 880
 881	$old = (int) $post->comment_count;
 882	$new = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
 883	$wpdb->query("UPDATE $wpdb->posts SET comment_count = '$new' WHERE ID = '$post_id'");
 884
 885	if ( 'page' == $post->post_type )
 886		clean_page_cache( $post_id );
 887	else
 888		clean_post_cache( $post_id );
 889
 890	do_action('wp_update_comment_count', $post_id, $new, $old);
 891	do_action('edit_post', $post_id, $post);
 892
 893	return true;
 894}
 895
 896//
 897// Ping and trackback functions.
 898//
 899
 900/**
 901 * discover_pingback_server_uri() - Finds a pingback server URI based on the given URL
 902 *
 903 * {@internal Missing Long Description}}
 904 *
 905 * @since 1.5.0
 906 * @uses $wp_version
 907 *
 908 * @param string $url URL to ping
 909 * @param int $timeout_bytes Number of bytes to timeout at. Prevents big file downloads, default is 2048.
 910 * @return bool|string False on failure, string containing URI on success.
 911 */
 912function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
 913	global $wp_version;
 914
 915	$byte_count = 0;
 916	$contents = '';
 917	$headers = '';
 918	$pingback_str_dquote = 'rel="pingback"';
 919	$pingback_str_squote = 'rel=\'pingback\'';
 920	$x_pingback_str = 'x-pingback: ';
 921
 922	extract(parse_url($url), EXTR_SKIP);
 923
 924	if ( !isset($host) ) // Not an URL. This should never happen.
 925		return false;
 926
 927	$path  = ( !isset($path) ) ? '/'          : $path;
 928	$path .= ( isset($query) ) ? '?' . $query : '';
 929	$port  = ( isset($port)  ) ? $port        : 80;
 930
 931	// Try to connect to the server at $host
 932	$fp = @fsockopen($host, $port, $errno, $errstr, 2);
 933	if ( !$fp ) // Couldn't open a connection to $host
 934		return false;
 935
 936	// Send the GET request
 937	$request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
 938	// ob_end_flush();
 939	fputs($fp, $request);
 940
 941	// Let's check for an X-Pingback header first
 942	while ( !feof($fp) ) {
 943		$line = fgets($fp, 512);
 944		if ( trim($line) == '' )
 945			break;
 946		$headers .= trim($line)."\n";
 947		$x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
 948		if ( $x_pingback_header_offset ) {
 949			// We got it!
 950			preg_match('#x-pingback: (.+)#is', $headers, $matches);
 951			$pingback_server_url = trim($matches[1]);
 952			return $pingback_server_url;
 953		}
 954		if ( strpos(strtolower($headers), 'content-type: ') ) {
 955			preg_match('#content-type: (.+)#is', $headers, $matches);
 956			$content_type = trim($matches[1]);
 957		}
 958	}
 959
 960	if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
 961		return false;
 962
 963	while ( !feof($fp) ) {
 964		$line = fgets($fp, 1024);
 965		$contents .= trim($line);
 966		$pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
 967		$pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
 968		if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
 969			$quote = ($pingback_link_offset_dquote) ? '"' : '\'';
 970			$pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
 971			$pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
 972			$pingback_href_start = $pingback_href_pos+6;
 973			$pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
 974			$pingback_server_url_len = $pingback_href_end - $pingback_href_start;
 975			$pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
 976			// We may find rel="pingback" but an incomplete pingback URL
 977			if ( $pingback_server_url_len > 0 ) // We got it!
 978				return $pingback_server_url;
 979		}
 980		$byte_count += strlen($line);
 981		if ( $byte_count > $timeout_bytes ) {
 982			// It's no use going further, there probably isn't any pingback
 983			// server to find in this file. (Prevents loading large files.)
 984			return false;
 985		}
 986	}
 987
 988	// We didn't find anything.
 989	return false;
 990}
 991
 992/**
 993 * do_all_pings() - {@internal Missing Short Description}}
 994 *
 995 * {@internal Missing Long Description}}
 996 *
 997 * @since 2.1.0
 998 * @uses $wpdb
 999 */
1000function do_all_pings() {
1001	global $wpdb;
1002
1003	// Do pingbacks
1004	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")) {
1005		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1006		pingback($ping->post_content, $ping->ID);
1007	}
1008
1009	// Do Enclosures
1010	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")) {
1011		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
1012		do_enclose($enclosure->post_content, $enclosure->ID);
1013	}
1014
1015	// Do Trackbacks
1016	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
1017	if ( is_array($trackbacks) )
1018		foreach ( $trackbacks as $trackback )
1019			do_trackbacks($trackback);
1020
1021	//Do Update Services/Generic Pings
1022	generic_ping();
1023}
1024
1025/**
1026 * do_trackbacks() - {@internal Missing Short Description}}
1027 *
1028 * {@internal Missing Long Description}}
1029 *
1030 * @since 1.5.0
1031 * @uses $wpdb
1032 *
1033 * @param int $post_id Post ID to do trackbacks on
1034 */
1035function do_trackbacks($post_id) {
1036	global $wpdb;
1037
1038	$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
1039	$to_ping = get_to_ping($post_id);
1040	$pinged  = get_pung($post_id);
1041	if ( empty($to_ping) ) {
1042		$wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
1043		return;
1044	}
1045
1046	if ( empty($post->post_excerpt) )
1047		$excerpt = apply_filters('the_content', $post->post_content);
1048	else
1049		$excerpt = apply_filters('the_excerpt', $post->post_excerpt);
1050	$excerpt = str_replace(']]>', ']]&gt;', $excerpt);
1051	$excerpt = wp_html_excerpt($excerpt, 252) . '...';
1052
1053	$post_title = apply_filters('the_title', $post->post_title);
1054	$post_title = strip_tags($post_title);
1055
1056	if ( $to_ping ) {
1057		foreach ( (array) $to_ping as $tb_ping ) {
1058			$tb_ping = trim($tb_ping);
1059			if ( !in_array($tb_ping, $pinged) ) {
1060				trackback($tb_ping, $post_title, $excerpt, $post_id);
1061				$pinged[] = $tb_ping;
1062			} else {
1063				$wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
1064			}
1065		}
1066	}
1067}
1068
1069/**
1070 * generic_ping() - {@internal Missing Short Description}}
1071 *
1072 * {@internal Missing Long Description}}
1073 *
1074 * @since 1.2.0
1075 *
1076 * @param int $post_id Post ID. Not actually used.
1077 * @return int Same as Post ID from parameter
1078 */
1079function generic_ping($post_id = 0) {
1080	$services = get_option('ping_sites');
1081
1082	$services = explode("\n", $services);
1083	foreach ( (array) $services as $service ) {
1084		$service = trim($service);
1085		if ( '' != $service )
1086			weblog_ping($service);
1087	}
1088
1089	return $post_id;
1090}
1091
1092/**
1093 * pingback() - Pings back the links found in a post
1094 *
1095 * {@internal Missing Long Description}}
1096 *
1097 * @since 0.71
1098 * @uses $wp_version
1099 * @uses IXR_Client
1100 *
1101 * @param string $content {@internal Missing Description}}
1102 * @param int $post_ID {@internal Missing Description}}
1103 */
1104function pingback($content, $post_ID) {
1105	global $wp_version;
1106	include_once(ABSPATH . WPINC . '/class-IXR.php');
1107
1108	// original code by Mort (http://mort.mine.nu:8080)
1109	$post_links = array();
1110
1111	$pung = get_pung($post_ID);
1112
1113	// Variables
1114	$ltrs = '\w';
1115	$gunk = '/#~:.?+=&%@!\-';
1116	$punc = '.:?\-';
1117	$any = $ltrs . $gunk . $punc;
1118
1119	// Step 1
1120	// Parsing the post, external links (if any) are stored in the $post_links array
1121	// This regexp comes straight from phpfreaks.com
1122	// http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
1123	preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
1124
1125	// Step 2.
1126	// Walking thru the links array
1127	// first we get rid of links pointing to sites, not to specific files
1128	// Example:
1129	// http://dummy-weblog.org
1130	// http://dummy-weblog.org/
1131	// http://dummy-weblog.org/post.php
1132	// We don't wanna ping first and second types, even if they have a valid <link/>
1133
1134	foreach ( $post_links_temp[0] as $link_test ) :
1135		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
1136				&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
1137			$test = parse_url($link_test);
1138			if ( isset($test['query']) )
1139				$post_links[] = $link_test;
1140			elseif ( ($test['path'] != '/') && ($test['path'] != '') )
1141				$post_links[] = $link_test;
1142		endif;
1143	endforeach;
1144
1145	do_action_ref_array('pre_ping', array(&$post_links, &$pung));
1146
1147	foreach ( (array) $post_links as $pagelinkedto ) {
1148		$pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
1149
1150		if ( $pingback_server_url ) {
1151			@ set_time_limit( 60 );
1152			 // Now, the RPC call
1153			$pagelinkedfrom = get_permalink($post_ID);
1154
1155			// using a timeout of 3 seconds should be enough to cover slow servers
1156			$client = new IXR_Client($pingback_server_url);
1157			$client->timeout = 3;
1158			$client->useragent .= ' -- WordPress/' . $wp_version;
1159
1160			// when set to true, this outputs debug messages by itself
1161			$client->debug = false;
1162
1163			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
1164				add_ping( $post_ID, $pagelinkedto );
1165		}
1166	}
1167}
1168
1169/**
1170 * privacy_ping_filter() - {@internal Missing Short Description}}
1171 *
1172 * {@internal Missing Long Description}}
1173 *
1174 * @since 2.1.0
1175 *
1176 * @param unknown_type $sites {@internal Missing Description}}
1177 * @return unknown {@internal Missing Description}}
1178 */
1179function privacy_ping_filter($sites) {
1180	if ( '0' != get_option('blog_public') )
1181		return $sites;
1182	else
1183		return '';
1184}
1185
1186/**
1187 * trackback() - Send a Trackback
1188 *
1189 * {@internal Missing Long Description}}
1190 *
1191 * @since 0.71
1192 * @uses $wpdb
1193 * @uses $wp_version WordPress version
1194 *
1195 * @param string $trackback_url {@internal Missing Description}}
1196 * @param string $title {@internal Missing Description}}
1197 * @param string $excerpt {@internal Missing Description}}
1198 * @param int $ID {@internal Missing Description}}
1199 * @return unknown {@internal Missing Description}}
1200 */
1201function trackback($trackback_url, $title, $excerpt, $ID) {
1202	global $wpdb, $wp_version;
1203
1204	if ( empty($trackback_url) )
1205		return;
1206
1207	$title = urlencode($title);
1208	$excerpt = urlencode($excerpt);
1209	$blog_name = urlencode(get_option('blogname'));
1210	$tb_url = $trackback_url;
1211	$url = urlencode(get_permalink($ID));
1212	$query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
1213	$trackback_url = parse_url($trackback_url);
1214	$http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
1215	$http_request .= 'Host: '.$trackback_url['host']."\r\n";
1216	$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
1217	$http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
1218	$http_request .= "User-Agent: WordPress/" . $wp_version;
1219	$http_request .= "\r\n\r\n";
1220	$http_request .= $query_string;
1221	if ( '' == $trackback_url['port'] )
1222		$trackback_url['port'] = 80;
1223	$fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
1224	@fputs($fs, $http_request);
1225	@fclose($fs);
1226
1227	$tb_url = addslashes( $tb_url );
1228	$wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
1229	return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
1230}
1231
1232/**
1233 * weblog_ping() - {@internal Missing Short Description}}
1234 *
1235 * {@internal Missing Long Description}}
1236 *
1237 * @since 1.2.0
1238 * @uses $wp_version
1239 * @uses IXR_Client
1240 *
1241 * @param unknown_type $server
1242 * @param unknown_type $path
1243 */
1244function weblog_ping($server = '', $path = '') {
1245	global $wp_version;
1246	include_once(ABSPATH . WPINC . '/class-IXR.php');
1247
1248	// using a timeout of 3 seconds should be enough to cover slow servers
1249	$client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1250	$client->timeout = 3;
1251	$client->useragent .= ' -- WordPress/'.$wp_version;
1252
1253	// when set to true, this outputs debug messages by itself
1254	$client->debug = false;
1255	$home = trailingslashit( get_option('home') );
1256	if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
1257		$client->query('weblogUpdates.ping', get_option('blogname'), $home);
1258}
1259
1260//
1261// Cache
1262//
1263
1264/**
1265 * clean_comment_cache() - Removes comment ID from the comment cache
1266 *
1267 * @since 2.3.0
1268 * @package WordPress
1269 * @subpackage Cache
1270 *
1271 * @param int $id Comment ID to remove from cache
1272 */
1273function clean_comment_cache($id) {
1274	wp_cache_delete($id, 'comment');
1275}
1276
1277/**
1278 * update_comment_cache() - Updates the comment cache of given comments
1279 *
1280 * Will add the comments in $comments to the cache. If comment ID already
1281 * exists in the comment cache then it will not be updated.
1282 *
1283 * The comment is added to the cache using the comment group with the key
1284 * using the ID of the comments.
1285 *
1286 * @since 2.3.0
1287 *
1288 * @param array $comments Array of comment row objects
1289 */
1290function update_comment_cache($comments) {
1291	foreach ( (array) $comments as $comment )
1292		wp_cache_add($comment->comment_ID, $comment, 'comment');
1293}
1294
1295?>