[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Member Activity
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersActivity
   7   * @since 2.2.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Register the 'new member' activity type.
  15   *
  16   * @since 2.2.0
  17   */
  18  function bp_members_register_activity_actions() {
  19  
  20      bp_activity_set_action(
  21          buddypress()->members->id,
  22          'new_member',
  23          __( 'New member registered', 'buddypress' ),
  24          'bp_members_format_activity_action_new_member',
  25          __( 'New Members', 'buddypress' ),
  26          array( 'activity' )
  27      );
  28  
  29      // Register the activity stream actions for this component.
  30      bp_activity_set_action(
  31          // Older avatar activity items use 'profile' for component. See r4273.
  32          buddypress()->members->id,
  33          'new_avatar',
  34          __( 'Member changed profile picture', 'buddypress' ),
  35          'bp_members_format_activity_action_new_avatar',
  36          __( 'Updated Profile Photos', 'buddypress' )
  37      );
  38  
  39      /**
  40       * Fires after the default 'new member' activity types are registered.
  41       *
  42       * @since 2.2.0
  43       */
  44      do_action( 'bp_members_register_activity_actions' );
  45  }
  46  add_action( 'bp_register_activity_actions', 'bp_members_register_activity_actions' );
  47  
  48  /**
  49   * Format 'new_member' activity actions.
  50   *
  51   * @since 2.2.0
  52   *
  53   * @param string $action   Static activity action.
  54   * @param object $activity Activity object.
  55   * @return string $action
  56   */
  57  function bp_members_format_activity_action_new_member( $action, $activity ) {
  58      $userlink         = bp_core_get_userlink( $activity->user_id );
  59      $inviter_userlink = false;
  60      $invite_id        = bp_get_user_meta( $activity->user_id, 'accepted_members_invitation', true );
  61  
  62      if ( $invite_id ) {
  63          $invite = new BP_Invitation( (int) $invite_id );
  64  
  65          if ( $invite->inviter_id ) {
  66              $inviter_userlink = bp_core_get_userlink( $invite->inviter_id );
  67          }
  68      }
  69  
  70      if ( $inviter_userlink ) {
  71          $action = sprintf(
  72              /* translators: 1: new user link. 2: inviter user link. */
  73              esc_html__( '%1$s accepted an invitation from %2$s and became a registered member', 'buddypress' ),
  74              $userlink,
  75              $inviter_userlink
  76          );
  77      } else {
  78          /* translators: %s: user link */
  79          $action = sprintf( esc_html__( '%s became a registered member', 'buddypress' ), $userlink );
  80      }
  81  
  82      // Legacy filter - pass $user_id instead of $activity.
  83      if ( has_filter( 'bp_core_activity_registered_member_action' ) ) {
  84          $action = apply_filters( 'bp_core_activity_registered_member_action', $action, $activity->user_id );
  85      }
  86  
  87      /**
  88       * Filters the formatted 'new member' activity actions.
  89       *
  90       * @since 2.2.0
  91       * @since 8.0.0 Added $invite_id
  92       *
  93       * @param string $action    Static activity action.
  94       * @param object $activity  Activity object.
  95       * @param int    $invite_id The ID of the invite.
  96       */
  97      return apply_filters( 'bp_members_format_activity_action_new_member', $action, $activity, $invite_id );
  98  }
  99  
 100  /**
 101   * Format 'new_avatar' activity actions.
 102   *
 103   * @since 8.0.0
 104   *
 105   * @param string $action   Static activity action.
 106   * @param object $activity Activity object.
 107   * @return string
 108   */
 109  function bp_members_format_activity_action_new_avatar( $action, $activity ) {
 110      $userlink = bp_core_get_userlink( $activity->user_id );
 111  
 112      /* translators: %s: user link */
 113      $action = sprintf( esc_html__( '%s changed their profile picture', 'buddypress' ), $userlink );
 114  
 115      // Legacy filter - pass $user_id instead of $activity.
 116      if ( has_filter( 'bp_xprofile_new_avatar_action' ) ) {
 117          $action = apply_filters( 'bp_xprofile_new_avatar_action', $action, $activity->user_id );
 118      }
 119  
 120      /** This filter is documented in wp-includes/deprecated.php */
 121      $action = apply_filters_deprecated( 'bp_xprofile_format_activity_action_new_avatar', array( $action, $activity ), '8.0.0', 'bp_members_format_activity_action_new_avatar' );
 122  
 123      /**
 124       * Filters the formatted 'new_avatar' activity stream action.
 125       *
 126       * @since 8.0.0
 127       *
 128       * @param string $action   Formatted action for activity stream.
 129       * @param object $activity Activity object.
 130       */
 131      return apply_filters( 'bp_members_format_activity_action_new_avatar', $action, $activity );
 132  }
 133  
 134  /**
 135   * Create a "became a registered user" activity item when a user activates his account.
 136   *
 137   * @since 1.2.2
 138   *
 139   * @param array $user Array of userdata passed to bp_core_activated_user hook.
 140   * @return bool
 141   */
 142  function bp_core_new_user_activity( $user ) {
 143      if ( empty( $user ) ) {
 144          return false;
 145      }
 146  
 147      if ( is_array( $user ) ) {
 148          $user_id = $user['user_id'];
 149      } else {
 150          $user_id = $user;
 151      }
 152  
 153      if ( empty( $user_id ) ) {
 154          return false;
 155      }
 156  
 157      bp_activity_add( array(
 158          'user_id'   => $user_id,
 159          'component' => buddypress()->members->id,
 160          'type'      => 'new_member'
 161      ) );
 162  }
 163  add_action( 'bp_core_activated_user', 'bp_core_new_user_activity' );
 164  
 165  /**
 166   * Adds an activity stream item when a user has uploaded a new avatar.
 167   *
 168   * @since 8.0.0
 169   * @since 10.0.0 Adds the `$type`, `$crop_data` and `$cropped_avatar` parameters.
 170   *
 171   * @param int    $user_id        The user id the avatar was set for.
 172   * @param string $type           The way the avatar was set ('camera' or `crop`).
 173   * @param array  $crop_data      Array of parameters passed to the crop handler.
 174   * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
 175   */
 176  function bp_members_new_avatar_activity( $user_id = 0, $type = '', $crop_data = array(), $cropped_avatar = array() ) {
 177  
 178      // Bail if activity component is not active.
 179      if ( ! bp_is_active( 'activity' ) ) {
 180          return false;
 181      }
 182  
 183      if ( empty( $user_id ) ) {
 184          $user_id = bp_displayed_user_id();
 185      }
 186  
 187      /** This filter is documented in wp-includes/deprecated.php */
 188      $user_id = apply_filters_deprecated( 'bp_xprofile_new_avatar_user_id', array( $user_id ), '8.0.0', 'bp_members_new_avatar_user_id' );
 189  
 190      /**
 191       * Filters the user ID when a user has uploaded a new avatar.
 192       *
 193       * @since 8.0.0
 194       *
 195       * @param int $user_id ID of the user the avatar was set for.
 196       */
 197      $user_id = apply_filters( 'bp_members_new_avatar_user_id', $user_id );
 198  
 199      // Get the BuddyPress main plugin instance.
 200      $bp = buddypress();
 201  
 202      // Check to make sure that a user has just one `new_avatar` activity per throttle time.
 203      $last_new_avatar_activity = bp_activity_get(
 204          array(
 205              'per_page'  => 1,
 206              'filter'    => array(
 207                  'object'  => $bp->members->id,
 208                  'user_id' => $user_id,
 209                  'action'  => 'new_avatar',
 210              )
 211          )
 212      );
 213  
 214      if ( ! empty( $last_new_avatar_activity['activities'] ) ) {
 215          /**
 216           * Filters the throttle time, in seconds, used to prevent generating multiple `new_avatar` activity
 217           * in a short amount of time.
 218           *
 219           * @since 10.0.0
 220           *
 221           * @param int $value Throttle time, in seconds.
 222           */
 223          $throttle_period = apply_filters( 'bp_members_new_avatar_throttle_time', HOUR_IN_SECONDS );
 224          $then            = strtotime( $last_new_avatar_activity['activities'][0]->date_recorded );
 225          $now             = bp_core_current_time( true, 'timestamp' );
 226  
 227          // Delete the old activity.
 228          if ( ( $now - $then ) < $throttle_period ) {
 229              bp_activity_delete(
 230                  array(
 231                      'id' => $last_new_avatar_activity['activities'][0]->id,
 232                  )
 233              );
 234          }
 235      }
 236  
 237      $recorded_time = '';
 238      if ( isset( $cropped_avatar['timestamp'] ) && $cropped_avatar['timestamp'] ) {
 239          $recorded_time = date( 'Y-m-d H:i:s', $cropped_avatar['timestamp'] );
 240      }
 241  
 242      // Add the activity.
 243      $activity_id = bp_activity_add(
 244          array(
 245              'user_id'       => $user_id,
 246              'component'     => $bp->members->id,
 247              'type'          => 'new_avatar',
 248              'recorded_time' => $recorded_time,
 249          )
 250      );
 251  }
 252  add_action( 'bp_members_avatar_uploaded', 'bp_members_new_avatar_activity', 10, 4 );
 253  
 254  /**
 255   * Remove the `new_avatar` activity corresponding to the deleted previous avatar.
 256   *
 257   * @since 10.0.0
 258   *
 259   * @param int $user_id   The user ID.
 260   * @param int $timestamp The timestamp when the activity was created.
 261   * @return bool True on success. False otherwise.
 262   */
 263  function bp_members_remove_previous_avatar_activity( $user_id = 0, $timestamp = 0 ) {
 264      if ( ! $user_id || ! $timestamp || ! bp_is_active( 'activity' ) ) {
 265          return false;
 266      }
 267  
 268      // Look for a `new_avatar` activity corresponding to the date and user.
 269      $activity_id = BP_Activity_Activity::get_id(
 270          array(
 271              'user_id'       => $user_id,
 272              'component'     => buddypress()->members->id,
 273              'type'          => 'new_avatar',
 274              'date_recorded' => date( 'Y-m-d H:i:s', $timestamp ),
 275          )
 276      );
 277  
 278      if ( $activity_id ) {
 279          return bp_activity_delete(
 280              array(
 281                  'id' => $activity_id,
 282              )
 283          );
 284      }
 285  
 286      return false;
 287  }
 288  add_action( 'bp_previous_user_avatar_deleted', 'bp_members_remove_previous_avatar_activity', 10, 2 );


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1