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


Generated: Tue Jul 14 01:01:30 2020 Cross-referenced by PHPXref 0.7.1