PageRenderTime 70ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/site/blog/wp-includes/comment.php

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