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


Generated: Wed Jul 17 01:01:40 2019 Cross-referenced by PHPXref 0.7.1