[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Members 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 MembersNotifications
  10   * @since 8.0.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Notification formatting callback for bp-members notifications.
  18   *
  19   * @since 8.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 members-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 members_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
  31  
  32      switch ( $action ) {
  33          case 'accepted_invitation':
  34  
  35              // Set up the string and the filter.
  36              if ( (int) $total_items > 1 ) {
  37                  $link   = bp_get_notifications_permalink();
  38                  $amount = 'multiple';
  39  
  40                  // This is the inviter whose invitation was accepted.
  41                  if ( 0 !== (int) $secondary_item_id )  {
  42                      /* translators: %d: the number of new users */
  43                      $text = sprintf( __( '%d members accepted your membership invitations', 'buddypress' ), (int) $total_items );
  44                  // This is someone who also invited that user to join.
  45                  } else {
  46                      /* translators: %d: the number of new users */
  47                      $text = sprintf( __( '%d members are now members of the site', 'buddypress' ), (int) $total_items );
  48                  }
  49              } else {
  50                  $link   = add_query_arg( 'welcome', 1, bp_core_get_user_domain( $item_id ) );
  51                  $amount = 'single';
  52  
  53                  // This is the inviter whose invitation was accepted.
  54                  if ( 0 !== (int) $secondary_item_id )  {
  55                      /* translators: %s: new user name */
  56                      $text = sprintf( __( '%s accepted your membership invitation', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
  57                      // This is someone who also invited that user to join.
  58                  } else {
  59                      /* translators: %s: new user name */
  60                      $text = sprintf( __( '%s is now a member of the site', 'buddypress' ),  bp_core_get_user_displayname( $item_id ) );
  61                  }
  62              }
  63              break;
  64      }
  65  
  66      // Return either an HTML link or an array, depending on the requested format.
  67      if ( 'string' == $format ) {
  68  
  69          /**
  70           * Filters the format of members notifications based on type and amount * of notifications pending.
  71           *
  72           * This is a variable filter that has several versions.
  73           * The possible versions are:
  74           *   - bp_members_single_accepted_invitation_notification
  75           *   - bp_members_multiple_accepted_invitation_notification
  76           *
  77           * @since 8.0.0
  78           *
  79           * @param string|array $value             Depending on format, an HTML link to new requests profile tab or array with link and text.
  80           * @param int          $total_items       The total number of messaging-related notifications waiting for the user.
  81           * @param int          $item_id           The primary item ID.
  82           * @param int          $secondary_item_id The secondary item ID.
  83           */
  84          $return = apply_filters( 'bp_members_' . $amount . '_'. $action . '_notification', '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', (int) $total_items, $item_id, $secondary_item_id );
  85      } else {
  86          /** This filter is documented in bp-members/bp-members-notifications.php */
  87          $return = apply_filters( 'bp_members_' . $amount . '_'. $action . '_notification', array(
  88              'link' => $link,
  89              'text' => $text
  90          ), (int) $total_items, $item_id, $secondary_item_id );
  91      }
  92  
  93      /**
  94       * Fires at the end of the bp-members notification format callback.
  95       *
  96       * @since 8.0.0
  97       *
  98       * @param string       $action            The kind of notification being rendered.
  99       * @param int          $item_id           The primary item ID.
 100       * @param int          $secondary_item_id The secondary item ID.
 101       * @param int          $total_items       The total number of members-related notifications
 102       *                                        waiting for the user.
 103       * @param array|string $return            Notification text string or array of link and text.
 104       */
 105      do_action( 'members_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
 106  
 107      return $return;
 108  }
 109  
 110  /**
 111   * Notify one use that another user has accepted their site membership invitation.
 112   *
 113   * @since 8.0.0
 114   *
 115   * @param BP_Invitation $invite     Invitation that was accepted.
 116   * @param WP_user       $new_user   User who accepted the membership invite.
 117   * @param int           $inviter_id ID of the user who invited this user to the site.
 118   */
 119  function bp_members_invitations_accepted_invitation_notification( $invite, $new_user, $inviter_id ) {
 120  
 121      // Notify all inviters.
 122      $args = array(
 123          'invitee_email' => $new_user->user_email,
 124          'accepted'      => 'all',
 125      );
 126      $invites = bp_members_invitations_get_invites( $args );
 127  
 128      if ( ! $invites ) {
 129          return;
 130      }
 131  
 132      foreach ( $invites as $invite ) {
 133          // Include the id of the "accepted" invitation.
 134          if ( $invite->inviter_id === $inviter_id ) {
 135              $secondary_item_id = $invite->id;
 136          } else {
 137              // Else don't store the invite id, so we know this is not the primary.
 138              $secondary_item_id = 0;
 139          }
 140  
 141          bp_notifications_add_notification( array(
 142              'user_id'           => $invite->inviter_id,
 143              'item_id'           => $new_user->ID,
 144              'secondary_item_id' => $secondary_item_id,
 145              'component_name'    => buddypress()->members->id,
 146              'component_action'  => 'accepted_invitation',
 147              'date_notified'     => bp_core_current_time(),
 148              'is_new'            => 1,
 149          ) );
 150      }
 151  }
 152  add_action( 'members_invitations_invite_accepted', 'bp_members_invitations_accepted_invitation_notification', 10, 3 );
 153  
 154  /**
 155   * Mark accepted invitation notifications as read when user visits new user profile.
 156   *
 157   * @since 8.0.0
 158   */
 159  function bp_members_mark_read_accepted_invitation_notification() {
 160      if ( false === is_singular() || false === is_user_logged_in() || ! bp_is_user() || empty( $_GET['welcome'] ) ) {
 161          return;
 162      }
 163  
 164      // Mark notification as read.
 165      BP_Notifications_Notification::update(
 166          array(
 167              'is_new' => false,
 168          ),
 169          array(
 170              'user_id' => bp_loggedin_user_id(),
 171              'item_id' => bp_displayed_user_id(),
 172          )
 173      );
 174  }
 175  add_action( 'bp_screens', 'bp_members_mark_read_accepted_invitation_notification' );
 176  
 177  /**
 178   * Add Members-related settings to the Settings > Notifications page.
 179   *
 180   * @since 8.0.0
 181   */
 182  function members_screen_notification_settings() {
 183  
 184      // Bail early if invitations are not allowed--they are the only members notification so far.
 185      if ( ! bp_get_members_invitations_allowed () ) {
 186          return;
 187      }
 188  
 189      if ( ! $allow_acceptance_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_invitation_accepted', true ) ) {
 190          $allow_acceptance_emails = 'yes';
 191      }
 192      ?>
 193  
 194      <table class="notification-settings" id="members-notification-settings">
 195          <thead>
 196              <tr>
 197                  <th class="icon"></th>
 198                  <th class="title"><?php _ex( 'Members', 'Member settings on notification settings page', 'buddypress' ) ?></th>
 199                  <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 200                  <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 201              </tr>
 202          </thead>
 203  
 204          <tbody>
 205              <tr id="members-notification-settings-invitation_accepted">
 206                  <td></td>
 207                  <td><?php _ex( 'Someone accepts your membership invitation', 'Member settings on notification settings page', 'buddypress' ) ?></td>
 208                  <td class="yes"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-yes" value="yes" <?php checked( $allow_acceptance_emails, 'yes', true ) ?>/><label for="notification-members-invitation-accepted-yes" class="bp-screen-reader-text"><?php
 209                      /* translators: accessibility text */
 210                      _e( 'Yes, send email', 'buddypress' );
 211                  ?></label></td>
 212                  <td class="no"><input type="radio" name="notifications[notification_members_invitation_accepted]" id="notification-members-invitation-accepted-no" value="no" <?php checked( $allow_acceptance_emails, 'no', true ) ?>/><label for="notification-members-invitation-accepted-no" class="bp-screen-reader-text"><?php
 213                      /* translators: accessibility text */
 214                      _e( 'No, do not send email', 'buddypress' );
 215                  ?></label></td>
 216              </tr>
 217  
 218              <?php
 219  
 220              /**
 221               * Fires after the last table row on the members notification screen.
 222               *
 223               * @since 1.0.0
 224               */
 225              do_action( 'members_screen_notification_settings' ); ?>
 226  
 227          </tbody>
 228      </table>
 229  <?php
 230  }
 231  add_action( 'bp_notification_settings', 'members_screen_notification_settings' );


Generated: Mon Nov 29 01:01:02 2021 Cross-referenced by PHPXref 0.7.1