[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-activity/classes/ -> class-bp-akismet.php (source)

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


Generated: Thu Nov 21 01:01:38 2019 Cross-referenced by PHPXref 0.7.1