PageRenderTime 74ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/comment.php

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