[ 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          case 'membership_request_submitted':
  66              // $item_id is the id of the signup, not the user ID.
  67              $signup = new BP_Signup( $item_id );
  68  
  69              // Set up the string and the filter.
  70              if ( (int) $total_items > 1 ) {
  71                  $link   = bp_get_notifications_permalink();
  72                  $amount = 'multiple';
  73  
  74                  /* translators: %d: is the number of people who requested site membership */
  75                  $text = sprintf( __( '%d people have requested site membership.', 'buddypress' ), (int) $total_items );
  76              } else {
  77                  $link   = add_query_arg( array(
  78                      'mod_req'   => 1,
  79                      'page'      => 'bp-signups',
  80                      'signup_id' => $item_id,
  81                      'action'    => 'resend',
  82                  ), bp_get_admin_url( 'users.php' ) );
  83                  $amount = 'single';
  84  
  85                  /* translators: %s: new user name */
  86                  $text = sprintf( __( '%s has requested site membership.', 'buddypress' ),  esc_html( $signup->user_login ) );
  87              }
  88              break;
  89      }
  90  
  91      // Return either an HTML link or an array, depending on the requested format.
  92      if ( 'string' == $format ) {
  93  
  94          /**
  95           * Filters the format of members notifications based on type and amount * of notifications pending.
  96           *
  97           * This is a variable filter that has several versions.
  98           * The possible versions are:
  99           *   - bp_members_single_accepted_invitation_notification
 100           *   - bp_members_multiple_accepted_invitation_notification
 101           *
 102           * @since 8.0.0
 103           *
 104           * @param string|array $value             Depending on format, an HTML link to new requests profile tab or array with link and text.
 105           * @param int          $total_items       The total number of messaging-related notifications waiting for the user.
 106           * @param int          $item_id           The primary item ID.
 107           * @param int          $secondary_item_id The secondary item ID.
 108           */
 109          $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 );
 110      } else {
 111          /** This filter is documented in bp-members/bp-members-notifications.php */
 112          $return = apply_filters( 'bp_members_' . $amount . '_'. $action . '_notification', array(
 113              'link' => $link,
 114              'text' => $text
 115          ), (int) $total_items, $item_id, $secondary_item_id );
 116      }
 117  
 118      /**
 119       * Fires at the end of the bp-members notification format callback.
 120       *
 121       * @since 8.0.0
 122       *
 123       * @param string       $action            The kind of notification being rendered.
 124       * @param int          $item_id           The primary item ID.
 125       * @param int          $secondary_item_id The secondary item ID.
 126       * @param int          $total_items       The total number of members-related notifications
 127       *                                        waiting for the user.
 128       * @param array|string $return            Notification text string or array of link and text.
 129       */
 130      do_action( 'members_format_notifications', $action, $item_id, $secondary_item_id, $total_items, $return );
 131  
 132      return $return;
 133  }
 134  
 135  /**
 136   * Notify one use that another user has accepted their site membership invitation.
 137   *
 138   * @since 8.0.0
 139   *
 140   * @param BP_Invitation $invite     Invitation that was accepted.
 141   * @param WP_user       $new_user   User who accepted the membership invite.
 142   * @param int           $inviter_id ID of the user who invited this user to the site.
 143   */
 144  function bp_members_invitations_accepted_invitation_notification( $invite, $new_user, $inviter_id ) {
 145  
 146      // Notify all inviters.
 147      $args = array(
 148          'invitee_email' => $new_user->user_email,
 149          'accepted'      => 'all',
 150      );
 151      $invites = bp_members_invitations_get_invites( $args );
 152  
 153      if ( ! $invites ) {
 154          return;
 155      }
 156  
 157      foreach ( $invites as $invite ) {
 158          // Include the id of the "accepted" invitation.
 159          if ( $invite->inviter_id === $inviter_id ) {
 160              $secondary_item_id = $invite->id;
 161          } else {
 162              // Else don't store the invite id, so we know this is not the primary.
 163              $secondary_item_id = 0;
 164          }
 165  
 166          bp_notifications_add_notification( array(
 167              'user_id'           => $invite->inviter_id,
 168              'item_id'           => $new_user->ID,
 169              'secondary_item_id' => $secondary_item_id,
 170              'component_name'    => buddypress()->members->id,
 171              'component_action'  => 'accepted_invitation',
 172              'date_notified'     => bp_core_current_time(),
 173              'is_new'            => 1,
 174          ) );
 175      }
 176  }
 177  add_action( 'members_invitations_invite_accepted', 'bp_members_invitations_accepted_invitation_notification', 10, 3 );
 178  
 179  /**
 180   * Mark accepted invitation notifications as read when user visits new user profile.
 181   *
 182   * @since 8.0.0
 183   */
 184  function bp_members_mark_read_accepted_invitation_notification() {
 185      if ( false === is_singular() || false === is_user_logged_in() || ! bp_is_user() || empty( $_GET['welcome'] ) ) {
 186          return;
 187      }
 188  
 189      // Mark notification as read.
 190      BP_Notifications_Notification::update(
 191          array(
 192              'is_new' => false,
 193          ),
 194          array(
 195              'user_id'          => bp_loggedin_user_id(),
 196              'item_id'          => bp_displayed_user_id(),
 197              'component_action' => 'accepted_invitation',
 198          )
 199      );
 200  }
 201  add_action( 'bp_screens', 'bp_members_mark_read_accepted_invitation_notification' );
 202  
 203  /**
 204   * Mark new membership request notifications as read when user visits Manage BP Signups screen.
 205   *
 206   * @since 10.0.0
 207   */
 208  function bp_members_mark_read_submitted_membership_request_notification() {
 209  
 210      $signup_screens = array( 'users_page_bp-signups', 'users_page_bp-signups-network' );
 211      if ( ! wp_doing_ajax() && in_array( get_current_screen()->base, $signup_screens, true ) && ! empty( $_GET['mod_req'] ) && ! empty( $_GET['signup_id'] ) ) {
 212          // Mark all notifications about this request as read.
 213          BP_Notifications_Notification::update(
 214              array(
 215                  'is_new' => false,
 216              ),
 217              array(
 218                  'item_id'          => $_GET['signup_id'],
 219                  'component_action' => 'membership_request_submitted',
 220              )
 221          );
 222      }
 223  }
 224  add_action( 'admin_footer', 'bp_members_mark_read_submitted_membership_request_notification' );
 225  
 226  /**
 227   * Add Members-related settings to the Settings > Notifications page.
 228   *
 229   * @since 8.0.0
 230   */
 231  function members_screen_notification_settings() {
 232  
 233      // Bail early if invitations and requests are not allowed--they are the only members notification so far.
 234      if ( ! bp_get_members_invitations_allowed() && ( ! bp_get_membership_requests_required() || ! user_can( bp_displayed_user_id(), 'bp_moderate' ) ) ) {
 235          return;
 236      }
 237      ?>
 238  
 239      <table class="notification-settings" id="members-notification-settings">
 240          <thead>
 241              <tr>
 242                  <th class="icon"></th>
 243                  <th class="title"><?php _ex( 'Members', 'Member settings on notification settings page', 'buddypress' ) ?></th>
 244                  <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 245                  <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 246              </tr>
 247          </thead>
 248  
 249          <tbody>
 250  
 251              <?php
 252              if ( bp_get_members_invitations_allowed() ) :
 253                  if ( ! $allow_acceptance_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_invitation_accepted', true ) ) {
 254                      $allow_acceptance_emails = 'yes';
 255                  }
 256                  ?>
 257                  <tr id="members-notification-settings-invitation_accepted">
 258                      <td></td>
 259                      <td><?php echo esc_html_x( 'Someone accepts your membership invitation', 'Member settings on notification settings page', 'buddypress' ); ?></td>
 260                      <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">
 261                          <?php
 262                          /* translators: accessibility text */
 263                          esc_html_e( 'Yes, send email', 'buddypress' );
 264                          ?>
 265                      </label></td>
 266                      <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">
 267                          <?php
 268                          /* translators: accessibility text */
 269                          esc_html_e( 'No, do not send email', 'buddypress' );
 270                          ?>
 271                      </label></td>
 272                  </tr>
 273                  <?php
 274              endif;
 275  
 276              if ( bp_get_membership_requests_required() && user_can( bp_displayed_user_id(), 'bp_moderate' ) ) :
 277                  if ( ! $allow_request_emails = bp_get_user_meta( bp_displayed_user_id(), 'notification_members_membership_request', true ) ) {
 278                      $allow_request_emails = 'yes';
 279                  }
 280                  ?>
 281                  <tr id="members-notification-settings-submitted_membership_request">
 282                      <td></td>
 283                      <td><?php echo esc_html_x( 'Someone has requested site membership', 'Member settings on notification settings page', 'buddypress' ) ?></td>
 284                      <td class="yes"><input type="radio" name="notifications[notification_members_membership_request]" id="notification-members-submitted_membership_request-yes" value="yes" <?php checked( $allow_request_emails, 'yes', true ) ?>/><label for="notification-members-submitted_membership_request-yes" class="bp-screen-reader-text">
 285                          <?php
 286                          /* translators: accessibility text */
 287                          esc_html_e( 'Yes, send email', 'buddypress' );
 288                          ?>
 289                      </label></td>
 290                      <td class="no"><input type="radio" name="notifications[notification_members_membership_request]" id="notification-members-submitted_membership_request-no" value="no" <?php checked( $allow_request_emails, 'no', true ) ?>/><label for="notification-members-submitted_membership_request-no" class="bp-screen-reader-text">
 291                          <?php
 292                          /* translators: accessibility text */
 293                          esc_html_e( 'No, do not send email', 'buddypress' );
 294                          ?>
 295                      </label></td>
 296                  </tr>
 297                  <?php
 298              endif;
 299  
 300              /**
 301               * Fires after the last table row on the members notification screen.
 302               *
 303               * @since 1.0.0
 304               */
 305              do_action( 'members_screen_notification_settings' ); ?>
 306  
 307          </tbody>
 308      </table>
 309  <?php
 310  }
 311  add_action( 'bp_notification_settings', 'members_screen_notification_settings' );


Generated: Wed Apr 24 01:01:03 2024 Cross-referenced by PHPXref 0.7.1