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