BBP_Akismet

Loads Akismet extension.

Defined (1)

The class is defined in the following location(s).

/includes/extend/akismet.php  
  1. class BBP_Akismet { 
  2.  
  3. /** 
  4. * The main bbPress Akismet loader 
  5. * @since bbPress (r3277) 
  6. * @uses add_filter() 
  7. */ 
  8. public function __construct() { 
  9. $this->setup_actions(); 
  10.  
  11. /** 
  12. * Setup the admin hooks 
  13. * @since bbPress (r3277) 
  14. * @access private 
  15. * @uses add_filter() To add various filters 
  16. * @uses add_action() To add various actions 
  17. */ 
  18. private function setup_actions() { 
  19.  
  20. // Prevent debug notices 
  21. $checks = array(); 
  22.  
  23. // bbPress functions to check for spam 
  24. $checks['check'] = array( 
  25. 'bbp_new_topic_pre_insert' => 1, // New topic check 
  26. 'bbp_new_reply_pre_insert' => 1, // New reply check 
  27. 'bbp_edit_topic_pre_insert' => 1, // Edit topic check 
  28. 'bbp_edit_reply_pre_insert' => 1 // Edit reply check 
  29. ); 
  30.  
  31. // bbPress functions for spam and ham submissions 
  32. $checks['submit'] = array( 
  33. 'bbp_spammed_topic' => 10, // Spammed topic 
  34. 'bbp_unspammed_topic' => 10, // Unspammed reply 
  35. 'bbp_spammed_reply' => 10, // Spammed reply 
  36. 'bbp_unspammed_reply' => 10, // Unspammed reply 
  37. ); 
  38.  
  39. // Add the checks 
  40. foreach ( $checks as $type => $functions ) 
  41. foreach ( $functions as $function => $priority ) 
  42. add_filter( $function, array( $this, $type . '_post' ), $priority ); 
  43.  
  44. // Update post meta 
  45. add_action( 'wp_insert_post', array( $this, 'update_post_meta' ), 10, 2 ); 
  46.  
  47. // Admin 
  48. if ( is_admin() ) { 
  49. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); 
  50.  
  51. /** 
  52. * Converts topic/reply data into Akismet comment checking format 
  53. * @since bbPress (r3277) 
  54. * @param string $post_data 
  55. * @uses get_userdata() To get the user data 
  56. * @uses bbp_filter_anonymous_user_data() To get anonymous user data 
  57. * @uses bbp_get_topic_permalink() To get the permalink of the topic 
  58. * @uses bbp_get_reply_url() To get the permalink of the reply 
  59. * @uses bbp_current_author_ip() To get the IP address of the current user 
  60. * @uses BBP_Akismet::maybe_spam() To check if post is spam 
  61. * @uses BBP_Akismet::get_user_roles() To get the role(s) of the current user 
  62. * @uses do_action() To call the 'bbp_akismet_spam_caught' hook 
  63. * @uses add_filter() To call the 'bbp_new_reply_pre_set_terms' hook 
  64. * @return array Array of post data 
  65. */ 
  66. public function check_post( $post_data ) { 
  67.  
  68. // Define local variables 
  69. $user_data = array(); 
  70. $post_permalink = ''; 
  71.  
  72. // Post is not published 
  73. if ( bbp_get_public_status_id() !== $post_data['post_status'] ) 
  74. return $post_data; 
  75.  
  76. // Cast the post_author to 0 if it's empty 
  77. if ( empty( $post_data['post_author'] ) ) 
  78. $post_data['post_author'] = 0; 
  79.  
  80. /** Author ************************************************************/ 
  81.  
  82. // Get user data 
  83. $userdata = get_userdata( $post_data['post_author'] ); 
  84. $anonymous_data = bbp_filter_anonymous_post_data(); 
  85.  
  86. // Author is anonymous 
  87. if ( !empty( $anonymous_data ) ) { 
  88. $user_data['name'] = $anonymous_data['bbp_anonymous_name']; 
  89. $user_data['email'] = $anonymous_data['bbp_anonymous_email']; 
  90. $user_data['website'] = $anonymous_data['bbp_anonymous_website']; 
  91.  
  92. // Author is logged in 
  93. } elseif ( !empty( $userdata ) ) { 
  94. $user_data['name'] = $userdata->display_name; 
  95. $user_data['email'] = $userdata->user_email; 
  96. $user_data['website'] = $userdata->user_url; 
  97.  
  98. // Missing author data, so set some empty strings 
  99. } else { 
  100. $user_data['name'] = ''; 
  101. $user_data['email'] = ''; 
  102. $user_data['website'] = ''; 
  103.  
  104. /** Post **************************************************************/ 
  105.  
  106. // Use post parent for permalink 
  107. if ( !empty( $post_data['post_parent'] ) ) 
  108. $post_permalink = get_permalink( $post_data['post_parent'] ); 
  109.  
  110. // Put post_data back into usable array 
  111. $_post = array( 
  112. 'comment_author' => $user_data['name'],  
  113. 'comment_author_email' => $user_data['email'],  
  114. 'comment_author_url' => $user_data['website'],  
  115. 'comment_content' => $post_data['post_content'],  
  116. 'comment_post_ID' => $post_data['post_parent'],  
  117. 'comment_type' => $post_data['post_type'],  
  118. 'permalink' => $post_permalink,  
  119. 'referrer' => $_SERVER['HTTP_REFERER'],  
  120. 'user_agent' => $_SERVER['HTTP_USER_AGENT'],  
  121. 'user_ID' => $post_data['post_author'],  
  122. 'user_ip' => bbp_current_author_ip(),  
  123. 'user_role' => $this->get_user_roles( $post_data['post_author'] ),  
  124. ); 
  125.  
  126. // Check the post_data 
  127. $_post = $this->maybe_spam( $_post ); 
  128.  
  129. // Get the result 
  130. $post_data['bbp_akismet_result'] = $_post['bbp_akismet_result']; 
  131. unset( $_post['bbp_akismet_result'] ); 
  132.  
  133. // Store the data as submitted 
  134. $post_data['bbp_post_as_submitted'] = $_post; 
  135.  
  136. // Allow post_data to be manipulated 
  137. do_action_ref_array( 'bbp_akismet_check_post', $post_data ); 
  138.  
  139. // Spam 
  140. if ( 'true' === $post_data['bbp_akismet_result'] ) { 
  141.  
  142. // Let plugins do their thing 
  143. do_action( 'bbp_akismet_spam_caught' ); 
  144.  
  145. // This is spam 
  146. $post_data['post_status'] = bbp_get_spam_status_id(); 
  147.  
  148. // We don't want your spam tags here 
  149. add_filter( 'bbp_new_reply_pre_set_terms', array( $this, 'filter_post_terms' ), 1, 3 ); 
  150.  
  151. // @todo Spam counter? 
  152.  
  153. // @todo Topic/reply moderation? No true/false response - 'pending' or 'draft' 
  154. // @todo Auto-delete old spam? 
  155.  
  156. // Log the last post 
  157. $this->last_post = $post_data; 
  158.  
  159. // Pass the data back to the filter 
  160. return $post_data; 
  161.  
  162. /** 
  163. * Submit a post for spamming or hamming 
  164. * @since bbPress (r3277) 
  165. * @param int $post_id 
  166. * @global WP_Query $wpdb 
  167. * @global string $akismet_api_host 
  168. * @global string $akismet_api_port 
  169. * @global object $current_user 
  170. * @global object $current_site 
  171. * @uses current_filter() To get the reply_id 
  172. * @uses get_post() To get the post object 
  173. * @uses get_the_author_meta() To get the author meta 
  174. * @uses get_post_meta() To get the post meta 
  175. * @uses bbp_get_user_profile_url() To get a user's profile url 
  176. * @uses get_permalink() To get the permalink of the post_parent 
  177. * @uses BBP_Akismet::get_user_roles() To get the role(s) of the post_author 
  178. * @uses bbp_current_author_ip() To get the IP address of the current user 
  179. * @uses BBP_Akismet::maybe_spam() To submit the post as ham or spam 
  180. * @uses update_post_meta() To update the post meta with some Akismet data 
  181. * @uses do_action() To call the 'bbp_akismet_submit_spam_post' and 'bbp_akismet_submit_ham_post' hooks 
  182. * @return array Array of existing topic terms 
  183. */ 
  184. public function submit_post( $post_id = 0 ) { 
  185. global $current_user, $current_site; 
  186.  
  187. // Innocent until proven guilty 
  188. $request_type = 'ham'; 
  189. $current_filter = current_filter(); 
  190.  
  191. // Check this filter and adjust the $request_type accordingly 
  192. switch ( $current_filter ) { 
  193.  
  194. // Mysterious, and straight from the can 
  195. case 'bbp_spammed_topic' : 
  196. case 'bbp_spammed_reply' : 
  197. $request_type = 'spam'; 
  198. break; 
  199.  
  200. // Honey-glazed, a straight off the bone 
  201. case 'bbp_unspammed_topic' : 
  202. case 'bbp_unspammed_reply' : 
  203. $request_type = 'ham'; 
  204. break; 
  205.  
  206. // Possibly poison... 
  207. default : 
  208. return; 
  209.  
  210. // Setup some variables 
  211. $post_id = (int) $post_id; 
  212.  
  213. // Make sure we have a post 
  214. $_post = get_post( $post_id ); 
  215.  
  216. // Bail if get_post() fails 
  217. if ( empty( $_post ) ) 
  218. return; 
  219.  
  220. // Bail if we're spamming, but the post_status isn't spam 
  221. if ( ( 'spam' === $request_type ) && ( bbp_get_spam_status_id() !== $_post->post_status ) ) 
  222. return; 
  223.  
  224. // Set some default post_data 
  225. $post_data = array( 
  226. 'comment_approved' => $_post->post_status,  
  227. 'comment_author' => $_post->post_author ? get_the_author_meta( 'display_name', $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_name', true ),  
  228. 'comment_author_email' => $_post->post_author ? get_the_author_meta( 'email', $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_email', true ),  
  229. 'comment_author_url' => $_post->post_author ? bbp_get_user_profile_url( $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_website', true ),  
  230. 'comment_content' => $_post->post_content,  
  231. 'comment_date' => $_post->post_date,  
  232. 'comment_ID' => $post_id,  
  233. 'comment_post_ID' => $_post->post_parent,  
  234. 'comment_type' => $_post->post_type,  
  235. 'permalink' => get_permalink( $post_id ),  
  236. 'user_ID' => $_post->post_author,  
  237. 'user_ip' => get_post_meta( $post_id, '_bbp_author_ip', true ),  
  238. 'user_role' => $this->get_user_roles( $_post->post_author ),  
  239. ); 
  240.  
  241. // Use the original version stored in post_meta if available 
  242. $as_submitted = get_post_meta( $post_id, '_bbp_akismet_as_submitted', true ); 
  243. if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) 
  244. $post_data = array_merge( $post_data, $as_submitted ); 
  245.  
  246. // Add the reporter IP address 
  247. $post_data['reporter_ip'] = bbp_current_author_ip(); 
  248.  
  249. // Add some reporter info 
  250. if ( is_object( $current_user ) ) 
  251. $post_data['reporter'] = $current_user->user_login; 
  252.  
  253. // Add the current site domain 
  254. if ( is_object( $current_site ) ) 
  255. $post_data['site_domain'] = $current_site->domain; 
  256.  
  257. // Place your slide beneath the microscope 
  258. $post_data = $this->maybe_spam( $post_data, 'submit', $request_type ); 
  259.  
  260. // Manual user action 
  261. if ( isset( $post_data['reporter'] ) ) { 
  262.  
  263. // What kind of action 
  264. switch ( $request_type ) { 
  265.  
  266. // Spammy 
  267. case 'spam' : 
  268. $this->update_post_history( $post_id, sprintf( esc_html__( '%1$s reported this %2$s as spam', 'bbpress' ), $post_data['reporter'], $post_data['comment_type'] ), 'report-spam' ); 
  269. update_post_meta( $post_id, '_bbp_akismet_user_result', 'true' ); 
  270. update_post_meta( $post_id, '_bbp_akismet_user', $post_data['reporter'] ); 
  271. break; 
  272.  
  273. // Hammy 
  274. case 'ham' : 
  275. $this->update_post_history( $post_id, sprintf( esc_html__( '%1$s reported this %2$s as not spam', 'bbpress' ), $post_data['reporter'], $post_data['comment_type'] ), 'report-ham' ); 
  276. update_post_meta( $post_id, '_bbp_akismet_user_result', 'false' ); 
  277. update_post_meta( $post_id, '_bbp_akismet_user', $post_data['reporter'] ); 
  278.  
  279. // @todo Topic term revision history 
  280. break; 
  281.  
  282. // Possible other actions 
  283. default : 
  284. break; 
  285.  
  286. do_action( 'bbp_akismet_submit_' . $request_type . '_post', $post_id, $post_data['bbp_akismet_result'] ); 
  287.  
  288. /** 
  289. * Ping Akismet service and check for spam/ham response 
  290. * @since bbPress (r3277) 
  291. * @param array $post_data 
  292. * @param string $check Accepts check|submit 
  293. * @param string $spam Accepts spam|ham 
  294. * @global string $akismet_api_host 
  295. * @global string $akismet_api_port 
  296. * @uses akismet_test_mode() To determine if Akismet is in test mode 
  297. * @uses akismet_http_post() To send data to the mothership for processing 
  298. * @return array Array of post data 
  299. */ 
  300. private function maybe_spam( $post_data, $check = 'check', $spam = 'spam' ) { 
  301. global $akismet_api_host, $akismet_api_port; 
  302.  
  303. // Define variables 
  304. $query_string = $path = $response = ''; 
  305.  
  306. // Populate post data 
  307. $post_data['blog'] = get_option( 'home' ); 
  308. $post_data['blog_charset'] = get_option( 'blog_charset' ); 
  309. $post_data['blog_lang'] = get_locale(); 
  310. $post_data['referrer'] = $_SERVER['HTTP_REFERER']; 
  311. $post_data['user_agent'] = $_SERVER['HTTP_USER_AGENT']; 
  312.  
  313. // Akismet Test Mode 
  314. if ( akismet_test_mode() ) 
  315. $post_data['is_test'] = 'true'; 
  316.  
  317. // Loop through _POST args and rekey strings 
  318. foreach ( $_POST as $key => $value ) 
  319. if ( is_string( $value ) ) 
  320. $post_data['POST_' . $key] = $value; 
  321.  
  322. // Keys to ignore 
  323. $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); 
  324.  
  325. // Loop through _SERVER args and remove whitelisted keys 
  326. foreach ( $_SERVER as $key => $value ) { 
  327.  
  328. // Key should not be ignored 
  329. if ( !in_array( $key, $ignore ) && is_string( $value ) ) { 
  330. $post_data[$key] = $value; 
  331.  
  332. // Key should be ignored 
  333. } else { 
  334. $post_data[$key] = ''; 
  335.  
  336. // Ready... 
  337. foreach ( $post_data as $key => $data ) 
  338. $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&'; 
  339.  
  340. // Aim... 
  341. if ( 'check' === $check ) { 
  342. $path = '/1.1/comment-check'; 
  343. } elseif ( 'submit' === $check ) { 
  344. $path = '/1.1/submit-' . $spam; 
  345.  
  346. // Fire! 
  347. $response = $this->http_post( $query_string, $akismet_api_host, $path, $akismet_api_port ); 
  348.  
  349. // Check the high-speed cam 
  350. if ( !empty( $response[1] ) ) { 
  351. $post_data['bbp_akismet_result'] = $response[1]; 
  352. } else { 
  353. $post_data['bbp_akismet_result'] = esc_html__( 'No response', 'bbpress' ); 
  354.  
  355. // This is ham 
  356. return $post_data; 
  357.  
  358. /** 
  359. * Update post meta after a spam check 
  360. * @since bbPress (r3308) 
  361. * @param int $post_id 
  362. * @param object $_post 
  363. * @global object $this->last_post 
  364. * @uses get_post() To get the post object 
  365. * @uses get_userdata() To get the user data 
  366. * @uses bbp_filter_anonymous_user_data() To get anonymous user data 
  367. * @uses update_post_meta() To update post meta with Akismet data 
  368. * @uses BBP_Akismet::update_post_history() To update post Akismet history 
  369. */ 
  370. public function update_post_meta( $post_id = 0, $_post = false ) { 
  371.  
  372. // Define local variable(s) 
  373. $as_submitted = false; 
  374.  
  375. // Setup some variables 
  376. $post_id = (int) $post_id; 
  377.  
  378. // Ensure we have a post object 
  379. if ( empty( $_post ) ) 
  380. $_post = get_post( $post_id ); 
  381.  
  382. // Set up Akismet last post data 
  383. if ( !empty( $this->last_post ) ) 
  384. $as_submitted = $this->last_post['bbp_post_as_submitted']; 
  385.  
  386. // wp_insert_post() might be called in other contexts. Ensure this is 
  387. // the same topic/reply as was checked by BBP_Akismet::check_post() 
  388. if ( is_object( $_post ) && !empty( $this->last_post ) && is_array( $as_submitted ) ) { 
  389.  
  390. // Get user data 
  391. $userdata = get_userdata( $_post->post_author ); 
  392. $anonymous_data = bbp_filter_anonymous_post_data(); 
  393.  
  394. // More checks 
  395. if ( intval( $as_submitted['comment_post_ID'] ) === intval( $_post->post_parent ) 
  396. && $as_submitted['comment_author'] === ( $anonymous_data ? $anonymous_data['bbp_anonymous_name'] : $userdata->display_name ) 
  397. && $as_submitted['comment_author_email'] === ( $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email ) 
  398. ) { 
  399.  
  400. // Normal result: true 
  401. if ( $this->last_post['bbp_akismet_result'] === 'true' ) { 
  402.  
  403. // Leave a trail so other's know what we did 
  404. update_post_meta( $post_id, '_bbp_akismet_result', 'true' ); 
  405. $this->update_post_history( $post_id, esc_html__( 'Akismet caught this post as spam', 'bbpress' ), 'check-spam' ); 
  406.  
  407. // If post_status isn't the spam status, as expected, leave a note 
  408. if ( bbp_get_spam_status_id() !== $_post->post_status ) { 
  409. $this->update_post_history( $post_id, sprintf( esc_html__( 'Post status was changed to %s', 'bbpress' ), $_post->post_status ), 'status-changed-' . $_post->post_status ); 
  410.  
  411. // Normal result: false 
  412. } elseif ( $this->last_post['bbp_akismet_result'] === 'false' ) { 
  413.  
  414. // Leave a trail so other's know what we did 
  415. update_post_meta( $post_id, '_bbp_akismet_result', 'false' ); 
  416. $this->update_post_history( $post_id, esc_html__( 'Akismet cleared this post as not spam', 'bbpress' ), 'check-ham' ); 
  417.  
  418. // If post_status is the spam status, which isn't expected, leave a note 
  419. if ( bbp_get_spam_status_id() === $_post->post_status ) { 
  420.  
  421. // @todo Use wp_blacklist_check() 
  422.  
  423. $this->update_post_history( $post_id, sprintf( esc_html__( 'Post status was changed to %s', 'bbpress' ), $_post->post_status ), 'status-changed-' . $_post->post_status ); 
  424.  
  425. // Abnormal result: error 
  426. } else { 
  427. // Leave a trail so other's know what we did 
  428. update_post_meta( $post_id, '_bbp_akismet_error', time() ); 
  429. $this->update_post_history( $post_id, sprintf( esc_html__( 'Akismet was unable to check this post (response: %s), will automatically retry again later.', 'bbpress' ), $this->last_post['bbp_akismet_result'] ), 'check-error' ); 
  430.  
  431. // Record the complete original data as submitted for checking 
  432. if ( isset( $this->last_post['bbp_post_as_submitted'] ) ) { 
  433. update_post_meta( $post_id, '_bbp_akismet_as_submitted', $this->last_post['bbp_post_as_submitted'] ); 
  434.  
  435. /** 
  436. * Update a post's Akismet history 
  437. * @since bbPress (r3308) 
  438. * @param int $post_id 
  439. * @param string $message 
  440. * @param string $event 
  441. * @uses wp_get_current_user() To get the current_user object 
  442. * @uses add_post_meta() Add Akismet post history 
  443. */ 
  444. private function update_post_history( $post_id = 0, $message = null, $event = null ) { 
  445.  
  446. // Define local variable(s) 
  447. $user = ''; 
  448.  
  449. // Get the current user 
  450. $current_user = wp_get_current_user(); 
  451.  
  452. // Get the user's login name if possible 
  453. if ( is_object( $current_user ) && isset( $current_user->user_login ) ) 
  454. $user = $current_user->user_login; 
  455.  
  456. // Setup the event to be saved 
  457. $event = array( 
  458. 'time' => akismet_microtime(),  
  459. 'message' => $message,  
  460. 'event' => $event,  
  461. 'user' => $user,  
  462. ); 
  463.  
  464. // Save the event data 
  465. add_post_meta( $post_id, '_bbp_akismet_history', $event ); 
  466.  
  467. /** 
  468. * Get a post's Akismet history 
  469. * @since bbPress (r3308) 
  470. * @param int $post_id 
  471. * @uses wp_get_current_user() To get the current_user object 
  472. * @uses get_post_meta() Get a post's Akismet history 
  473. * @return array Array of a post's Akismet history 
  474. */ 
  475. public function get_post_history( $post_id = 0 ) { 
  476.  
  477. // Retrieve any previous history 
  478. $history = get_post_meta( $post_id, '_bbp_akismet_history' ); 
  479.  
  480. // Sort it by the time recorded 
  481. usort( $history, 'akismet_cmp_time' ); 
  482.  
  483. return $history; 
  484.  
  485. /** 
  486. * Handle any terms submitted with a post flagged as spam 
  487. * @since bbPress (r3308) 
  488. * @param string $terms Comma-separated list of terms 
  489. * @param int $topic_id 
  490. * @param int $reply_id 
  491. * @uses bbp_get_reply_id() To get the reply_id 
  492. * @uses bbp_get_topic_id() To get the topic_id 
  493. * @uses wp_get_object_terms() To a post's current terms 
  494. * @uses update_post_meta() To add spam terms to post meta 
  495. * @return array Array of existing topic terms 
  496. */ 
  497. public function filter_post_terms( $terms = '', $topic_id = 0, $reply_id = 0 ) { 
  498.  
  499. // Validate the reply_id and topic_id 
  500. $reply_id = bbp_get_reply_id( $reply_id ); 
  501. $topic_id = bbp_get_topic_id( $topic_id ); 
  502.  
  503. // Get any pre-existing terms 
  504. $existing_terms = wp_get_object_terms( $topic_id, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) ); 
  505.  
  506. // Save the terms for later in case the reply gets hammed 
  507. if ( !empty( $terms ) ) 
  508. update_post_meta( $reply_id, '_bbp_akismet_spam_terms', $terms ); 
  509.  
  510. // Keep the topic tags the same for now 
  511. return $existing_terms; 
  512.  
  513. /** 
  514. * Submit data to Akismet service with unique bbPress User Agent 
  515. * This code is directly taken from the akismet_http_post() function and 
  516. * documented to bbPress 2.0 standard. 
  517. * @since bbPress (r3466) 
  518. * @param string $request The request we are sending 
  519. * @param string $host The host to send our request to 
  520. * @param string $path The path from the host 
  521. * @param string $port The port to use 
  522. * @param string $ip Optional Override $host with an IP address 
  523. * @uses bbp_get_version() To get the current bbPress version 
  524. * @return mixed WP_Error on error, array on success, empty on failure 
  525. */ 
  526. private function http_post( $request, $host, $path, $port = 80, $ip = '' ) { 
  527.  
  528. // Preload required variables 
  529. $bbp_version = bbp_get_version(); 
  530. $content_length = strlen( $request ); 
  531. $http_host = $host; 
  532. $blog_charset = get_option( 'blog_charset' ); 
  533. $response = ''; 
  534. $errno = null; 
  535. $errstr = null; 
  536.  
  537. // Untque User Agent 
  538. $akismet_ua = "bbPress/{$bbp_version} | "; 
  539. $akismet_ua .= 'Akismet/' . constant( 'AKISMET_VERSION' ); 
  540.  
  541. // Use specific IP (if provided) 
  542. if ( !empty( $ip ) && long2ip( ip2long( $ip ) ) ) 
  543. $http_host = $ip; 
  544.  
  545. // WP HTTP class is available 
  546. if ( function_exists( 'wp_remote_post' ) ) { 
  547.  
  548. // Setup the arguments 
  549. $http_args = array( 
  550. 'body' => $request,  
  551. 'headers' => array( 
  552. 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $blog_charset,  
  553. 'Host' => $host,  
  554. 'User-Agent' => $akismet_ua 
  555. ),  
  556. 'httpversion' => '1.0',  
  557. 'timeout' => 15 
  558. ); 
  559.  
  560. // Where we are sending our request 
  561. $akismet_url = 'http://' . $http_host . $path; 
  562.  
  563. // Send the request 
  564. $response = wp_remote_post( $akismet_url, $http_args ); 
  565.  
  566. // Bail if the response is an error 
  567. if ( is_wp_error( $response ) ) 
  568. return ''; 
  569.  
  570. // No errors so return response 
  571. return array( $response['headers'], $response['body'] ); 
  572.  
  573. // WP HTTP class is not available (Why not?) 
  574. } else { 
  575.  
  576. // Header info to use with our socket 
  577. $http_request = "POST {$path} HTTP/1.0\r\n"; 
  578. $http_request .= "Host: {$host}\r\n"; 
  579. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset={$blog_charset}\r\n"; 
  580. $http_request .= "Content-Length: {$content_length}\r\n"; 
  581. $http_request .= "User-Agent: {$akismet_ua}\r\n"; 
  582. $http_request .= "\r\n"; 
  583. $http_request .= $request; 
  584.  
  585. // Open a socket connection 
  586. if ( false !== ( $fs = @fsockopen( $http_host, $port, $errno, $errstr, 10 ) ) ) { 
  587.  
  588. // Write our request to the pointer 
  589. fwrite( $fs, $http_request ); 
  590.  
  591. // Loop through pointer and compile a response 
  592. while ( !feof( $fs ) ) { 
  593. // One TCP-IP packet at a time 
  594. $response .= fgets( $fs, 1160 ); 
  595.  
  596. // Close our socket 
  597. fclose( $fs ); 
  598.  
  599. // Explode the response into usable data 
  600. $response = explode( "\r\n\r\n", $response, 2 ); 
  601.  
  602. // Return the response ('' if error/empty) 
  603. return $response; 
  604.  
  605. /** 
  606. * Return a user's roles on this site (including super_admin) 
  607. * @since bbPress (r4812) 
  608. * @param type $user_id 
  609. * @return boolean 
  610. */ 
  611. private function get_user_roles( $user_id = 0 ) { 
  612.  
  613. // Default return value 
  614. $roles = array(); 
  615.  
  616. // Bail if cannot query the user 
  617. if ( ! class_exists( 'WP_User' ) || empty( $user_id ) ) { 
  618. return false; 
  619.  
  620. // User ID 
  621. $user = new WP_User( $user_id ); 
  622. if ( isset( $user->roles ) ) { 
  623. $roles = (array) $user->roles; 
  624.  
  625. // Super admin 
  626. if ( is_multisite() && is_super_admin( $user_id ) ) { 
  627. $roles[] = 'super_admin'; 
  628.  
  629. return implode( ', ', $roles ); 
  630.  
  631. /** Admin *****************************************************************/ 
  632.  
  633. /** 
  634. * Add Aksimet History metaboxes to topics and replies 
  635. * @since bbPress (r5049) 
  636. */ 
  637. public function add_metaboxes() { 
  638.  
  639. // Topics 
  640. add_meta_box( 
  641. 'bbp_akismet_topic_history',  
  642. __( 'Akismet History', 'bbpress' ),  
  643. array( $this, 'history_metabox' ),  
  644. bbp_get_topic_post_type(),  
  645. 'normal',  
  646. 'core' 
  647. ); 
  648.  
  649. // Replies 
  650. add_meta_box( 
  651. 'bbp_akismet_reply_history',  
  652. __( 'Akismet History', 'bbpress' ),  
  653. array( $this, 'history_metabox' ),  
  654. bbp_get_reply_post_type(),  
  655. 'normal',  
  656. 'core' 
  657. ); 
  658.  
  659. /** 
  660. * Output for Akismet History metabox 
  661. * @since bbPress (r5049) 
  662. * @uses get_post_history() To get the Akismet history for the post 
  663. * @uses get_the_ID() To get the post ID 
  664. * @uses bbp_time_since() To get the human readable time 
  665. */ 
  666. public function history_metabox() { 
  667.  
  668. // Post ID 
  669. $history = $this->get_post_history( get_the_ID() ); ?> 
  670.  
  671. <div class="akismet-history" style="margin: 13px 0;"> 
  672.  
  673. <?php if ( !empty( $history ) ) : ?> 
  674.  
  675. <table> 
  676. <tbody> 
  677.  
  678. <?php foreach ( $history as $row ) : ?> 
  679.  
  680. <tr> 
  681. <td style="color: #999; text-align: right; white-space: nowrap;"> 
  682. <span title="<?php echo esc_attr( date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT' ); ?>"> 
  683. <?php bbp_time_since( $row['time'], false, true ); ?> 
  684. </span> 
  685. </td> 
  686. <td style="padding-left: 5px;"> 
  687. <?php echo esc_html( $row['message'] ); ?> 
  688. </td> 
  689. </tr> 
  690.  
  691. <?php endforeach; ?> 
  692. </tbody> 
  693. </table> 
  694.  
  695. <?php else : ?> 
  696.  
  697. <p><?php esc_html_e( 'No recorded history. Akismet has not checked this post.', 'bbpress' ); ?></p> 
  698.  
  699. <?php endif; ?> 
  700.  
  701. </div> 
  702.  
  703. <?php