[ 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      $action = sprintf( esc_html__( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
 145  
 146      // Backward compatibility for legacy filter
 147      // The old filter has the $friendship object passed to it. We want to
 148      // avoid having to build this object if it's not necessary.
 149      if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) {
 150          $friendship = new BP_Friends_Friendship( $activity->item_id );
 151          $action     = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship );
 152      }
 153  
 154      /**
 155       * Filters the 'friendship_accepted' activity action format.
 156       *
 157       * @since 2.0.0
 158       *
 159       * @param string $action   String text for the 'friendship_accepted' action.
 160       * @param object $activity Activity data.
 161       */
 162      return apply_filters( 'bp_friends_format_activity_action_friendship_accepted', $action, $activity );
 163  }
 164  
 165  /**
 166   * Format 'friendship_created' activity actions.
 167   *
 168   * @since 2.0.0
 169   *
 170   * @param string $action   Static activity action.
 171   * @param object $activity Activity data.
 172   * @return string $action Formatted activity action.
 173   */
 174  function bp_friends_format_activity_action_friendship_created( $action, $activity ) {
 175      $initiator_link = bp_core_get_userlink( $activity->user_id );
 176      $friend_link    = bp_core_get_userlink( $activity->secondary_item_id );
 177  
 178      $action = sprintf( esc_html__( '%1$s and %2$s are now friends', 'buddypress' ), $initiator_link, $friend_link );
 179  
 180      // Backward compatibility for legacy filter
 181      // The old filter has the $friendship object passed to it. We want to
 182      // avoid having to build this object if it's not necessary.
 183      if ( has_filter( 'friends_activity_friendship_accepted_action' ) ) {
 184          $friendship = new BP_Friends_Friendship( $activity->item_id );
 185          $action     = apply_filters( 'friends_activity_friendsip_accepted_action', $action, $friendship );
 186      }
 187  
 188      /**
 189       * Filters the 'friendship_created' activity action format.
 190       *
 191       * @since 2.0.0
 192       *
 193       * @param string $action   String text for the 'friendship_created' action.
 194       * @param object $activity Activity data.
 195       */
 196      return apply_filters( 'bp_friends_format_activity_action_friendship_created', $action, $activity );
 197  }
 198  
 199  /**
 200   * Fetch data related to friended users at the beginning of an activity loop.
 201   *
 202   * This reduces database overhead during the activity loop.
 203   *
 204   * @since 2.0.0
 205   *
 206   * @param array $activities Array of activity items.
 207   * @return array
 208   */
 209  function bp_friends_prefetch_activity_object_data( $activities ) {
 210      if ( empty( $activities ) ) {
 211          return $activities;
 212      }
 213  
 214      $friend_ids = array();
 215  
 216      foreach ( $activities as $activity ) {
 217          if ( buddypress()->friends->id !== $activity->component ) {
 218              continue;
 219          }
 220  
 221          $friend_ids[] = $activity->secondary_item_id;
 222      }
 223  
 224      if ( ! empty( $friend_ids ) ) {
 225          // Fire a user query to prime user caches.
 226          new BP_User_Query( array(
 227              'user_ids'          => $friend_ids,
 228              'populate_extras'   => false,
 229              'update_meta_cache' => false,
 230          ) );
 231      }
 232  
 233      return $activities;
 234  }
 235  add_filter( 'bp_activity_prefetch_object_data', 'bp_friends_prefetch_activity_object_data' );
 236  
 237  /**
 238   * Set up activity arguments for use with the 'friends' scope.
 239   *
 240   * For details on the syntax, see {@link BP_Activity_Query}.
 241   *
 242   * @since 2.2.0
 243   *
 244   * @param array $retval Empty array by default.
 245   * @param array $filter Current activity arguments.
 246   * @return array
 247   */
 248  function bp_friends_filter_activity_scope( $retval = array(), $filter = array() ) {
 249  
 250      // Determine the user_id.
 251      if ( ! empty( $filter['user_id'] ) ) {
 252          $user_id = $filter['user_id'];
 253      } else {
 254          $user_id = bp_displayed_user_id()
 255              ? bp_displayed_user_id()
 256              : bp_loggedin_user_id();
 257      }
 258  
 259      // Determine friends of user.
 260      $friends = friends_get_friend_user_ids( $user_id );
 261      if ( empty( $friends ) ) {
 262          $friends = array( 0 );
 263      }
 264  
 265      $retval = array(
 266          'relation' => 'AND',
 267          array(
 268              'column'  => 'user_id',
 269              'compare' => 'IN',
 270              'value'   => (array) $friends
 271          ),
 272  
 273          // We should only be able to view sitewide activity content for friends.
 274          array(
 275              'column' => 'hide_sitewide',
 276              'value'  => 0
 277          ),
 278  
 279          // Overrides.
 280          'override' => array(
 281              'filter'      => array( 'user_id' => 0 ),
 282              'show_hidden' => true
 283          ),
 284      );
 285  
 286      return $retval;
 287  }
 288  add_filter( 'bp_activity_set_friends_scope_args', 'bp_friends_filter_activity_scope', 10, 2 );
 289  
 290  /**
 291   * Set up activity arguments for use with the 'just-me' scope.
 292   *
 293   * For details on the syntax, see {@link BP_Activity_Query}.
 294   *
 295   * @since 2.2.0
 296   *
 297   * @param array $retval Empty array by default.
 298   * @param array $filter Current activity arguments.
 299   * @return array
 300   */
 301  function bp_friends_filter_activity_just_me_scope( $retval = array(), $filter = array() ) {
 302  
 303      // Determine the user_id.
 304      if ( ! empty( $filter['user_id'] ) ) {
 305          $user_id = $filter['user_id'];
 306      } else {
 307          $user_id = bp_displayed_user_id()
 308              ? bp_displayed_user_id()
 309              : bp_loggedin_user_id();
 310      }
 311  
 312      // Get the requested action.
 313      $action = $filter['filter']['action'];
 314  
 315      // Make sure actions are listed in an array.
 316      if ( ! is_array( $action ) ) {
 317          $action = explode( ',', $filter['filter']['action'] );
 318      }
 319  
 320      $action = array_flip( array_filter( $action ) );
 321  
 322      /**
 323       * If filtering activities for something other than the friendship_created
 324       * action return without changing anything
 325       */
 326      if ( ! empty( $action ) && ! isset( $action['friendship_created'] ) ) {
 327          return $retval;
 328      }
 329  
 330      // Juggle existing override value.
 331      $override = array();
 332      if ( ! empty( $retval['override'] ) ) {
 333          $override = $retval['override'];
 334          unset( $retval['override'] );
 335      }
 336  
 337      /**
 338       * Else make sure to get the friendship_created action, the user is involved in
 339       * - user initiated the friendship
 340       * - user has been requested a friendship
 341       */
 342      $retval = array(
 343          'relation' => 'OR',
 344          $retval,
 345          array(
 346              'relation' => 'AND',
 347              array(
 348                  'column' => 'component',
 349                  'value'  => 'friends',
 350              ),
 351              array(
 352                  'column' => 'secondary_item_id',
 353                  'value'  => $user_id,
 354              ),
 355          )
 356      );
 357  
 358      // Juggle back override value.
 359      if ( ! empty( $override ) ) {
 360          $retval['override'] = $override;
 361      }
 362  
 363      return $retval;
 364  }
 365  add_filter( 'bp_activity_set_just-me_scope_args', 'bp_friends_filter_activity_just_me_scope', 20, 2 );
 366  
 367  /**
 368   * Add activity stream items when one members accepts another members request
 369   * for virtual friendship.
 370   *
 371   * @since 1.9.0
 372   *
 373   * @param int         $friendship_id       ID of the friendship.
 374   * @param int         $initiator_user_id   ID of friendship initiator.
 375   * @param int         $friend_user_id      ID of user whose friendship is requested.
 376   * @param object|bool $friendship Optional Friendship object.
 377   */
 378  function bp_friends_friendship_accepted_activity( $friendship_id, $initiator_user_id, $friend_user_id, $friendship = false ) {
 379      if ( ! bp_is_active( 'activity' ) ) {
 380          return;
 381      }
 382  
 383      // Record in activity streams for the initiator.
 384      friends_record_activity( array(
 385          'user_id'           => $initiator_user_id,
 386          'type'              => 'friendship_created',
 387          'item_id'           => $friendship_id,
 388          'secondary_item_id' => $friend_user_id
 389      ) );
 390  }
 391  add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 );
 392  
 393  /**
 394   * Deletes friendship activity items when a user is deleted.
 395   *
 396   * @since 2.5.0
 397   *
 398   * @param int $user_id The ID of the user being deleted.
 399   */
 400  function bp_friends_delete_activity_on_user_delete( $user_id = 0 ) {
 401      if ( ! bp_is_active( 'activity' ) ) {
 402          return;
 403      }
 404  
 405      bp_activity_delete( array(
 406          'component'         => buddypress()->friends->id,
 407          'type'              => 'friendship_created',
 408          'secondary_item_id' => $user_id
 409      ) );
 410  }
 411  add_action( 'friends_remove_data', 'bp_friends_delete_activity_on_user_delete' );
 412  
 413  /**
 414   * Remove friendship activity item when a friendship is deleted.
 415   *
 416   * @since 3.2.0
 417   *
 418   * @param int $friendship_id ID of the friendship.
 419   */
 420  function bp_friends_delete_activity_on_friendship_delete( $friendship_id ) {
 421      friends_delete_activity( array( 'item_id' => $friendship_id, 'type' => 'friendship_created', 'user_id' => 0 ) );
 422  }
 423  add_action( 'friends_friendship_deleted', 'bp_friends_delete_activity_on_friendship_delete' );


Generated: Tue Dec 10 01:01:39 2019 Cross-referenced by PHPXref 0.7.1