PageRenderTime 911ms CodeModel.GetById 202ms app.highlight 544ms RepoModel.GetById 151ms app.codeStats 0ms

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

#
PHP | 1308 lines | 684 code | 163 blank | 461 comment | 160 complexity | 79105a05a1967cede1b6ea1647a52736 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 = wp_cache_get('comments', 'counts');
 455
 456	if ( false !== $count )
 457		return $count;
 458
 459	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} GROUP BY comment_approved", ARRAY_A );
 460
 461	$stats = array( );
 462	$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
 463	foreach( (array) $count as $row_num => $row ) {
 464		$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
 465	}
 466
 467	foreach ( $approved as $key ) {
 468		if ( empty($stats[$key]) )
 469			$stats[$key] = 0;
 470	}
 471
 472	$stats = (object) $stats;
 473	wp_cache_set('comments', $stats, 'counts');
 474
 475	return $stats;
 476}
 477
 478/**
 479 * wp_delete_comment() - Removes comment ID and maybe updates post comment count
 480 *
 481 * The post comment count will be updated if the comment was approved and has a post
 482 * ID available.
 483 *
 484 * @since 2.0.0
 485 * @uses $wpdb
 486 * @uses do_action() Calls 'delete_comment' hook on comment ID
 487 * @uses do_action() Calls 'wp_set_comment_status' hook on comment ID with 'delete' set for the second parameter
 488 *
 489 * @param int $comment_id Comment ID
 490 * @return bool False if delete comment query failure, true on success
 491 */
 492function wp_delete_comment($comment_id) {
 493	global $wpdb;
 494	do_action('delete_comment', $comment_id);
 495
 496	$comment = get_comment($comment_id);
 497
 498	if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
 499		return false;
 500
 501	$post_id = $comment->comment_post_ID;
 502	if ( $post_id && $comment->comment_approved == 1 )
 503		wp_update_comment_count($post_id);
 504
 505	clean_comment_cache($comment_id);
 506
 507	do_action('wp_set_comment_status', $comment_id, 'delete');
 508	return true;
 509}
 510
 511/**
 512 * wp_get_comment_status() - The status of a comment by ID
 513 *
 514 * @since 1.0.0
 515 *
 516 * @param int $comment_id Comment ID
 517 * @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure
 518 */
 519function wp_get_comment_status($comment_id) {
 520	$comment = get_comment($comment_id);
 521	if ( !$comment )
 522		return false;
 523
 524	$approved = $comment->comment_approved;
 525
 526	if ( $approved == NULL )
 527		return 'deleted';
 528	elseif ( $approved == '1' )
 529		return 'approved';
 530	elseif ( $approved == '0' )
 531		return 'unapproved';
 532	elseif ( $approved == 'spam' )
 533		return 'spam';
 534	else
 535		return false;
 536}
 537
 538/**
 539 * wp_get_current_commenter() - Get current commenter's name, email, and URL
 540 *
 541 * Expects cookies content to already be sanitized. User of this function
 542 * might wish to recheck the returned array for validity.
 543 *
 544 * @see sanitize_comment_cookies() Use to sanitize cookies
 545 *
 546 * @since 2.0.4
 547 *
 548 * @return array Comment author, email, url respectively
 549 */
 550function wp_get_current_commenter() {
 551	// Cookies should already be sanitized.
 552
 553	$comment_author = '';
 554	if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
 555		$comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
 556
 557	$comment_author_email = '';
 558	if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
 559		$comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
 560
 561	$comment_author_url = '';
 562	if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
 563		$comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
 564
 565	return compact('comment_author', 'comment_author_email', 'comment_author_url');
 566}
 567
 568/**
 569 * wp_insert_comment() - Inserts a comment to the database
 570 *
 571 * {@internal Missing Long Description}}
 572 *
 573 * @since 2.0.0
 574 * @uses $wpdb
 575 *
 576 * @param array $commentdata Contains information on the comment
 577 * @return int The new comment's id
 578 */
 579function wp_insert_comment($commentdata) {
 580	global $wpdb;
 581	extract($commentdata, EXTR_SKIP);
 582
 583	if ( ! isset($comment_author_IP) )
 584		$comment_author_IP = '';
 585	if ( ! isset($comment_date) )
 586		$comment_date = current_time('mysql');
 587	if ( ! isset($comment_date_gmt) )
 588		$comment_date_gmt = get_gmt_from_date($comment_date);
 589	if ( ! isset($comment_parent) )
 590		$comment_parent = 0;
 591	if ( ! isset($comment_approved) )
 592		$comment_approved = 1;
 593	if ( ! isset($user_id) )
 594		$user_id = 0;
 595
 596	$result = $wpdb->query("INSERT INTO $wpdb->comments
 597	(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)
 598	VALUES
 599	('$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')
 600	");
 601
 602	$id = (int) $wpdb->insert_id;
 603
 604	if ( $comment_approved == 1)
 605		wp_update_comment_count($comment_post_ID);
 606
 607	return $id;
 608}
 609
 610/**
 611 * wp_filter_comment() - Parses and returns comment information
 612 *
 613 * Sets the comment data 'filtered' field to true when finished. This
 614 * can be checked as to whether the comment should be filtered and to
 615 * keep from filtering the same comment more than once.
 616 *
 617 * @since 2.0.0
 618 * @uses apply_filters() Calls 'pre_user_id' hook on comment author's user ID
 619 * @uses apply_filters() Calls 'pre_comment_user_agent' hook on comment author's user agent
 620 * @uses apply_filters() Calls 'pre_comment_author_name' hook on comment author's name
 621 * @uses apply_filters() Calls 'pre_comment_content' hook on the comment's content
 622 * @uses apply_filters() Calls 'pre_comment_user_ip' hook on comment author's IP
 623 * @uses apply_filters() Calls 'pre_comment_author_url' hook on comment author's URL
 624 * @uses apply_filters() Calls 'pre_comment_author_email' hook on comment author's email address
 625 *
 626 * @param array $commentdata Contains information on the comment
 627 * @return array Parsed comment information
 628 */
 629function wp_filter_comment($commentdata) {
 630	$commentdata['user_id']              = apply_filters('pre_user_id', $commentdata['user_ID']);
 631	$commentdata['comment_agent']        = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
 632	$commentdata['comment_author']       = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
 633	$commentdata['comment_content']      = apply_filters('pre_comment_content', $commentdata['comment_content']);
 634	$commentdata['comment_author_IP']    = apply_filters('pre_comment_user_ip', $commentdata['comment_author_IP']);
 635	$commentdata['comment_author_url']   = apply_filters('pre_comment_author_url', $commentdata['comment_author_url']);
 636	$commentdata['comment_author_email'] = apply_filters('pre_comment_author_email', $commentdata['comment_author_email']);
 637	$commentdata['filtered'] = true;
 638	return $commentdata;
 639}
 640
 641/**
 642 * wp_throttle_comment_flood() - {@internal Missing Short Description}}
 643 *
 644 * {@internal Missing Long Description}}
 645 *
 646 * @since 2.1.0
 647 *
 648 * @param unknown_type $block {@internal Missing Description}}
 649 * @param unknown_type $time_lastcomment {@internal Missing Description}}
 650 * @param unknown_type $time_newcomment {@internal Missing Description}}
 651 * @return unknown {@internal Missing Description}}
 652 */
 653function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
 654	if ( $block ) // a plugin has already blocked... we'll let that decision stand
 655		return $block;
 656	if ( ($time_newcomment - $time_lastcomment) < 15 )
 657		return true;
 658	return false;
 659}
 660
 661/**
 662 * wp_new_comment() - Parses and adds a new comment to the database
 663 *
 664 * {@internal Missing Long Description}}
 665 *
 666 * @since 1.5.0
 667 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
 668 * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
 669 * @uses wp_filter_comment() Used to filter comment before adding comment
 670 * @uses wp_allow_comment() checks to see if comment is approved.
 671 * @uses wp_insert_comment() Does the actual comment insertion to the database
 672 *
 673 * @param array $commentdata Contains information on the comment
 674 * @return int The ID of the comment after adding.
 675 */
 676function wp_new_comment( $commentdata ) {
 677	$commentdata = apply_filters('preprocess_comment', $commentdata);
 678
 679	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
 680	$commentdata['user_ID']         = (int) $commentdata['user_ID'];
 681
 682	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
 683	$commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT'];
 684
 685	$commentdata['comment_date']     = current_time('mysql');
 686	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
 687
 688	$commentdata = wp_filter_comment($commentdata);
 689
 690	$commentdata['comment_approved'] = wp_allow_comment($commentdata);
 691
 692	$comment_ID = wp_insert_comment($commentdata);
 693
 694	do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
 695
 696	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
 697		if ( '0' == $commentdata['comment_approved'] )
 698			wp_notify_moderator($comment_ID);
 699
 700		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
 701
 702		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
 703			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
 704	}
 705
 706	return $comment_ID;
 707}
 708
 709/**
 710 * wp_set_comment_status() - Sets the status of comment ID
 711 *
 712 * {@internal Missing Long Description}}
 713 *
 714 * @since 1.0.0
 715 *
 716 * @param int $comment_id Comment ID
 717 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'
 718 * @return bool False on failure or deletion and true on success.
 719 */
 720function wp_set_comment_status($comment_id, $comment_status) {
 721	global $wpdb;
 722
 723	switch ( $comment_status ) {
 724		case 'hold':
 725			$query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
 726			break;
 727		case 'approve':
 728			$query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
 729			if ( get_option('comments_notify') ) {
 730				$comment = get_comment($comment_id);
 731				wp_notify_postauthor($comment_id, $comment->comment_type);
 732			}
 733
 734			break;
 735		case 'spam':
 736			$query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
 737			break;
 738		case 'delete':
 739			return wp_delete_comment($comment_id);
 740			break;
 741		default:
 742			return false;
 743	}
 744
 745	if ( !$wpdb->query($query) )
 746		return false;
 747
 748	clean_comment_cache($comment_id);
 749
 750	do_action('wp_set_comment_status', $comment_id, $comment_status);
 751	$comment = get_comment($comment_id);
 752	wp_update_comment_count($comment->comment_post_ID);
 753
 754	return true;
 755}
 756
 757/**
 758 * wp_update_comment() - Parses and updates an existing comment in the database
 759 *
 760 * {@internal Missing Long Description}}
 761 *
 762 * @since 2.0.0
 763 * @uses $wpdb
 764 *
 765 * @param array $commentarr Contains information on the comment
 766 * @return int Comment was updated if value is 1, or was not updated if value is 0.
 767 */
 768function wp_update_comment($commentarr) {
 769	global $wpdb;
 770
 771	// First, get all of the original fields
 772	$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
 773
 774	// Escape data pulled from DB.
 775	foreach ( (array) $comment as $key => $value )
 776		$comment[$key] = $wpdb->escape($value);
 777
 778	// Merge old and new fields with new fields overwriting old ones.
 779	$commentarr = array_merge($comment, $commentarr);
 780
 781	$commentarr = wp_filter_comment( $commentarr );
 782
 783	// Now extract the merged array.
 784	extract($commentarr, EXTR_SKIP);
 785
 786	$comment_content = apply_filters('comment_save_pre', $comment_content);
 787
 788	$comment_date_gmt = get_gmt_from_date($comment_date);
 789
 790	$wpdb->query(
 791		"UPDATE $wpdb->comments SET
 792			comment_content      = '$comment_content',
 793			comment_author       = '$comment_author',
 794			comment_author_email = '$comment_author_email',
 795			comment_approved     = '$comment_approved',
 796			comment_author_url   = '$comment_author_url',
 797			comment_date         = '$comment_date',
 798			comment_date_gmt     = '$comment_date_gmt'
 799		WHERE comment_ID = $comment_ID" );
 800
 801	$rval = $wpdb->rows_affected;
 802
 803	clean_comment_cache($comment_ID);
 804	wp_update_comment_count($comment_post_ID);
 805	do_action('edit_comment', $comment_ID);
 806	return $rval;
 807}
 808
 809/**
 810 * wp_defer_comment_counting() - Whether to defer comment counting
 811 *
 812 * When setting $defer to true, all post comment counts will not be updated
 813 * until $defer is set to false. When $defer is set to false, then all
 814 * previously deferred updated post comment counts will then be automatically
 815 * updated without having to call wp_update_comment_count() after.
 816 *
 817 * @since 2.5
 818 * @staticvar bool $_defer
 819 *
 820 * @param bool $defer
 821 * @return unknown
 822 */
 823function wp_defer_comment_counting($defer=null) {
 824	static $_defer = false;
 825
 826	if ( is_bool($defer) ) {
 827		$_defer = $defer;
 828		// flush any deferred counts
 829		if ( !$defer )
 830			wp_update_comment_count( null, true );
 831	}
 832
 833	return $_defer;
 834}
 835
 836/**
 837 * wp_update_comment_count() - Updates the comment count for post(s)
 838 *
 839 * When $do_deferred is false (is by default) and the comments have been
 840 * set to be deferred, the post_id will be added to a queue, which will
 841 * be updated at a later date and only updated once per post ID.
 842 *
 843 * If the comments have not be set up to be deferred, then the post will
 844 * be updated. When $do_deferred is set to true, then all previous deferred
 845 * post IDs will be updated along with the current $post_id.
 846 *
 847 * @since 2.1.0
 848 * @see wp_update_comment_count_now() For what could cause a false return value
 849 *
 850 * @param int $post_id Post ID
 851 * @param bool $do_deferred Whether to process previously deferred post comment counts
 852 * @return bool True on success, false on failure
 853 */
 854function wp_update_comment_count($post_id, $do_deferred=false) {
 855	static $_deferred = array();
 856
 857	if ( $do_deferred ) {
 858		$_deferred = array_unique($_deferred);
 859		foreach ( $_deferred as $i => $_post_id ) {
 860			wp_update_comment_count_now($_post_id);
 861			unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
 862		}
 863	}
 864
 865	if ( wp_defer_comment_counting() ) {
 866		$_deferred[] = $post_id;
 867		return true;
 868	}
 869	elseif ( $post_id ) {
 870		return wp_update_comment_count_now($post_id);
 871	}
 872
 873}
 874
 875/**
 876 * wp_update_comment_count_now() - Updates the comment count for the post
 877 *
 878 * @since 2.5
 879 * @uses $wpdb
 880 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
 881 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
 882 *
 883 * @param int $post_id Post ID
 884 * @return bool False on '0' $post_id or if post with ID does not exist. True on success.
 885 */
 886function wp_update_comment_count_now($post_id) {
 887	global $wpdb;
 888	$post_id = (int) $post_id;
 889	if ( !$post_id )
 890		return false;
 891	if ( !$post = get_post($post_id) )
 892		return false;
 893
 894	$old = (int) $post->comment_count;
 895	$new = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
 896	$wpdb->query("UPDATE $wpdb->posts SET comment_count = '$new' WHERE ID = '$post_id'");
 897
 898	if ( 'page' == $post->post_type )
 899		clean_page_cache( $post_id );
 900	else
 901		clean_post_cache( $post_id );
 902
 903	do_action('wp_update_comment_count', $post_id, $new, $old);
 904	do_action('edit_post', $post_id, $post);
 905
 906	return true;
 907}
 908
 909//
 910// Ping and trackback functions.
 911//
 912
 913/**
 914 * discover_pingback_server_uri() - Finds a pingback server URI based on the given URL
 915 *
 916 * {@internal Missing Long Description}}
 917 *
 918 * @since 1.5.0
 919 * @uses $wp_version
 920 *
 921 * @param string $url URL to ping
 922 * @param int $timeout_bytes Number of bytes to timeout at. Prevents big file downloads, default is 2048.
 923 * @return bool|string False on failure, string containing URI on success.
 924 */
 925function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
 926	global $wp_version;
 927
 928	$byte_count = 0;
 929	$contents = '';
 930	$headers = '';
 931	$pingback_str_dquote = 'rel="pingback"';
 932	$pingback_str_squote = 'rel=\'pingback\'';
 933	$x_pingback_str = 'x-pingback: ';
 934
 935	extract(parse_url($url), EXTR_SKIP);
 936
 937	if ( !isset($host) ) // Not an URL. This should never happen.
 938		return false;
 939
 940	$path  = ( !isset($path) ) ? '/'          : $path;
 941	$path .= ( isset($query) ) ? '?' . $query : '';
 942	$port  = ( isset($port)  ) ? $port        : 80;
 943
 944	// Try to connect to the server at $host
 945	$fp = @fsockopen($host, $port, $errno, $errstr, 2);
 946	if ( !$fp ) // Couldn't open a connection to $host
 947		return false;
 948
 949	// Send the GET request
 950	$request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
 951	// ob_end_flush();
 952	fputs($fp, $request);
 953
 954	// Let's check for an X-Pingback header first
 955	while ( !feof($fp) ) {
 956		$line = fgets($fp, 512);
 957		if ( trim($line) == '' )
 958			break;
 959		$headers .= trim($line)."\n";
 960		$x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
 961		if ( $x_pingback_header_offset ) {
 962			// We got it!
 963			preg_match('#x-pingback: (.+)#is', $headers, $matches);
 964			$pingback_server_url = trim($matches[1]);
 965			return $pingback_server_url;
 966		}
 967		if ( strpos(strtolower($headers), 'content-type: ') ) {
 968			preg_match('#content-type: (.+)#is', $headers, $matches);
 969			$content_type = trim($matches[1]);
 970		}
 971	}
 972
 973	if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
 974		return false;
 975
 976	while ( !feof($fp) ) {
 977		$line = fgets($fp, 1024);
 978		$contents .= trim($line);
 979		$pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
 980		$pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
 981		if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
 982			$quote = ($pingback_link_offset_dquote) ? '"' : '\'';
 983			$pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
 984			$pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
 985			$pingback_href_start = $pingback_href_pos+6;
 986			$pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
 987			$pingback_server_url_len = $pingback_href_end - $pingback_href_start;
 988			$pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
 989			// We may find rel="pingback" but an incomplete pingback URL
 990			if ( $pingback_server_url_len > 0 ) // We got it!
 991				return $pingback_server_url;
 992		}
 993		$byte_count += strlen($line);
 994		if ( $byte_count > $timeout_bytes ) {
 995			// It's no use going further, there probably isn't any pingback
 996			// server to find in this file. (Prevents loading large files.)
 997			return false;
 998		}
 999	}
