BP_Akismet

Akismet support for the Activity component.

Defined (1)

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

/bp-activity/classes/class-bp-akismet.php  
  1. class BP_Akismet { 
  2.  
  3. /** 
  4. * The activity last marked as spam. 
  5. * @since 1.6.0 
  6. * @var BP_Activity_Activity 
  7. */ 
  8. protected $last_activity = null; 
  9.  
  10. /** 
  11. * Constructor. 
  12. * @since 1.6.0 
  13. */ 
  14. public function __construct() { 
  15. $this->setup_actions(); 
  16.  
  17. /** 
  18. * Hook Akismet into the activity stream. 
  19. * @since 1.6.0 
  20. */ 
  21. protected function setup_actions() { 
  22. // Add nonces to activity stream lists. 
  23. add_action( 'bp_after_activity_post_form', array( $this, 'add_activity_stream_nonce' ) ); 
  24. add_action( 'bp_activity_entry_comments', array( $this, 'add_activity_stream_nonce' ) ); 
  25.  
  26. // Add a "mark as spam" button to individual activity items. 
  27. add_action( 'bp_activity_entry_meta', array( $this, 'add_activity_spam_button' ) ); 
  28. add_action( 'bp_activity_comment_options', array( $this, 'add_activity_comment_spam_button' ) ); 
  29.  
  30. // Check activity for spam. 
  31. add_action( 'bp_activity_before_save', array( $this, 'check_activity' ), 4, 1 ); 
  32.  
  33. // Tidy up member's latest (activity) update. 
  34. add_action( 'bp_activity_posted_update', array( $this, 'check_member_activity_update' ), 1, 3 ); 
  35.  
  36. // Hooks to extend Activity core spam/ham functions for Akismet. 
  37. add_action( 'bp_activity_mark_as_spam', array( $this, 'mark_as_spam' ), 10, 2 ); 
  38. add_action( 'bp_activity_mark_as_ham', array( $this, 'mark_as_ham' ), 10, 2 ); 
  39.  
  40. // Hook into the Activity wp-admin screen. 
  41. add_action( 'bp_activity_admin_comment_row_actions', array( $this, 'comment_row_action' ), 10, 2 ); 
  42. add_action( 'bp_activity_admin_load', array( $this, 'add_history_metabox' ) ); 
  43.  
  44. /** 
  45. * Add a history item to the hover links in an activity's row. 
  46. * This function lifted with love from the Akismet WordPress plugin's 
  47. * akismet_comment_row_action() function. Thanks! 
  48. * @since 1.6.0 
  49. * @param array $actions The hover links. 
  50. * @param array $activity The activity for the current row being processed. 
  51. * @return array The hover links. 
  52. */ 
  53. function comment_row_action( $actions, $activity ) { 
  54. $akismet_result = bp_activity_get_meta( $activity['id'], '_bp_akismet_result' ); 
  55. $user_result = bp_activity_get_meta( $activity['id'], '_bp_akismet_user_result' ); 
  56. $desc = ''; 
  57.  
  58. if ( !$user_result || $user_result == $akismet_result ) { 
  59. // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same. 
  60. if ( 'true' == $akismet_result && $activity['is_spam'] ) 
  61. $desc = __( 'Flagged as spam by Akismet', 'buddypress' ); 
  62.  
  63. elseif ( 'false' == $akismet_result && !$activity['is_spam'] ) 
  64. $desc = __( 'Cleared by Akismet', 'buddypress' ); 
  65.  
  66. } else { 
  67. $who = bp_activity_get_meta( $activity['id'], '_bp_akismet_user' ); 
  68.  
  69. if ( 'true' == $user_result ) 
  70. $desc = sprintf( __( 'Flagged as spam by %s', 'buddypress' ), $who ); 
  71. else 
  72. $desc = sprintf( __( 'Un-spammed by %s', 'buddypress' ), $who ); 
  73.  
  74. // Add a History item to the hover links, just after Edit. 
  75. if ( $akismet_result ) { 
  76. $b = array(); 
  77. foreach ( $actions as $k => $item ) { 
  78. $b[ $k ] = $item; 
  79. if ( $k == 'edit' ) 
  80. $b['history'] = '<a href="' . esc_url( bp_get_admin_url( 'admin.php?page=bp-activity&action=edit&aid=' . $activity['id'] ) ) . '#bp_activity_history"> '. __( 'History', 'buddypress' ) . '</a>'; 
  81.  
  82. $actions = $b; 
  83.  
  84. if ( $desc ) 
  85. echo '<span class="akismet-status"><a href="' . esc_url( bp_get_admin_url( 'admin.php?page=bp-activity&action=edit&aid=' . $activity['id'] ) ) . '#bp_activity_history">' . htmlspecialchars( $desc ) . '</a></span>'; 
  86.  
  87. /** 
  88. * Filters the list of actions for the current activity's row. 
  89. * @since 1.6.0 
  90. * @param array $actions Array of available actions for the current activity item's row. 
  91. */ 
  92. return apply_filters( 'bp_akismet_comment_row_action', $actions ); 
  93.  
  94. /** 
  95. * Generate nonces for activity forms. 
  96. * These nonces appear in the member profile status form, as well as in 
  97. * the reply form of each activity item. The nonces are, in turn, used 
  98. * by Akismet to help detect spam activity. 
  99. * @since 1.6.0 
  100. * @see https://plugins.trac.wordpress.org/ticket/1232 
  101. */ 
  102. public function add_activity_stream_nonce() { 
  103. $form_id = '_bp_as_nonce'; 
  104. $value = '_bp_as_nonce_' . bp_loggedin_user_id(); 
  105.  
  106. // If we're in the activity stream loop, we can use the current item's ID to make the nonce unique. 
  107. if ( 'bp_activity_entry_comments' == current_filter() ) { 
  108. $form_id .= '_' . bp_get_activity_id(); 
  109. $value .= '_' . bp_get_activity_id(); 
  110.  
  111. wp_nonce_field( $value, $form_id, false ); 
  112.  
  113. /** 
  114. * Clean up the bp_latest_update usermeta in case of spamming. 
  115. * Run just after an update is posted, this method check to see whether 
  116. * the newly created update has been marked as spam by Akismet. If so,  
  117. * the cached update is cleared from the user's 'bp_latest_update' 
  118. * usermeta, ensuring that it won't appear in the member header and 
  119. * elsewhere in the theme. 
  120. * This can't be done in BP_Akismet::check_activity() due to the 
  121. * default AJAX implementation; see bp_dtheme_post_update(). 
  122. * @since 1.6.0 
  123. * @see bp_dtheme_post_update() 
  124. * @param string $content Activity update text. 
  125. * @param int $user_id User ID. 
  126. * @param int $activity_id Activity ID. 
  127. */ 
  128. public function check_member_activity_update( $content, $user_id, $activity_id ) { 
  129. // By default, only handle activity updates and activity comments. 
  130. if ( empty( $this->last_activity ) || !in_array( $this->last_activity->type, BP_Akismet::get_activity_types() ) ) 
  131. return; 
  132.  
  133. // Was this $activity_id just marked as spam? If not, bail out. 
  134. if ( !$this->last_activity->id || $activity_id != $this->last_activity->id || 'false' == $this->last_activity->akismet_submission['bp_as_result'] ) 
  135. return; 
  136.  
  137. // It was, so delete the member's latest activity update. 
  138. bp_delete_user_meta( $user_id, 'bp_latest_update' ); 
  139.  
  140. /** 
  141. * Adds a "mark as spam" button to each activity item for site admins. 
  142. * This function is intended to be used inside the activity stream loop. 
  143. * @since 1.6.0 
  144. */ 
  145. public function add_activity_spam_button() { 
  146. if ( !bp_activity_user_can_mark_spam() ) 
  147. return; 
  148.  
  149. // By default, only handle activity updates and activity comments. 
  150. if ( !in_array( bp_get_activity_type(), BP_Akismet::get_activity_types() ) ) 
  151. return; 
  152.  
  153. bp_button( 
  154. array( 
  155. 'block_self' => false,  
  156. 'component' => 'activity',  
  157. 'id' => 'activity_make_spam_' . bp_get_activity_id(),  
  158. 'link_class' => 'bp-secondary-action spam-activity confirm button item-button',  
  159. 'link_href' => wp_nonce_url( bp_get_root_domain() . '/' . bp_get_activity_slug() . '/spam/' . bp_get_activity_id() . '/', 'bp_activity_akismet_spam_' . bp_get_activity_id() ),  
  160. 'link_text' => __( 'Spam', 'buddypress' ),  
  161. 'wrapper' => false,  
  162. ); 
  163.  
  164. /** 
  165. * Adds a "mark as spam" button to each activity COMMENT item for site admins. 
  166. * This function is intended to be used inside the activity stream loop. 
  167. * @since 1.6.0 
  168. */ 
  169. public function add_activity_comment_spam_button() { 
  170. if ( !bp_activity_user_can_mark_spam() ) 
  171. return; 
  172.  
  173. // By default, only handle activity updates and activity comments. 
  174. $current_comment = bp_activity_current_comment(); 
  175. if ( empty( $current_comment ) || !in_array( $current_comment->type, BP_Akismet::get_activity_types() ) ) 
  176. return; 
  177.  
  178. bp_button( 
  179. array( 
  180. 'block_self' => false,  
  181. 'component' => 'activity',  
  182. 'id' => 'activity_make_spam_' . bp_get_activity_comment_id(),  
  183. 'link_class' => 'bp-secondary-action spam-activity-comment confirm',  
  184. 'link_href' => wp_nonce_url( bp_get_root_domain() . '/' . bp_get_activity_slug() . '/spam/' . bp_get_activity_comment_id() . '/?cid=' . bp_get_activity_comment_id(), 'bp_activity_akismet_spam_' . bp_get_activity_comment_id() ),  
  185. 'link_text' => __( 'Spam', 'buddypress' ),  
  186. 'wrapper' => false,  
  187. ); 
  188.  
  189. /** 
  190. * Get a filterable list of activity types that Akismet should automatically check for spam. 
  191. * @since 1.6.0 
  192. * @static 
  193. * @return array $value List of activity types. 
  194. */ 
  195. public static function get_activity_types() { 
  196.  
  197. /** 
  198. * Filters the list of activity types that Akismet should automatically check for spam. 
  199. * @since 1.6.0 
  200. * @param array $value Array of default activity types for Akismet to check. 
  201. */ 
  202. return apply_filters( 'bp_akismet_get_activity_types', array( 'activity_comment', 'activity_update' ) ); 
  203.  
  204. /** 
  205. * Mark activity item as spam. 
  206. * @since 1.6.0 
  207. * @param BP_Activity_Activity $activity Activity item being spammed. 
  208. * @param string $source Either "by_a_person" (e.g. a person has 
  209. * manually marked the activity as spam) or 
  210. * "by_akismet" (automatically spammed). 
  211. */ 
  212. public function mark_as_spam( $activity, $source ) { 
  213. // Record this item so we can do some tidyup in BP_Akismet::check_member_activity_update(). 
  214. $this->last_activity = $activity; 
  215.  
  216. /** 
  217. * Fires after marking an activity item has been marked as spam. 
  218. * @since 1.6.0 
  219. * @param BP_Activity_Activity $activity Activity object being marked as spam. 
  220. * @param string $source Source of the whom marked as spam. 
  221. * Either "by_a_person" (e.g. a person has 
  222. * manually marked the activity as spam) 
  223. * or "by_akismet". 
  224. */ 
  225. do_action( 'bp_activity_akismet_mark_as_spam', $activity, $source ); 
  226.  
  227. /** 
  228. * Mark activity item as ham. 
  229. * @since 1.6.0 
  230. * @param BP_Activity_Activity $activity Activity item being hammed. 
  231. * @param string $source Either "by_a_person" (e.g. a person has 
  232. * manually marked the activity as ham) or 
  233. * "by_akismet" (automatically hammed). 
  234. */ 
  235. public function mark_as_ham( $activity, $source ) { 
  236. // If the activity was, originally, automatically marked as spam by Akismet, run the @mentions filter as it would have been skipped. 
  237. if ( 'true' == bp_activity_get_meta( $activity->id, '_bp_akismet_result' ) && !bp_activity_get_meta( $activity->id, '_bp_akismet_user_result' ) ) 
  238. $activity->content = bp_activity_at_name_filter( $activity->content, $activity->id ); 
  239.  
  240. /** 
  241. * Fires after marking an activity item has been marked as ham. 
  242. * @since 1.6.0 
  243. * @param BP_Activity_Activity $activity Activity object being marked as ham. 
  244. * @param string $source Source of the whom marked as ham. 
  245. * Either "by_a_person" (e.g. a person has 
  246. * manually marked the activity as ham) or 
  247. * "by_akismet" (automatically hammed). 
  248. */ 
  249. do_action( 'bp_activity_akismet_mark_as_ham', $activity, $source ); 
  250.  
  251. /** 
  252. * Build a data package for the Akismet service to inspect. 
  253. * @since 1.6.0 
  254. * @see http://akismet.com/development/api/#comment-check 
  255. * @static 
  256. * @param BP_Activity_Activity $activity Activity item data. 
  257. * @return array $activity_data 
  258. */ 
  259. public static function build_akismet_data_package( $activity ) { 
  260. $userdata = get_userdata( $activity->user_id ); 
  261.  
  262. $activity_data = array(); 
  263. $activity_data['akismet_comment_nonce'] = 'inactive'; 
  264. $activity_data['comment_author'] = $userdata->display_name; 
  265. $activity_data['comment_author_email'] = $userdata->user_email; 
  266. $activity_data['comment_author_url'] = bp_core_get_userlink( $userdata->ID, false, true); 
  267. $activity_data['comment_content'] = $activity->content; 
  268. $activity_data['comment_type'] = $activity->type; 
  269. $activity_data['permalink'] = bp_activity_get_permalink( $activity->id, $activity ); 
  270. $activity_data['user_ID'] = $userdata->ID; 
  271. $activity_data['user_role'] = Akismet::get_user_roles( $userdata->ID ); 
  272.  
  273. /** 
  274. * Get the nonce if the new activity was submitted through the "what's up, Paul?" form. 
  275. * This helps Akismet ensure that the update was a valid form submission. 
  276. */ 
  277. if ( !empty( $_POST['_bp_as_nonce'] ) ) 
  278. $activity_data['akismet_comment_nonce'] = wp_verify_nonce( $_POST['_bp_as_nonce'], "_bp_as_nonce_{$userdata->ID}" ) ? 'passed' : 'failed'; 
  279.  
  280. /** 
  281. * If the new activity was a reply to an existing item, check the nonce with the activity parent ID. 
  282. * This helps Akismet ensure that the update was a valid form submission. 
  283. */ 
  284. elseif ( !empty( $activity->secondary_item_id ) && !empty( $_POST['_bp_as_nonce_' . $activity->secondary_item_id] ) ) 
  285. $activity_data['akismet_comment_nonce'] = wp_verify_nonce( $_POST["_bp_as_nonce_{$activity->secondary_item_id}"], "_bp_as_nonce_{$userdata->ID}_{$activity->secondary_item_id}" ) ? 'passed' : 'failed'; 
  286.  
  287. /** 
  288. * Filters activity data before being sent to Akismet to inspect. 
  289. * @since 1.6.0 
  290. * @param array $activity_data Array of activity data for Akismet to inspect. 
  291. * @param BP_Activity_Activity $activity Activity item data. 
  292. */ 
  293. return apply_filters( 'bp_akismet_build_akismet_data_package', $activity_data, $activity ); 
  294.  
  295. /** 
  296. * Check if the activity item is spam or ham. 
  297. * @since 1.6.0 
  298. * @see http://akismet.com/development/api/ 
  299. * @todo Spam counter? 
  300. * @todo Auto-delete old spam? 
  301. * @param BP_Activity_Activity $activity The activity item to check. 
  302. */ 
  303. public function check_activity( $activity ) { 
  304. // By default, only handle activity updates and activity comments. 
  305. if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 
  306. return; 
  307.  
  308. // Make sure last_activity is clear to avoid any confusion. 
  309. $this->last_activity = null; 
  310.  
  311. // Build data package for Akismet. 
  312. $activity_data = BP_Akismet::build_akismet_data_package( $activity ); 
  313.  
  314. // Check with Akismet to see if this is spam. 
  315. $activity_data = $this->send_akismet_request( $activity_data, 'check', 'spam' ); 
  316.  
  317. // Record this item. 
  318. $this->last_activity = $activity; 
  319.  
  320. // Store a copy of the data that was submitted to Akismet. 
  321. $this->last_activity->akismet_submission = $activity_data; 
  322.  
  323. // Spam. 
  324. if ( 'true' == $activity_data['bp_as_result'] ) { 
  325. /** 
  326. * Fires after an activity item has been proven to be spam, but before officially being marked as spam. 
  327. * @since 1.6.0 
  328. * @param BP_Activity_Activity $activity The activity item proven to be spam. 
  329. * @param array $activity_data Array of activity data for item including 
  330. * Akismet check results data. 
  331. */ 
  332. do_action_ref_array( 'bp_activity_akismet_spam_caught', array( &$activity, $activity_data ) ); 
  333.  
  334. // Mark as spam. 
  335. bp_activity_mark_as_spam( $activity, 'by_akismet' ); 
  336.  
  337. // Update activity meta after a spam check. 
  338. add_action( 'bp_activity_after_save', array( $this, 'update_activity_akismet_meta' ), 1, 1 ); 
  339.  
  340. /** 
  341. * Update activity meta after a manual spam change (user-initiated). 
  342. * @since 1.6.0 
  343. * @param BP_Activity_Activity $activity The activity to check. 
  344. */ 
  345. public function update_activity_spam_meta( $activity ) { 
  346. // By default, only handle activity updates and activity comments. 
  347. if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 
  348. return; 
  349.  
  350. $this->update_activity_history( $activity->id, sprintf( __( '%s reported this activity as spam', 'buddypress' ), bp_get_loggedin_user_username() ), 'report-spam' ); 
  351. bp_activity_update_meta( $activity->id, '_bp_akismet_user_result', 'true' ); 
  352. bp_activity_update_meta( $activity->id, '_bp_akismet_user', bp_get_loggedin_user_username() ); 
  353.  
  354. /** 
  355. * Update activity meta after a manual ham change (user-initiated). 
  356. * @since 1.6.0 
  357. * @param BP_Activity_Activity $activity The activity to check. 
  358. */ 
  359. public function update_activity_ham_meta( $activity ) { 
  360. // By default, only handle activity updates and activity comments. 
  361. if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 
  362. return; 
  363.  
  364. $this->update_activity_history( $activity->id, sprintf( __( '%s reported this activity as not spam', 'buddypress' ), bp_get_loggedin_user_username() ), 'report-ham' ); 
  365. bp_activity_update_meta( $activity->id, '_bp_akismet_user_result', 'false' ); 
  366. bp_activity_update_meta( $activity->id, '_bp_akismet_user', bp_get_loggedin_user_username() ); 
  367.  
  368. /** 
  369. * Update activity meta after an automatic spam check (not user-initiated). 
  370. * @since 1.6.0 
  371. * @param BP_Activity_Activity $activity The activity to check. 
  372. */ 
  373. public function update_activity_akismet_meta( $activity ) { 
  374. // Check we're dealing with what was last updated by Akismet. 
  375. if ( empty( $this->last_activity ) || !empty( $this->last_activity ) && $activity->id != $this->last_activity->id ) 
  376. return; 
  377.  
  378. // By default, only handle activity updates and activity comments. 
  379. if ( !in_array( $this->last_activity->type, BP_Akismet::get_activity_types() ) ) 
  380. return; 
  381.  
  382. // Spam. 
  383. if ( 'true' == $this->last_activity->akismet_submission['bp_as_result'] ) { 
  384. bp_activity_update_meta( $activity->id, '_bp_akismet_result', 'true' ); 
  385. $this->update_activity_history( $activity->id, __( 'Akismet caught this item as spam', 'buddypress' ), 'check-spam' ); 
  386.  
  387. // Not spam. 
  388. } elseif ( 'false' == $this->last_activity->akismet_submission['bp_as_result'] ) { 
  389. bp_activity_update_meta( $activity->id, '_bp_akismet_result', 'false' ); 
  390. $this->update_activity_history( $activity->id, __( 'Akismet cleared this item', 'buddypress' ), 'check-ham' ); 
  391.  
  392. // Uh oh, something's gone horribly wrong. Unexpected result. 
  393. } else { 
  394. bp_activity_update_meta( $activity->id, '_bp_akismet_error', bp_core_current_time() ); 
  395. $this->update_activity_history( $activity->id, sprintf( __( 'Akismet was unable to check this item (response: %s), will automatically retry again later.', 'buddypress' ), $this->last_activity->akismet_submission['bp_as_result'] ), 'check-error' ); 
  396.  
  397. // Record the original data which was submitted to Akismet for checking. 
  398. bp_activity_update_meta( $activity->id, '_bp_akismet_submission', $this->last_activity->akismet_submission ); 
  399.  
  400. /** 
  401. * Contact Akismet to check if this is spam or ham. 
  402. * Props to WordPress core Akismet plugin for a lot of this. 
  403. * @since 1.6.0 
  404. * @param array $activity_data Packet of information to submit to Akismet. 
  405. * @param string $check "check" or "submit". 
  406. * @param string $spam "spam" or "ham". 
  407. * @return array $activity_data Activity data, with Akismet data added. 
  408. */ 
  409. public function send_akismet_request( $activity_data, $check = 'check', $spam = 'spam' ) { 
  410. $query_string = $path = ''; 
  411.  
  412. $activity_data['blog'] = bp_get_option( 'home' ); 
  413. $activity_data['blog_charset'] = bp_get_option( 'blog_charset' ); 
  414. $activity_data['blog_lang'] = get_locale(); 
  415. $activity_data['referrer'] = $_SERVER['HTTP_REFERER']; 
  416. $activity_data['user_agent'] = bp_core_current_user_ua(); 
  417. $activity_data['user_ip'] = bp_core_current_user_ip(); 
  418.  
  419. if ( Akismet::is_test_mode() ) 
  420. $activity_data['is_test'] = 'true'; 
  421.  
  422. // Loop through _POST args and rekey strings. 
  423. foreach ( $_POST as $key => $value ) 
  424. if ( is_string( $value ) && 'cookie' != $key ) 
  425. $activity_data['POST_' . $key] = $value; 
  426.  
  427. // Keys to ignore. 
  428. $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); 
  429.  
  430. // Loop through _SERVER args and remove whitelisted keys. 
  431. foreach ( $_SERVER as $key => $value ) { 
  432.  
  433. // Key should not be ignored. 
  434. if ( !in_array( $key, $ignore ) && is_string( $value ) ) { 
  435. $activity_data[$key] = $value; 
  436.  
  437. // Key should be ignored. 
  438. } else { 
  439. $activity_data[$key] = ''; 
  440.  
  441. foreach ( $activity_data as $key => $data ) 
  442. $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&'; 
  443.  
  444. if ( 'check' == $check ) 
  445. $path = 'comment-check'; 
  446. elseif ( 'submit' == $check ) 
  447. $path = 'submit-' . $spam; 
  448.  
  449. // Send to Akismet. 
  450. add_filter( 'akismet_ua', array( $this, 'buddypress_ua' ) ); 
  451. $response = Akismet::http_post( $query_string, $path ); 
  452. remove_filter( 'akismet_ua', array( $this, 'buddypress_ua' ) ); 
  453.  
  454. // Get the response. 
  455. if ( ! empty( $response[1] ) && ! is_wp_error( $response[1] ) ) 
  456. $activity_data['bp_as_result'] = $response[1]; 
  457. else 
  458. $activity_data['bp_as_result'] = false; 
  459.  
  460. // Perform a daily tidy up. 
  461. if ( ! wp_next_scheduled( 'bp_activity_akismet_delete_old_metadata' ) ) 
  462. wp_schedule_event( time(), 'daily', 'bp_activity_akismet_delete_old_metadata' ); 
  463.  
  464. return $activity_data; 
  465.  
  466. /** 
  467. * Filters user agent when sending to Akismet to add BuddyPress info. 
  468. * @since 1.6.0 
  469. * @param string $user_agent User agent string, as generated by Akismet. 
  470. * @return string $user_agent Modified user agent string. 
  471. */ 
  472. public function buddypress_ua( $user_agent ) { 
  473. $user_agent = 'BuddyPress/' . bp_get_version() . ' | Akismet/'. constant( 'AKISMET_VERSION' ); 
  474. return $user_agent; 
  475.  
  476. /** 
  477. * Adds a "History" meta box to the activity edit screen. 
  478. * @since 1.6.0 
  479. * @param string $screen_action The type of screen that has been requested. 
  480. */ 
  481. function add_history_metabox( $screen_action ) { 
  482. // Only proceed if we're on the edit screen. 
  483. if ( 'edit' != $screen_action ) 
  484. return; 
  485.  
  486. // Display meta box with a low priority (low position on screen by default). 
  487. add_meta_box( 'bp_activity_history', __( 'Activity History', 'buddypress' ), array( $this, 'history_metabox' ), get_current_screen()->id, 'normal', 'low' ); 
  488.  
  489. /** 
  490. * History meta box for the Activity admin edit screen. 
  491. * @since 1.6.0 
  492. * @see https://buddypress.trac.wordpress.org/ticket/3907 
  493. * @todo Update activity meta to allow >1 record with the same key (iterate through $history). 
  494. * @param object $item Activity item. 
  495. */ 
  496. function history_metabox( $item ) { 
  497. $history = BP_Akismet::get_activity_history( $item->id ); 
  498.  
  499. if ( empty( $history ) ) 
  500. return; 
  501.  
  502. echo '<div class="akismet-history"><div>'; 
  503. printf( _x( '%1$s — %2$s', 'x hours ago - akismet cleared this item', 'buddypress' ), '<span>' . bp_core_time_since( $history[2] ) . '</span>', esc_html( $history[1] ) ); 
  504. echo '</div></div>'; 
  505.  
  506. /** 
  507. * Update an activity item's Akismet history. 
  508. * @since 1.6.0 
  509. * @param int $activity_id Activity item ID. 
  510. * @param string $message Human-readable description of what's changed. 
  511. * @param string $event The type of check we were carrying out. 
  512. */ 
  513. public function update_activity_history( $activity_id = 0, $message = '', $event = '' ) { 
  514. $event = array( 
  515. 'event' => $event,  
  516. 'message' => $message,  
  517. 'time' => Akismet::_get_microtime(),  
  518. 'user' => bp_loggedin_user_id(),  
  519. ); 
  520.  
  521. // Save the history data. 
  522. bp_activity_update_meta( $activity_id, '_bp_akismet_history', $event ); 
  523.  
  524. /** 
  525. * Get an activity item's Akismet history. 
  526. * @since 1.6.0 
  527. * @param int $activity_id Activity item ID. 
  528. * @return array The activity item's Akismet history. 
  529. */ 
  530. public function get_activity_history( $activity_id = 0 ) { 
  531. $history = bp_activity_get_meta( $activity_id, '_bp_akismet_history' ); 
  532. if ( $history === false ) 
  533. $history = array(); 
  534.  
  535. // Sort it by the time recorded. 
  536. usort( $history, 'akismet_cmp_time' ); 
  537.  
  538. return $history;