[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-activity/ -> bp-activity-functions.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Activity Functions.
   4   *
   5   * Functions for the Activity Streams component.
   6   *
   7   * @package BuddyPress
   8   * @subpackage ActivityFunctions
   9   * @since 1.5.0
  10   */
  11  
  12  // Exit if accessed directly.
  13  defined( 'ABSPATH' ) || exit;
  14  
  15  /**
  16   * Check whether the $bp global lists an activity directory page.
  17   *
  18   * @since 1.5.0
  19   *
  20   * @return bool True if activity directory page is found, otherwise false.
  21   */
  22  function bp_activity_has_directory() {
  23      return isset( buddypress()->pages->activity->id ) && buddypress()->pages->activity->id;
  24  }
  25  
  26  /**
  27   * Are mentions enabled or disabled?
  28   *
  29   * The Mentions feature does a number of things, all of which will be turned
  30   * off if you disable mentions:
  31   *   - Detecting and auto-linking @username in all BP/WP content.
  32   *   - Sending BP notifications and emails to users when they are mentioned
  33   *     using the @username syntax.
  34   *   - The Public Message button on user profiles.
  35   *
  36   * Mentions are enabled by default. To disable, put the following line in
  37   * bp-custom.php or your theme's functions.php file:
  38   *
  39   *   add_filter( 'bp_activity_do_mentions', '__return_false' );
  40   *
  41   * @since 1.8.0
  42   *
  43   * @return bool $retval True to enable mentions, false to disable.
  44   */
  45  function bp_activity_do_mentions() {
  46  
  47      /**
  48       * Filters whether or not mentions are enabled.
  49       *
  50       * @since 1.8.0
  51       *
  52       * @param bool $enabled True to enable mentions, false to disable.
  53       */
  54      return (bool) apply_filters( 'bp_activity_do_mentions', true );
  55  }
  56  
  57  /**
  58   * Should BuddyPress load the mentions scripts and related assets, including results to prime the
  59   * mentions suggestions?
  60   *
  61   * @since 2.1.0
  62   *
  63   * @return bool True if mentions scripts should be loaded.
  64   */
  65  function bp_activity_maybe_load_mentions_scripts() {
  66      $mentions_enabled = bp_activity_do_mentions() && bp_is_user_active();
  67      $load_mentions    = $mentions_enabled && ( bp_is_activity_component() || is_admin() );
  68  
  69      /**
  70       * Filters whether or not BuddyPress should load mentions scripts and assets.
  71       *
  72       * @since 2.1.0
  73       *
  74       * @param bool $load_mentions    True to load mentions assets, false otherwise.
  75       * @param bool $mentions_enabled True if mentions are enabled.
  76       */
  77      return (bool) apply_filters( 'bp_activity_maybe_load_mentions_scripts', $load_mentions, $mentions_enabled );
  78  }
  79  
  80  /**
  81   * Locate usernames in an activity content string, as designated by an @ sign.
  82   *
  83   * @since 1.5.0
  84   *
  85   * @param string $content The content of the activity, usually found in
  86   *                        $activity->content.
  87   * @return array|bool Associative array with user ID as key and username as
  88   *                    value. Boolean false if no mentions found.
  89   */
  90  function bp_activity_find_mentions( $content ) {
  91  
  92      $pattern = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
  93      preg_match_all( $pattern, $content, $usernames );
  94  
  95      // Make sure there's only one instance of each username.
  96      $usernames = array_unique( $usernames[1] );
  97  
  98      // Bail if no usernames.
  99      if ( empty( $usernames ) ) {
 100          return false;
 101      }
 102  
 103      $mentioned_users = array();
 104  
 105      // We've found some mentions! Check to see if users exist.
 106      foreach( (array) array_values( $usernames ) as $username ) {
 107          $user_id = bp_activity_get_userid_from_mentionname( $username );
 108  
 109          // The user ID exists, so let's add it to our array.
 110          if ( ! empty( $user_id ) ) {
 111              $mentioned_users[ $user_id ] = $username;
 112          }
 113      }
 114  
 115      if ( empty( $mentioned_users ) ) {
 116          return false;
 117      }
 118  
 119      /**
 120       * Filters the mentioned users.
 121       *
 122       * @since 2.5.0
 123       *
 124       * @param array $mentioned_users Associative array with user IDs as keys and usernames as values.
 125       */
 126      return apply_filters( 'bp_activity_mentioned_users', $mentioned_users );
 127  }
 128  
 129  /**
 130   * Reset a user's unread mentions list and count.
 131   *
 132   * @since 1.5.0
 133   *
 134   * @param int $user_id The id of the user whose unread mentions are being reset.
 135   */
 136  function bp_activity_clear_new_mentions( $user_id ) {
 137      bp_delete_user_meta( $user_id, 'bp_new_mention_count' );
 138      bp_delete_user_meta( $user_id, 'bp_new_mentions'      );
 139  
 140      /**
 141       * Fires once mentions has been reset for a given user.
 142       *
 143       * @since  2.5.0
 144       *
 145       * @param  int $user_id The id of the user whose unread mentions are being reset.
 146       */
 147      do_action( 'bp_activity_clear_new_mentions', $user_id );
 148  }
 149  
 150  /**
 151   * Adjusts mention count for mentioned users in activity items.
 152   *
 153   * This function is useful if you only have the activity ID handy and you
 154   * haven't parsed an activity item for @mentions yet.
 155   *
 156   * Currently, only used in {@link bp_activity_delete()}.
 157   *
 158   * @since 1.5.0
 159   *
 160   * @param int    $activity_id The unique id for the activity item.
 161   * @param string $action      Can be 'delete' or 'add'. Defaults to 'add'.
 162   * @return bool
 163   */
 164  function bp_activity_adjust_mention_count( $activity_id = 0, $action = 'add' ) {
 165  
 166      // Bail if no activity ID passed.
 167      if ( empty( $activity_id ) ) {
 168          return false;
 169      }
 170  
 171      // Get activity object.
 172      $activity  = new BP_Activity_Activity( $activity_id );
 173  
 174      // Try to find mentions.
 175      $usernames = bp_activity_find_mentions( strip_tags( $activity->content ) );
 176  
 177      // Still empty? Stop now.
 178      if ( empty( $usernames ) ) {
 179          return false;
 180      }
 181  
 182      // Increment mention count foreach mentioned user.
 183      foreach( (array) array_keys( $usernames ) as $user_id ) {
 184          bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action );
 185      }
 186  }
 187  
 188  /**
 189   * Update the mention count for a given user.
 190   *
 191   * This function should be used when you've already parsed your activity item
 192   * for @mentions.
 193   *
 194   * @since 1.7.0
 195   *
 196   * @param int    $user_id     The user ID.
 197   * @param int    $activity_id The unique ID for the activity item.
 198   * @param string $action      'delete' or 'add'. Default: 'add'.
 199   * @return bool
 200   */
 201  function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) {
 202  
 203      if ( empty( $user_id ) || empty( $activity_id ) ) {
 204          return false;
 205      }
 206  
 207      // Adjust the mention list and count for the member.
 208      $new_mention_count = (int) bp_get_user_meta( $user_id, 'bp_new_mention_count', true );
 209      $new_mentions      =       bp_get_user_meta( $user_id, 'bp_new_mentions',      true );
 210  
 211      // Make sure new mentions is an array.
 212      if ( empty( $new_mentions ) ) {
 213          $new_mentions = array();
 214      }
 215  
 216      switch ( $action ) {
 217          case 'delete' :
 218              $key = array_search( $activity_id, $new_mentions );
 219  
 220              if ( $key !== false ) {
 221                  unset( $new_mentions[$key] );
 222              }
 223  
 224              break;
 225  
 226          case 'add' :
 227          default :
 228              if ( !in_array( $activity_id, $new_mentions ) ) {
 229                  $new_mentions[] = (int) $activity_id;
 230              }
 231  
 232              break;
 233      }
 234  
 235      // Get an updated mention count.
 236      $new_mention_count = count( $new_mentions );
 237  
 238      // Resave the user_meta.
 239      bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count );
 240      bp_update_user_meta( $user_id, 'bp_new_mentions',      $new_mentions );
 241  
 242      return true;
 243  }
 244  
 245  /**
 246   * Determine a user's "mentionname", the name used for that user in @-mentions.
 247   *
 248   * @since 1.9.0
 249   *
 250   * @param int|string $user_id ID of the user to get @-mention name for.
 251   * @return string $mentionname User name appropriate for @-mentions.
 252   */
 253  function bp_activity_get_user_mentionname( $user_id ) {
 254      $mentionname = '';
 255  
 256      $userdata = bp_core_get_core_userdata( $user_id );
 257  
 258      if ( $userdata ) {
 259          if ( bp_is_username_compatibility_mode() ) {
 260              $mentionname = str_replace( ' ', '-', $userdata->user_login );
 261          } else {
 262              $mentionname = $userdata->user_nicename;
 263          }
 264      }
 265  
 266      return $mentionname;
 267  }
 268  
 269  /**
 270   * Get a user ID from a "mentionname", the name used for a user in @-mentions.
 271   *
 272   * @since 1.9.0
 273   *
 274   * @param string $mentionname Username of user in @-mentions.
 275   * @return int|bool ID of the user, if one is found. Otherwise false.
 276   */
 277  function bp_activity_get_userid_from_mentionname( $mentionname ) {
 278      $user_id = false;
 279  
 280      /*
 281       * In username compatibility mode, hyphens are ambiguous between
 282       * actual hyphens and converted spaces.
 283       *
 284       * @todo There is the potential for username clashes between 'foo bar'
 285       * and 'foo-bar' in compatibility mode. Come up with a system for
 286       * unique mentionnames.
 287       */
 288      if ( bp_is_username_compatibility_mode() ) {
 289          // First, try the raw username.
 290          $userdata = get_user_by( 'login', $mentionname );
 291  
 292          // Doing a direct query to use proper regex. Necessary to
 293          // account for hyphens + spaces in the same user_login.
 294          if ( empty( $userdata ) || ! is_a( $userdata, 'WP_User' ) ) {
 295              global $wpdb;
 296              $regex   = esc_sql( str_replace( '-', '[ \-]', $mentionname ) );
 297              $user_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->users} WHERE user_login REGEXP '{$regex}'" );
 298          } else {
 299              $user_id = $userdata->ID;
 300          }
 301  
 302      // When username compatibility mode is disabled, the mentionname is
 303      // the same as the nicename.
 304      } else {
 305          $user_id = bp_core_get_userid_from_nicename( $mentionname );
 306      }
 307  
 308  
 309      return $user_id;
 310  }
 311  
 312  /** Actions ******************************************************************/
 313  
 314  /**
 315   * Register an activity 'type' and its action description/callback.
 316   *
 317   * Activity actions are strings used to describe items in the activity stream,
 318   * such as 'Joe became a registered member' or 'Bill and Susie are now
 319   * friends'. Each activity type (such as 'new_member' or 'friendship_created')
 320   * used by a component should be registered using this function.
 321   *
 322   * While it's possible to post items to the activity stream whose types are
 323   * not registered using bp_activity_set_action(), it is not recommended;
 324   * unregistered types will not be displayed properly in the activity admin
 325   * panel, and dynamic action generation (which is essential for multilingual
 326   * sites, etc) will not work.
 327   *
 328   * @since 1.1.0
 329   *
 330   * @param  string        $component_id    The unique string ID of the component.
 331   * @param  string        $type            The action type.
 332   * @param  string        $description     The action description.
 333   * @param  callable|bool $format_callback Callback for formatting the action string.
 334   * @param  string|bool   $label           String to describe this action in the activity stream filter dropdown.
 335   * @param  array         $context         Optional. Activity stream contexts where the filter should appear.
 336   *                                        Values: 'activity', 'member', 'member_groups', 'group'.
 337   * @param  int           $position        Optional. The position of the action when listed in dropdowns.
 338   * @return bool False if any param is empty, otherwise true.
 339   */
 340  function bp_activity_set_action( $component_id, $type, $description, $format_callback = false, $label = false, $context = array(), $position = 0 ) {
 341      $bp = buddypress();
 342  
 343      // Return false if any of the above values are not set.
 344      if ( empty( $component_id ) || empty( $type ) || empty( $description ) ) {
 345          return false;
 346      }
 347  
 348      // Set activity action.
 349      if ( ! isset( $bp->activity->actions ) || ! is_object( $bp->activity->actions ) ) {
 350          $bp->activity->actions = new stdClass;
 351      }
 352  
 353      // Verify callback.
 354      if ( ! is_callable( $format_callback ) ) {
 355          $format_callback = '';
 356      }
 357  
 358      if ( ! isset( $bp->activity->actions->{$component_id} ) || ! is_object( $bp->activity->actions->{$component_id} ) ) {
 359          $bp->activity->actions->{$component_id} = new stdClass;
 360      }
 361  
 362      /**
 363       * Filters the action type being set for the current activity item.
 364       *
 365       * @since 1.1.0
 366       *
 367       * @param array    $array           Array of arguments for action type being set.
 368       * @param string   $component_id    ID of the current component being set.
 369       * @param string   $type            Action type being set.
 370       * @param string   $description     Action description for action being set.
 371       * @param callable $format_callback Callback for formatting the action string.
 372       * @param string   $label           String to describe this action in the activity stream filter dropdown.
 373       * @param array    $context         Activity stream contexts where the filter should appear. 'activity', 'member',
 374       *                                  'member_groups', 'group'.
 375       */
 376      $bp->activity->actions->{$component_id}->{$type} = apply_filters( 'bp_activity_set_action', array(
 377          'key'             => $type,
 378          'value'           => $description,
 379          'format_callback' => $format_callback,
 380          'label'           => $label,
 381          'context'         => $context,
 382          'position'        => $position,
 383      ), $component_id, $type, $description, $format_callback, $label, $context );
 384  
 385      // Sort the actions of the affected component.
 386      $action_array = (array) $bp->activity->actions->{$component_id};
 387      $action_array = bp_sort_by_key( $action_array, 'position', 'num' );
 388  
 389      // Restore keys.
 390      $bp->activity->actions->{$component_id} = new stdClass;
 391      foreach ( $action_array as $key_ordered ) {
 392          $bp->activity->actions->{$component_id}->{$key_ordered['key']} = $key_ordered;
 393      }
 394  
 395      return true;
 396  }
 397  
 398  /**
 399   * Set tracking arguments for a given post type.
 400   *
 401   * @since 2.2.0
 402   *
 403   * @global $wp_post_types
 404   *
 405   * @param string $post_type The name of the post type, as registered with WordPress. Eg 'post' or 'page'.
 406   * @param array  $args {
 407   *     An associative array of tracking parameters. All items are optional.
 408   *     @type string   $bp_activity_admin_filter String to use in the Dashboard > Activity dropdown.
 409   *     @type string   $bp_activity_front_filter String to use in the front-end dropdown.
 410   *     @type string   $bp_activity_new_post     String format to use for generating the activity action. Should be a
 411   *                                              translatable string where %1$s is replaced by a user link and %2$s is
 412   *                                              the URL of the newly created post.
 413   *     @type string   $bp_activity_new_post_ms  String format to use for generating the activity action on Multisite.
 414   *                                              Should be a translatable string where %1$s is replaced by a user link,
 415   *                                              %2$s is the URL of the newly created post, and %3$s is a link to
 416   *                                              the site.
 417   *     @type string   $component_id             ID of the BuddyPress component to associate the activity item.
 418   *     @type string   $action_id                Value for the 'type' param of the new activity item.
 419   *     @type callable $format_callback          Callback for formatting the activity action string.
 420   *                                              Default: 'bp_activity_format_activity_action_custom_post_type_post'.
 421   *     @type array    $contexts                 The directory contexts in which the filter will show.
 422   *                                              Default: array( 'activity' ).
 423   *     @type array    $position                 Position of the item in filter dropdowns.
 424   *     @type string   $singular                 Singular, translatable name of the post type item. If no value is
 425   *                                              provided, it's pulled from the 'singular_name' of the post type.
 426   *     @type bool     $activity_comment         Whether to allow comments on the activity items. Defaults to true if
 427   *                                              the post type does not natively support comments, otherwise false.
 428   * }
 429   * @return bool
 430   */
 431  function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array() ) {
 432      global $wp_post_types;
 433  
 434      if ( empty( $wp_post_types[ $post_type ] ) || ! post_type_supports( $post_type, 'buddypress-activity' ) || ! is_array( $args ) ) {
 435          return false;
 436      }
 437  
 438      $activity_labels = array(
 439          /* Post labels */
 440          'bp_activity_admin_filter',
 441          'bp_activity_front_filter',
 442          'bp_activity_new_post',
 443          'bp_activity_new_post_ms',
 444          /* Comment labels */
 445          'bp_activity_comments_admin_filter',
 446          'bp_activity_comments_front_filter',
 447          'bp_activity_new_comment',
 448          'bp_activity_new_comment_ms'
 449      );
 450  
 451      // Labels are loaded into the post type object.
 452      foreach ( $activity_labels as $label_type ) {
 453          if ( ! empty( $args[ $label_type ] ) ) {
 454              $wp_post_types[ $post_type ]->labels->{$label_type} = $args[ $label_type ];
 455              unset( $args[ $label_type ] );
 456          }
 457      }
 458  
 459      // If there are any additional args, put them in the bp_activity attribute of the post type.
 460      if ( ! empty( $args ) ) {
 461          $wp_post_types[ $post_type ]->bp_activity = $args;
 462      }
 463  }
 464  
 465  /**
 466   * Get tracking arguments for a specific post type.
 467   *
 468   * @since 2.2.0
 469   * @since 2.5.0 Add post type comments tracking args
 470   *
 471   * @param  string $post_type Name of the post type.
 472   * @return object The tracking arguments of the post type.
 473   */
 474  function bp_activity_get_post_type_tracking_args( $post_type ) {
 475      if ( ! post_type_supports( $post_type, 'buddypress-activity' ) ) {
 476          return false;
 477      }
 478  
 479      $post_type_object           = get_post_type_object( $post_type );
 480      $post_type_support_comments = post_type_supports( $post_type, 'comments' );
 481  
 482      $post_type_activity = array(
 483          'component_id'            => buddypress()->activity->id,
 484          'action_id'               => 'new_' . $post_type,
 485          'format_callback'         => 'bp_activity_format_activity_action_custom_post_type_post',
 486          'front_filter'            => $post_type_object->labels->name,
 487          'contexts'                => array( 'activity' ),
 488          'position'                => 0,
 489          'singular'                => strtolower( $post_type_object->labels->singular_name ),
 490          'activity_comment'        => ! $post_type_support_comments,
 491          'comment_action_id'       => false,
 492          'comment_format_callback' => 'bp_activity_format_activity_action_custom_post_type_comment',
 493      );
 494  
 495      if ( ! empty( $post_type_object->bp_activity ) ) {
 496          $post_type_activity = bp_parse_args(
 497              (array) $post_type_object->bp_activity,
 498              $post_type_activity,
 499              $post_type . '_tracking_args'
 500          );
 501      }
 502  
 503      $post_type_activity = (object) $post_type_activity;
 504  
 505      // Try to get the admin filter from the post type labels.
 506      if ( ! empty( $post_type_object->labels->bp_activity_admin_filter ) ) {
 507          $post_type_activity->admin_filter = $post_type_object->labels->bp_activity_admin_filter;
 508  
 509      // Fall back to a generic name.
 510      } else {
 511          $post_type_activity->admin_filter = _x( 'New item published', 'Post Type generic activity post admin filter', 'buddypress' );
 512      }
 513  
 514      // Check for the front filter in the post type labels.
 515      if ( ! empty( $post_type_object->labels->bp_activity_front_filter ) ) {
 516          $post_type_activity->front_filter = $post_type_object->labels->bp_activity_front_filter;
 517      }
 518  
 519      // Try to get the action for new post type action on non-multisite installations.
 520      if ( ! empty( $post_type_object->labels->bp_activity_new_post ) ) {
 521          $post_type_activity->new_post_type_action = $post_type_object->labels->bp_activity_new_post;
 522      }
 523  
 524      // Try to get the action for new post type action on multisite installations.
 525      if ( ! empty( $post_type_object->labels->bp_activity_new_post_ms ) ) {
 526          $post_type_activity->new_post_type_action_ms = $post_type_object->labels->bp_activity_new_post_ms;
 527      }
 528  
 529      // If the post type supports comments and has a comment action id, build the comments tracking args.
 530      if ( $post_type_support_comments && ! empty( $post_type_activity->comment_action_id ) ) {
 531          // Init a new container for the activity type for comments.
 532          $post_type_activity->comments_tracking = new stdClass();
 533  
 534          // Build the activity type for comments.
 535          $post_type_activity->comments_tracking->component_id = $post_type_activity->component_id;
 536          $post_type_activity->comments_tracking->action_id    = $post_type_activity->comment_action_id;
 537  
 538          // Try to get the comments admin filter from the post type labels.
 539          if ( ! empty( $post_type_object->labels->bp_activity_comments_admin_filter ) ) {
 540              $post_type_activity->comments_tracking->admin_filter = $post_type_object->labels->bp_activity_comments_admin_filter;
 541  
 542          // Fall back to a generic name.
 543          } else {
 544              $post_type_activity->comments_tracking->admin_filter = _x( 'New item comment posted', 'Post Type generic comments activity admin filter', 'buddypress' );
 545          }
 546  
 547          $post_type_activity->comments_tracking->format_callback = $post_type_activity->comment_format_callback;
 548  
 549          // Check for the comments front filter in the post type labels.
 550          if ( ! empty( $post_type_object->labels->bp_activity_comments_front_filter ) ) {
 551              $post_type_activity->comments_tracking->front_filter = $post_type_object->labels->bp_activity_comments_front_filter;
 552  
 553          // Fall back to a generic name.
 554          } else {
 555              $post_type_activity->comments_tracking->front_filter = _x( 'Item comments', 'Post Type generic comments activity front filter', 'buddypress' );
 556          }
 557  
 558          $post_type_activity->comments_tracking->contexts = $post_type_activity->contexts;
 559          $post_type_activity->comments_tracking->position = (int) $post_type_activity->position + 1;
 560  
 561          // Try to get the action for new post type comment action on non-multisite installations.
 562          if ( ! empty( $post_type_object->labels->bp_activity_new_comment ) ) {
 563              $post_type_activity->comments_tracking->new_post_type_comment_action = $post_type_object->labels->bp_activity_new_comment;
 564          }
 565  
 566          // Try to get the action for new post type comment action on multisite installations.
 567          if ( ! empty( $post_type_object->labels->bp_activity_new_comment_ms ) ) {
 568              $post_type_activity->comments_tracking->new_post_type_comment_action_ms = $post_type_object->labels->bp_activity_new_comment_ms;
 569          }
 570      }
 571  
 572      // Finally make sure we'll be able to find the post type this activity type is associated to.
 573      $post_type_activity->post_type = $post_type;
 574  
 575      /**
 576       * Filters tracking arguments for a specific post type.
 577       *
 578       * @since 2.2.0
 579       *
 580       * @param object $post_type_activity The tracking arguments of the post type.
 581       * @param string $post_type          Name of the post type.
 582       */
 583      return apply_filters( 'bp_activity_get_post_type_tracking_args', $post_type_activity, $post_type );
 584  }
 585  
 586  /**
 587   * Get tracking arguments for all post types.
 588   *
 589   * @since 2.2.0
 590   * @since 2.5.0 Include post type comments tracking args if needed
 591   *
 592   * @return array List of post types with their tracking arguments.
 593   */
 594  function bp_activity_get_post_types_tracking_args() {
 595      // Fetch all public post types.
 596      $post_types = get_post_types( array( 'public' => true ), 'names' );
 597  
 598      $post_types_tracking_args = array();
 599  
 600      foreach ( $post_types as $post_type ) {
 601          $track_post_type = bp_activity_get_post_type_tracking_args( $post_type );
 602  
 603          if ( ! empty( $track_post_type ) ) {
 604              // Set the post type comments tracking args.
 605              if ( ! empty( $track_post_type->comments_tracking->action_id ) ) {
 606                  // Used to check support for comment tracking by activity type (new_post_type_comment).
 607                  $track_post_type->comments_tracking->comments_tracking = true;
 608  
 609                  // Used to be able to find the post type this activity type is associated to.
 610                  $track_post_type->comments_tracking->post_type = $post_type;
 611  
 612                  $post_types_tracking_args[ $track_post_type->comments_tracking->action_id ] = $track_post_type->comments_tracking;
 613  
 614                  // Used to check support for comment tracking by activity type (new_post_type).
 615                  $track_post_type->comments_tracking = true;
 616              }
 617  
 618              $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type;
 619          }
 620  
 621      }
 622  
 623      /**
 624       * Filters tracking arguments for all post types.
 625       *
 626       * @since 2.2.0
 627       *
 628       * @param array $post_types_tracking_args Array of post types with
 629       *                                        their tracking arguments.
 630       */
 631      return apply_filters( 'bp_activity_get_post_types_tracking_args', $post_types_tracking_args );
 632  }
 633  
 634  /**
 635   * Check if the *Post Type* activity supports a specific feature.
 636   *
 637   * @since 2.5.0
 638   *
 639   * @param  string $activity_type The activity type to check.
 640   * @param  string $feature       The feature to check. Currently supports:
 641   *                               'post-type-comment-tracking', 'post-type-comment-reply' & 'comment-reply'.
 642   *                               See inline doc for more info.
 643   * @return bool
 644   */
 645  function bp_activity_type_supports( $activity_type = '', $feature = '' ) {
 646      $retval = false;
 647  
 648      $bp = buddypress();
 649  
 650      switch ( $feature ) {
 651          /**
 652           * Does this activity type support comment tracking?
 653           *
 654           * eg. 'new_blog_post' and 'new_blog_comment' will both return true.
 655           */
 656          case 'post-type-comment-tracking' :
 657              // Set the activity track global if not set yet.
 658              if ( empty( $bp->activity->track ) ) {
 659                  $bp->activity->track = bp_activity_get_post_types_tracking_args();
 660              }
 661  
 662              if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
 663                  $retval = true;
 664              }
 665              break;
 666  
 667          /**
 668           * Is this a parent activity type that support post comments?
 669           *
 670           * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false.
 671           */
 672          case 'post-type-comment-reply' :
 673              // Set the activity track global if not set yet.
 674              if ( empty( $bp->activity->track ) ) {
 675                  $bp->activity->track = bp_activity_get_post_types_tracking_args();
 676              }
 677  
 678              if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) {
 679                  $retval = true;
 680              }
 681              break;
 682  
 683          /**
 684           * Does this activity type support comment & reply?
 685           */
 686          case 'comment-reply' :
 687              // Set the activity track global if not set yet.
 688              if ( empty( $bp->activity->track ) ) {
 689                  $bp->activity->track = bp_activity_get_post_types_tracking_args();
 690              }
 691  
 692              // Post Type activities
 693              if ( ! empty( $bp->activity->track[ $activity_type ] ) ) {
 694                  if ( isset( $bp->activity->track[ $activity_type ]->activity_comment ) ) {
 695                      $retval = $bp->activity->track[ $activity_type ]->activity_comment;
 696                  }
 697  
 698                  // Eventually override with comment synchronization feature.
 699                  if ( isset( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
 700                      $retval = $bp->activity->track[ $activity_type ]->comments_tracking && ! bp_disable_blogforum_comments();
 701                  }
 702  
 703              // Retired Forums component
 704              } elseif ( 'new_forum_topic' === $activity_type || 'new_forum_post' === $activity_type ) {
 705                  $retval = ! bp_disable_blogforum_comments();
 706  
 707              // By Default, all other activity types are supporting comments.
 708              } else {
 709                  $retval = true;
 710              }
 711              break;
 712      }
 713  
 714      return $retval;
 715  }
 716  
 717  /**
 718   * Get a specific tracking argument for a given activity type
 719   *
 720   * @since 2.5.0
 721   *
 722   * @param  string       $activity_type the activity type.
 723   * @param  string       $arg           the key of the tracking argument.
 724   * @return mixed        the value of the tracking arg, false if not found.
 725   */
 726  function bp_activity_post_type_get_tracking_arg( $activity_type, $arg = '' ) {
 727      if ( empty( $activity_type ) || empty( $arg ) ) {
 728          return false;
 729      }
 730  
 731      $bp = buddypress();
 732  
 733      // Set the activity track global if not set yet.
 734      if ( empty( $bp->activity->track ) ) {
 735          $bp->activity->track = bp_activity_get_post_types_tracking_args();
 736      }
 737  
 738      if ( isset( $bp->activity->track[ $activity_type ]->{$arg} ) ) {
 739          return $bp->activity->track[ $activity_type ]->{$arg};
 740      } else {
 741          return false;
 742      }
 743  }
 744  
 745  /**
 746   * Get all components' activity actions, sorted by their position attribute.
 747   *
 748   * @since 2.2.0
 749   *
 750   * @return object Actions ordered by their position.
 751   */
 752  function bp_activity_get_actions() {
 753      $bp = buddypress();
 754  
 755      // Set the activity track global if not set yet.
 756      if ( empty( $bp->activity->track ) ) {
 757          $bp->activity->track = bp_activity_get_post_types_tracking_args();
 758      }
 759  
 760      // Create the actions for the post types, if they haven't already been created.
 761      if ( ! empty( $bp->activity->track ) ) {
 762          foreach ( $bp->activity->track as $post_type ) {
 763              if ( isset( $bp->activity->actions->{$post_type->component_id}->{$post_type->action_id} ) ) {
 764                  continue;
 765              }
 766  
 767              bp_activity_set_action(
 768                  $post_type->component_id,
 769                  $post_type->action_id,
 770                  $post_type->admin_filter,
 771                  $post_type->format_callback,
 772                  $post_type->front_filter,
 773                  $post_type->contexts,
 774                  $post_type->position
 775              );
 776          }
 777      }
 778  
 779      return $bp->activity->actions;
 780  }
 781  
 782  /**
 783   * Retrieve the current action from a component and key.
 784   *
 785   * @since 1.1.0
 786   *
 787   * @param string $component_id The unique string ID of the component.
 788   * @param string $key          The action key.
 789   * @return string|bool Action value if found, otherwise false.
 790   */
 791  function bp_activity_get_action( $component_id, $key ) {
 792  
 793      // Return false if any of the above values are not set.
 794      if ( empty( $component_id ) || empty( $key ) ) {
 795          return false;
 796      }
 797  
 798      $actions = bp_activity_get_actions();
 799      $retval  = false;
 800  
 801      if ( isset( $actions->{$component_id}->{$key} ) ) {
 802          $retval = $actions->{$component_id}->{$key};
 803      }
 804  
 805      /**
 806       * Filters the current action by component and key.
 807       *
 808       * @since 1.1.0
 809       *
 810       * @param string|bool $retval       The action key.
 811       * @param string      $component_id The unique string ID of the component.
 812       * @param string      $key          The action key.
 813       */
 814      return apply_filters( 'bp_activity_get_action', $retval, $component_id, $key );
 815  }
 816  
 817  /**
 818   * Fetch details of all registered activity types.
 819   *
 820   * @since 1.7.0
 821   *
 822   * @return array array( type => description ), ...
 823   */
 824  function bp_activity_get_types() {
 825      $actions  = array();
 826  
 827      // Walk through the registered actions, and build an array of actions/values.
 828      foreach ( bp_activity_get_actions() as $action ) {
 829          $action = array_values( (array) $action );
 830  
 831          for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) {
 832              $actions[ $action[$i]['key'] ] = $action[$i]['value'];
 833          }
 834      }
 835  
 836      // This was a mis-named activity type from before BP 1.6.
 837      unset( $actions['friends_register_activity_action'] );
 838  
 839      /**
 840       * Filters the available activity types.
 841       *
 842       * @since 1.7.0
 843       *
 844       * @param array $actions Array of registered activity types.
 845       */
 846      return apply_filters( 'bp_activity_get_types', $actions );
 847  }
 848  
 849  /**
 850   * Returns the list of available BuddyPress activity types.
 851   *
 852   * @since 9.0.0
 853   *
 854   * @return array An array of activity type labels keyed by type names.
 855   */
 856  function bp_activity_get_types_list() {
 857      $actions_object = bp_activity_get_actions();
 858      $actions_array  = get_object_vars( $actions_object );
 859  
 860      $types = array();
 861      foreach ( $actions_array as $component => $actions ) {
 862          $new_types = wp_list_pluck( $actions, 'label', 'key' );
 863  
 864          if ( $types ) {
 865              // Makes sure activity types are unique.
 866              $new_types = array_diff_key( $new_types, $types );
 867  
 868              if ( 'friends' === $component ) {
 869                  $new_types = array_diff_key(
 870                      array(
 871                          'friendship_accepted'              => false,
 872                          'friendship_created'               => false,
 873                          'friends_register_activity_action' => false,
 874                      ),
 875                      $new_types
 876                  );
 877  
 878                  $new_types['friendship_accepted,friendship_created'] = __( 'Friendships', 'buddypress' );
 879              }
 880          }
 881  
 882          $types = array_merge( $types, $new_types );
 883      }
 884  
 885      /**
 886       * Filter here to edit the activity types list.
 887       *
 888       * @since 9.0.0
 889       *
 890       * @param array $types An array of activity type labels keyed by type names.
 891       */
 892      return apply_filters( 'bp_activity_get_types_list', $types );
 893  }
 894  
 895  /**
 896   * Gets the current activity context.
 897   *
 898   * The "context" is the current view type, corresponding roughly to the
 899   * current component. Use this context to determine which activity actions
 900   * should be permitted in the filter dropdown.
 901   *
 902   * @since 2.8.0
 903   *
 904   * @return string Activity context. 'member', 'member_groups', 'group', 'activity'.
 905   */
 906  function bp_activity_get_current_context() {
 907      // On member pages, default to 'member', unless this is a user's Groups activity.
 908      if ( bp_is_user() ) {
 909          if ( bp_is_active( 'groups' ) && bp_is_current_action( bp_get_groups_slug() ) ) {
 910              $context = 'member_groups';
 911          } else {
 912              $context = 'member';
 913          }
 914  
 915      // On individual group pages, default to 'group'.
 916      } elseif ( bp_is_active( 'groups' ) && bp_is_group() ) {
 917          $context = 'group';
 918  
 919      // 'activity' everywhere else.
 920      } else {
 921          $context = 'activity';
 922      }
 923  
 924      return $context;
 925  }
 926  
 927  /**
 928   * Gets a flat list of activity actions compatible with a given context.
 929   *
 930   * @since 2.8.0
 931   *
 932   * @param string $context Optional. Name of the context. Defaults to the current context.
 933   * @return array
 934   */
 935  function bp_activity_get_actions_for_context( $context = '' ) {
 936      if ( ! $context ) {
 937          $context = bp_activity_get_current_context();
 938      }
 939  
 940      $actions = array();
 941      foreach ( bp_activity_get_actions() as $component_actions ) {
 942          foreach ( $component_actions as $component_action ) {
 943              if ( in_array( $context, (array) $component_action['context'], true ) ) {
 944                  $actions[] = $component_action;
 945              }
 946          }
 947      }
 948  
 949      return $actions;
 950  }
 951  
 952  /** Favorites ****************************************************************/
 953  
 954  /**
 955   * Get a users favorite activity stream items.
 956   *
 957   * @since 1.2.0
 958   *
 959   * @param int $user_id ID of the user whose favorites are being queried.
 960   * @return array IDs of the user's favorite activity items.
 961   */
 962  function bp_activity_get_user_favorites( $user_id = 0 ) {
 963  
 964      // Fallback to logged in user if no user_id is passed.
 965      if ( empty( $user_id ) ) {
 966          $user_id = bp_displayed_user_id();
 967      }
 968  
 969      // Get favorites for user.
 970      $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
 971  
 972      /**
 973       * Filters the favorited activity items for a specified user.
 974       *
 975       * @since 1.2.0
 976       *
 977       * @param array $favs Array of user's favorited activity items.
 978       */
 979      return apply_filters( 'bp_activity_get_user_favorites', $favs );
 980  }
 981  
 982  /**
 983   * Add an activity stream item as a favorite for a user.
 984   *
 985   * @since 1.2.0
 986   *
 987   * @param int $activity_id ID of the activity item being favorited.
 988   * @param int $user_id     ID of the user favoriting the activity item.
 989   * @return bool True on success, false on failure.
 990   */
 991  function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {
 992  
 993      // Fallback to logged in user if no user_id is passed.
 994      if ( empty( $user_id ) ) {
 995          $user_id = bp_loggedin_user_id();
 996      }
 997  
 998      $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
 999      if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {
1000          $my_favs = array();
1001      }
1002  
1003      // Bail if the user has already favorited this activity item.
1004      if ( in_array( $activity_id, $my_favs ) ) {
1005          return false;
1006      }
1007  
1008      // Add to user's favorites.
1009      $my_favs[] = $activity_id;
1010  
1011      // Update the total number of users who have favorited this activity.
1012      $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
1013      $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;
1014  
1015      // Update user meta.
1016      bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
1017  
1018      // Update activity meta counts.
1019      if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
1020  
1021          /**
1022           * Fires if bp_activity_update_meta() for favorite_count is successful and before returning a true value for success.
1023           *
1024           * @since 1.2.1
1025           *
1026           * @param int $activity_id ID of the activity item being favorited.
1027           * @param int $user_id     ID of the user doing the favoriting.
1028           */
1029          do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
1030  
1031          // Success.
1032          return true;
1033  
1034      // Saving meta was unsuccessful for an unknown reason.
1035      } else {
1036  
1037          /**
1038           * Fires if bp_activity_update_meta() for favorite_count is unsuccessful and before returning a false value for failure.
1039           *
1040           * @since 1.5.0
1041           *
1042           * @param int $activity_id ID of the activity item being favorited.
1043           * @param int $user_id     ID of the user doing the favoriting.
1044           */
1045          do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id );
1046  
1047          return false;
1048      }
1049  }
1050  
1051  /**
1052   * Remove an activity stream item as a favorite for a user.
1053   *
1054   * @since 1.2.0
1055   *
1056   * @param int $activity_id ID of the activity item being unfavorited.
1057   * @param int $user_id     ID of the user unfavoriting the activity item.
1058   * @return bool True on success, false on failure.
1059   */
1060  function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {
1061  
1062      // Fallback to logged in user if no user_id is passed.
1063      if ( empty( $user_id ) ) {
1064          $user_id = bp_loggedin_user_id();
1065      }
1066  
1067      $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );
1068      $my_favs = array_flip( (array) $my_favs );
1069  
1070      // Bail if the user has not previously favorited the item.
1071      if ( ! isset( $my_favs[ $activity_id ] ) ) {
1072          return false;
1073      }
1074  
1075      // Remove the fav from the user's favs.
1076      unset( $my_favs[$activity_id] );
1077      $my_favs = array_unique( array_flip( $my_favs ) );
1078  
1079      // Update the total number of users who have favorited this activity.
1080      $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
1081      if ( ! empty( $fav_count ) ) {
1082  
1083          // Deduct from total favorites.
1084          if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {
1085  
1086              // Update users favorites.
1087              if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
1088  
1089                  /**
1090                   * Fires if bp_update_user_meta() is successful and before returning a true value for success.
1091                   *
1092                   * @since 1.2.1
1093                   *
1094                   * @param int $activity_id ID of the activity item being unfavorited.
1095                   * @param int $user_id     ID of the user doing the unfavoriting.
1096                   */
1097                  do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
1098  
1099                  // Success.
1100                  return true;
1101  
1102              // Error updating.
1103              } else {
1104                  return false;
1105              }
1106  
1107          // Error updating favorite count.
1108          } else {
1109              return false;
1110          }
1111  
1112      // Error getting favorite count.
1113      } else {
1114          return false;
1115      }
1116  }
1117  
1118  /**
1119   * Check whether an activity item exists with a given content string.
1120   *
1121   * @since 1.1.0
1122   *
1123   * @param string $content The content to filter by.
1124   * @return int|null The ID of the located activity item. Null if none is found.
1125   */
1126  function bp_activity_check_exists_by_content( $content ) {
1127  
1128      /**
1129       * Filters the results of the check for whether an activity item exists by specified content.
1130       *
1131       * @since 1.1.0
1132       *
1133       * @param BP_Activity_Activity $value ID of the activity if found, else null.
1134       */
1135      return apply_filters( 'bp_activity_check_exists_by_content', BP_Activity_Activity::check_exists_by_content( $content ) );
1136  }
1137  
1138  /**
1139   * Retrieve the last time activity was updated.
1140   *
1141   * @since 1.0.0
1142   *
1143   * @return string Date last updated.
1144   */
1145  function bp_activity_get_last_updated() {
1146  
1147      /**
1148       * Filters the value for the last updated time for an activity item.
1149       *
1150       * @since 1.1.0
1151       *
1152       * @param BP_Activity_Activity $last_updated Date last updated.
1153       */
1154      return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() );
1155  }
1156  
1157  /**
1158   * Retrieve the number of favorite activity stream items a user has.
1159   *
1160   * @since 1.2.0
1161   *
1162   * @param int $user_id ID of the user whose favorite count is being requested.
1163   * @return int Total favorite count for the user.
1164   */
1165  function bp_activity_total_favorites_for_user( $user_id = 0 ) {
1166  
1167      // Fallback on displayed user, and then logged in user.
1168      if ( empty( $user_id ) ) {
1169          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
1170      }
1171  
1172      return BP_Activity_Activity::total_favorite_count( $user_id );
1173  }
1174  
1175  /** Meta *********************************************************************/
1176  
1177  /**
1178   * Delete a meta entry from the DB for an activity stream item.
1179   *
1180   * @since 1.2.0
1181   *
1182   * @global object $wpdb WordPress database access object.
1183   *
1184   * @param int    $activity_id ID of the activity item whose metadata is being deleted.
1185   * @param string $meta_key    Optional. The key of the metadata being deleted. If
1186   *                            omitted, all metadata associated with the activity
1187   *                            item will be deleted.
1188   * @param string $meta_value  Optional. If present, the metadata will only be
1189   *                            deleted if the meta_value matches this parameter.
1190   * @param bool   $delete_all  Optional. If true, delete matching metadata entries
1191   *                            for all objects, ignoring the specified object_id. Otherwise,
1192   *                            only delete matching metadata entries for the specified
1193   *                            activity item. Default: false.
1194   * @return bool True on success, false on failure.
1195   */
1196  function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
1197  
1198      // Legacy - if no meta_key is passed, delete all for the item.
1199      if ( empty( $meta_key ) ) {
1200          $all_meta = bp_activity_get_meta( $activity_id );
1201          $keys     = ! empty( $all_meta ) ? array_keys( $all_meta ) : array();
1202  
1203          // With no meta_key, ignore $delete_all.
1204          $delete_all = false;
1205      } else {
1206          $keys = array( $meta_key );
1207      }
1208  
1209      $retval = true;
1210  
1211      add_filter( 'query', 'bp_filter_metaid_column_name' );
1212      foreach ( $keys as $key ) {
1213          $retval = delete_metadata( 'activity', $activity_id, $key, $meta_value, $delete_all );
1214      }
1215      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1216  
1217      return $retval;
1218  }
1219  
1220  /**
1221   * Get metadata for a given activity item.
1222   *
1223   * @since 1.2.0
1224   *
1225   * @param int    $activity_id ID of the activity item whose metadata is being requested.
1226   * @param string $meta_key    Optional. If present, only the metadata matching
1227   *                            that meta key will be returned. Otherwise, all metadata for the
1228   *                            activity item will be fetched.
1229   * @param bool   $single      Optional. If true, return only the first value of the
1230   *                            specified meta_key. This parameter has no effect if meta_key is not
1231   *                            specified. Default: true.
1232   * @return mixed The meta value(s) being requested.
1233   */
1234  function bp_activity_get_meta( $activity_id = 0, $meta_key = '', $single = true ) {
1235      add_filter( 'query', 'bp_filter_metaid_column_name' );
1236      $retval = get_metadata( 'activity', $activity_id, $meta_key, $single );
1237      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1238  
1239      /**
1240       * Filters the metadata for a specified activity item.
1241       *
1242       * @since 1.5.0
1243       *
1244       * @param mixed  $retval      The meta values for the activity item.
1245       * @param int    $activity_id ID of the activity item.
1246       * @param string $meta_key    Meta key for the value being requested.
1247       * @param bool   $single      Whether to return one matched meta key row or all.
1248       */
1249      return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key, $single );
1250  }
1251  
1252  /**
1253   * Update a piece of activity meta.
1254   *
1255   * @since 1.2.0
1256   *
1257   * @param int    $activity_id ID of the activity item whose metadata is being updated.
1258   * @param string $meta_key    Key of the metadata being updated.
1259   * @param mixed  $meta_value  Value to be set.
1260   * @param mixed  $prev_value  Optional. If specified, only update existing metadata entries
1261   *                            with the specified value. Otherwise, update all entries.
1262   * @return bool|int Returns false on failure. On successful update of existing
1263   *                  metadata, returns true. On successful creation of new metadata,
1264   *                  returns the integer ID of the new metadata row.
1265   */
1266  function bp_activity_update_meta( $activity_id, $meta_key, $meta_value, $prev_value = '' ) {
1267      add_filter( 'query', 'bp_filter_metaid_column_name' );
1268      $retval = update_metadata( 'activity', $activity_id, $meta_key, $meta_value, $prev_value );
1269      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1270  
1271      return $retval;
1272  }
1273  
1274  /**
1275   * Add a piece of activity metadata.
1276   *
1277   * @since 2.0.0
1278   *
1279   * @param int    $activity_id ID of the activity item.
1280   * @param string $meta_key    Metadata key.
1281   * @param mixed  $meta_value  Metadata value.
1282   * @param bool   $unique      Optional. Whether to enforce a single metadata value for the
1283   *                            given key. If true, and the object already has a value for
1284   *                            the key, no change will be made. Default: false.
1285   * @return int|bool The meta ID on successful update, false on failure.
1286   */
1287  function bp_activity_add_meta( $activity_id, $meta_key, $meta_value, $unique = false ) {
1288      add_filter( 'query', 'bp_filter_metaid_column_name' );
1289      $retval = add_metadata( 'activity', $activity_id, $meta_key, $meta_value, $unique );
1290      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1291  
1292      return $retval;
1293  }
1294  
1295  /** Clean up *****************************************************************/
1296  
1297  /**
1298   * Completely remove a user's activity data.
1299   *
1300   * @since 1.5.0
1301   *
1302   * @param int $user_id ID of the user whose activity is being deleted.
1303   * @return bool
1304   */
1305  function bp_activity_remove_all_user_data( $user_id = 0 ) {
1306      if ( empty( $user_id ) ) {
1307          return false;
1308      }
1309  
1310      // Clear the user's activity from the sitewide stream and clear their activity tables.
1311      bp_activity_delete( array( 'user_id' => $user_id ) );
1312  
1313      // Remove any usermeta.
1314      bp_delete_user_meta( $user_id, 'bp_latest_update'       );
1315      bp_delete_user_meta( $user_id, 'bp_favorite_activities' );
1316  
1317      // Execute additional code
1318      do_action( 'bp_activity_remove_data', $user_id ); // Deprecated! Do not use!
1319  
1320      /**
1321       * Fires after the removal of all of a user's activity data.
1322       *
1323       * @since 1.5.0
1324       *
1325       * @param int $user_id ID of the user being deleted.
1326       */
1327      do_action( 'bp_activity_remove_all_user_data', $user_id );
1328  }
1329  add_action( 'wpmu_delete_user',  'bp_activity_remove_all_user_data' );
1330  
1331  /**
1332   * Deletes user activity data on the 'delete_user' hook.
1333   *
1334   * @since 6.0.0
1335   *
1336   * @param int $user_id The ID of the deleted user.
1337   */
1338  function bp_activity_remove_all_user_data_on_delete_user( $user_id ) {
1339      if ( ! bp_remove_user_data_on_delete_user_hook( 'activity', $user_id ) ) {
1340          return;
1341      }
1342  
1343      bp_activity_remove_all_user_data( $user_id );
1344  }
1345  add_action( 'delete_user', 'bp_activity_remove_all_user_data_on_delete_user' );
1346  
1347  /**
1348   * Mark all of the user's activity as spam.
1349   *
1350   * @since 1.6.0
1351   *
1352   * @global object $wpdb WordPress database access object.
1353   *
1354   * @param int $user_id ID of the user whose activity is being spammed.
1355   * @return bool
1356   */
1357  function bp_activity_spam_all_user_data( $user_id = 0 ) {
1358      global $wpdb;
1359  
1360      // Do not delete user data unless a logged in user says so.
1361      if ( empty( $user_id ) || ! is_user_logged_in() ) {
1362          return false;
1363      }
1364  
1365      // Get all the user's activities.
1366      $activities = bp_activity_get( array(
1367          'display_comments' => 'stream',
1368          'filter'           => array( 'user_id' => $user_id ),
1369          'show_hidden'      => true
1370      ) );
1371  
1372      $bp = buddypress();
1373  
1374      // Mark each as spam.
1375      foreach ( (array) $activities['activities'] as $activity ) {
1376  
1377          // Create an activity object.
1378          $activity_obj = new BP_Activity_Activity;
1379          foreach ( $activity as $k => $v ) {
1380              $activity_obj->$k = $v;
1381          }
1382  
1383          // Mark as spam.
1384          bp_activity_mark_as_spam( $activity_obj );
1385  
1386          /*
1387           * If Akismet is present, update the activity history meta.
1388           *
1389           * This is usually taken care of when BP_Activity_Activity::save() happens, but
1390           * as we're going to be updating all the activity statuses directly, for efficiency,
1391           * we need to update manually.
1392           */
1393          if ( ! empty( $bp->activity->akismet ) ) {
1394              $bp->activity->akismet->update_activity_spam_meta( $activity_obj );
1395          }
1396  
1397          // Tidy up.
1398          unset( $activity_obj );
1399      }
1400  
1401      // Mark all of this user's activities as spam.
1402      $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET is_spam = 1 WHERE user_id = %d", $user_id ) );
1403  
1404      /**
1405       * Fires after all activity data from a user has been marked as spam.
1406       *
1407       * @since 1.6.0
1408       *
1409       * @param int   $user_id    ID of the user whose activity is being marked as spam.
1410       * @param array $activities Array of activity items being marked as spam.
1411       */
1412      do_action( 'bp_activity_spam_all_user_data', $user_id, $activities['activities'] );
1413  }
1414  add_action( 'bp_make_spam_user', 'bp_activity_spam_all_user_data' );
1415  
1416  /**
1417   * Mark all of the user's activity as ham (not spam).
1418   *
1419   * @since 1.6.0
1420   *
1421   * @global object $wpdb WordPress database access object.
1422   *
1423   * @param int $user_id ID of the user whose activity is being hammed.
1424   * @return bool
1425   */
1426  function bp_activity_ham_all_user_data( $user_id = 0 ) {
1427      global $wpdb;
1428  
1429      // Do not delete user data unless a logged in user says so.
1430      if ( empty( $user_id ) || ! is_user_logged_in() ) {
1431          return false;
1432      }
1433  
1434      // Get all the user's activities.
1435      $activities = bp_activity_get( array(
1436          'display_comments' => 'stream',
1437          'filter'           => array( 'user_id' => $user_id ),
1438          'show_hidden'      => true,
1439          'spam'             => 'all'
1440      ) );
1441  
1442      $bp = buddypress();
1443  
1444      // Mark each as not spam.
1445      foreach ( (array) $activities['activities'] as $activity ) {
1446  
1447          // Create an activity object.
1448          $activity_obj = new BP_Activity_Activity;
1449          foreach ( $activity as $k => $v ) {
1450              $activity_obj->$k = $v;
1451          }
1452  
1453          // Mark as not spam.
1454          bp_activity_mark_as_ham( $activity_obj );
1455  
1456          /*
1457           * If Akismet is present, update the activity history meta.
1458           *
1459           * This is usually taken care of when BP_Activity_Activity::save() happens, but
1460           * as we're going to be updating all the activity statuses directly, for efficiency,
1461           * we need to update manually.
1462           */
1463          if ( ! empty( $bp->activity->akismet ) ) {
1464              $bp->activity->akismet->update_activity_ham_meta( $activity_obj );
1465          }
1466  
1467          // Tidy up.
1468          unset( $activity_obj );
1469      }
1470  
1471      // Mark all of this user's activities as not spam.
1472      $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET is_spam = 0 WHERE user_id = %d", $user_id ) );
1473  
1474      /**
1475       * Fires after all activity data from a user has been marked as ham.
1476       *
1477       * @since 1.6.0
1478       *
1479       * @param int   $user_id    ID of the user whose activity is being marked as ham.
1480       * @param array $activities Array of activity items being marked as ham.
1481       */
1482      do_action( 'bp_activity_ham_all_user_data', $user_id, $activities['activities'] );
1483  }
1484  add_action( 'bp_make_ham_user', 'bp_activity_ham_all_user_data' );
1485  
1486  /**
1487   * Allow core components and dependent plugins to register activity actions.
1488   *
1489   * @since 1.2.0
1490   */
1491  function bp_register_activity_actions() {
1492      /**
1493       * Fires on bp_init to allow core components and dependent plugins to register activity actions.
1494       *
1495       * @since 1.2.0
1496       */
1497      do_action( 'bp_register_activity_actions' );
1498  }
1499  add_action( 'bp_init', 'bp_register_activity_actions', 8 );
1500  
1501  /**
1502   * Register the activity stream actions for updates.
1503   *
1504   * @since 1.6.0
1505   */
1506  function bp_activity_register_activity_actions() {
1507      $bp = buddypress();
1508  
1509      bp_activity_set_action(
1510          $bp->activity->id,
1511          'activity_update',
1512          __( 'Posted a status update', 'buddypress' ),
1513          'bp_activity_format_activity_action_activity_update',
1514          __( 'Updates', 'buddypress' ),
1515          array( 'activity', 'group', 'member', 'member_groups' )
1516      );
1517  
1518      bp_activity_set_action(
1519          $bp->activity->id,
1520          'activity_comment',
1521          __( 'Replied to a status update', 'buddypress' ),
1522          'bp_activity_format_activity_action_activity_comment',
1523          __( 'Activity Comments', 'buddypress' )
1524      );
1525  
1526      /**
1527       * Fires at the end of the activity actions registration.
1528       *
1529       * Allows plugin authors to add their own activity actions alongside the core actions.
1530       *
1531       * @since 1.6.0
1532       */
1533      do_action( 'bp_activity_register_activity_actions' );
1534  
1535      // Backpat. Don't use this.
1536      do_action( 'updates_register_activity_actions' );
1537  }
1538  add_action( 'bp_register_activity_actions', 'bp_activity_register_activity_actions' );
1539  
1540  /**
1541   * Generate an activity action string for an activity item.
1542   *
1543   * @since 2.0.0
1544   *
1545   * @param object $activity Activity data object.
1546   * @return string|bool Returns false if no callback is found, otherwise returns
1547   *                     the formatted action string.
1548   */
1549  function bp_activity_generate_action_string( $activity ) {
1550  
1551      // Check for valid input.
1552      if ( empty( $activity->component ) || empty( $activity->type ) ) {
1553          return false;
1554      }
1555  
1556      // Check for registered format callback.
1557      $actions = bp_activity_get_actions();
1558      if ( empty( $actions->{$activity->component}->{$activity->type}['format_callback'] ) ) {
1559          return false;
1560      }
1561  
1562      // We apply the format_callback as a filter.
1563      add_filter( 'bp_activity_generate_action_string', $actions->{$activity->component}->{$activity->type}['format_callback'], 10, 2 );
1564  
1565      /**
1566       * Filters the string for the activity action being returned.
1567       *
1568       * @since 2.0.0
1569       *
1570       * @param BP_Activity_Activity $action   Action string being requested.
1571       * @param BP_Activity_Activity $activity Activity item object.
1572       */
1573      $action = apply_filters( 'bp_activity_generate_action_string', $activity->action, $activity );
1574  
1575      // Remove the filter for future activity items.
1576      remove_filter( 'bp_activity_generate_action_string', $actions->{$activity->component}->{$activity->type}['format_callback'], 10 );
1577  
1578      return $action;
1579  }
1580  
1581  /**
1582   * Format 'activity_update' activity actions.
1583   *
1584   * @since 2.0.0
1585   *
1586   * @param string $action   Static activity action.
1587   * @param object $activity Activity data object.
1588   * @return string $action
1589   */
1590  function bp_activity_format_activity_action_activity_update( $action, $activity ) {
1591      $action = sprintf(
1592          /* translators: %s: the activity author user link */
1593          esc_html__( '%s posted an update', 'buddypress' ),
1594          bp_core_get_userlink( $activity->user_id )
1595      );
1596  
1597      /**
1598       * Filters the formatted activity action update string.
1599       *
1600       * @since 1.2.0
1601       *
1602       * @param string               $action   Activity action string value.
1603       * @param BP_Activity_Activity $activity Activity item object.
1604       */
1605      return apply_filters( 'bp_activity_new_update_action', $action, $activity );
1606  }
1607  
1608  /**
1609   * Format 'activity_comment' activity actions.
1610   *
1611   * @since 2.0.0
1612   *
1613   * @param string $action   Static activity action.
1614   * @param object $activity Activity data object.
1615   * @return string $action
1616   */
1617  function bp_activity_format_activity_action_activity_comment( $action, $activity ) {
1618      $action = sprintf(
1619          /* translators: %s: the activity author user link */
1620          esc_html__( '%s posted a new activity comment', 'buddypress' ),
1621          bp_core_get_userlink( $activity->user_id )
1622      );
1623  
1624      /**
1625       * Filters the formatted activity action comment string.
1626       *
1627       * @since 1.2.0
1628       *
1629       * @param string               $action   Activity action string value.
1630       * @param BP_Activity_Activity $activity Activity item object.
1631       */
1632      return apply_filters( 'bp_activity_comment_action', $action, $activity );
1633  }
1634  
1635  /**
1636   * Format activity action strings for custom post types.
1637   *
1638   * @since 2.2.0
1639   *
1640   * @param string $action   Static activity action.
1641   * @param object $activity Activity data object.
1642   * @return string $action
1643   */
1644  function bp_activity_format_activity_action_custom_post_type_post( $action, $activity ) {
1645      $bp = buddypress();
1646  
1647      // Fetch all the tracked post types once.
1648      if ( empty( $bp->activity->track ) ) {
1649          $bp->activity->track = bp_activity_get_post_types_tracking_args();
1650      }
1651  
1652      if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
1653          return $action;
1654      }
1655  
1656      $user_link = bp_core_get_userlink( $activity->user_id );
1657      $blog_url  = get_home_url( $activity->item_id );
1658  
1659      if ( empty( $activity->post_url ) ) {
1660          $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
1661      } else {
1662          $post_url = $activity->post_url;
1663      }
1664  
1665      $post_link = '<a href="' . esc_url( $post_url ) . '">' . esc_html_x( 'item', 'Default text for the post type name', 'buddypress' ) . '</a>';
1666  
1667      if ( is_multisite() ) {
1668          $blog_link = '<a href="' . esc_url( $blog_url ) . '">' . esc_html( get_blog_option( $activity->item_id, 'blogname' ) ) . '</a>';
1669  
1670          if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_action_ms ) ) {
1671              $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_action_ms, $user_link, esc_url( $post_url ), $blog_link );
1672          } else {
1673              /* translators: 1: the activity author user link. 2: the post link. 3: the blog link. */
1674              $action = sprintf( esc_html_x( '%1$s wrote a new %2$s, on the site %3$s', 'Activity Custom Post Type post action', 'buddypress' ), $user_link, $post_link, $blog_link );
1675          }
1676      } else {
1677          if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_action ) ) {
1678              $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_action, $user_link, $post_url );
1679          } else {
1680              /* translators: 1: the activity author user link. 2: the post link. */
1681              $action = sprintf( esc_html_x( '%1$s wrote a new %2$s', 'Activity Custom Post Type post action', 'buddypress' ), $user_link, $post_link );
1682          }
1683      }
1684  
1685      /**
1686       * Filters the formatted custom post type activity post action string.
1687       *
1688       * @since 2.2.0
1689       *
1690       * @param string               $action   Activity action string value.
1691       * @param BP_Activity_Activity $activity Activity item object.
1692       */
1693      return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity );
1694  }
1695  
1696  /**
1697   * Format activity action strings for custom post types comments.
1698   *
1699   * @since 2.5.0
1700   *
1701   * @param string $action   Static activity action.
1702   * @param object $activity Activity data object.
1703   *
1704   * @return string
1705   */
1706  function bp_activity_format_activity_action_custom_post_type_comment( $action, $activity ) {
1707      $bp = buddypress();
1708  
1709      // Fetch all the tracked post types once.
1710      if ( empty( $bp->activity->track ) ) {
1711          $bp->activity->track = bp_activity_get_post_types_tracking_args();
1712      }
1713  
1714      if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
1715          return $action;
1716      }
1717  
1718      $user_link = bp_core_get_userlink( $activity->user_id );
1719      $post_link = '<a href="' . esc_url( $activity->primary_link ) . '">' . esc_html_x( 'item', 'Default text for the post type name', 'buddypress' ) . '</a>';
1720  
1721      if ( is_multisite() ) {
1722          $blog_link = '<a href="' . esc_url( get_home_url( $activity->item_id ) ) . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>';
1723  
1724          if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms ) ) {
1725              $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms, $user_link, $activity->primary_link, $blog_link );
1726          } else {
1727              /* translators: 1: the activity author user link. 2: the post link. 3: the blog link. */
1728              $action = sprintf( esc_html_x( '%1$s commented on the %2$s, on the site %3$s', 'Activity Custom Post Type comment action', 'buddypress' ), $user_link, $post_link, $blog_link );
1729          }
1730      } else {
1731          if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action ) ) {
1732              $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action, $user_link, $activity->primary_link );
1733          } else {
1734              /* translators: 1: the activity author user link. 2: the post link. */
1735              $action = sprintf( esc_html_x( '%1$s commented on the %2$s', 'Activity Custom Post Type post comment action', 'buddypress' ), $user_link, $post_link );
1736          }
1737      }
1738  
1739      /**
1740       * Filters the formatted custom post type activity comment action string.
1741       *
1742       * @since 2.5.0
1743       *
1744       * @param string               $action   Activity action string value.
1745       * @param BP_Activity_Activity $activity Activity item object.
1746       */
1747      return apply_filters( 'bp_activity_custom_post_type_comment_action', $action, $activity );
1748  }
1749  
1750  /*
1751   * Business functions are where all the magic happens in BuddyPress. They will
1752   * handle the actual saving or manipulation of information. Usually they will
1753   * hand off to a database class for data access, then return
1754   * true or false on success or failure.
1755   */
1756  
1757  /**
1758   * Retrieve an activity or activities.
1759   *
1760   * The bp_activity_get() function shares all arguments with BP_Activity_Activity::get().
1761   * The following is a list of bp_activity_get() parameters that have different
1762   * default values from BP_Activity_Activity::get() (value in parentheses is
1763   * the default for the bp_activity_get()).
1764   *   - 'per_page' (false)
1765   *
1766   * @since 1.2.0
1767   * @since 2.4.0 Introduced the `$fields` parameter.
1768   *
1769   * @see BP_Activity_Activity::get() For more information on accepted arguments
1770   *      and the format of the returned value.
1771   *
1772   * @param array|string $args See BP_Activity_Activity::get() for description.
1773   * @return array $activity See BP_Activity_Activity::get() for description.
1774   */
1775  function bp_activity_get( $args = '' ) {
1776  
1777      $r = bp_parse_args(
1778          $args,
1779          array(
1780              'max'               => false,        // Maximum number of results to return.
1781              'fields'            => 'all',
1782              'page'              => 1,            // Page 1 without a per_page will result in no pagination.
1783              'per_page'          => false,        // results per page.
1784              'sort'              => 'DESC',       // sort ASC or DESC.
1785              'display_comments'  => false,        // False for no comments. 'stream' for within stream display, 'threaded' for below each activity item.
1786  
1787              'search_terms'      => false,        // Pass search terms as a string.
1788              'meta_query'        => false,        // Filter by activity meta. See WP_Meta_Query for format.
1789              'date_query'        => false,        // Filter by date. See first parameter of WP_Date_Query for format.
1790              'filter_query'      => false,
1791              'show_hidden'       => false,        // Show activity items that are hidden site-wide?
1792              'exclude'           => false,        // Comma-separated list of activity IDs to exclude.
1793              'in'                => false,        // Comma-separated list or array of activity IDs to which you.
1794                                                  // want to limit the query.
1795              'spam'              => 'ham_only',   // 'ham_only' (default), 'spam_only' or 'all'.
1796              'update_meta_cache' => true,
1797              'count_total'       => false,
1798              'scope'             => false,
1799  
1800              /**
1801               * Pass filters as an array -- all filter items can be multiple values comma separated:
1802               * array(
1803               *     'user_id'      => false, // User ID to filter on.
1804               *     'object'       => false, // Object to filter on e.g. groups, profile, status, friends.
1805               *     'action'       => false, // Action to filter on e.g. activity_update, profile_updated.
1806               *     'primary_id'   => false, // Object ID to filter on e.g. a group_id or blog_id etc.
1807               *     'secondary_id' => false, // Secondary object ID to filter on e.g. a post_id.
1808               * );
1809               */
1810              'filter' => array()
1811          ),
1812          'activity_get'
1813      );
1814  
1815      $activity = BP_Activity_Activity::get( array(
1816          'page'              => $r['page'],
1817          'per_page'          => $r['per_page'],
1818          'max'               => $r['max'],
1819          'sort'              => $r['sort'],
1820          'search_terms'      => $r['search_terms'],
1821          'meta_query'        => $r['meta_query'],
1822          'date_query'        => $r['date_query'],
1823          'filter_query'      => $r['filter_query'],
1824          'filter'            => $r['filter'],
1825          'scope'             => $r['scope'],
1826          'display_comments'  => $r['display_comments'],
1827          'show_hidden'       => $r['show_hidden'],
1828          'exclude'           => $r['exclude'],
1829          'in'                => $r['in'],
1830          'spam'              => $r['spam'],
1831          'update_meta_cache' => $r['update_meta_cache'],
1832          'count_total'       => $r['count_total'],
1833          'fields'            => $r['fields'],
1834      ) );
1835  
1836      /**
1837       * Filters the requested activity item(s).
1838       *
1839       * @since 1.2.0
1840       *
1841       * @param BP_Activity_Activity $activity Requested activity object.
1842       * @param array                $r        Arguments used for the activity query.
1843       */
1844      return apply_filters_ref_array( 'bp_activity_get', array( &$activity, &$r ) );
1845  }
1846  
1847  /**
1848   * Fetch specific activity items.
1849   *
1850   * @since 1.2.0
1851   *
1852   * @see BP_Activity_Activity::get() For more information on accepted arguments.
1853   *
1854   * @param array|string $args {
1855   *     An array of arguments.
1856   *     All arguments and defaults are shared with BP_Activity_Activity::get(),
1857   *     except for the following:
1858   *     @type string|int|array Single activity ID, comma-separated list of IDs,
1859   *                            or array of IDs.
1860   * }
1861   * @return array See BP_Activity_Activity::get() for description.
1862   */
1863  function bp_activity_get_specific( $args = '' ) {
1864  
1865      $r = bp_parse_args(
1866          $args,
1867          array(
1868              'activity_ids'      => false,      // A single activity_id or array of IDs.
1869              'display_comments'  => false,      // True or false to display threaded comments for these specific activity items.
1870              'max'               => false,      // Maximum number of results to return.
1871              'page'              => 1,          // Page 1 without a per_page will result in no pagination.
1872              'per_page'          => false,      // Results per page.
1873              'show_hidden'       => true,       // When fetching specific items, show all.
1874              'sort'              => 'DESC',     // Sort ASC or DESC.
1875              'spam'              => 'ham_only', // Retrieve items marked as spam.
1876              'update_meta_cache' => true,
1877          ),
1878          'activity_get_specific'
1879      );
1880  
1881      $get_args = array(
1882          'display_comments'  => $r['display_comments'],
1883          'in'                => $r['activity_ids'],
1884          'max'               => $r['max'],
1885          'page'              => $r['page'],
1886          'per_page'          => $r['per_page'],
1887          'show_hidden'       => $r['show_hidden'],
1888          'sort'              => $r['sort'],
1889          'spam'              => $r['spam'],
1890          'update_meta_cache' => $r['update_meta_cache'],
1891      );
1892  
1893      $activity = BP_Activity_Activity::get( $get_args );
1894  
1895      /**
1896       * Filters the requested specific activity item.
1897       *
1898       * @since 1.2.0
1899       *
1900       * @param array $activity The array returned has two keys: total and activity.
1901       * @param array $args     Original passed in arguments.
1902       * @param array $get_args Constructed arguments used with request.
1903       */
1904      return apply_filters( 'bp_activity_get_specific', $activity, $args, $get_args );
1905  }
1906  
1907  /**
1908   * Add an activity item.
1909   *
1910   * @since 1.1.0
1911   * @since 2.6.0 Added 'error_type' parameter to $args.
1912   *
1913   * @param array|string $args {
1914   *     An array of arguments.
1915   *     @type int|bool $id                Pass an activity ID to update an existing item, or
1916   *                                       false to create a new item. Default: false.
1917   *     @type string   $action            Optional. The activity action/description, typically
1918   *                                       something like "Joe posted an update". Values passed to this param
1919   *                                       will be stored in the database and used as a fallback for when the
1920   *                                       activity item's format_callback cannot be found (eg, when the
1921   *                                       component is disabled). As long as you have registered a
1922   *                                       format_callback for your $type, it is unnecessary to include this
1923   *                                       argument - BP will generate it automatically.
1924   *                                       See {@link bp_activity_set_action()}.
1925   *     @type string   $content           Optional. The content of the activity item.
1926   *     @type string   $component         The unique name of the component associated with
1927   *                                       the activity item - 'groups', 'profile', etc.
1928   *     @type string   $type              The specific activity type, used for directory
1929   *                                       filtering. 'new_blog_post', 'activity_update', etc.
1930   *     @type string   $primary_link      Optional. The URL for this item, as used in
1931   *                                       RSS feeds. Defaults to the URL for this activity
1932   *                                       item's permalink page.
1933   *     @type int|bool $user_id           Optional. The ID of the user associated with the activity
1934   *                                       item. May be set to false or 0 if the item is not related
1935   *                                       to any user. Default: the ID of the currently logged-in user.
1936   *     @type int      $item_id           Optional. The ID of the associated item.
1937   *     @type int      $secondary_item_id Optional. The ID of a secondary associated item.
1938   *     @type string   $date_recorded     Optional. The GMT time, in Y-m-d h:i:s format, when
1939   *                                       the item was recorded. Defaults to the current time.
1940   *     @type bool     $hide_sitewide     Should the item be hidden on sitewide streams?
1941   *                                       Default: false.
1942   *     @type bool     $is_spam           Should the item be marked as spam? Default: false.
1943   *     @type string   $error_type        Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
1944   * }
1945   * @return WP_Error|bool|int The ID of the activity on success. False on error.
1946   */
1947  function bp_activity_add( $args = '' ) {
1948  
1949      $r = bp_parse_args(
1950          $args,
1951          array(
1952              'id'                => false,                  // Pass an existing activity ID to update an existing entry.
1953              'action'            => '',                     // The activity action - e.g. "Jon Doe posted an update".
1954              'content'           => '',                     // Optional: The content of the activity item e.g. "BuddyPress is awesome guys!".
1955              'component'         => false,                  // The name/ID of the component e.g. groups, profile, mycomponent.
1956              'type'              => false,                  // The activity type e.g. activity_update, profile_updated.
1957              'primary_link'      => '',                     // Optional: The primary URL for this item in RSS feeds (defaults to activity permalink).
1958              'user_id'           => bp_loggedin_user_id(),  // Optional: The user to record the activity for, can be false if this activity is not for a user.
1959              'item_id'           => false,                  // Optional: The ID of the specific item being recorded, e.g. a blog_id.
1960              'secondary_item_id' => false,                  // Optional: A second ID used to further filter e.g. a comment_id.
1961              'recorded_time'     => bp_core_current_time(), // The GMT time that this activity was recorded.
1962              'hide_sitewide'     => false,                  // Should this be hidden on the sitewide activity stream?
1963              'is_spam'           => false,                  // Is this activity item to be marked as spam?
1964              'error_type'        => 'bool',
1965          ),
1966          'activity_add'
1967      );
1968  
1969      // Make sure we are backwards compatible.
1970      if ( empty( $r['component'] ) && ! empty( $r['component_name'] ) ) {
1971          $r['component'] = $r['component_name'];
1972      }
1973  
1974      if ( empty( $r['type'] ) && ! empty( $r['component_action'] ) ) {
1975          $r['type'] = $r['component_action'];
1976      }
1977  
1978      // Setup activity to be added.
1979      $activity                    = new BP_Activity_Activity( $r['id'] );
1980      $activity->user_id           = $r['user_id'];
1981      $activity->component         = $r['component'];
1982      $activity->type              = $r['type'];
1983      $activity->content           = $r['content'];
1984      $activity->primary_link      = $r['primary_link'];
1985      $activity->item_id           = $r['item_id'];
1986      $activity->secondary_item_id = $r['secondary_item_id'];
1987      $activity->date_recorded     = $r['recorded_time'];
1988      $activity->hide_sitewide     = $r['hide_sitewide'];
1989      $activity->is_spam           = $r['is_spam'];
1990      $activity->error_type        = $r['error_type'];
1991      $activity->action            = ! empty( $r['action'] )
1992                          ? $r['action']
1993                          : bp_activity_generate_action_string( $activity );
1994  
1995      $save = $activity->save();
1996  
1997      if ( 'wp_error' === $r['error_type'] && is_wp_error( $save ) ) {
1998          return $save;
1999      } elseif ( 'bool' === $r['error_type'] && false === $save ) {
2000          return false;
2001      }
2002  
2003      // If this is an activity comment, rebuild the tree.
2004      if ( 'activity_comment' === $activity->type ) {
2005          // Also clear the comment cache for the parent activity ID.
2006          wp_cache_delete( $activity->item_id, 'bp_activity_comments' );
2007  
2008          BP_Activity_Activity::rebuild_activity_comment_tree( $activity->item_id );
2009      }
2010  
2011      wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
2012  
2013      /**
2014       * Fires at the end of the execution of adding a new activity item, before returning the new activity item ID.
2015       *
2016       * @since 1.1.0
2017       * @since 4.0.0 Added the `$activity_id` parameter.
2018       *
2019       * @param array $r           Array of parsed arguments for the activity item being added.
2020       * @param int   $activity_id The id of the activity item being added.
2021       */
2022      do_action( 'bp_activity_add', $r, $activity->id );
2023  
2024      return $activity->id;
2025  }
2026  
2027  /**
2028   * Post an activity update.
2029   *
2030   * @since 1.2.0
2031   *
2032   * @param array|string $args {
2033   *     An array of arguments.
2034   *     @type string $content    The content of the activity update.
2035   *     @type int    $user_id    Optional. Defaults to the logged-in user.
2036   *     @type string $error_type Optional. Error type to return. Either 'bool' or 'wp_error'. Defaults to
2037   *                              'bool' for boolean. 'wp_error' will return a WP_Error object.
2038   * }
2039   * @return int|bool|WP_Error $activity_id The activity id on success. On failure, either boolean false or WP_Error
2040   *                                        object depending on the 'error_type' $args parameter.
2041   */
2042  function bp_activity_post_update( $args = '' ) {
2043  
2044      $r = bp_parse_args(
2045          $args,
2046          array(
2047              'content'    => false,
2048              'user_id'    => bp_loggedin_user_id(),
2049              'error_type' => 'bool',
2050          )
2051      );
2052  
2053      if ( empty( $r['content'] ) || ! strlen( trim( $r['content'] ) ) ) {
2054          if ( 'wp_error' === $r['error_type'] ) {
2055              return new WP_Error( 'bp_activity_missing_content', __( 'Please enter some content to post.', 'buddypress' ) );
2056          }
2057  
2058          return false;
2059      }
2060  
2061      if ( bp_is_user_inactive( $r['user_id'] ) ) {
2062          if ( 'wp_error' === $r['error_type'] ) {
2063              return new WP_Error( 'bp_activity_inactive_user', __( 'User account has not yet been activated.', 'buddypress' ) );
2064          }
2065  
2066          return false;
2067      }
2068  
2069      // Record this on the user's profile.
2070      $activity_content = $r['content'];
2071      $primary_link     = bp_core_get_userlink( $r['user_id'], false, true );
2072  
2073      /**
2074       * Filters the new activity content for current activity item.
2075       *
2076       * @since 1.2.0
2077       *
2078       * @param string $activity_content Activity content posted by user.
2079       */
2080      $add_content = apply_filters( 'bp_activity_new_update_content', $activity_content );
2081  
2082      /**
2083       * Filters the activity primary link for current activity item.
2084       *
2085       * @since 1.2.0
2086       *
2087       * @param string $primary_link Link to the profile for the user who posted the activity.
2088       */
2089      $add_primary_link = apply_filters( 'bp_activity_new_update_primary_link', $primary_link );
2090  
2091      // Now write the values.
2092      $activity_id = bp_activity_add( array(
2093          'user_id'      => $r['user_id'],
2094          'content'      => $add_content,
2095          'primary_link' => $add_primary_link,
2096          'component'    => buddypress()->activity->id,
2097          'type'         => 'activity_update',
2098          'error_type'   => $r['error_type']
2099      ) );
2100  
2101      // Bail on failure.
2102      if ( false === $activity_id || is_wp_error( $activity_id ) ) {
2103          return $activity_id;
2104      }
2105  
2106      /**
2107       * Filters the latest update content for the activity item.
2108       *
2109       * @since 1.6.0
2110       *
2111       * @param string $r                Content of the activity update.
2112       * @param string $activity_content Content of the activity update.
2113       */
2114      $activity_content = apply_filters( 'bp_activity_latest_update_content', $r['content'], $activity_content );
2115  
2116      // Add this update to the "latest update" usermeta so it can be fetched anywhere.
2117      bp_update_user_meta( bp_loggedin_user_id(), 'bp_latest_update', array(
2118          'id'      => $activity_id,
2119          'content' => $activity_content
2120      ) );
2121  
2122      /**
2123       * Fires at the end of an activity post update, before returning the updated activity item ID.
2124       *
2125       * @since 1.2.0
2126       *
2127       * @param string $content     Content of the activity post update.
2128       * @param int    $user_id     ID of the user posting the activity update.
2129       * @param int    $activity_id ID of the activity item being updated.
2130       */
2131      do_action( 'bp_activity_posted_update', $r['content'], $r['user_id'], $activity_id );
2132  
2133      return $activity_id;
2134  }
2135  
2136  /**
2137   * Create an activity item for a newly published post type post.
2138   *
2139   * @since 2.2.0
2140   *
2141   * @param int          $post_id ID of the new post.
2142   * @param WP_Post|null $post    Post object.
2143   * @param int          $user_id ID of the post author.
2144   * @return null|WP_Error|bool|int The ID of the activity on success. False on error.
2145   */
2146  function bp_activity_post_type_publish( $post_id = 0, $post = null, $user_id = 0 ) {
2147  
2148      if ( ! is_a( $post, 'WP_Post' ) ) {
2149          return;
2150      }
2151  
2152      // Get the post type tracking args.
2153      $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
2154  
2155      if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $activity_post_object->action_id ) ) {
2156          return;
2157      }
2158  
2159      if ( empty( $post_id ) ) {
2160          $post_id = $post->ID;
2161      }
2162  
2163      $blog_id = get_current_blog_id();
2164  
2165      if ( empty( $user_id ) ) {
2166          $user_id = (int) $post->post_author;
2167      }
2168  
2169      // Bail if an activity item already exists for this post.
2170      $existing = bp_activity_get( array(
2171          'filter' => array(
2172              'action'       => $activity_post_object->action_id,
2173              'primary_id'   => $blog_id,
2174              'secondary_id' => $post_id,
2175          )
2176      ) );
2177  
2178      if ( ! empty( $existing['activities'] ) ) {
2179          return;
2180      }
2181  
2182      /**
2183       * Filters whether or not to post the activity.
2184       *
2185       * This is a variable filter, dependent on the post type,
2186       * that lets components or plugins bail early if needed.
2187       *
2188       * @since 2.2.0
2189       *
2190       * @param bool $value   Whether or not to continue.
2191       * @param int  $blog_id ID of the current site.
2192       * @param int  $post_id ID of the current post being published.
2193       * @param int  $user_id ID of the current user or post author.
2194       */
2195      if ( false === apply_filters( "bp_activity_{$post->post_type}_pre_publish", true, $blog_id, $post_id, $user_id ) ) {
2196          return;
2197      }
2198  
2199      // Record this in activity streams.
2200      $blog_url = get_home_url( $blog_id );
2201      $post_url = add_query_arg(
2202          'p',
2203          $post_id,
2204          trailingslashit( $blog_url )
2205      );
2206  
2207      // Backward compatibility filters for the 'blogs' component.
2208      if ( 'blogs' == $activity_post_object->component_id )  {
2209          $activity_content      = apply_filters( 'bp_blogs_activity_new_post_content', $post->post_content, $post, $post_url, $post->post_type );
2210          $activity_primary_link = apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_url, $post_id, $post->post_type );
2211      } else {
2212          $activity_content      = $post->post_content;
2213          $activity_primary_link = $post_url;
2214      }
2215  
2216      $activity_args = array(
2217          'user_id'           => $user_id,
2218          'content'           => $activity_content,
2219          'primary_link'      => $activity_primary_link,
2220          'component'         => $activity_post_object->component_id,
2221          'type'              => $activity_post_object->action_id,
2222          'item_id'           => $blog_id,
2223          'secondary_item_id' => $post_id,
2224          'recorded_time'     => $post->post_date_gmt,
2225      );
2226  
2227      if ( ! empty( $activity_args['content'] ) ) {
2228          // Create the excerpt.
2229          $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
2230  
2231          // Backward compatibility filter for blog posts.
2232          if ( 'blogs' == $activity_post_object->component_id )  {
2233              $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post->post_type );
2234          } else {
2235              $activity_args['content'] = $activity_summary;
2236          }
2237      }
2238  
2239      // Set up the action by using the format functions.
2240      $action_args = array_merge( $activity_args, array(
2241          'post_title' => $post->post_title,
2242          'post_url'   => $post_url,
2243      ) );
2244  
2245      $activity_args['action'] = call_user_func_array( $activity_post_object->format_callback, array( '', (object) $action_args ) );
2246  
2247      // Make sure the action is set.
2248      if ( empty( $activity_args['action'] ) ) {
2249          return;
2250      } else {
2251          // Backward compatibility filter for the blogs component.
2252          if ( 'blogs' == $activity_post_object->component_id )  {
2253              $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
2254          }
2255      }
2256  
2257      $activity_id = bp_activity_add( $activity_args );
2258  
2259      /**
2260       * Fires after the publishing of an activity item for a newly published post type post.
2261       *
2262       * @since 2.2.0
2263       *
2264       * @param int     $activity_id   ID of the newly published activity item.
2265       * @param WP_Post $post          Post object.
2266       * @param array   $activity_args Array of activity arguments.
2267       */
2268      do_action( 'bp_activity_post_type_published', $activity_id, $post, $activity_args );
2269  
2270      return $activity_id;
2271  }
2272  
2273  /**
2274   * Update the activity item for a custom post type entry.
2275   *
2276   * @since 2.2.0
2277   *
2278   * @param WP_Post|null $post Post item.
2279   * @return null|WP_Error|bool True on success, false on failure.
2280   */
2281  function bp_activity_post_type_update( $post = null ) {
2282  
2283      if ( ! is_a( $post, 'WP_Post' ) ) {
2284          return;
2285      }
2286  
2287      // Get the post type tracking args.
2288      $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
2289  
2290      if ( empty( $activity_post_object->action_id ) ) {
2291          return;
2292      }
2293  
2294      $activity_id = bp_activity_get_activity_id( array(
2295          'component'         => $activity_post_object->component_id,
2296          'item_id'           => get_current_blog_id(),
2297          'secondary_item_id' => $post->ID,
2298          'type'              => $activity_post_object->action_id,
2299      ) );
2300  
2301      // Activity ID doesn't exist, so stop!
2302      if ( empty( $activity_id ) ) {
2303          return;
2304      }
2305  
2306      // Delete the activity if the post was updated with a password.
2307      if ( ! empty( $post->post_password ) ) {
2308          bp_activity_delete( array( 'id' => $activity_id ) );
2309      }
2310  
2311      // Update the activity entry.
2312      $activity = new BP_Activity_Activity( $activity_id );
2313  
2314      if ( ! empty( $post->post_content ) ) {
2315          $activity_summary = bp_activity_create_summary( $post->post_content, (array) $activity );
2316  
2317          // Backward compatibility filter for the blogs component.
2318          if ( 'blogs' == $activity_post_object->component_id ) {
2319              $activity->content = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $post->post_content, (array) $activity, $post->post_type );
2320          } else {
2321              $activity->content = $activity_summary;
2322          }
2323      }
2324  
2325      // Save the updated activity.
2326      $updated = $activity->save();
2327  
2328      /**
2329       * Fires after the updating of an activity item for a custom post type entry.
2330       *
2331       * @since 2.2.0
2332       * @since 2.5.0 Add the post type tracking args parameter
2333       *
2334       * @param WP_Post              $post                 Post object.
2335       * @param BP_Activity_Activity $activity             Activity object.
2336       * @param object               $activity_post_object The post type tracking args object.
2337       */
2338      do_action( 'bp_activity_post_type_updated', $post, $activity, $activity_post_object );
2339  
2340      return $updated;
2341  }
2342  
2343  /**
2344   * Unpublish an activity for the custom post type.
2345   *
2346   * @since 2.2.0
2347   *
2348   * @param int          $post_id ID of the post being unpublished.
2349   * @param WP_Post|null $post    Post object.
2350   * @return bool True on success, false on failure.
2351   */
2352  function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) {
2353  
2354      if ( ! is_a( $post, 'WP_Post' ) ) {
2355          return;
2356      }
2357  
2358      // Get the post type tracking args.
2359      $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
2360  
2361      if ( empty( $activity_post_object->action_id ) ) {
2362          return;
2363      }
2364  
2365      if ( empty( $post_id ) ) {
2366          $post_id = $post->ID;
2367      }
2368  
2369      $delete_activity_args = array(
2370          'item_id'           => get_current_blog_id(),
2371          'secondary_item_id' => $post_id,
2372          'component'         => $activity_post_object->component_id,
2373          'type'              => $activity_post_object->action_id,
2374          'user_id'           => false,
2375      );
2376  
2377      $deleted = bp_activity_delete_by_item_id( $delete_activity_args );
2378  
2379      /**
2380       * Fires after the unpublishing for the custom post type.
2381       *
2382       * @since 2.2.0
2383       *
2384       * @param array   $delete_activity_args Array of arguments for activity deletion.
2385       * @param WP_Post $post                 Post object.
2386       * @param bool    $activity             Whether or not the activity was successfully deleted.
2387       */
2388      do_action( 'bp_activity_post_type_unpublished', $delete_activity_args, $post, $deleted );
2389  
2390      return $deleted;
2391  }
2392  
2393  /**
2394   * Create an activity item for a newly posted post type comment.
2395   *
2396   * @since 2.5.0
2397   *
2398   * @param  int         $comment_id           ID of the comment.
2399   * @param  bool        $is_approved          Whether the comment is approved or not.
2400   * @param  object|null $activity_post_object The post type tracking args object.
2401   * @return null|WP_Error|bool|int The ID of the activity on success. False on error.
2402   */
2403  function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) {
2404      // Get the users comment.
2405      $post_type_comment = get_comment( $comment_id );
2406  
2407      // Don't record activity if the comment hasn't been approved.
2408      if ( empty( $is_approved ) ) {
2409          return false;
2410      }
2411  
2412      // Don't record activity if no email address has been included.
2413      if ( empty( $post_type_comment->comment_author_email ) ) {
2414          return false;
2415      }
2416  
2417      // Don't record activity if the comment has already been marked as spam.
2418      if ( 'spam' === $is_approved ) {
2419          return false;
2420      }
2421  
2422      // Get the user by the comment author email.
2423      $user = get_user_by( 'email', $post_type_comment->comment_author_email );
2424  
2425      // If user isn't registered, don't record activity.
2426      if ( empty( $user ) ) {
2427          return false;
2428      }
2429  
2430      // Get the user_id.
2431      $user_id = (int) $user->ID;
2432  
2433      // Get blog and post data.
2434      $blog_id = get_current_blog_id();
2435  
2436      // Get the post.
2437      $post_type_comment->post = get_post( $post_type_comment->comment_post_ID );
2438  
2439      if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) {
2440          return false;
2441      }
2442  
2443      /**
2444       * Filters whether to publish activities about the comment regarding the post status
2445       *
2446       * @since 2.5.0
2447       *
2448       * @param bool true to bail, false otherwise.
2449       */
2450      $is_post_status_not_allowed = (bool) apply_filters( 'bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || ! empty( $post_type_comment->post->post_password ) );
2451  
2452      // If this is a password protected post, or not a public post don't record the comment.
2453      if ( $is_post_status_not_allowed ) {
2454          return false;
2455      }
2456  
2457      // Set post type.
2458      $post_type = $post_type_comment->post->post_type;
2459  
2460      if ( empty( $activity_post_object ) ) {
2461          // Get the post type tracking args.
2462          $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
2463  
2464          // Bail if the activity type does not exist.
2465          if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
2466              return false;
2467          }
2468      }
2469  
2470      // Set the $activity_comment_object.
2471      $activity_comment_object = $activity_post_object->comments_tracking;
2472  
2473      /**
2474       * Filters whether or not to post the activity about the comment.
2475       *
2476       * This is a variable filter, dependent on the post type,
2477       * that lets components or plugins bail early if needed.
2478       *
2479       * @since 2.5.0
2480       *
2481       * @param bool $value      Whether or not to continue.
2482       * @param int  $blog_id    ID of the current site.
2483       * @param int  $post_id    ID of the current post being commented.
2484       * @param int  $user_id    ID of the current user.
2485       * @param int  $comment_id ID of the current comment being posted.
2486       */
2487      if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) {
2488          return false;
2489      }
2490  
2491      // Is this an update ?
2492      $activity_id = bp_activity_get_activity_id( array(
2493          'user_id'           => $user_id,
2494          'component'         => $activity_comment_object->component_id,
2495          'type'              => $activity_comment_object->action_id,
2496          'item_id'           => $blog_id,
2497          'secondary_item_id' => $comment_id,
2498      ) );
2499  
2500      // Record this in activity streams.
2501      $comment_link = get_comment_link( $post_type_comment->comment_ID );
2502  
2503      // Backward compatibility filters for the 'blogs' component.
2504      if ( 'blogs' == $activity_comment_object->component_id )  {
2505          $activity_content      = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) );
2506          $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) );
2507      } else {
2508          $activity_content      = $post_type_comment->comment_content;
2509          $activity_primary_link = $comment_link;
2510      }
2511  
2512      $activity_args = array(
2513          'id'            => $activity_id,
2514          'user_id'       => $user_id,
2515          'content'       => $activity_content,
2516          'primary_link'  => $activity_primary_link,
2517          'component'     => $activity_comment_object->component_id,
2518          'recorded_time' => $post_type_comment->comment_date_gmt,
2519      );
2520  
2521      if ( bp_disable_blogforum_comments() ) {
2522          $blog_url = get_home_url( $blog_id );
2523          $post_url = add_query_arg(
2524              'p',
2525              $post_type_comment->post->ID,
2526              trailingslashit( $blog_url )
2527          );
2528  
2529          $activity_args['type']              = $activity_comment_object->action_id;
2530          $activity_args['item_id']           = $blog_id;
2531          $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
2532  
2533          if ( ! empty( $activity_args['content'] ) ) {
2534              // Create the excerpt.
2535              $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
2536  
2537              // Backward compatibility filter for blog comments.
2538              if ( 'blogs' == $activity_post_object->component_id )  {
2539                  $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type );
2540              } else {
2541                  $activity_args['content'] = $activity_summary;
2542              }
2543          }
2544  
2545          // Set up the action by using the format functions.
2546          $action_args = array_merge( $activity_args, array(
2547              'post_title' => $post_type_comment->post->post_title,
2548              'post_url'   => $post_url,
2549              'blog_url'   => $blog_url,
2550              'blog_name'  => get_blog_option( $blog_id, 'blogname' ),
2551          ) );
2552  
2553          $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) );
2554  
2555          // Make sure the action is set.
2556          if ( empty( $activity_args['action'] ) ) {
2557              return;
2558          } else {
2559              // Backward compatibility filter for the blogs component.
2560              if ( 'blogs' === $activity_post_object->component_id )  {
2561                  $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
2562              }
2563          }
2564  
2565          $activity_id = bp_activity_add( $activity_args );
2566      }
2567  
2568      /**
2569       * Fires after the publishing of an activity item for a newly published post type post.
2570       *
2571       * @since 2.5.0
2572       *
2573       * @param int        $activity_id          ID of the newly published activity item.
2574       * @param WP_Comment $post_type_comment    Comment object.
2575       * @param array      $activity_args        Array of activity arguments.
2576       * @param object     $activity_post_object the post type tracking args object.
2577       */
2578      do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) );
2579  
2580      return $activity_id;
2581  }
2582  add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
2583  add_action( 'edit_comment', 'bp_activity_post_type_comment', 10    );
2584  
2585  /**
2586   * Remove an activity item when a comment about a post type is deleted.
2587   *
2588   * @since 2.5.0
2589   *
2590   * @param  int         $comment_id           ID of the comment.
2591   * @param  object|null $activity_post_object The post type tracking args object.
2592   * @return bool True on success. False on error.
2593   */
2594  function bp_activity_post_type_remove_comment( $comment_id = 0, $activity_post_object = null ) {
2595      if ( empty( $activity_post_object ) ) {
2596          $comment = get_comment( $comment_id );
2597          if ( ! $comment ) {
2598              return;
2599          }
2600  
2601          $post_type = get_post_type( $comment->comment_post_ID );
2602          if ( ! $post_type ) {
2603              return;
2604          }
2605  
2606          // Get the post type tracking args.
2607          $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
2608  
2609          // Bail if the activity type does not exist.
2610          if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
2611              return false;
2612          }
2613      }
2614  
2615      // Set the $activity_comment_object.
2616      $activity_comment_object = $activity_post_object->comments_tracking;
2617  
2618      if ( empty( $activity_comment_object->action_id ) ) {
2619          return false;
2620      }
2621  
2622      $deleted = false;
2623  
2624      if ( bp_disable_blogforum_comments() ) {
2625          $deleted = bp_activity_delete_by_item_id( array(
2626              'item_id'           => get_current_blog_id(),
2627              'secondary_item_id' => $comment_id,
2628              'component'         => $activity_comment_object->component_id,
2629              'type'              => $activity_comment_object->action_id,
2630              'user_id'           => false,
2631          ) );
2632      }
2633  
2634      /**
2635       * Fires after the custom post type comment activity was removed.
2636       *
2637       * @since 2.5.0
2638       *
2639       * @param bool       $deleted              True if the activity was deleted false otherwise
2640       * @param WP_Comment $comment              Comment object.
2641       * @param object     $activity_post_object The post type tracking args object.
2642       * @param string     $value                The post type comment activity type.
2643       */
2644      do_action( 'bp_activity_post_type_remove_comment', $deleted, $comment_id, $activity_post_object, $activity_comment_object->action_id );
2645  
2646      return $deleted;
2647  }
2648  add_action( 'delete_comment', 'bp_activity_post_type_remove_comment', 10, 1 );
2649  
2650  /**
2651   * Add an activity comment.
2652   *
2653   * @since 1.2.0
2654   * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments.
2655   *              Add the $primary_link parameter for the array of arguments.
2656   * @since 2.6.0 Added 'error_type' parameter to $args.
2657   *
2658   * @param array|string $args {
2659   *     An array of arguments.
2660   *     @type int    $id                Optional. Pass an ID to update an existing comment.
2661   *     @type string $content           The content of the comment.
2662   *     @type int    $user_id           Optional. The ID of the user making the comment.
2663   *                                     Defaults to the ID of the logged-in user.
2664   *     @type int    $activity_id       The ID of the "root" activity item, ie the oldest
2665   *                                     ancestor of the comment.
2666   *     @type int    $parent_id         Optional. The ID of the parent activity item, ie the item to
2667   *                                     which the comment is an immediate reply. If not provided,
2668   *                                     this value defaults to the $activity_id.
2669   *     @type string $primary_link      Optional. the primary link for the comment.
2670   *                                     Defaults to an empty string.
2671   *     @type bool   $skip_notification Optional. false to send a comment notification, false otherwise.
2672   *                                     Defaults to false.
2673   *     @type string $error_type        Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
2674   * }
2675   * @return WP_Error|bool|int The ID of the comment on success, otherwise false.
2676   */
2677  function bp_activity_new_comment( $args = '' ) {
2678      $bp = buddypress();
2679      $r  = bp_parse_args(
2680          $args,
2681          array(
2682              'id'                => false,
2683              'content'           => false,
2684              'user_id'           => bp_loggedin_user_id(),
2685              'activity_id'       => false, // ID of the root activity item.
2686              'parent_id'         => false, // ID of a parent comment (optional).
2687              'primary_link'      => '',
2688              'skip_notification' => false,
2689              'error_type'        => 'bool',
2690          )
2691      );
2692  
2693      // Error type is boolean; need to initialize some variables for backpat.
2694      if ( 'bool' === $r['error_type'] ) {
2695          if ( empty( $bp->activity->errors ) ) {
2696              $bp->activity->errors = array();
2697          }
2698      }
2699  
2700      // Default error message.
2701      $feedback = __( 'There was an error posting your reply. Please try again.', 'buddypress' );
2702  
2703      // Bail if missing necessary data.
2704      if ( empty( $r['content'] ) || empty( $r['user_id'] ) || empty( $r['activity_id'] ) ) {
2705          $error = new WP_Error( 'missing_data', $feedback );
2706  
2707          if ( 'wp_error' === $r['error_type'] ) {
2708              return $error;
2709  
2710          // Backpat.
2711          } else {
2712              $bp->activity->errors['new_comment'] = $error;
2713              return false;
2714          }
2715      }
2716  
2717      // Maybe set current activity ID as the parent.
2718      if ( empty( $r['parent_id'] ) ) {
2719          $r['parent_id'] = $r['activity_id'];
2720      }
2721  
2722      $activity_id = $r['activity_id'];
2723  
2724      // Get the parent activity.
2725      $activity  = new BP_Activity_Activity( $activity_id );
2726  
2727      // Bail if the parent activity does not exist.
2728      if ( empty( $activity->date_recorded ) ) {
2729          $error = new WP_Error( 'missing_activity', __( 'The item you were replying to no longer exists.', 'buddypress' ) );
2730  
2731          if ( 'wp_error' === $r['error_type'] ) {
2732              return $error;
2733  
2734          // Backpat.
2735          } else {
2736              $bp->activity->errors['new_comment'] = $error;
2737              return false;
2738          }
2739  
2740      }
2741  
2742      // Check to see if the parent activity is hidden, and if so, hide this comment publicly.
2743      $is_hidden = $activity->hide_sitewide ? 1 : 0;
2744  
2745      /**
2746       * Filters the content of a new comment.
2747       *
2748       * @since 1.2.0
2749       * @since 3.0.0 Added $context parameter to disambiguate from bp_get_activity_comment_content().
2750       *
2751       * @param string $r       Content for the newly posted comment.
2752       * @param string $context This filter's context ("new").
2753       */
2754      $comment_content = apply_filters( 'bp_activity_comment_content', $r['content'], 'new' );
2755  
2756      // Insert the activity comment.
2757      $comment_id = bp_activity_add( array(
2758          'id'                => $r['id'],
2759          'content'           => $comment_content,
2760          'component'         => $bp->activity->id,
2761          'type'              => 'activity_comment',
2762          'primary_link'      => $r['primary_link'],
2763          'user_id'           => $r['user_id'],
2764          'item_id'           => $activity_id,
2765          'secondary_item_id' => $r['parent_id'],
2766          'hide_sitewide'     => $is_hidden,
2767          'error_type'        => $r['error_type']
2768      ) );
2769  
2770      // Bail on failure.
2771      if ( false === $comment_id || is_wp_error( $comment_id ) ) {
2772          return $comment_id;
2773      }
2774  
2775      // Comment caches are stored only with the top-level item.
2776      wp_cache_delete( $activity_id, 'bp_activity_comments' );
2777  
2778      // Walk the tree to clear caches for all parent items.
2779      $clear_id = $r['parent_id'];
2780      while ( $clear_id != $activity_id ) {
2781          $clear_object = new BP_Activity_Activity( $clear_id );
2782          wp_cache_delete( $clear_id, 'bp_activity' );
2783          $clear_id = intval( $clear_object->secondary_item_id );
2784      }
2785      wp_cache_delete( $activity_id, 'bp_activity' );
2786  
2787      if ( empty( $r[ 'skip_notification' ] ) ) {
2788          /**
2789           * Fires near the end of an activity comment posting, before the returning of the comment ID.
2790           * Sends a notification to the user @see bp_activity_new_comment_notification_helper().
2791           *
2792           * @since 1.2.0
2793           *
2794           * @param int                  $comment_id ID of the newly posted activity comment.
2795           * @param array                $r          Array of parsed comment arguments.
2796           * @param BP_Activity_Activity $activity   Activity item being commented on.
2797           */
2798          do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
2799      } else {
2800          /**
2801           * Fires near the end of an activity comment posting, before the returning of the comment ID.
2802           * without sending a notification to the user
2803           *
2804           * @since 2.5.0
2805           *
2806           * @param int                  $comment_id ID of the newly posted activity comment.
2807           * @param array                $r          Array of parsed comment arguments.
2808           * @param BP_Activity_Activity $activity   Activity item being commented on.
2809           */
2810          do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
2811      }
2812  
2813      if ( empty( $comment_id ) ) {
2814          $error = new WP_Error( 'comment_failed', $feedback );
2815  
2816          if ( 'wp_error' === $r['error_type'] ) {
2817              return $error;
2818  
2819          // Backpat.
2820          } else {
2821              $bp->activity->errors['new_comment'] = $error;
2822          }
2823      }
2824  
2825      return $comment_id;
2826  }
2827  
2828  /**
2829   * Fetch the activity_id for an existing activity entry in the DB.
2830   *
2831   * @since 1.2.0
2832   *
2833   * @see BP_Activity_Activity::get() For more information on accepted arguments.
2834   *
2835   * @param array|string $args See BP_Activity_Activity::get() for description.
2836   * @return int $activity_id The ID of the activity item found.
2837   */
2838  function bp_activity_get_activity_id( $args = '' ) {
2839  
2840      $r = bp_parse_args(
2841          $args,
2842          array(
2843              'user_id'           => false,
2844              'component'         => false,
2845              'type'              => false,
2846              'item_id'           => false,
2847              'secondary_item_id' => false,
2848              'action'            => false,
2849              'content'           => false,
2850              'date_recorded'     => false,
2851          )
2852      );
2853  
2854      /**
2855       * Filters the activity ID being requested.
2856       *
2857       * @since 1.2.0
2858       * @since 2.5.0 Added the `$r` and `$args` parameters.
2859       *
2860       * @param BP_Activity_Activity $value ID returned by BP_Activity_Activity get_id() method with provided arguments.
2861       * @param array                $r     Parsed function arguments.
2862       * @param array                $args  Arguments passed to the function.
2863       */
2864      return apply_filters( 'bp_activity_get_activity_id', BP_Activity_Activity::get_id(
2865          $r['user_id'],
2866          $r['component'],
2867          $r['type'],
2868          $r['item_id'],
2869          $r['secondary_item_id'],
2870          $r['action'],
2871          $r['content'],
2872          $r['date_recorded']
2873      ), $r, $args );
2874  }
2875  
2876  /**
2877   * Delete activity item(s).
2878   *
2879   * If you're looking to hook into one action that provides the ID(s) of
2880   * the activity/activities deleted, then use:
2881   *
2882   * add_action( 'bp_activity_deleted_activities', 'my_function' );
2883   *
2884   * The action passes one parameter that is a single activity ID or an
2885   * array of activity IDs depending on the number deleted.
2886   *
2887   * If you are deleting an activity comment please use bp_activity_delete_comment();
2888   *
2889   * @since 1.0.0
2890   *
2891   * @see BP_Activity_Activity::get() For more information on accepted arguments.
2892   *
2893   * @param array|string $args To delete specific activity items, use
2894   *                           $args = array( 'id' => $ids ); Otherwise, to use
2895   *                           filters for item deletion, the argument format is
2896   *                           the same as BP_Activity_Activity::get().
2897   *                           See that method for a description.
2898   * @return bool True on success, false on failure.
2899   */
2900  function bp_activity_delete( $args = '' ) {
2901  
2902      // Pass one or more the of following variables to delete by those variables.
2903      $args = bp_parse_args(
2904          $args,
2905          array(
2906              'id'                => false,
2907              'action'            => false,
2908              'content'           => false,
2909              'component'         => false,
2910              'type'              => false,
2911              'primary_link'      => false,
2912              'user_id'           => false,
2913              'item_id'           => false,
2914              'secondary_item_id' => false,
2915              'date_recorded'     => false,
2916              'hide_sitewide'     => false,
2917          )
2918      );
2919  
2920      /**
2921       * Fires before an activity item proceeds to be deleted.
2922       *
2923       * @since 1.5.0
2924       *
2925       * @param array $args Array of arguments to be used with the activity deletion.
2926       */
2927      do_action( 'bp_before_activity_delete', $args );
2928  
2929      // Adjust the new mention count of any mentioned member.
2930      bp_activity_adjust_mention_count( $args['id'], 'delete' );
2931  
2932      $activity_ids_deleted = BP_Activity_Activity::delete( $args );
2933      if ( empty( $activity_ids_deleted ) ) {
2934          return false;
2935      }
2936  
2937      // Check if the user's latest update has been deleted.
2938      $user_id = empty( $args['user_id'] )
2939          ? bp_loggedin_user_id()
2940          : $args['user_id'];
2941  
2942      $latest_update = bp_get_user_meta( $user_id, 'bp_latest_update', true );
2943      if ( !empty( $latest_update ) ) {
2944          if ( in_array( (int) $latest_update['id'], (array) $activity_ids_deleted ) ) {
2945              bp_delete_user_meta( $user_id, 'bp_latest_update' );
2946          }
2947      }
2948  
2949      /**
2950       * Fires after the activity item has been deleted.
2951       *
2952       * @since 1.0.0
2953       *
2954       * @param array $args Array of arguments used with the activity deletion.
2955       */
2956      do_action( 'bp_activity_delete', $args );
2957  
2958      /**
2959       * Fires after the activity item has been deleted.
2960       *
2961       * @since 1.2.0
2962       *
2963       * @param array $activity_ids_deleted Array of affected activity item IDs.
2964       */
2965      do_action( 'bp_activity_deleted_activities', $activity_ids_deleted );
2966  
2967      wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
2968  
2969      return true;
2970  }
2971  
2972      /**
2973       * Delete an activity item by activity id.
2974       *
2975       * You should use bp_activity_delete() instead.
2976       *
2977       * @since 1.1.0
2978       * @deprecated 1.2.0
2979       *
2980       * @param array|string $args See BP_Activity_Activity::get for a
2981       *                           description of accepted arguments.
2982       * @return bool True on success, false on failure.
2983       */
2984  	function bp_activity_delete_by_item_id( $args = '' ) {
2985  
2986          $r = bp_parse_args(
2987              $args,
2988              array(
2989                  'item_id'           => false,
2990                  'component'         => false,
2991                  'type'              => false,
2992                  'user_id'           => false,
2993                  'secondary_item_id' => false,
2994              )
2995          );
2996  
2997          return bp_activity_delete( $r );
2998      }
2999  
3000      /**
3001       * Delete an activity item by activity id.
3002       *
3003       * @since 1.1.0
3004       *
3005       *
3006       * @param int $activity_id ID of the activity item to be deleted.
3007       * @return bool True on success, false on failure.
3008       */
3009  	function bp_activity_delete_by_activity_id( $activity_id ) {
3010          return bp_activity_delete( array( 'id' => $activity_id ) );
3011      }
3012  
3013      /**
3014       * Delete an activity item by its content.
3015       *
3016       * You should use bp_activity_delete() instead.
3017       *
3018       * @since 1.1.0
3019       * @deprecated 1.2.0
3020       *
3021       *
3022       * @param int    $user_id   The user id.
3023       * @param string $content   The activity id.
3024       * @param string $component The activity component.
3025       * @param string $type      The activity type.
3026       * @return bool True on success, false on failure.
3027       */
3028  	function bp_activity_delete_by_content( $user_id, $content, $component, $type ) {
3029          return bp_activity_delete( array(
3030              'user_id'   => $user_id,
3031              'content'   => $content,
3032              'component' => $component,
3033              'type'      => $type
3034          ) );
3035      }
3036  
3037      /**
3038       * Delete a user's activity for a component.
3039       *
3040       * You should use bp_activity_delete() instead.
3041       *
3042       * @since 1.1.0
3043       * @deprecated 1.2.0
3044       *
3045       *
3046       * @param int    $user_id   The user id.
3047       * @param string $component The activity component.
3048       * @return bool True on success, false on failure.
3049       */
3050  	function bp_activity_delete_for_user_by_component( $user_id, $component ) {
3051          return bp_activity_delete( array(
3052              'user_id'   => $user_id,
3053              'component' => $component
3054          ) );
3055      }
3056  
3057  /**
3058   * Delete an activity comment.
3059   *
3060   * @since 1.2.0
3061   *
3062   * @todo Why is an activity id required? We could look this up.
3063   * @todo Why do we encourage users to call this function directly? We could just
3064   *       as easily examine the activity type in bp_activity_delete() and then
3065   *       call this function with the proper arguments if necessary.
3066   *
3067   * @param int $activity_id The ID of the "root" activity, ie the comment's
3068   *                         oldest ancestor.
3069   * @param int $comment_id  The ID of the comment to be deleted.
3070   * @return bool True on success, false on failure.
3071   */
3072  function bp_activity_delete_comment( $activity_id, $comment_id ) {
3073      $deleted = false;
3074  
3075      /**
3076       * Filters whether BuddyPress should delete an activity comment or not.
3077       *
3078       * You may want to hook into this filter if you want to override this function and
3079       * handle the deletion of child comments differently. Make sure you return false.
3080       *
3081       * @since 1.2.0
3082       * @since 2.5.0 Add the deleted parameter (passed by reference)
3083       *
3084       * @param bool $value       Whether BuddyPress should continue or not.
3085       * @param int  $activity_id ID of the root activity item being deleted.
3086       * @param int  $comment_id  ID of the comment being deleted.
3087       * @param bool $deleted     Whether the activity comment has been deleted or not.
3088       */
3089      if ( ! apply_filters_ref_array( 'bp_activity_delete_comment_pre', array( true, $activity_id, $comment_id, &$deleted ) ) ) {
3090          return $deleted;
3091      }
3092  
3093      // Check if comment still exists.
3094      $comment = new BP_Activity_Activity( $comment_id );
3095      if ( empty( $comment->id ) ) {
3096          return false;
3097      }
3098  
3099      // Delete any children of this comment.
3100      bp_activity_delete_children( $activity_id, $comment_id );
3101  
3102      // Delete the actual comment.
3103      if ( ! bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) ) {
3104          return false;
3105      } else {
3106          $deleted = true;
3107      }
3108  
3109      // Purge comment cache for the root activity update.
3110      wp_cache_delete( $activity_id, 'bp_activity_comments' );
3111  
3112      // Recalculate the comment tree.
3113      BP_Activity_Activity::rebuild_activity_comment_tree( $activity_id );
3114  
3115      /**
3116       * Fires at the end of the deletion of an activity comment, before returning success.
3117       *
3118       * @since 1.2.0
3119       *
3120       * @param int $activity_id ID of the activity that has had a comment deleted from.
3121       * @param int $comment_id  ID of the comment that was deleted.
3122       */
3123      do_action( 'bp_activity_delete_comment', $activity_id, $comment_id );
3124  
3125      return $deleted;
3126  }
3127  
3128      /**
3129       * Delete an activity comment's children.
3130       *
3131       * @since 1.2.0
3132       *
3133       *
3134       * @param int $activity_id The ID of the "root" activity, ie the
3135       *                         comment's oldest ancestor.
3136       * @param int $comment_id  The ID of the comment to be deleted.
3137       */
3138  	function bp_activity_delete_children( $activity_id, $comment_id ) {
3139          // Check if comment still exists.
3140          $comment = new BP_Activity_Activity( $comment_id );
3141          if ( empty( $comment->id ) ) {
3142              return;
3143          }
3144  
3145          // Get activity children to delete.
3146          $children = BP_Activity_Activity::get_child_comments( $comment_id );
3147  
3148          // Recursively delete all children of this comment.
3149          if ( ! empty( $children ) ) {
3150              foreach( (array) $children as $child ) {
3151                  bp_activity_delete_children( $activity_id, $child->id );
3152              }
3153          }
3154  
3155          // Delete the comment itself.
3156          bp_activity_delete( array(
3157              'secondary_item_id' => $comment_id,
3158              'type'              => 'activity_comment',
3159              'item_id'           => $activity_id
3160          ) );
3161      }
3162  
3163  /**
3164   * Get the permalink for a single activity item.
3165   *
3166   * When only the $activity_id param is passed, BP has to instantiate a new
3167   * BP_Activity_Activity object. To save yourself some processing overhead,
3168   * be sure to pass the full $activity_obj parameter as well, if you already
3169   * have it available.
3170   *
3171   * @since 1.2.0
3172   *
3173   * @param int         $activity_id  The unique id of the activity object.
3174   * @param object|bool $activity_obj Optional. The activity object.
3175   * @return string $link Permalink for the activity item.
3176   */
3177  function bp_activity_get_permalink( $activity_id, $activity_obj = false ) {
3178      $bp = buddypress();
3179  
3180      if ( empty( $activity_obj ) ) {
3181          $activity_obj = new BP_Activity_Activity( $activity_id );
3182      }
3183  
3184      if ( isset( $activity_obj->current_comment ) ) {
3185          $activity_obj = $activity_obj->current_comment;
3186      }
3187  
3188      $use_primary_links = array(
3189          'new_blog_post',
3190          'new_blog_comment',
3191          'new_forum_topic',
3192          'new_forum_post',
3193      );
3194  
3195      if ( ! empty( $bp->activity->track ) ) {
3196          $use_primary_links = array_merge( $use_primary_links, array_keys( $bp->activity->track ) );
3197      }
3198  
3199      if ( false !== array_search( $activity_obj->type, $use_primary_links ) ) {
3200          $link = $activity_obj->primary_link;
3201      } else {
3202          if ( 'activity_comment' == $activity_obj->type ) {
3203              $link = bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $activity_obj->item_id . '/#acomment-' . $activity_obj->id;
3204          } else {
3205              $link = bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $activity_obj->id . '/';
3206          }
3207      }
3208  
3209      /**
3210       * Filters the activity permalink for the specified activity item.
3211       *
3212       * @since 1.2.0
3213       *
3214       * @param array $array Array holding activity permalink and activity item object.
3215       */
3216      return apply_filters_ref_array( 'bp_activity_get_permalink', array( $link, &$activity_obj ) );
3217  }
3218  
3219  /**
3220   * Can a user see a particular activity item?
3221   *
3222   * @since 3.0.0
3223   *
3224   * @param  BP_Activity_Activity $activity Activity object.
3225   * @param  integer              $user_id  User ID.
3226   * @return boolean True on success, false on failure.
3227   */
3228  function bp_activity_user_can_read( $activity, $user_id = 0 ) {
3229      $retval = true;
3230  
3231      // Fallback.
3232      if ( empty( $user_id ) ) {
3233          $user_id = bp_loggedin_user_id();
3234      }
3235  
3236      // If activity is from a group, do extra cap checks.
3237      if ( bp_is_active( 'groups' ) && buddypress()->groups->id === $activity->component ) {
3238          // Check to see if the user has access to the activity's parent group.
3239          $group = groups_get_group( $activity->item_id );
3240          if ( $group ) {
3241              // For logged-in user, we can check against the 'user_has_access' prop.
3242              if ( bp_loggedin_user_id() === $user_id ) {
3243                  $retval = $group->user_has_access;
3244  
3245              // Manually check status.
3246              } elseif ( 'private' === $group->status || 'hidden' === $group->status ) {
3247                  // Only group members that are not banned can view.
3248                  if ( ! groups_is_user_member( $user_id, $activity->item_id ) || groups_is_user_banned( $user_id, $activity->item_id ) ) {
3249                      $retval = false;
3250                  }
3251              }
3252          }
3253      }
3254  
3255      // Spammed items are not visible to the public.
3256      if ( $activity->is_spam ) {
3257          $retval = false;
3258      }
3259  
3260      // Site moderators can view anything.
3261      if ( bp_current_user_can( 'bp_moderate' ) ) {
3262          $retval = true;
3263      }
3264  
3265      /**
3266       * Filters whether the current user has access to an activity item.
3267       *
3268       * @since 3.0.0
3269       *
3270       * @param bool                 $retval   Return value.
3271       * @param int                  $user_id  Current user ID.
3272       * @param BP_Activity_Activity $activity Activity object.
3273       */
3274      return apply_filters( 'bp_activity_user_can_read', $retval, $user_id, $activity );
3275  }
3276  
3277  /**
3278   * Hide a user's activity.
3279   *
3280   * @since 1.2.0
3281   *
3282   * @param int $user_id The ID of the user whose activity is being hidden.
3283   * @return bool True on success, false on failure.
3284   */
3285  function bp_activity_hide_user_activity( $user_id ) {
3286      return BP_Activity_Activity::hide_all_for_user( $user_id );
3287  }
3288  
3289  /**
3290   * Take content, remove images, and replace them with a single thumbnail image.
3291   *
3292   * The format of items in the activity stream is such that we do not want to
3293   * allow an arbitrary number of arbitrarily large images to be rendered.
3294   * However, the activity stream is built to elegantly display a single
3295   * thumbnail corresponding to the activity comment. This function looks
3296   * through the content, grabs the first image and converts it to a thumbnail,
3297   * and removes the rest of the images from the string.
3298   *
3299   * As of BuddyPress 2.3, this function is no longer in use.
3300   *
3301   * @since 1.2.0
3302   *
3303   * @param string      $content The content of the activity item.
3304   * @param string|bool $link    Optional. The unescaped URL that the image should link
3305   *                             to. If absent, the image will not be a link.
3306   * @param array|bool  $args    Optional. The args passed to the activity
3307   *                             creation function (eg bp_blogs_record_activity()).
3308   * @return string $content The content with images stripped and replaced with a
3309   *                         single thumb.
3310   */
3311  function bp_activity_thumbnail_content_images( $content, $link = false, $args = false ) {
3312  
3313      preg_match_all( '/<img[^>]*>/Ui', $content, $matches );
3314  
3315      // Remove <img> tags. Also remove caption shortcodes and caption text if present.
3316      $content = preg_replace('|(\[caption(.*?)\])?<img[^>]*>([^\[\[]*\[\/caption\])?|', '', $content );
3317  
3318      if ( !empty( $matches ) && !empty( $matches[0] ) ) {
3319  
3320          // Get the SRC value.
3321          preg_match( '/<img.*?(src\=[\'|"]{0,1}.*?[\'|"]{0,1})[\s|>]{1}/i',    $matches[0][0], $src    );
3322  
3323          // Get the width and height.
3324          preg_match( '/<img.*?(height\=[\'|"]{0,1}.*?[\'|"]{0,1})[\s|>]{1}/i', $matches[0][0], $height );
3325          preg_match( '/<img.*?(width\=[\'|"]{0,1}.*?[\'|"]{0,1})[\s|>]{1}/i',  $matches[0][0], $width  );
3326  
3327          if ( ! empty( $src ) ) {
3328              $src = substr( substr( str_replace( 'src=', '', $src[1] ), 0, -1 ), 1 );
3329  
3330              if ( isset( $width[1] ) ) {
3331                  $width = substr( substr( str_replace( 'width=', '', $width[1] ), 0, -1 ), 1 );
3332              }
3333  
3334              if ( isset( $height[1] ) ) {
3335                  $height = substr( substr( str_replace( 'height=', '', $height[1] ), 0, -1 ), 1 );
3336              }
3337  
3338              if ( empty( $width ) || empty( $height ) ) {
3339                  $width  = 100;
3340                  $height = 100;
3341              }
3342  
3343              $ratio      = (int) $width / (int) $height;
3344              $new_height = (int) $height >= 100 ? 100 : $height;
3345              $new_width  = $new_height * $ratio;
3346              $image      = '<img src="' . esc_url( $src ) . '" width="' . absint( $new_width ) . '" height="' . absint( $new_height ) . '" alt="' . __( 'Thumbnail', 'buddypress' ) . '" class="align-left thumbnail" />';
3347  
3348              if ( !empty( $link ) ) {
3349                  $image = '<a href="' . esc_url( $link ) . '">' . $image . '</a>';
3350              }
3351  
3352              $content = $image . $content;
3353          }
3354      }
3355  
3356      /**
3357       * Filters the activity content that had a thumbnail replace images.
3358       *
3359       * @since 1.2.0
3360       *
3361       * @param string $content Activity content that had images replaced in.
3362       * @param array  $matches Array of all image tags found in the posted content.
3363       * @param array  $args    Arguments passed into function creating the activity update.
3364       */
3365      return apply_filters( 'bp_activity_thumbnail_content_images', $content, $matches, $args );
3366  }
3367  
3368  /**
3369   * Gets the excerpt length for activity items.
3370   *
3371   * @since 2.8.0
3372   *
3373   * @return int Character length for activity excerpts.
3374   */
3375  function bp_activity_get_excerpt_length() {
3376      /**
3377       * Filters the excerpt length for the activity excerpt.
3378       *
3379       * @since 1.5.0
3380       *
3381       * @param int Character length for activity excerpts.
3382       */
3383      return (int) apply_filters( 'bp_activity_excerpt_length', 358 );
3384  }
3385  
3386  /**
3387   * Create a rich summary of an activity item for the activity stream.
3388   *
3389   * More than just a simple excerpt, the summary could contain oEmbeds and other types of media.
3390   * Currently, it's only used for blog post items, but it will probably be used for all types of
3391   * activity in the future.
3392   *
3393   * @since 2.3.0
3394   *
3395   * @param string $content  The content of the activity item.
3396   * @param array  $activity The data passed to bp_activity_add() or the values
3397   *                         from an Activity obj.
3398   * @return string $summary
3399   */
3400  function bp_activity_create_summary( $content, $activity ) {
3401      $args = array(
3402          'width' => isset( $GLOBALS['content_width'] ) ? (int) $GLOBALS['content_width'] : 'medium',
3403      );
3404  
3405      // Get the WP_Post object if this activity type is a blog post.
3406      if ( $activity['type'] === 'new_blog_post' ) {
3407          $content = get_post( $activity['secondary_item_id'] );
3408      }
3409  
3410      /**
3411       * Filter the class name of the media extractor when creating an Activity summary.
3412       *
3413       * Use this filter to change the media extractor used to extract media info for the activity item.
3414       *
3415       * @since 2.3.0
3416       *
3417       * @param string $extractor Class name.
3418       * @param string $content   The content of the activity item.
3419       * @param array  $activity  The data passed to bp_activity_add() or the values from an Activity obj.
3420       */
3421      $extractor = apply_filters( 'bp_activity_create_summary_extractor_class', 'BP_Media_Extractor', $content, $activity );
3422      $extractor = new $extractor;
3423  
3424      /**
3425       * Filter the arguments passed to the media extractor when creating an Activity summary.
3426       *
3427       * @since 2.3.0
3428       *
3429       * @param array              $args      Array of bespoke data for the media extractor.
3430       * @param string             $content   The content of the activity item.
3431       * @param array              $activity  The data passed to bp_activity_add() or the values from an Activity obj.
3432       * @param BP_Media_Extractor $extractor The media extractor object.
3433       */
3434      $args = apply_filters( 'bp_activity_create_summary_extractor_args', $args, $content, $activity, $extractor );
3435  
3436  
3437      // Extract media information from the $content.
3438      $media = $extractor->extract( $content, BP_Media_Extractor::ALL, $args );
3439  
3440      // If we converted $content to an object earlier, flip it back to a string.
3441      if ( is_a( $content, 'WP_Post' ) ) {
3442          $content = $content->post_content;
3443      }
3444  
3445      $para_count     = substr_count( strtolower( wpautop( $content ) ), '<p>' );
3446      $has_audio      = ! empty( $media['has']['audio'] )           && $media['has']['audio'];
3447      $has_videos     = ! empty( $media['has']['videos'] )          && $media['has']['videos'];
3448      $has_feat_image = ! empty( $media['has']['featured_images'] ) && $media['has']['featured_images'];
3449      $has_galleries  = ! empty( $media['has']['galleries'] )       && $media['has']['galleries'];
3450      $has_images     = ! empty( $media['has']['images'] )          && $media['has']['images'];
3451      $has_embeds     = false;
3452  
3453      // Embeds must be subtracted from the paragraph count.
3454      if ( ! empty( $media['has']['embeds'] ) ) {
3455          $has_embeds = $media['has']['embeds'] > 0;
3456          $para_count -= $media['has']['embeds'];
3457      }
3458  
3459      $extracted_media = array();
3460      $use_media_type  = '';
3461      $image_source    = '';
3462  
3463      // If it's a short article and there's an embed/audio/video, use it.
3464      if ( $para_count <= 3 ) {
3465          if ( $has_embeds ) {
3466              $use_media_type = 'embeds';
3467          } elseif ( $has_audio ) {
3468              $use_media_type = 'audio';
3469          } elseif ( $has_videos ) {
3470              $use_media_type = 'videos';
3471          }
3472      }
3473  
3474      // If not, or in any other situation, try to use an image.
3475      if ( ! $use_media_type && $has_images ) {
3476          $use_media_type = 'images';
3477          $image_source   = 'html';
3478  
3479          // Featured Image > Galleries > inline <img>.
3480          if ( $has_feat_image ) {
3481              $image_source = 'featured_images';
3482  
3483          } elseif ( $has_galleries ) {
3484              $image_source = 'galleries';
3485          }
3486      }
3487  
3488      // Extract an item from the $media results.
3489      if ( $use_media_type ) {
3490          if ( $use_media_type === 'images' ) {
3491              $extracted_media = wp_list_filter( $media[ $use_media_type ], array( 'source' => $image_source ) );
3492              $extracted_media = array_shift( $extracted_media );
3493          } else {
3494              $extracted_media = array_shift( $media[ $use_media_type ] );
3495          }
3496  
3497          /**
3498           * Filter the results of the media extractor when creating an Activity summary.
3499           *
3500           * @since 2.3.0
3501           *
3502           * @param array  $extracted_media Extracted media item. See {@link BP_Media_Extractor::extract()} for format.
3503           * @param string $content         Content of the activity item.
3504           * @param array  $activity        The data passed to bp_activity_add() or the values from an Activity obj.
3505           * @param array  $media           All results from the media extraction.
3506           *                                See {@link BP_Media_Extractor::extract()} for format.
3507           * @param string $use_media_type  The kind of media item that was preferentially extracted.
3508           * @param string $image_source    If $use_media_type was "images", the preferential source of the image.
3509           *                                Otherwise empty.
3510           */
3511          $extracted_media = apply_filters(
3512              'bp_activity_create_summary_extractor_result',
3513              $extracted_media,
3514              $content,
3515              $activity,
3516              $media,
3517              $use_media_type,
3518              $image_source
3519          );
3520      }
3521  
3522      // Generate a text excerpt for this activity item (and remove any oEmbeds URLs).
3523      $summary = bp_create_excerpt( html_entity_decode( $content ), 225, array(
3524          'html' => false,
3525          'filter_shortcodes' => true,
3526          'strip_tags'        => true,
3527          'remove_links'      => true
3528      ) );
3529  
3530      if ( $use_media_type === 'embeds' ) {
3531          $summary .= PHP_EOL . PHP_EOL . $extracted_media['url'];
3532      } elseif ( $use_media_type === 'images' ) {
3533          $extracted_media_url = isset( $extracted_media['url'] ) ? $extracted_media['url'] : '';
3534          $summary .= sprintf( ' <img src="%s">', esc_url( $extracted_media_url ) );
3535      } elseif ( in_array( $use_media_type, array( 'audio', 'videos' ), true ) ) {
3536          $summary .= PHP_EOL . PHP_EOL . $extracted_media['original'];  // Full shortcode.
3537      }
3538  
3539      /**
3540       * Filters the newly-generated summary for the activity item.
3541       *
3542       * @since 2.3.0
3543       *
3544       * @param string $summary         Activity summary HTML.
3545       * @param string $content         Content of the activity item.
3546       * @param array  $activity        The data passed to bp_activity_add() or the values from an Activity obj.
3547       * @param array  $extracted_media Media item extracted. See {@link BP_Media_Extractor::extract()} for format.
3548       */
3549      return apply_filters( 'bp_activity_create_summary', $summary, $content, $activity, $extracted_media );
3550  }
3551  
3552  /**
3553   * Fetch whether the current user is allowed to mark items as spam.
3554   *
3555   * @since 1.6.0
3556   *
3557   * @return bool True if user is allowed to mark activity items as spam.
3558   */
3559  function bp_activity_user_can_mark_spam() {
3560  
3561      /**
3562       * Filters whether the current user should be able to mark items as spam.
3563       *
3564       * @since 1.6.0
3565       *
3566       * @param bool $moderate Whether or not the current user has bp_moderate capability.
3567       */
3568      return apply_filters( 'bp_activity_user_can_mark_spam', bp_current_user_can( 'bp_moderate' ) );
3569  }
3570  
3571  /**
3572   * Mark an activity item as spam.
3573   *
3574   * @since 1.6.0
3575   *
3576   * @todo We should probably save $source to activity meta.
3577   *
3578   * @param BP_Activity_Activity $activity The activity item to be spammed.
3579   * @param string               $source   Optional. Default is "by_a_person" (ie, a person has
3580   *                                       manually marked the activity as spam). BP core also
3581   *                                       accepts 'by_akismet'.
3582   */
3583  function bp_activity_mark_as_spam( &$activity, $source = 'by_a_person' ) {
3584      $bp = buddypress();
3585  
3586      $activity->is_spam = 1;
3587  
3588      // Clear the activity stream first page cache.
3589      wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
3590  
3591      // Clear the activity comment cache for this activity item.
3592      wp_cache_delete( $activity->id, 'bp_activity_comments' );
3593  
3594      // If Akismet is active, and this was a manual spam/ham request, stop Akismet checking the activity.
3595      if ( 'by_a_person' == $source && !empty( $bp->activity->akismet ) ) {
3596          remove_action( 'bp_activity_before_save', array( $bp->activity->akismet, 'check_activity' ), 4 );
3597  
3598          // Build data package for Akismet.
3599          $activity_data = BP_Akismet::build_akismet_data_package( $activity );
3600  
3601          // Tell Akismet this is spam.
3602          $activity_data = $bp->activity->akismet->send_akismet_request( $activity_data, 'submit', 'spam' );
3603  
3604          // Update meta.
3605          add_action( 'bp_activity_after_save', array( $bp->activity->akismet, 'update_activity_spam_meta' ), 1, 1 );
3606      }
3607  
3608      /**
3609       * Fires at the end of the process to mark an activity item as spam.
3610       *
3611       * @since 1.6.0
3612       *
3613       * @param BP_Activity_Activity $activity Activity item being marked as spam.
3614       * @param string               $source   Source of determination of spam status. For example
3615       *                                       "by_a_person" or "by_akismet".
3616       */
3617      do_action( 'bp_activity_mark_as_spam', $activity, $source );
3618  }
3619  
3620  /**
3621   * Mark an activity item as ham.
3622   *
3623   * @since 1.6.0
3624   *
3625   * @param BP_Activity_Activity $activity The activity item to be hammed. Passed by reference.
3626   * @param string               $source   Optional. Default is "by_a_person" (ie, a person has
3627   *                                       manually marked the activity as spam). BP core also accepts
3628   *                                       'by_akismet'.
3629   */
3630  function bp_activity_mark_as_ham( &$activity, $source = 'by_a_person' ) {
3631      $bp = buddypress();
3632  
3633      $activity->is_spam = 0;
3634  
3635      // Clear the activity stream first page cache.
3636      wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
3637  
3638      // Clear the activity comment cache for this activity item.
3639      wp_cache_delete( $activity->id, 'bp_activity_comments' );
3640  
3641      // If Akismet is active, and this was a manual spam/ham request, stop Akismet checking the activity.
3642      if ( 'by_a_person' == $source && !empty( $bp->activity->akismet ) ) {
3643          remove_action( 'bp_activity_before_save', array( $bp->activity->akismet, 'check_activity' ), 4 );
3644  
3645          // Build data package for Akismet.
3646          $activity_data = BP_Akismet::build_akismet_data_package( $activity );
3647  
3648          // Tell Akismet this is spam.
3649          $activity_data = $bp->activity->akismet->send_akismet_request( $activity_data, 'submit', 'ham' );
3650  
3651          // Update meta.
3652          add_action( 'bp_activity_after_save', array( $bp->activity->akismet, 'update_activity_ham_meta' ), 1, 1 );
3653      }
3654  
3655      /**
3656       * Fires at the end of the process to mark an activity item as ham.
3657       *
3658       * @since 1.6.0
3659       *
3660       * @param BP_Activity_Activity $activity Activity item being marked as ham.
3661       * @param string               $source   Source of determination of ham status. For example
3662       *                                       "by_a_person" or "by_akismet".
3663       */
3664      do_action( 'bp_activity_mark_as_ham', $activity, $source );
3665  }
3666  
3667  /* Emails *********************************************************************/
3668  
3669  /**
3670   * Send email and BP notifications when a user is mentioned in an update.
3671   *
3672   * @since 1.2.0
3673   *
3674   * @param int $activity_id      The ID of the activity update.
3675   * @param int $receiver_user_id The ID of the user who is receiving the update.
3676   */
3677  function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
3678      $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
3679  
3680      // Don't leave multiple notifications for the same activity item.
3681      foreach( $notifications as $notification ) {
3682          if ( $activity_id == $notification->item_id ) {
3683              return;
3684          }
3685      }
3686  
3687      $activity     = new BP_Activity_Activity( $activity_id );
3688      $email_type   = 'activity-at-message';
3689      $group_name   = '';
3690      $message_link = bp_activity_get_permalink( $activity_id );
3691      $poster_name  = bp_core_get_user_displayname( $activity->user_id );
3692  
3693      remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
3694      remove_filter( 'bp_get_activity_content_body', 'wpautop' );
3695      remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
3696  
3697      /** This filter is documented in bp-activity/bp-activity-template.php */
3698      $content = apply_filters_ref_array( 'bp_get_activity_content_body', array( $activity->content, &$activity ) );
3699  
3700      add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
3701      add_filter( 'bp_get_activity_content_body', 'wpautop' );
3702      add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
3703  
3704      // Now email the user with the contents of the message (if they have enabled email notifications).
3705      if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
3706          if ( bp_is_active( 'groups' ) && bp_is_group() ) {
3707              $email_type = 'groups-at-message';
3708              $group_name = bp_get_current_group_name();
3709          }
3710  
3711          $unsubscribe_args = array(
3712              'user_id'           => $receiver_user_id,
3713              'notification_type' => $email_type,
3714          );
3715  
3716          $args = array(
3717              'tokens' => array(
3718                  'activity'         => $activity,
3719                  'usermessage'      => wp_strip_all_tags( $content ),
3720                  'group.name'       => $group_name,
3721                  'mentioned.url'    => $message_link,
3722                  'poster.name'      => $poster_name,
3723                  'receiver-user.id' => $receiver_user_id,
3724                  'unsubscribe'        => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
3725              ),
3726          );
3727  
3728          bp_send_email( $email_type, $receiver_user_id, $args );
3729      }
3730  
3731      /**
3732       * Fires after the sending of an @mention email notification.
3733       *
3734       * @since 1.5.0
3735       * @since 2.5.0 $subject, $message, $content arguments unset and deprecated.
3736       *
3737       * @param BP_Activity_Activity $activity         Activity Item object.
3738       * @param string               $deprecated       Removed in 2.5; now an empty string.
3739       * @param string               $deprecated       Removed in 2.5; now an empty string.
3740       * @param string               $deprecated       Removed in 2.5; now an empty string.
3741       * @param int                  $receiver_user_id The ID of the user who is receiving the update.
3742       */
3743      do_action( 'bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id );
3744  }
3745  
3746  /**
3747   * Send email and BP notifications when an activity item receives a comment.
3748   *
3749   * @since 1.2.0
3750   * @since 2.5.0 Updated to use new email APIs.
3751   *
3752   * @param int   $comment_id   The comment id.
3753   * @param int   $commenter_id The ID of the user who posted the comment.
3754   * @param array $params       {@link bp_activity_new_comment()}.
3755   */
3756  function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
3757      $original_activity = new BP_Activity_Activity( $params['activity_id'] );
3758      $poster_name       = bp_core_get_user_displayname( $commenter_id );
3759      $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
3760  
3761      remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
3762      remove_filter( 'bp_get_activity_content_body', 'wpautop' );
3763      remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
3764  
3765      /** This filter is documented in bp-activity/bp-activity-template.php */
3766      $content = apply_filters_ref_array( 'bp_get_activity_content_body', array( $params['content'], &$original_activity ) );
3767  
3768      add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
3769      add_filter( 'bp_get_activity_content_body', 'wpautop' );
3770      add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
3771  
3772      if ( $original_activity->user_id != $commenter_id ) {
3773  
3774          // Send an email if the user hasn't opted-out.
3775          if ( 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
3776  
3777              $unsubscribe_args = array(
3778                  'user_id'           => $original_activity->user_id,
3779                  'notification_type' => 'activity-comment',
3780              );
3781  
3782              $args = array(
3783                  'tokens' => array(
3784                      'comment.id'                => $comment_id,
3785                      'commenter.id'              => $commenter_id,
3786                      'usermessage'               => wp_strip_all_tags( $content ),
3787                      'original_activity.user_id' => $original_activity->user_id,
3788                      'poster.name'               => $poster_name,
3789                      'thread.url'                => esc_url( $thread_link ),
3790                      'unsubscribe'               => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
3791                  ),
3792              );
3793  
3794              bp_send_email( 'activity-comment', $original_activity->user_id, $args );
3795          }
3796  
3797          /**
3798           * Fires at the point that notifications should be sent for activity comments.
3799           *
3800           * @since 2.6.0
3801           *
3802           * @param BP_Activity_Activity $original_activity The original activity.
3803           * @param int                  $comment_id        ID for the newly received comment.
3804           * @param int                  $commenter_id      ID of the user who made the comment.
3805           * @param array                $params            Arguments used with the original activity comment.
3806           */
3807          do_action( 'bp_activity_sent_reply_to_update_notification', $original_activity, $comment_id, $commenter_id, $params );
3808      }
3809  
3810  
3811      /*
3812       * If this is a reply to another comment, send an email notification to the
3813       * author of the immediate parent comment.
3814       */
3815      if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
3816          return;
3817      }
3818  
3819      $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
3820  
3821      if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id ) {
3822  
3823          // Send an email if the user hasn't opted-out.
3824          if ( 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
3825  
3826              $unsubscribe_args = array(
3827                  'user_id'           => $parent_comment->user_id,
3828                  'notification_type' => 'activity-comment-author',
3829              );
3830  
3831              $args = array(
3832                  'tokens' => array(
3833                      'comment.id'             => $comment_id,
3834                      'commenter.id'           => $commenter_id,
3835                      'usermessage'            => wp_strip_all_tags( $content ),
3836                      'parent-comment-user.id' => $parent_comment->user_id,
3837                      'poster.name'            => $poster_name,
3838                      'thread.url'             => esc_url( $thread_link ),
3839                      'unsubscribe'            => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
3840                  ),
3841              );
3842  
3843              bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
3844          }
3845  
3846          /**
3847           * Fires at the point that notifications should be sent for comments on activity replies.
3848           *
3849           * @since 2.6.0
3850           *
3851           * @param BP_Activity_Activity $parent_comment The parent activity.
3852           * @param int                  $comment_id     ID for the newly received comment.
3853           * @param int                  $commenter_id   ID of the user who made the comment.
3854           * @param array                $params         Arguments used with the original activity comment.
3855           */
3856          do_action( 'bp_activity_sent_reply_to_reply_notification', $parent_comment, $comment_id, $commenter_id, $params );
3857      }
3858  }
3859  
3860  /**
3861   * Helper method to map action arguments to function parameters.
3862   *
3863   * @since 1.9.0
3864   *
3865   * @param int   $comment_id ID of the comment being notified about.
3866   * @param array $params     Parameters to use with notification.
3867   */
3868  function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
3869      bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
3870  }
3871  add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
3872  
3873  /** Embeds *******************************************************************/
3874  
3875  /**
3876   * Set up activity oEmbed cache during the activity loop.
3877   *
3878   * During an activity loop, this function sets up the hooks necessary to grab
3879   * each item's embeds from the cache, or put them in the cache if they are
3880   * not there yet.
3881   *
3882   * This does not cover recursive activity comments, as they do not use a real loop.
3883   * For that, see {@link bp_activity_comment_embed()}.
3884   *
3885   * @since 1.5.0
3886   *
3887   * @see BP_Embed
3888   * @see bp_embed_activity_cache()
3889   * @see bp_embed_activity_save_cache()
3890   *
3891   */
3892  function bp_activity_embed() {
3893      add_filter( 'embed_post_id',         'bp_get_activity_id'                  );
3894      add_filter( 'oembed_dataparse',      'bp_activity_oembed_dataparse', 10, 2 );
3895      add_filter( 'bp_embed_get_cache',    'bp_embed_activity_cache',      10, 3 );
3896      add_action( 'bp_embed_update_cache', 'bp_embed_activity_save_cache', 10, 3 );
3897  }
3898  add_action( 'activity_loop_start', 'bp_activity_embed' );
3899  
3900  /**
3901   * Cache full oEmbed response from oEmbed.
3902   *
3903   * @since 2.6.0
3904   *
3905   * @param string $retval Current oEmbed result.
3906   * @param object $data   Full oEmbed response.
3907   * @param string $url    URL used for the oEmbed request.
3908   * @return string
3909   */
3910  function bp_activity_oembed_dataparse( $retval, $data ) {
3911      buddypress()->activity->oembed_response = $data;
3912  
3913      return $retval;
3914  }
3915  
3916  /**
3917   * Set up activity oEmbed cache while recursing through activity comments.
3918   *
3919   * While crawling through an activity comment tree
3920   * ({@link bp_activity_recurse_comments}), this function sets up the hooks
3921   * necessary to grab each comment's embeds from the cache, or put them in
3922   * the cache if they are not there yet.
3923   *
3924   * @since 1.5.0
3925   *
3926   * @see BP_Embed
3927   * @see bp_embed_activity_cache()
3928   * @see bp_embed_activity_save_cache()
3929   *
3930   */
3931  function bp_activity_comment_embed() {
3932      add_filter( 'embed_post_id',         'bp_get_activity_comment_id'          );
3933      add_filter( 'bp_embed_get_cache',    'bp_embed_activity_cache',      10, 3 );
3934      add_action( 'bp_embed_update_cache', 'bp_embed_activity_save_cache', 10, 3 );
3935  }
3936  add_action( 'bp_before_activity_comment', 'bp_activity_comment_embed' );
3937  
3938  /**
3939   * When a user clicks on a "Read More" item, make sure embeds are correctly parsed and shown for the expanded content.
3940   *
3941   * @since 1.5.0
3942   *
3943   * @see BP_Embed
3944   *
3945   * @param object $activity The activity that is being expanded.
3946   */
3947  function bp_dtheme_embed_read_more( $activity ) {
3948      buddypress()->activity->read_more_id = $activity->id;
3949  
3950      add_filter( 'embed_post_id',         function() { return buddypress()->activity->read_more_id; } );
3951      add_filter( 'bp_embed_get_cache',    'bp_embed_activity_cache',      10, 3 );
3952      add_action( 'bp_embed_update_cache', 'bp_embed_activity_save_cache', 10, 3 );
3953  }
3954  add_action( 'bp_dtheme_get_single_activity_content',       'bp_dtheme_embed_read_more' );
3955  add_action( 'bp_legacy_theme_get_single_activity_content', 'bp_dtheme_embed_read_more' );
3956  
3957  /**
3958   * Clean up 'embed_post_id' filter after comment recursion.
3959   *
3960   * This filter must be removed so that the non-comment filters take over again
3961   * once the comments are done being processed.
3962   *
3963   * @since 1.5.0
3964   *
3965   * @see bp_activity_comment_embed()
3966   */
3967  function bp_activity_comment_embed_after_recurse() {
3968      remove_filter( 'embed_post_id', 'bp_get_activity_comment_id' );
3969  }
3970  add_action( 'bp_after_activity_comment', 'bp_activity_comment_embed_after_recurse' );
3971  
3972  /**
3973   * Fetch an activity item's cached embeds.
3974   *
3975   * Used during {@link BP_Embed::parse_oembed()} via {@link bp_activity_embed()}.
3976   *
3977   * @since 1.5.0
3978   *
3979   * @see BP_Embed::parse_oembed()
3980   *
3981   * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
3982   *                         functions like this one to filter.
3983   * @param int    $id       The ID of the activity item.
3984   * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
3985   * @return mixed The cached embeds for this activity item.
3986   */
3987  function bp_embed_activity_cache( $cache, $id, $cachekey ) {
3988      return bp_activity_get_meta( $id, $cachekey );
3989  }
3990  
3991  /**
3992   * Set an activity item's embed cache.
3993   *
3994   * Used during {@link BP_Embed::parse_oembed()} via {@link bp_activity_embed()}.
3995   *
3996   * @since 1.5.0
3997   *
3998   * @see BP_Embed::parse_oembed()
3999   *
4000   * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
4001   *                         functions like this one to filter.
4002   * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
4003   * @param int    $id       The ID of the activity item.
4004   */
4005  function bp_embed_activity_save_cache( $cache, $cachekey, $id ) {
4006      bp_activity_update_meta( $id, $cachekey, $cache );
4007  
4008      // Cache full oEmbed response.
4009      if ( true === isset( buddypress()->activity->oembed_response ) ) {
4010          $cachekey = str_replace( '_oembed', '_oembed_response', $cachekey );
4011          bp_activity_update_meta( $id, $cachekey, buddypress()->activity->oembed_response );
4012      }
4013  }
4014  
4015  /**
4016   * Should we use Heartbeat to refresh activities?
4017   *
4018   * @since 2.0.0
4019   *
4020   * @return bool True if activity heartbeat is enabled, otherwise false.
4021   */
4022  function bp_activity_do_heartbeat() {
4023      $retval = false;
4024  
4025      if ( bp_is_activity_heartbeat_active() && ( bp_is_activity_directory() || bp_is_group_activity() ) ) {
4026          $retval = true;
4027      }
4028  
4029      /**
4030       * Filters whether the heartbeat feature in the activity stream should be active.
4031       *
4032       * @since 2.8.0
4033       *
4034       * @param bool $retval Whether or not activity heartbeat is active.
4035       */
4036      return (bool) apply_filters( 'bp_activity_do_heartbeat', $retval );
4037  }
4038  
4039  /**
4040   * Detect a change in post type status, and initiate an activity update if necessary.
4041   *
4042   * @since 2.2.0
4043   *
4044   * @todo Support untrashing better.
4045   *
4046   * @param string $new_status New status for the post.
4047   * @param string $old_status Old status for the post.
4048   * @param object $post       Post data.
4049   */
4050  function bp_activity_catch_transition_post_type_status( $new_status, $old_status, $post ) {
4051      if ( ! post_type_supports( $post->post_type, 'buddypress-activity' ) ) {
4052          return;
4053      }
4054  
4055      // This is an edit.
4056      if ( $new_status === $old_status ) {
4057          // An edit of an existing post should update the existing activity item.
4058          if ( $new_status == 'publish' ) {
4059              $edit = bp_activity_post_type_update( $post );
4060  
4061              // Post was never recorded into activity stream, so record it now!
4062              if ( null === $edit ) {
4063                  bp_activity_post_type_publish( $post->ID, $post );
4064              }
4065  
4066          // Allow plugins to eventually deal with other post statuses.
4067          } else {
4068              /**
4069               * Fires when editing the post and the new status is not 'publish'.
4070               *
4071               * This is a variable filter that is dependent on the post type
4072               * being untrashed.
4073               *
4074               * @since 2.5.0
4075               *
4076               * @param WP_Post $post Post data.
4077               * @param string $new_status New status for the post.
4078               * @param string $old_status Old status for the post.
4079               */
4080              do_action( 'bp_activity_post_type_edit_' . $post->post_type, $post, $new_status, $old_status );
4081          }
4082  
4083          return;
4084      }
4085  
4086      // Publishing a previously unpublished post.
4087      if ( 'publish' === $new_status ) {
4088          // Untrashing the post type - nothing here yet.
4089          if ( 'trash' == $old_status ) {
4090  
4091              /**
4092               * Fires if untrashing post in a post type.
4093               *
4094               * This is a variable filter that is dependent on the post type
4095               * being untrashed.
4096               *
4097               * @since 2.2.0
4098               *
4099               * @param WP_Post $post Post data.
4100               */
4101              do_action( 'bp_activity_post_type_untrash_' . $post->post_type, $post );
4102          } else {
4103              // Record the post.
4104              bp_activity_post_type_publish( $post->ID, $post );
4105          }
4106  
4107      // Unpublishing a previously published post.
4108      } elseif ( 'publish' === $old_status ) {
4109          // Some form of pending status - only remove the activity entry.
4110          bp_activity_post_type_unpublish( $post->ID, $post );
4111  
4112      // For any other cases, allow plugins to eventually deal with it.
4113      } else {
4114          /**
4115           * Fires when the old and the new post status are not 'publish'.
4116           *
4117           * This is a variable filter that is dependent on the post type
4118           * being untrashed.
4119           *
4120           * @since 2.5.0
4121           *
4122           * @param WP_Post $post Post data.
4123           * @param string $new_status New status for the post.
4124           * @param string $old_status Old status for the post.
4125           */
4126          do_action( 'bp_activity_post_type_transition_status_' . $post->post_type, $post, $new_status, $old_status );
4127      }
4128  }
4129  add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
4130  
4131  /**
4132   * When a post type comment status transition occurs, update the relevant activity's status.
4133   *
4134   * @since 2.5.0
4135   *
4136   * @param string     $new_status New comment status.
4137   * @param string     $old_status Previous comment status.
4138   * @param WP_Comment $comment Comment data.
4139   */
4140  function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) {
4141      $post_type = get_post_type( $comment->comment_post_ID );
4142      if ( ! $post_type ) {
4143          return;
4144      }
4145  
4146      // Get the post type tracking args.
4147      $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
4148  
4149      // Bail if the activity type does not exist.
4150      if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
4151          return false;
4152  
4153      // Set the $activity_comment_object.
4154      } else {
4155          $activity_comment_object = $activity_post_object->comments_tracking;
4156      }
4157  
4158      // Init an empty activity ID.
4159      $activity_id = 0;
4160  
4161      /**
4162       * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
4163       *
4164       * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
4165       * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
4166       * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
4167       * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
4168       * Otherwise, record the comment into the activity stream.
4169       */
4170  
4171      // This clause handles delete/hold.
4172      if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
4173          return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object );
4174  
4175      // These clauses handle trash, spam, and un-spams.
4176      } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
4177          $action = 'spam_activity';
4178      } elseif ( 'approved' == $new_status ) {
4179          $action = 'ham_activity';
4180      }
4181  
4182      // Get the activity.
4183      if ( bp_disable_blogforum_comments() ) {
4184          $activity_id = bp_activity_get_activity_id( array(
4185              'component'         => $activity_comment_object->component_id,
4186              'item_id'           => get_current_blog_id(),
4187              'secondary_item_id' => $comment->comment_ID,
4188              'type'              => $activity_comment_object->action_id,
4189          ) );
4190      } else {
4191          $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
4192      }
4193  
4194      /**
4195       * Leave a chance to plugins to manage activity comments differently.
4196       *
4197       * @since  2.5.0
4198       *
4199       * @param bool        $value       True to override BuddyPress management.
4200       * @param string      $post_type   The post type name.
4201       * @param int         $activity_id The post type activity (0 if not found).
4202       * @param string      $new_status  The new status of the post type comment.
4203       * @param string      $old_status  The old status of the post type comment.
4204       * @param WP_Comment  $comment Comment data.
4205       */
4206      if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) {
4207          return false;
4208      }
4209  
4210      // Check activity item exists.
4211      if ( empty( $activity_id ) ) {
4212          // If no activity exists, but the comment has been approved, record it into the activity table.
4213          if ( 'approved' == $new_status ) {
4214              return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object );
4215          }
4216  
4217          return;
4218      }
4219  
4220      // Create an activity object.
4221      $activity = new BP_Activity_Activity( $activity_id );
4222      if ( empty( $activity->component ) ) {
4223          return;
4224      }
4225  
4226      // Spam/ham the activity if it's not already in that state.
4227      if ( 'spam_activity' === $action && ! $activity->is_spam ) {
4228          bp_activity_mark_as_spam( $activity );
4229      } elseif ( 'ham_activity' == $action) {
4230          bp_activity_mark_as_ham( $activity );
4231      }
4232  
4233      // Add "new_post_type_comment" to the allowed activity types, so that the activity's Akismet history is generated.
4234      $post_type_comment_action = $activity_comment_object->action_id;
4235      $comment_akismet_history = function ( $activity_types ) use ( $post_type_comment_action ) {
4236          $activity_types[] = $post_type_comment_action;
4237  
4238          return $activity_types;
4239      };
4240      add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
4241  
4242      // Make sure the activity change won't edit the comment if sync is on.
4243      remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
4244  
4245      // Save the updated activity.
4246      $activity->save();
4247  
4248      // Restore the action.
4249      add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
4250  
4251      // Remove the dynamic permitting of the "new_blog_comment" activity type so we don't break anything.
4252      remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
4253  }
4254  add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 );
4255  
4256  /**
4257   * Finds and exports personal data associated with an email address from the Activity tables.
4258   *
4259   * @since 4.0.0
4260   *
4261   * @param string $email_address  The user's email address.
4262   * @param int    $page           Batch number.
4263   * @return array An array of personal data.
4264   */
4265  function bp_activity_personal_data_exporter( $email_address, $page ) {
4266      $number = 50;
4267  
4268      $email_address = trim( $email_address );
4269  
4270      $data_to_export = array();
4271  
4272      $user = get_user_by( 'email', $email_address );
4273  
4274      if ( ! $user ) {
4275          return array(
4276              'data' => array(),
4277              'done' => true,
4278          );
4279      }
4280  
4281      $activities = bp_activity_get( array(
4282          'display_comments' => 'stream',
4283          'per_page'         => $number,
4284          'page'             => $page,
4285          'show_hidden'      => true,
4286          'filter'           => array(
4287              'user_id' => $user->ID,
4288          ),
4289      ) );
4290  
4291      $user_data_to_export = array();
4292      $activity_actions    = bp_activity_get_actions();
4293  
4294      foreach ( $activities['activities'] as $activity ) {
4295          if ( ! empty( $activity_actions->{$activity->component}->{$activity->type}['format_callback'] ) ) {
4296              $description = call_user_func( $activity_actions->{$activity->component}->{$activity->type}['format_callback'], '', $activity );
4297          } elseif ( ! empty( $activity->action ) ) {
4298              $description = $activity->action;
4299          } else {
4300              $description = $activity->type;
4301          }
4302  
4303          $item_data = array(
4304              array(
4305                  'name'  => __( 'Activity Date', 'buddypress' ),
4306                  'value' => $activity->date_recorded,
4307              ),
4308              array(
4309                  'name'  => __( 'Activity Description', 'buddypress' ),
4310                  'value' => $description,
4311              ),
4312              array(
4313                  'name'  => __( 'Activity URL', 'buddypress' ),
4314                  'value' => bp_activity_get_permalink( $activity->id, $activity ),
4315              ),
4316          );
4317  
4318          if ( ! empty( $activity->content ) ) {
4319              $item_data[] = array(
4320                  'name'  => __( 'Activity Content', 'buddypress' ),
4321                  'value' => $activity->content,
4322              );
4323          }
4324  
4325          /**
4326           * Filters the data associated with an activity item when assembled for a WP personal data export.
4327           *
4328           * Plugins that register activity types whose `action` string doesn't adequately
4329           * describe the activity item for the purposes of data export may filter the activity
4330           * item data here.
4331           *
4332           * @since 4.0.0
4333           *
4334           * @param array                $item_data Array of data describing the activity item.
4335           * @param BP_Activity_Activity $activity  Activity item.
4336           */
4337          $item_data = apply_filters( 'bp_activity_personal_data_export_item_data', $item_data, $activity );
4338  
4339          $data_to_export[] = array(
4340              'group_id'    => 'bp_activity',
4341              'group_label' => __( 'Activity', 'buddypress' ),
4342              'item_id'     => "bp-activity-{$activity->id}",
4343              'data'        => $item_data,
4344          );
4345      }
4346  
4347      // Tell core if we have more items to process.
4348      $done = count( $activities['activities'] ) < $number;
4349  
4350      return array(
4351          'data' => $data_to_export,
4352          'done' => $done,
4353      );
4354  }
4355  
4356  /**
4357   * Checks whether an activity feed is enabled.
4358   *
4359   * @since 8.0.0
4360   *
4361   * @param string $feed_id The feed identifier. Possible values are:
4362   *                        'sitewide', 'personal', 'friends', 'mygroups', 'mentions', 'favorites'.
4363   */
4364  function bp_activity_is_feed_enable( $feed_id = '' ) {
4365      /**
4366       * Filters if BuddyPress should consider feeds enabled. If disabled, it will return early.
4367       *
4368       * @since 1.8.0
4369       * @since 8.0.0 Adds the `$feed_id` parameter.
4370       *
4371       * @param bool   $value   Defaults to true aka feeds are enabled.
4372       * @param string $feed_id The feed identifier.
4373       */
4374      return (bool) apply_filters( 'bp_activity_enable_feeds', true, $feed_id );
4375  }


Generated: Tue Oct 19 01:00:57 2021 Cross-referenced by PHPXref 0.7.1