[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Messages Notifications.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MessagesNotifications
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Format notifications for the Messages component.
  15   *
  16   * @since 1.0.0
  17   *
  18   * @param string $action            The kind of notification being rendered.
  19   * @param int    $item_id           The primary item id.
  20   * @param int    $secondary_item_id The secondary item id.
  21   * @param int    $total_items       The total number of messaging-related notifications
  22   *                                  waiting for the user.
  23   * @param string $format            'string' for notification HTML link or 'array' for separate link and text.
  24   * @return string|array Formatted notifications.
  25   */
  26  function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
  27      $total_items = (int) $total_items;
  28      $text        = '';
  29      $link        = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
  30      $title       = __( 'Inbox', 'buddypress' );
  31      $amount      = 'single';
  32  
  33      if ( 'new_message' === $action ) {
  34          if ( $total_items > 1 ) {
  35              $amount = 'multiple';
  36  
  37              /* translators: %s: number of new messages */
  38              $text = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items );
  39  
  40          } else {
  41              // Get message thread ID.
  42              $message   = new BP_Messages_Message( $item_id );
  43              $thread_id = $message->thread_id;
  44              $link      = ( ! empty( $thread_id ) )
  45                  ? bp_get_message_thread_view_link( $thread_id )
  46                  : false;
  47  
  48              if ( ! empty( $secondary_item_id ) ) {
  49                  /* translators: %s: member name */
  50                  $text = sprintf( __( '%s sent you a new private message', 'buddypress' ), bp_core_get_user_displayname( $secondary_item_id ) );
  51              } else {
  52                  /* translators: %s: number of private messages */
  53                  $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) );
  54              }
  55          }
  56  
  57          if ( 'string' === $format ) {
  58              if ( ! empty( $link ) ) {
  59                  $return = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
  60              } else {
  61                  $return = esc_html( $text );
  62              }
  63  
  64              /**
  65               * Filters the new message notification text before the notification is created.
  66               *
  67               * This is a dynamic filter. Possible filter names are:
  68               *   - 'bp_messages_multiple_new_message_notification'.
  69               *   - 'bp_messages_single_new_message_notification'.
  70               *
  71               * @param string $return            Notification text.
  72               * @param int    $total_items       Number of messages referred to by the notification.
  73               * @param string $text              The raw notification test (ie, not wrapped in a link).
  74               * @param int    $item_id           ID of the associated item.
  75               * @param int    $secondary_item_id ID of the secondary associated item.
  76               */
  77              $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
  78          } else {
  79              /** This filter is documented in bp-messages/bp-messages-notifications.php */
  80              $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
  81                  'text' => $text,
  82                  'link' => $link
  83              ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
  84          }
  85  
  86      // Custom notification action for the Messages component
  87      } else {
  88          if ( 'string' === $format ) {
  89              $return = $text;
  90          } else {
  91              $return = array(
  92                  'text' => $text,
  93                  'link' => $link
  94              );
  95          }
  96  
  97          /**
  98           * Backcompat for plugins that used to filter bp_messages_single_new_message_notification
  99           * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification'
 100           */
 101          if ( has_filter( 'bp_messages_single_new_message_notification' ) ) {
 102              if ( 'string' === $format ) {
 103                  /** This filter is documented in bp-messages/bp-messages-notifications.php */
 104                  $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
 105  
 106              // Notice that there are seven parameters instead of six? Ugh...
 107              } else {
 108                  /** This filter is documented in bp-messages/bp-messages-notifications.php */
 109                  $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
 110              }
 111          }
 112  
 113          /**
 114           * Filters the custom action notification before the notification is created.
 115           *
 116           * This is a dynamic filter based on the message notification action.
 117           *
 118           * @since 2.6.0
 119           *
 120           * @param array  $value             An associative array containing the text and the link of the notification
 121           * @param int    $item_id           ID of the associated item.
 122           * @param int    $secondary_item_id ID of the secondary associated item.
 123           * @param int    $total_items       Number of messages referred to by the notification.
 124           * @param string $format            Return value format. 'string' for BuddyBar-compatible
 125           *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
 126           */
 127          $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format );
 128      }
 129  
 130      /**
 131       * Fires right before returning the formatted message notifications.
 132       *
 133       * @since 1.0.0
 134       *
 135       * @param string $action            The type of message notification.
 136       * @param int    $item_id           The primary item ID.
 137       * @param int    $secondary_item_id The secondary item ID.
 138       * @param int    $total_items       Total amount of items to format.
 139       */
 140      do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 141  
 142      return $return;
 143  }
 144  
 145  /**
 146   * Send notifications to message recipients.
 147   *
 148   * @since 1.9.0
 149   *
 150   * @param BP_Messages_Message $message Message object.
 151   */
 152  function bp_messages_message_sent_add_notification( $message ) {
 153      if ( ! empty( $message->recipients ) ) {
 154          foreach ( (array) $message->recipients as $recipient ) {
 155              bp_notifications_add_notification( array(
 156                  'user_id'           => $recipient->user_id,
 157                  'item_id'           => $message->id,
 158                  'secondary_item_id' => $message->sender_id,
 159                  'component_name'    => buddypress()->messages->id,
 160                  'component_action'  => 'new_message',
 161                  'date_notified'     => bp_core_current_time(),
 162                  'is_new'            => 1,
 163              ) );
 164          }
 165      }
 166  }
 167  add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
 168  
 169  /**
 170   * Mark new message notification when member reads a message thread directly.
 171   *
 172   * @since 1.9.0
 173   */
 174  function bp_messages_screen_conversation_mark_notifications() {
 175      global $thread_template;
 176  
 177      /*
 178       * Only run on the logged-in user's profile.
 179       * If an admin visits a thread, it shouldn't change the read status.
 180       */
 181      if ( ! bp_is_my_profile() ) {
 182          return;
 183      }
 184  
 185      // Get unread PM notifications for the user.
 186      $new_pm_notifications = BP_Notifications_Notification::get( array(
 187          'user_id'           => bp_loggedin_user_id(),
 188          'component_name'    => buddypress()->messages->id,
 189          'component_action'  => 'new_message',
 190          'is_new'            => 1,
 191      ) );
 192      $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
 193  
 194      // No unread PMs, so stop!
 195      if ( empty( $unread_message_ids ) ) {
 196          return;
 197      }
 198  
 199      // Get the unread message ids for this thread only.
 200      $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
 201  
 202      // Mark each notification for each PM message as read.
 203      foreach ( $message_ids as $message_id ) {
 204          bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
 205      }
 206  }
 207  add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 );
 208  
 209  /**
 210   * Mark new message notification as read when the corresponding message is mark read.
 211   *
 212   * This callback covers mark-as-read bulk actions.
 213   *
 214   * @since 3.0.0
 215   *
 216   * @param int $thread_id ID of the thread being marked as read.
 217   */
 218  function bp_messages_mark_notification_on_mark_thread( $thread_id ) {
 219      $thread_messages = BP_Messages_Thread::get_messages( $thread_id );
 220  
 221      foreach ( $thread_messages as $thread_message ) {
 222          bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $thread_message->id, buddypress()->messages->id, 'new_message' );
 223      }
 224  }
 225  add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' );
 226  
 227  /**
 228   * When a message is deleted, delete corresponding notifications.
 229   *
 230   * @since 2.0.0
 231   *
 232   * @param int   $thread_id   ID of the thread.
 233   * @param array $message_ids IDs of the messages.
 234   */
 235  function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
 236      // For each recipient, delete notifications corresponding to each message.
 237      $thread = new BP_Messages_Thread( $thread_id );
 238      foreach ( $thread->get_recipients() as $recipient ) {
 239          foreach ( $message_ids as $message_id ) {
 240              bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' );
 241          }
 242      }
 243  }
 244  add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 );
 245  
 246  /**
 247   * Render the markup for the Messages section of Settings > Notifications.
 248   *
 249   * @since 1.0.0
 250   */
 251  function messages_screen_notification_settings() {
 252  
 253      if ( bp_action_variables() ) {
 254          bp_do_404();
 255          return;
 256      }
 257  
 258      if ( !$new_messages = bp_get_user_meta( bp_displayed_user_id(), 'notification_messages_new_message', true ) ) {
 259          $new_messages = 'yes';
 260      } ?>
 261  
 262      <table class="notification-settings" id="messages-notification-settings">
 263          <thead>
 264              <tr>
 265                  <th class="icon"></th>
 266                  <th class="title"><?php _e( 'Messages', 'buddypress' ) ?></th>
 267                  <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 268                  <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 269              </tr>
 270          </thead>
 271  
 272          <tbody>
 273              <tr id="messages-notification-settings-new-message">
 274                  <td></td>
 275                  <td><?php _e( 'A member sends you a new message', 'buddypress' ) ?></td>
 276                  <td class="yes"><input type="radio" name="notifications[notification_messages_new_message]" id="notification-messages-new-messages-yes" value="yes" <?php checked( $new_messages, 'yes', true ) ?>/><label for="notification-messages-new-messages-yes" class="bp-screen-reader-text"><?php
 277                      /* translators: accessibility text */
 278                      _e( 'Yes, send email', 'buddypress' );
 279                  ?></label></td>
 280                  <td class="no"><input type="radio" name="notifications[notification_messages_new_message]" id="notification-messages-new-messages-no" value="no" <?php checked( $new_messages, 'no', true ) ?>/><label for="notification-messages-new-messages-no" class="bp-screen-reader-text"><?php
 281                      /* translators: accessibility text */
 282                      _e( 'No, do not send email', 'buddypress' );
 283                  ?></label></td>
 284              </tr>
 285  
 286              <?php
 287  
 288              /**
 289               * Fires inside the closing </tbody> tag for messages screen notification settings.
 290               *
 291               * @since 1.0.0
 292               */
 293              do_action( 'messages_screen_notification_settings' ); ?>
 294          </tbody>
 295      </table>
 296  
 297  <?php
 298  }
 299  add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );


Generated: Thu Jun 24 01:01:38 2021 Cross-referenced by PHPXref 0.7.1