[ 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: %d: 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      = '';
  45  
  46              if ( ! empty( $thread_id ) ) {
  47                  $link = bp_get_message_thread_view_link( $thread_id );
  48              }
  49  
  50              if ( ! empty( $secondary_item_id ) ) {
  51                  /* translators: %s: member name */
  52                  $text = sprintf( __( '%s sent you a new private message', 'buddypress' ), bp_core_get_user_displayname( $secondary_item_id ) );
  53              } else {
  54                  /* translators: %s: number of private messages */
  55                  $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) );
  56              }
  57          }
  58  
  59          if ( 'string' === $format ) {
  60              if ( ! empty( $link ) ) {
  61                  $retval = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
  62              } else {
  63                  $retval = esc_html( $text );
  64              }
  65  
  66              /** This filter is documented in wp-includes/deprecated.php */
  67              $retval = apply_filters_deprecated(
  68                  'bp_messages_' . $amount . '_new_message_notification',
  69                  array( $retval, $total_items, $text, $link, $item_id, $secondary_item_id ),
  70                  '10.0.0',
  71                  'bp_messages_' . $amount . '_new_message_' . $format . '_notification'
  72              );
  73          } else {
  74              $retval = array(
  75                  'text' => $text,
  76                  'link' => $link,
  77              );
  78  
  79              /** This filter is documented in wp-includes/deprecated.php */
  80              $retval = apply_filters_deprecated(
  81                  'bp_messages_' . $amount . '_new_message_notification',
  82                  array(
  83                      $retval,
  84                      $link, // This extra `$link` variable is the reason why we deprecated the filter.
  85                      $total_items,
  86                      $text,
  87                      $link,
  88                      $item_id,
  89                      $secondary_item_id,
  90                  ),
  91                  '10.0.0',
  92                  'bp_messages_' . $amount . '_new_message_' . $format . '_notification'
  93              );
  94          }
  95  
  96          /**
  97           * Filters the new message notification text before the notification is created.
  98           *
  99           * This is a dynamic filter. Possible filter names are:
 100           *   - 'bp_messages_multiple_new_message_string_notification'.
 101           *   - 'bp_messages_single_new_message_string_notification'.
 102           *   - 'bp_messages_multiple_new_message_array_notification'.
 103           *   - 'bp_messages_single_new_message_array_notification'.
 104           *
 105           * @param array|string $retval            An array containing the text and the link of the Notification or simply its text.
 106           * @param int          $total_items       Number of messages referred to by the notification.
 107           * @param string       $text              The raw notification text (ie, not wrapped in a link).
 108           * @param string       $link              The link of the notification.
 109           * @param int          $item_id           ID of the associated item.
 110           * @param int          $secondary_item_id ID of the secondary associated item.
 111           */
 112          $retval = apply_filters(
 113              'bp_messages_' . $amount . '_new_message_' . $format . '_notification',
 114              $retval,
 115              $total_items,
 116              $text,
 117              $link,
 118              $item_id,
 119              $secondary_item_id
 120          );
 121  
 122      // Custom notification action for the Messages component.
 123      } else {
 124          if ( 'string' === $format ) {
 125              $retval = $text;
 126          } else {
 127              $retval = array(
 128                  'text' => $text,
 129                  'link' => $link,
 130              );
 131          }
 132  
 133          /**
 134           * Backcompat for plugins that used to filter bp_messages_single_new_message_notification
 135           * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification'
 136           */
 137          if ( has_filter( 'bp_messages_single_new_message_notification' ) ) {
 138              if ( 'string' === $format ) {
 139                  /** This filter is documented in wp-includes/deprecated.php */
 140                  $retval = apply_filters_deprecated(
 141                      'bp_messages_' . $amount . '_new_message_notification',
 142                      array( $retval, $total_items, $text, $link, $item_id, $secondary_item_id ),
 143                      '10.0.0',
 144                      "bp_messages_{$action}_notification"
 145                  );
 146  
 147              } else {
 148                  /** This filter is documented in wp-includes/deprecated.php */
 149                  $retval = apply_filters_deprecated(
 150                      'bp_messages_' . $amount . '_new_message_notification',
 151                      array( $retval, $link, $total_items, $text, $link, $item_id, $secondary_item_id ),
 152                      '10.0.0',
 153                      "bp_messages_{$action}_notification"
 154                  );
 155              }
 156          }
 157  
 158          /**
 159           * Filters the custom action notification before the notification is created.
 160           *
 161           * This is a dynamic filter based on the message notification action.
 162           *
 163           * @since 2.6.0
 164           *
 165           * @param array  $value             An associative array containing the text and the link of the notification
 166           * @param int    $item_id           ID of the associated item.
 167           * @param int    $secondary_item_id ID of the secondary associated item.
 168           * @param int    $total_items       Number of messages referred to by the notification.
 169           * @param string $format            Return value format. 'string' for BuddyBar-compatible
 170           *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
 171           */
 172          $retval = apply_filters( "bp_messages_{$action}_notification", $retval, $item_id, $secondary_item_id, $total_items, $format );
 173      }
 174  
 175      /**
 176       * Fires right before returning the formatted message notifications.
 177       *
 178       * @since 1.0.0
 179       *
 180       * @param string $action            The type of message notification.
 181       * @param int    $item_id           The primary item ID.
 182       * @param int    $secondary_item_id The secondary item ID.
 183       * @param int    $total_items       Total amount of items to format.
 184       */
 185      do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 186  
 187      return $retval;
 188  }
 189  
 190  /**
 191   * Send notifications to message recipients.
 192   *
 193   * @since 1.9.0
 194   *
 195   * @param BP_Messages_Message $message Message object.
 196   */
 197  function bp_messages_message_sent_add_notification( $message ) {
 198      if ( ! empty( $message->recipients ) ) {
 199          foreach ( (array) $message->recipients as $recipient ) {
 200              bp_notifications_add_notification( array(
 201                  'user_id'           => $recipient->user_id,
 202                  'item_id'           => $message->id,
 203                  'secondary_item_id' => $message->sender_id,
 204                  'component_name'    => buddypress()->messages->id,
 205                  'component_action'  => 'new_message',
 206                  'date_notified'     => bp_core_current_time(),
 207                  'is_new'            => 1,
 208              ) );
 209          }
 210      }
 211  }
 212  add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
 213  
 214  /**
 215   * Mark new message notification when member reads a message thread directly.
 216   *
 217   * @since 1.9.0
 218   *
 219   * @global BP_Messages_Thread_Template $thread_template
 220   */
 221  function bp_messages_screen_conversation_mark_notifications() {
 222      global $thread_template;
 223  
 224      /*
 225       * Bail if viewing the logged-in user's profile or a single message thread.
 226       * The `messages_action_conversation()` action already marks the current thread as read.
 227       */
 228      if ( ! bp_is_my_profile() || ( bp_is_current_action( 'view' ) && bp_action_variable( 0 ) ) ) {
 229          return;
 230      }
 231  
 232      // Get unread PM notifications for the user.
 233      $new_pm_notifications = BP_Notifications_Notification::get( array(
 234          'user_id'           => bp_loggedin_user_id(),
 235          'component_name'    => buddypress()->messages->id,
 236          'component_action'  => 'new_message',
 237          'is_new'            => 1,
 238      ) );
 239      $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
 240  
 241      // No unread PMs, so stop!
 242      if ( empty( $unread_message_ids ) ) {
 243          return;
 244      }
 245  
 246      // Get the unread message ids for this thread only.
 247      $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
 248  
 249      // Mark each notification for each PM message as read.
 250      bp_notifications_mark_notifications_by_item_ids( bp_loggedin_user_id(), $message_ids, 'messages', 'new_message', false );
 251  }
 252  add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 );
 253  
 254  /**
 255   * Mark new message notification as read when the corresponding message is mark read.
 256   *
 257   * This callback covers mark-as-read bulk actions.
 258   *
 259   * @since 3.0.0
 260   *
 261   * @param int $thread_id ID of the thread being marked as read.
 262   * @param int $user_id   ID of the user who read the thread.
 263   * @param int $num_rows  The number of affected rows by the "mark read" update query.
 264   */
 265  function bp_messages_mark_notification_on_mark_thread( $thread_id, $user_id = 0, $num_rows = 0 ) {
 266      if ( ! $num_rows ) {
 267          return;
 268      }
 269  
 270      $thread_messages = BP_Messages_Thread::get_messages( $thread_id );
 271      $message_ids     = wp_list_pluck( $thread_messages, 'id' );
 272  
 273      bp_notifications_mark_notifications_by_item_ids( $user_id, $message_ids, 'messages', 'new_message', false );
 274  }
 275  add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread', 10, 3 );
 276  
 277  /**
 278   * When a message is deleted, delete corresponding notifications.
 279   *
 280   * @since 2.0.0
 281   *
 282   * @param int   $thread_id   ID of the thread.
 283   * @param int[] $message_ids The list of message IDs to delete.
 284   */
 285  function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
 286      // For each recipient, delete notifications corresponding to each message.
 287      $thread = new BP_Messages_Thread( $thread_id );
 288      foreach ( $thread->get_recipients() as $recipient ) {
 289          if ( ! isset( $recipient->user_id ) || ! $recipient->user_id ) {
 290              continue;
 291          }
 292  
 293          bp_notifications_delete_notifications_by_item_ids( $recipient->user_id, $message_ids, buddypress()->messages->id, 'new_message' );
 294      }
 295  }
 296  add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 );
 297  
 298  /**
 299   * Render the markup for the Messages section of Settings > Notifications.
 300   *
 301   * @since 1.0.0
 302   */
 303  function messages_screen_notification_settings() {
 304  
 305      if ( bp_action_variables() ) {
 306          bp_do_404();
 307          return;
 308      }
 309  
 310      if ( !$new_messages = bp_get_user_meta( bp_displayed_user_id(), 'notification_messages_new_message', true ) ) {
 311          $new_messages = 'yes';
 312      } ?>
 313  
 314      <table class="notification-settings" id="messages-notification-settings">
 315          <thead>
 316              <tr>
 317                  <th class="icon"></th>
 318                  <th class="title"><?php esc_html_e( 'Messages', 'buddypress' ); ?></th>
 319                  <th class="yes"><?php esc_html_e( 'Yes', 'buddypress' ); ?></th>
 320                  <th class="no"><?php esc_html_e( 'No', 'buddypress' ); ?></th>
 321              </tr>
 322          </thead>
 323  
 324          <tbody>
 325              <tr id="messages-notification-settings-new-message">
 326                  <td></td>
 327                  <td><?php esc_html_e( 'A member sends you a new message', 'buddypress' ); ?></td>
 328                  <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
 329                      /* translators: accessibility text */
 330                      esc_html_e( 'Yes, send email', 'buddypress' );
 331                  ?></label></td>
 332                  <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
 333                      /* translators: accessibility text */
 334                      esc_html_e( 'No, do not send email', 'buddypress' );
 335                  ?></label></td>
 336              </tr>
 337  
 338              <?php
 339  
 340              /**
 341               * Fires inside the closing </tbody> tag for messages screen notification settings.
 342               *
 343               * @since 1.0.0
 344               */
 345              do_action( 'messages_screen_notification_settings' ); ?>
 346          </tbody>
 347      </table>
 348  
 349  <?php
 350  }
 351  add_action( 'bp_notification_settings', 'messages_screen_notification_settings', 2 );


Generated: Tue Mar 19 01:01:09 2024 Cross-referenced by PHPXref 0.7.1