1000
1001	// We didn't find anything.
1002	return false;
1003}
1004
1005/**
1006 * do_all_pings() - {@internal Missing Short Description}}
1007 *
1008 * {@internal Missing Long Description}}
1009 *
1010 * @since 2.1.0
1011 * @uses $wpdb
1012 */
1013function do_all_pings() {
1014	global $wpdb;
1015
1016	// Do pingbacks
1017	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")) {
1018		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1019		pingback($ping->post_content, $ping->ID);
1020	}
1021
1022	// Do Enclosures
1023	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")) {
1024		$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
1025		do_enclose($enclosure->post_content, $enclosure->ID);
1026	}
1027
1028	// Do Trackbacks
1029	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
1030	if ( is_array($trackbacks) )
1031		foreach ( $trackbacks as $trackback )
1032			do_trackbacks($trackback);
1033
1034	//Do Update Services/Generic Pings
1035	generic_ping();
1036}
1037
1038/**
1039 * do_trackbacks() - {@internal Missing Short Description}}
1040 *
1041 * {@internal Missing Long Description}}
1042 *
1043 * @since 1.5.0
1044 * @uses $wpdb
1045 *
1046 * @param int $post_id Post ID to do trackbacks on
1047 */
1048function do_trackbacks($post_id) {
1049	global $wpdb;
1050
1051	$post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
1052	$to_ping = get_to_ping($post_id);
1053	$pinged  = get_pung($post_id);
1054	if ( empty($to_ping) ) {
1055		$wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
1056		return;
1057	}
1058
1059	if ( empty($post->post_excerpt) )
1060		$excerpt = apply_filters('the_content', $post->post_content);
1061	else
1062		$excerpt = apply_filters('the_excerpt', $post->post_excerpt);
1063	$excerpt = str_replace(']]>', ']]&gt;', $excerpt);
1064	$excerpt = wp_html_excerpt($excerpt, 252) . '...';
1065
1066	$post_title = apply_filters('the_title', $post->post_title);
1067	$post_title = strip_tags($post_title);
1068
1069	if ( $to_ping ) {
1070		foreach ( (array) $to_ping as $tb_ping ) {
1071			$tb_ping = trim($tb_ping);
1072			if ( !in_array($tb_ping, $pinged) ) {
1073				trackback($tb_ping, $post_title, $excerpt, $post_id);
1074				$pinged[] = $tb_ping;
1075			} else {
1076				$wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
1077			}
1078		}
1079	}
1080}
1081
1082/**
1083 * generic_ping() - {@internal Missing Short Description}}
1084 *
1085 * {@internal Missing Long Description}}
1086 *
1087 * @since 1.2.0
1088 *
1089 * @param int $post_id Post ID. Not actually used.
1090 * @return int Same as Post ID from parameter
1091 */
1092function generic_ping($post_id = 0) {
1093	$services = get_option('ping_sites');
1094
1095	$services = explode("\n", $services);
1096	foreach ( (array) $services as $service ) {
1097		$service = trim($service);
1098		if ( '' != $service )
1099			weblog_ping($service);
1100	}
1101
1102	return $post_id;
1103}
1104
1105/**
1106 * pingback() - Pings back the links found in a post
1107 *
1108 * {@internal Missing Long Description}}
1109 *
1110 * @since 0.71
1111 * @uses $wp_version
1112 * @uses IXR_Client
1113 *
1114 * @param string $content {@internal Missing Description}}
1115 * @param int $post_ID {@internal Missing Description}}
1116 */
1117function pingback($content, $post_ID) {
1118	global $wp_version;
1119	include_once(ABSPATH . WPINC . '/class-IXR.php');
1120
1121	// original code by Mort (http://mort.mine.nu:8080)
1122	$post_links = array();
1123
1124	$pung = get_pung($post_ID);
1125
1126	// Variables
1127	$ltrs = '\w';
1128	$gunk = '/#~:.?+=&%@!\-';
1129	$punc = '.:?\-';
1130	$any = $ltrs . $gunk . $punc;
1131
1132	// Step 1
1133	// Parsing the post, external links (if any) are stored in the $post_links array
1134	// This regexp comes straight from phpfreaks.com
1135	// http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
1136	preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
1137
1138	// Step 2.
1139	// Walking thru the links array
1140	// first we get rid of links pointing to sites, not to specific files
1141	// Example:
1142	// http://dummy-weblog.org
1143	// http://dummy-weblog.org/
1144	// http://dummy-weblog.org/post.php
1145	// We don't wanna ping first and second types, even if they have a valid <link/>
1146
1147	foreach ( $post_links_temp[0] as $link_test ) :
1148		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
1149				&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
1150			$test = parse_url($link_test);
1151			if ( isset($test['query']) )
1152				$post_links[] = $link_test;
1153			elseif ( ($test['path'] != '/') && ($test['path'] != '') )
1154				$post_links[] = $link_test;
1155		endif;
1156	endforeach;
1157
1158	do_action_ref_array('pre_ping', array(&$post_links, &$pung));
1159
1160	foreach ( (array) $post_links as $pagelinkedto ) {
1161		$pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
1162
1163		if ( $pingback_server_url ) {
1164			@ set_time_limit( 60 );
1165			 // Now, the RPC call
1166			$pagelinkedfrom = get_permalink($post_ID);
1167
1168			// using a timeout of 3 seconds should be enough to cover slow servers
1169			$client = new IXR_Client($pingback_server_url);
1170			$client->timeout = 3;
1171			$client->useragent .= ' -- WordPress/' . $wp_version;
1172
1173			// when set to true, this outputs debug messages by itself
1174			$client->debug = false;
1175
1176			if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
1177				add_ping( $post_ID, $pagelinkedto );
1178		}
1179	}
1180}
1181
1182/**
1183 * privacy_ping_filter() - {@internal Missing Short Description}}
1184 *
1185 * {@internal Missing Long Description}}
1186 *
1187 * @since 2.1.0
1188 *
1189 * @param unknown_type $sites {@internal Missing Description}}
1190 * @return unknown {@internal Missing Description}}
1191 */
1192function privacy_ping_filter($sites) {
1193	if ( '0' != get_option('blog_public') )
1194		return $sites;
1195	else
1196		return '';
1197}
1198
1199/**
1200 * trackback() - Send a Trackback
1201 *
1202 * {@internal Missing Long Description}}
1203 *
1204 * @since 0.71
1205 * @uses $wpdb
1206 * @uses $wp_version WordPress version
1207 *
1208 * @param string $trackback_url {@internal Missing Description}}
1209 * @param string $title {@internal Missing Description}}
1210 * @param string $excerpt {@internal Missing Description}}
1211 * @param int $ID {@internal Missing Description}}
1212 * @return unknown {@internal Missing Description}}
1213 */
1214function trackback($trackback_url, $title, $excerpt, $ID) {
1215	global $wpdb, $wp_version;
1216
1217	if ( empty($trackback_url) )
1218		return;
1219
1220	$title = urlencode($title);
1221	$excerpt = urlencode($excerpt);
1222	$blog_name = urlencode(get_option('blogname'));
1223	$tb_url = $trackback_url;
1224	$url = urlencode(get_permalink($ID));
1225	$query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
1226	$trackback_url = parse_url($trackback_url);
1227	$http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
1228	$http_request .= 'Host: '.$trackback_url['host']."\r\n";
1229	$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
1230	$http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
1231	$http_request .= "User-Agent: WordPress/" . $wp_version;
1232	$http_request .= "\r\n\r\n";
1233	$http_request .= $query_string;
1234	if ( '' == $trackback_url['port'] )
1235		$trackback_url['port'] = 80;
1236	$fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
1237	@fputs($fs, $http_request);
1238	@fclose($fs);
1239
1240	$tb_url = addslashes( $tb_url );
1241	$wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
1242	return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
1243}
1244
1245/**
1246 * weblog_ping() - {@internal Missing Short Description}}
1247 *
1248 * {@internal Missing Long Description}}
1249 *
1250 * @since 1.2.0
1251 * @uses $wp_version
1252 * @uses IXR_Client
1253 *
1254 * @param unknown_type $server
1255 * @param unknown_type $path
1256 */
1257function weblog_ping($server = '', $path = '') {
1258	global $wp_version;
1259	include_once(ABSPATH . WPINC . '/class-IXR.php');
1260
1261	// using a timeout of 3 seconds should be enough to cover slow servers
1262	$client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1263	$client->timeout = 3;
1264	$client->useragent .= ' -- WordPress/'.$wp_version;
1265
1266	// when set to true, this outputs debug messages by itself
1267	$client->debug = false;
1268	$home = trailingslashit( get_option('home') );
1269	if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
1270		$client->query('weblogUpdates.ping', get_option('blogname'), $home);
1271}
1272
1273//
1274// Cache
1275//
1276
1277/**
1278 * clean_comment_cache() - Removes comment ID from the comment cache
1279 *
1280 * @since 2.3.0
1281 * @package WordPress
1282 * @subpackage Cache
1283 *
1284 * @param int $id Comment ID to remove from cache
1285 */
1286function clean_comment_cache($id) {
1287	wp_cache_delete($id, 'comment');
1288}
1289
1290/**
1291 * update_comment_cache() - Updates the comment cache of given comments
1292 *
1293 * Will add the comments in $comments to the cache. If comment ID already
1294 * exists in the comment cache then it will not be updated.
1295 *
1296 * The comment is added to the cache using the comment group with the key
1297 * using the ID of the comments.
1298 *
1299 * @since 2.3.0
1300 *
1301 * @param array $comments Array of comment row objects
1302 */
1303function update_comment_cache($comments) {
1304	foreach ( (array) $comments as $comment )
1305		wp_cache_add($comment->comment_ID, $comment, 'comment');
1306}
1307
1308?>