[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-friends/ -> bp-friends-notifications.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 FriendsNotifications
  10   * @since 1.0.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Notification formatting callback for bp-friends notifications.
  18   *
  19   * @since 1.0.0
  20   *
  21   * @param string $action            The kind of notification being rendered.
  22   * @param int    $item_id           The primary item ID.
  23   * @param int    $secondary_item_id The secondary item ID.
  24   * @param int    $total_items       The total number of messaging-related notifications
  25   *                                  waiting for the user.
  26   * @param string $format            'string' for BuddyBar-compatible notifications;
  27   *                                  'array' for WP Toolbar. Default: 'string'.
  28   * @return array|string
  29   */
  30  function friends_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
  31  
  32      switch ( $action ) {
  33          case 'friendship_accepted':
  34              $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() . '/my-friends' );
  35  
  36              // $action and $amount are used to generate dynamic filter names.
  37              $action = 'accepted';
  38  
  39              // Set up the string and the filter.
  40              if ( (int) $total_items > 1 ) {
  41                  /* translators: %d: the number of friends */
  42                  $text = sprintf( __( '%d friends accepted your friendship requests', 'buddypress' ), (int) $total_items );
  43                  $amount = 'multiple';
  44              } else {
  45                  /* translators: %s: friend name */
  46                  $text = sprintf( __( '%s accepted your friendship request', 'buddypress' ), bp_core_get_user_displayname( $item_id ) );
  47                  $amount = 'single';
  48              }
  49  
  50              break;
  51  
  52          case 'friendship_request':
  53              $link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/?new';
  54  
  55              $action = 'request';
  56  
  57              // Set up the string and the filter.
  58              if ( (int) $total_items > 1 ) {
  59                  /* translators: %d: the number of pending requests */
  60                  $text = sprintf( __( 'You have %d pending friendship requests', 'buddypress' ), (int) $total_items );
  61                  $amount = 'multiple';
  62              } else {
  63                  /* translators: %s: friend name */
  64                  $text = sprintf( __( 'You have a friendship request from %s', 'buddypress' ), bp_core_get_user_displayname( $item_id ) );
  65                  $amount = 'single';
  66              }
  67  
  68              break;
  69      }
  70  
  71      // Return either an HTML link or an array, depending on the requested format.
  72      if ( 'string' === $format ) {
  73  
  74          /**
  75           * Filters the format of friendship notifications based on type and amount * of notifications pending.
  76           *
  77           * This is a variable filter that has four possible versions.
  78           * The four possible versions are:
  79           *   - bp_friends_single_friendship_accepted_notification
  80           *   - bp_friends_multiple_friendship_accepted_notification
  81           *   - bp_friends_single_friendship_request_notification
  82           *   - bp_friends_multiple_friendship_request_notification
  83           *
  84           * @since 1.0.0
  85           * @since 6.0.0 Adds the $secondary_item_id parameter.
  86           *
  87           * @param string|array $value             Depending on format, an HTML link to new requests profile tab or array with link and text.
  88           * @param int          $total_items       The total number of messaging-related notifications waiting for the user.
  89           * @param int          $item_id           The primary item ID.
  90           * @param int          $secondary_item_id The secondary item ID.
  91           */
  92          $return = apply_filters( 'bp_friends_' . $amount . '_friendship_' . $action . '_notification', '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', (int) $total_items, $item_id, $secondary_item_id );
  93      } else {
  94          /** This filter is documented in bp-friends/bp-friends-notifications.php */
  95          $return = apply_filters( 'bp_friends_' . $amount . '_friendship_' . $action . '_notification', array(
  96              'link' => $link,
  97              'text' => $text
  98          ), (int) $total_items, $item_id, $secondary_item_id );
  99      }
 100  
 101      /**
 102       * Fires at the end of the bp-friends notification format callback.
 103       *
 104       * @since 1.0.0
 105       *
 106       * @param string       $action            The kind of notification being rendered.
 107       * @param int          $item_id           The primary item ID.
 108       * @param int          $secondary_item_id The secondary item ID.
 109       * @param int          $total_items       The total number of messaging-related notifications
 110       *                                        waiting for the user.
 111       * @param array|string $return            Notification text string or array of link and text.
 112       */
 113      do_action( 'friends_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
 114  
 115      return $return;
 116  }
 117  
 118  /**
 119   * Clear friend-related notifications when ?new=1
 120   *
 121   * @since 1.2.0
 122   */
 123  function friends_clear_friend_notifications() {
 124      if ( isset( $_GET['new'] ) ) {
 125          bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
 126      }
 127  }
 128  add_action( 'bp_activity_screen_my_activity', 'friends_clear_friend_notifications' );
 129  
 130  /**
 131   * Delete any friendship request notifications for the logged in user.
 132   *
 133   * @since 1.9.0
 134   */
 135  function bp_friends_mark_friendship_request_notifications_by_type() {
 136      if ( isset( $_GET['new'] ) ) {
 137          bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
 138      }
 139  }
 140  add_action( 'friends_screen_requests', 'bp_friends_mark_friendship_request_notifications_by_type' );
 141  
 142  /**
 143   * Delete any friendship acceptance notifications for the logged in user.
 144   *
 145   * @since 1.9.0
 146   */
 147  function bp_friends_mark_friendship_accepted_notifications_by_type() {
 148      bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
 149  }
 150  add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
 151  
 152  /**
 153   * Notify one use that another user has requested their virtual friendship.
 154   *
 155   * @since 1.9.0
 156   *
 157   * @param int $friendship_id     The unique ID of the friendship.
 158   * @param int $initiator_user_id The friendship initiator user ID.
 159   * @param int $friend_user_id    The friendship request receiver user ID.
 160   */
 161  function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
 162      bp_notifications_add_notification( array(
 163          'user_id'           => $friend_user_id,
 164          'item_id'           => $initiator_user_id,
 165          'secondary_item_id' => $friendship_id,
 166          'component_name'    => buddypress()->friends->id,
 167          'component_action'  => 'friendship_request',
 168          'date_notified'     => bp_core_current_time(),
 169          'is_new'            => 1,
 170      ) );
 171  }
 172  add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 );
 173  
 174  /**
 175   * Remove friend request notice when a member rejects another members
 176   *
 177   * @since 1.9.0
 178   *
 179   * @param int                   $friendship_id Friendship ID (not used).
 180   * @param BP_Friends_Friendship $friendship    The friendship object.
 181   */
 182  function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
 183      bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
 184  }
 185  add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
 186  
 187  /**
 188   * Notify a member when another member accepts their virtual friendship request.
 189   *
 190   * @since 1.9.0
 191   *
 192   * @param int $friendship_id     The unique ID of the friendship.
 193   * @param int $initiator_user_id The friendship initiator user ID.
 194   * @param int $friend_user_id    The friendship request receiver user ID.
 195   */
 196  function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
 197      // Remove the friend request notice.
 198      bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
 199  
 200      // Add a friend accepted notice for the initiating user.
 201      bp_notifications_add_notification(  array(
 202          'user_id'           => $initiator_user_id,
 203          'item_id'           => $friend_user_id,
 204          'secondary_item_id' => $friendship_id,
 205          'component_name'    => buddypress()->friends->id,
 206          'component_action'  => 'friendship_accepted',
 207          'date_notified'     => bp_core_current_time(),
 208          'is_new'            => 1,
 209      ) );
 210  }
 211  add_action( 'friends_friendship_accepted', 'bp_friends_add_friendship_accepted_notification', 10, 3 );
 212  
 213  /**
 214   * Remove friend request notice when a member withdraws their friend request.
 215   *
 216   * @since 1.9.0
 217   *
 218   * @param int                   $friendship_id Friendship ID (not used).
 219   * @param BP_Friends_Friendship $friendship    The friendship object.
 220   */
 221  function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
 222      bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
 223  }
 224  add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
 225  
 226  /**
 227   * Remove friendship requests FROM user, used primarily when a user is deleted.
 228   *
 229   * @since 1.9.0
 230   *
 231   * @param int $user_id ID of the user whose notifications are removed.
 232   */
 233  function bp_friends_remove_notifications_data( $user_id = 0 ) {
 234      bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
 235  }
 236  add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
 237  
 238  /**
 239   * Add Friends-related settings to the Settings > Notifications page.
 240   *
 241   * @since 1.0.0
 242   */
 243  function friends_screen_notification_settings() {
 244  
 245      if ( ! $send_requests = bp_get_user_meta( bp_displayed_user_id(), 'notification_friends_friendship_request', true ) ) {
 246          $send_requests = 'yes';
 247      }
 248  
 249      if ( ! $accept_requests = bp_get_user_meta( bp_displayed_user_id(), 'notification_friends_friendship_accepted', true ) )
 250          $accept_requests = 'yes'; ?>
 251  
 252      <table class="notification-settings" id="friends-notification-settings">
 253          <thead>
 254              <tr>
 255                  <th class="icon"></th>
 256                  <th class="title"><?php _ex( 'Friends', 'Friend settings on notification settings page', 'buddypress' ); ?></th>
 257                  <th class="yes"><?php esc_html_e( 'Yes', 'buddypress' ); ?></th>
 258                  <th class="no"><?php esc_html_e( 'No', 'buddypress' ); ?></th>
 259              </tr>
 260          </thead>
 261  
 262          <tbody>
 263              <tr id="friends-notification-settings-request">
 264                  <td></td>
 265                  <td><?php _ex( 'A member sends you a friendship request', 'Friend settings on notification settings page', 'buddypress' ); ?></td>
 266                  <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_request]" id="notification-friends-friendship-request-yes" value="yes" <?php checked( $send_requests, 'yes', true ) ?>/><label for="notification-friends-friendship-request-yes" class="bp-screen-reader-text"><?php
 267                      /* translators: accessibility text */
 268                      esc_html_e( 'Yes, send email', 'buddypress' );
 269                  ?></label></td>
 270                  <td class="no"><input type="radio" name="notifications[notification_friends_friendship_request]" id="notification-friends-friendship-request-no" value="no" <?php checked( $send_requests, 'no', true ) ?>/><label for="notification-friends-friendship-request-no" class="bp-screen-reader-text"><?php
 271                      /* translators: accessibility text */
 272                      esc_html_e( 'No, do not send email', 'buddypress' );
 273                  ?></label></td>
 274              </tr>
 275              <tr id="friends-notification-settings-accepted">
 276                  <td></td>
 277                  <td><?php _ex( 'A member accepts your friendship request', 'Friend settings on notification settings page', 'buddypress' ) ?></td>
 278                  <td class="yes"><input type="radio" name="notifications[notification_friends_friendship_accepted]" id="notification-friends-friendship-accepted-yes" value="yes" <?php checked( $accept_requests, 'yes', true ) ?>/><label for="notification-friends-friendship-accepted-yes" class="bp-screen-reader-text"><?php
 279                      /* translators: accessibility text */
 280                      esc_html_e( 'Yes, send email', 'buddypress' );
 281                  ?></label></td>
 282                  <td class="no"><input type="radio" name="notifications[notification_friends_friendship_accepted]" id="notification-friends-friendship-accepted-no" value="no" <?php checked( $accept_requests, 'no', true ) ?>/><label for="notification-friends-friendship-accepted-no" class="bp-screen-reader-text"><?php
 283                      /* translators: accessibility text */
 284                      esc_html_e( 'No, do not send email', 'buddypress' );
 285                  ?></label></td>
 286              </tr>
 287  
 288              <?php
 289  
 290              /**
 291               * Fires after the last table row on the friends notification screen.
 292               *
 293               * @since 1.0.0
 294               */
 295              do_action( 'friends_screen_notification_settings' ); ?>
 296  
 297          </tbody>
 298      </table>
 299  
 300  <?php
 301  }
 302  add_action( 'bp_notification_settings', 'friends_screen_notification_settings' );


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1