[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Friends Activity Functions.
   4   *
   5   * These functions handle the recording, deleting and formatting of activity
   6   * for the user and for this specific component.
   7   *
   8   * @package BuddyPress
   9   * @subpackage FriendsActivity
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Record an activity item related to the Friends component.
  18   *
  19   * A wrapper for {@link bp_activity_add()} that provides some Friends-specific
  20   * defaults.
  21   *
  22   * @since 1.0.0
  23   *
  24   * @see bp_activity_add() for more detailed description of parameters and
  25   *      return values.
  26   *
  27   * @param array|string $args {
  28   *     An array of arguments for the new activity item. Accepts all parameters
  29   *     of {@link bp_activity_add()}. The one difference is the following
  30   *     argument, which has a different default here:
  31   *     @type string $component Default: the id of your Friends component
  32   *                             (usually 'friends').
  33   * }
  34   * @return WP_Error|bool|int See {@link bp_activity_add()}.
  35   */
  36  function friends_record_activity( $args = '' ) {
  37  
  38      if ( ! bp_is_active( 'activity' ) ) {
  39          return false;
  40      }
  41  
  42      $r = wp_parse_args( $args, array(
  43          'user_id'           => bp_loggedin_user_id(),
  44          'action'            => '',
  45          'content'           => '',
  46          'primary_link'      => '',
  47          'component'         => buddypress()->friends->id,
  48          'type'              => false,
  49          'item_id'           => false,
  50          'secondary_item_id' => false,
  51          'recorded_time'     => bp_core_current_time(),
  52          'hide_sitewide'     => false
  53      ) );
  54  
  55      return bp_activity_add( $r );
  56  }
  57  
  58  /**
  59   * Delete an activity item related to the Friends component.
  60   *
  61   * @since 1.0.0
  62   *
  63   * @param array $args {
  64   *     An array of arguments for the item to delete.
  65   *     @type int    $item_id ID of the 'item' associated with the activity item.
  66   *                           For Friends activity items, this is usually the user ID of one
  67   *                           of the friends.
  68   *     @type string $type    The 'type' of the activity item (eg
  69   *                           'friendship_accepted').
  70   *     @type int    $user_id ID of the user associated with the activity item.
  71   * }
  72   * @return bool True on success, false on failure.
  73   */
  74  function friends_delete_activity( $args ) {
  75      if ( ! bp_is_active( 'activity' ) ) {
  76          return;
  77      }
  78  
  79      bp_activity_delete_by_item_id( array(
  80          'component' => buddypress()->friends->id,
  81          'item_id'   => $args['item_id'],
  82          'type'      => $args['type'],
  83          'user_id'   => $args['user_id']
  84      ) );
  85  }
  86  
  87  /**
  88   * Register the activity actions for bp-friends.
  89   *
  90   * @since 1.1.0
  91   */
  92  function friends_register_activity_actions() {
  93  
  94      if ( !bp_is_active( 'activity' ) ) {
  95          return false;
  96      }
  97  
  98      $bp = buddypress();
  99  
 100      // These two added in BP 1.6.
 101      bp_activity_set_action(
 102          $bp->friends->id,
 103          'friendship_accepted',
 104          __( 'Friendships accepted', 'buddypress' ),
 105          'bp_friends_format_activity_action_friendship_accepted',
 106          __( 'Friendships', 'buddypress' ),
 107          array( 'activity', 'member' )
 108      );
 109  
 110      bp_activity_set_action(
 111          $bp->friends->id,
 112          'friendship_created',
 113          __( 'New friendships', 'buddypress' ),
 114          'bp_friends_format_activity_action_friendship_created',
 115          __( 'Friendships', 'buddypress' ),
 116          array( 'activity', 'member' )
 117      );
 118  
 119      // < BP 1.6 backpat.
 120      bp_activity_set_action( $bp->friends->id, 'friends_register_activity_action', __( 'New friendship created', 'buddypress' ) );
 121  
 122      /**
 123       * Fires after all default bp-friends activity actions have been registered.
 124       *
 125       * @since 1.1.0
 126       */
 127      do_action( 'friends_register_activity_actions' );
 128  }
 129  add_action( 'bp_register_activity_actions', 'friends_register_activity_actions' );
 130  
 131  /**
 132   * Format 'friendship_accepted' activity actions.
 133   *
 134   * @since 2.0.0
 135   *
 136   * @param string $action   Activity action string.
 137   * @param object $activity Activity data.
 138   * @return string $action Formatted activity action.
 139   */
 140  function bp_friends_format_activity_action_friendship_accepted( $action, $activity ) {
 141      $initiator_link = bp_core_get_userlink( $activity->user_id );
 142      $friend_link    = bp_core_get_userlink( $activity->secondary_item_id );
 143  
 144      /* translators: 1: the initiator user link. 2: the friend user link. */
 145      $action = sprintf( esc_html__( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
 146  
 147      // Backward compatibility for legacy filter
 148      // The old filter has the $friendship object passed to it. We want to
 149      // avoid having to build this object if it's not necessary.
 150      if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) {
 151          $friendship = new BP_Friends_Friendship( $activity->item_id );
 152          $action     = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship );
 153      }
 154  
 155      /**
 156       * Filters the 'friendship_accepted' activity action format.
 157       *
 158       * @since 2.0.0
 159       *
 160       * @param string $action   String text for the 'friendship_accepted' action.
 161       * @param object $activity Activity data.
 162       */
 163      return apply_filters( 'bp_friends_format_activity_action_friendship_accepted', $action, $activity );
 164  }
 165  
 166  /**
 167   * Format 'friendship_created' activity actions.
 168   *
 169   * @since 2.0.0
 170   *
 171   * @param string $action   Static activity action.
 172   * @param object $activity Activity data.
 173   * @return string $action Formatted activity action.
 174   */
 175  function bp_friends_format_activity_action_friendship_created( $action, $activity ) {
 176      $initiator_link = bp_core_get_userlink( $activity->user_id );
 177      $friend_link    = bp_core_get_userlink( $activity->secondary_item_id );
 178  
 179      /* translators: 1: the initiator user link. 2: the friend user link. */
 180      $action = sprintf( esc_html__( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
 181  
 182      // Backward compatibility for legacy filter
 183      // The old filter has the $friendship object passed to it. We want to
 184      // avoid having to build this object if it's not necessary.
 185      if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) {
 186          $friendship = new BP_Friends_Friendship( $activity->item_id );
 187          $action     = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship );
 188      }
 189  
 190      /**
 191       * Filters the 'friendship_created' activity action format.
 192       *
 193       * @since 2.0.0
 194       *
 195       * @param string $action   String text for the 'friendship_created' action.
 196       * @param object $activity Activity data.
 197       */
 198      return apply_filters( 'bp_friends_format_activity_action_friendship_created', $action, $activity );
 199  }
 200  
 201  /**
 202   * Fetch data related to friended users at the beginning of an activity loop.
 203   *
 204   * This reduces database overhead during the activity loop.
 205   *
 206   * @since 2.0.0
 207   *
 208   * @param array $activities Array of activity items.
 209   * @return array
 210   */
 211  function bp_friends_prefetch_activity_object_data( $activities ) {
 212      if ( empty( $activities ) ) {
 213          return $activities;
 214      }
 215  
 216      $friend_ids = array();
 217  
 218      foreach ( $activities as $activity ) {
 219          if ( buddypress()->friends->id !== $activity->component ) {
 220              continue;
 221          }
 222  
 223          $friend_ids[] = $activity->secondary_item_id;
 224      }
 225  
 226      if ( ! empty( $friend_ids ) ) {
 227          // Fire a user query to prime user caches.
 228          new BP_User_Query( array(
 229              'user_ids'          => $friend_ids,
 230              'populate_extras'   => false,
 231              'update_meta_cache' => false,
 232          ) );
 233      }
 234  
 235      return $activities;
 236  }
 237  add_filter( 'bp_activity_prefetch_object_data', 'bp_friends_prefetch_activity_object_data' );
 238  
 239  /**
 240   * Set up activity arguments for use with the 'friends' scope.
 241   *
 242   * For details on the syntax, see {@link BP_Activity_Query}.
 243   *
 244   * @since 2.2.0
 245   *
 246   * @param array $retval Empty array by default.
 247   * @param array $filter Current activity arguments.
 248   * @return array
 249   */
 250  function bp_friends_filter_activity_scope( $retval = array(), $filter = array() ) {
 251  
 252      // Determine the user_id.
 253      if ( ! empty( $filter['user_id'] ) ) {
 254          $user_id = $filter['user_id'];
 255      } else {
 256          $user_id = bp_displayed_user_id()
 257              ? bp_displayed_user_id()
 258              : bp_loggedin_user_id();
 259      }
 260  
 261      // Determine friends of user.
 262      $friends = friends_get_friend_user_ids( $user_id );
 263      if ( empty( $friends ) ) {
 264          $friends = array( 0 );
 265      }
 266  
 267      $retval = array(
 268          'relation' => 'AND',
 269          array(
 270              'column'  => 'user_id',
 271              'compare' => 'IN',
 272              'value'   => (array) $friends
 273          ),
 274  
 275          // We should only be able to view sitewide activity content for friends.
 276          array(
 277              'column' => 'hide_sitewide',
 278              'value'  => 0
 279          ),
 280  
 281          // Overrides.
 282          'override' => array(
 283              'filter'      => array( 'user_id' => 0 ),
 284              'show_hidden' => true
 285          ),
 286      );
 287  
 288      return $retval;
 289  }
 290  add_filter( 'bp_activity_set_friends_scope_args', 'bp_friends_filter_activity_scope', 10, 2 );
 291  
 292  /**
 293   * Set up activity arguments for use with the 'just-me' scope.
 294   *
 295   * For details on the syntax, see {@link BP_Activity_Query}.
 296   *
 297   * @since 2.2.0
 298   *
 299   * @param array $retval Empty array by default.
 300   * @param array $filter Current activity arguments.
 301   * @return array
 302   */
 303  function bp_friends_filter_activity_just_me_scope( $retval = array(), $filter = array() ) {
 304  
 305      // Determine the user_id.
 306      if ( ! empty( $filter['user_id'] ) ) {
 307          $user_id = $filter['user_id'];
 308      } else {
 309          $user_id = bp_displayed_user_id()
 310              ? bp_displayed_user_id()
 311              : bp_loggedin_user_id();
 312      }
 313  
 314      // Get the requested action.
 315      $action = isset( $filter['filter']['action'] ) ? $filter['filter']['action'] : array();
 316  
 317      // Make sure actions are listed in an array.
 318      if ( ! is_array( $action ) ) {
 319          $action = explode( ',', $filter['filter']['action'] );
 320      }
 321  
 322      $action = array_flip( array_filter( $action ) );
 323  
 324      /**
 325       * If filtering activities for something other than the friendship_created
 326       * action return without changing anything
 327       */
 328      if ( ! empty( $action ) && ! isset( $action['friendship_created'] ) ) {
 329          return $retval;
 330      }
 331  
 332      // Juggle existing override value.
 333      $override = array();
 334      if ( ! empty( $retval['override'] ) ) {
 335          $override = $retval['override'];
 336          unset( $retval['override'] );
 337      }
 338  
 339      /**
 340       * Else make sure to get the friendship_created action, the user is involved in
 341       * - user initiated the friendship
 342       * - user has been requested a friendship
 343       */
 344      $retval = array(
 345          'relation' => 'OR',
 346          $retval,
 347          array(
 348              'relation' => 'AND',
 349              array(
 350                  'column' => 'component',
 351                  'value'  => 'friends',
 352              ),
 353              array(
 354                  'column' => 'secondary_item_id',
 355                  'value'  => $user_id,
 356              ),
 357          )
 358      );
 359  
 360      // Juggle back override value.
 361      if ( ! empty( $override ) ) {
 362          $retval['override'] = $override;
 363      }
 364  
 365      return $retval;
 366  }
 367  add_filter( 'bp_activity_set_just-me_scope_args', 'bp_friends_filter_activity_just_me_scope', 20, 2 );
 368  
 369  /**
 370   * Add activity stream items when one members accepts another members request
 371   * for virtual friendship.
 372   *
 373   * @since 1.9.0
 374   *
 375   * @param int         $friendship_id       ID of the friendship.
 376   * @param int         $initiator_user_id   ID of friendship initiator.
 377   * @param int         $friend_user_id      ID of user whose friendship is requested.
 378   * @param object|bool $friendship Optional Friendship object.
 379   */
 380  function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship = false ) {
 381      if ( ! bp_is_active( 'activity' ) ) {
 382          return;
 383      }
 384  
 385      // Record in activity streams for the initiator.
 386      friends_record_activity( array(
 387          'user_id'           => $initiator_user_id,
 388          'type'              => 'friendship_created',
 389          'item_id'           => $friendship_id,
 390          'secondary_item_id' => $friend_user_id
 391      ) );
 392  }
 393  add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
 394  
 395  /**
 396   * Deletes friendship activity items when a user is deleted.
 397   *
 398   * @since 2.5.0
 399   *
 400   * @param int $user_id The ID of the user being deleted.
 401   */
 402  function bp_friends_delete_activity_on_user_delete( $user_id = 0 ) {
 403      if ( ! bp_is_active( 'activity' ) ) {
 404          return;
 405      }
 406  
 407      bp_activity_delete( array(
 408          'component'         => buddypress()->friends->id,
 409          'type'              => 'friendship_created',
 410          'secondary_item_id' => $user_id
 411      ) );
 412  }
 413  add_action( 'friends_remove_data', 'bp_friends_delete_activity_on_user_delete' );
 414  
 415  /**
 416   * Remove friendship activity item when a friendship is deleted.
 417   *
 418   * @since 3.2.0
 419   *
 420   * @param int $friendship_id ID of the friendship.
 421   */
 422  function bp_friends_delete_activity_on_friendship_delete( $friendship_id ) {
 423      friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => 'friendship_created', 'user_id' => 0 ) );
 424  }
 425  add_action( 'friends_friendship_deleted', 'bp_friends_delete_activity_on_friendship_delete' );


Generated: Thu Jul 2 01:01:34 2020 Cross-referenced by PHPXref 0.7.1