[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Activity Notifications.
   4   *
   5   * @package BuddyPress
   6   * @subpackage ActivityNotifications
   7   * @since 1.2.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Format notifications related to activity.
  15   *
  16   * @since 1.5.0
  17   *
  18   * @param string $action            The type of activity item. Just 'new_at_mention' for now.
  19   * @param int    $item_id           The activity ID.
  20   * @param int    $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
  21   * @param int    $total_items       The total number of notifications to format.
  22   * @param string $format            'string' to get a BuddyBar-compatible notification, 'array' otherwise.
  23   * @param int    $id                Optional. The notification ID.
  24   * @return string $return Formatted @mention notification.
  25   */
  26  function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) {
  27      $action_filter = $action;
  28      $return        = false;
  29      $activity_id   = $item_id;
  30      $user_id       = $secondary_item_id;
  31      $user_fullname = bp_core_get_user_displayname( $user_id );
  32  
  33      switch ( $action ) {
  34          case 'new_at_mention':
  35              $action_filter = 'at_mentions';
  36              $link          = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
  37              $title         = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
  38              $amount        = 'single';
  39  
  40              if ( (int) $total_items > 1 ) {
  41                  $text   = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
  42                  $amount = 'multiple';
  43              } else {
  44                  $text = sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
  45              }
  46          break;
  47  
  48          case 'update_reply':
  49              $link   = bp_get_notifications_permalink();
  50              $title  = __( 'New Activity reply', 'buddypress' );
  51              $amount = 'single';
  52  
  53              if ( (int) $total_items > 1 ) {
  54                  $link   = add_query_arg( 'type', $action, $link );
  55                  $text   = sprintf( __( 'You have %1$d new replies', 'buddypress' ), (int) $total_items );
  56                  $amount = 'multiple';
  57              } else {
  58                  $link = add_query_arg( 'rid', (int) $id, bp_activity_get_permalink( $activity_id ) );
  59                  $text = sprintf( __( '%1$s commented on one of your updates', 'buddypress' ), $user_fullname );
  60              }
  61          break;
  62  
  63          case 'comment_reply':
  64              $link   = bp_get_notifications_permalink();
  65              $title  = __( 'New Activity comment reply', 'buddypress' );
  66              $amount = 'single';
  67  
  68              if ( (int) $total_items > 1 ) {
  69                  $link   = add_query_arg( 'type', $action, $link );
  70                  $text   = sprintf( __( 'You have %1$d new comment replies', 'buddypress' ), (int) $total_items );
  71                  $amount = 'multiple';
  72              } else {
  73                  $link = add_query_arg( 'crid', (int) $id, bp_activity_get_permalink( $activity_id ) );
  74                  $text = sprintf( __( '%1$s replied to one of your activity comments', 'buddypress' ), $user_fullname );
  75              }
  76          break;
  77      }
  78  
  79      if ( 'string' == $format ) {
  80  
  81          /**
  82           * Filters the activity notification for the string format.
  83           *
  84           * This is a variable filter that is dependent on how many items
  85           * need notified about. The two possible hooks are bp_activity_single_at_mentions_notification
  86           * or bp_activity_multiple_at_mentions_notification.
  87           *
  88           * @since 1.5.0
  89           * @since 2.6.0 use the $action_filter as a new dynamic portion of the filter name.
  90           *
  91           * @param string $string          HTML anchor tag for the interaction.
  92           * @param string $link            The permalink for the interaction.
  93           * @param int    $total_items     How many items being notified about.
  94           * @param int    $activity_id     ID of the activity item being formatted.
  95           * @param int    $user_id         ID of the user who inited the interaction.
  96           */
  97          $return = apply_filters( 'bp_activity_' . $amount . '_' . $action_filter . '_notification', '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>', $link, (int) $total_items, $activity_id, $user_id );
  98      } else {
  99  
 100          /**
 101           * Filters the activity notification for any non-string format.
 102           *
 103           * This is a variable filter that is dependent on how many items need notified about.
 104           * The two possible hooks are bp_activity_single_at_mentions_notification
 105           * or bp_activity_multiple_at_mentions_notification.
 106           *
 107           * @since 1.5.0
 108           * @since 2.6.0 use the $action_filter as a new dynamic portion of the filter name.
 109           *
 110           * @param array  $array           Array holding the content and permalink for the interaction notification.
 111           * @param string $link            The permalink for the interaction.
 112           * @param int    $total_items     How many items being notified about.
 113           * @param int    $activity_id     ID of the activity item being formatted.
 114           * @param int    $user_id         ID of the user who inited the interaction.
 115           */
 116          $return = apply_filters( 'bp_activity_' . $amount . '_' . $action_filter . '_notification', array(
 117              'text' => $text,
 118              'link' => $link
 119          ), $link, (int) $total_items, $activity_id, $user_id );
 120      }
 121  
 122      /**
 123       * Fires right before returning the formatted activity notifications.
 124       *
 125       * @since 1.2.0
 126       *
 127       * @param string $action            The type of activity item.
 128       * @param int    $item_id           The activity ID.
 129       * @param int    $secondary_item_id The user ID who inited the interaction.
 130       * @param int    $total_items       Total amount of items to format.
 131       */
 132      do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 133  
 134      return $return;
 135  }
 136  
 137  /**
 138   * Notify a member when their nicename is mentioned in an activity stream item.
 139   *
 140   * Hooked to the 'bp_activity_sent_mention_email' action, we piggy back off the
 141   * existing email code for now, since it does the heavy lifting for us. In the
 142   * future when we separate emails from Notifications, this will need its own
 143   * 'bp_activity_at_name_send_emails' equivalent helper function.
 144   *
 145   * @since 1.9.0
 146   *
 147   * @param object $activity           Activity object.
 148   * @param string $subject (not used) Notification subject.
 149   * @param string $message (not used) Notification message.
 150   * @param string $content (not used) Notification content.
 151   * @param int    $receiver_user_id   ID of user receiving notification.
 152   */
 153  function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
 154      bp_notifications_add_notification( array(
 155              'user_id'           => $receiver_user_id,
 156              'item_id'           => $activity->id,
 157              'secondary_item_id' => $activity->user_id,
 158              'component_name'    => buddypress()->activity->id,
 159              'component_action'  => 'new_at_mention',
 160              'date_notified'     => bp_core_current_time(),
 161              'is_new'            => 1,
 162      ) );
 163  }
 164  add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
 165  
 166  /**
 167   * Notify a member one of their activity received a reply.
 168   *
 169   * @since 2.6.0
 170   *
 171   * @param BP_Activity_Activity $activity     The original activity.
 172   * @param int                  $comment_id   ID for the newly received comment.
 173   * @param int                  $commenter_id ID of the user who made the comment.
 174   */
 175  function bp_activity_update_reply_add_notification( $activity, $comment_id, $commenter_id ) {
 176      bp_notifications_add_notification( array(
 177          'user_id'           => $activity->user_id,
 178          'item_id'           => $comment_id,
 179          'secondary_item_id' => $commenter_id,
 180          'component_name'    => buddypress()->activity->id,
 181          'component_action'  => 'update_reply',
 182          'date_notified'     => bp_core_current_time(),
 183          'is_new'            => 1,
 184      ) );
 185  }
 186  add_action( 'bp_activity_sent_reply_to_update_notification', 'bp_activity_update_reply_add_notification', 10, 3 );
 187  
 188  /**
 189   * Notify a member one of their activity comment received a reply.
 190   *
 191   * @since 2.6.0
 192   *
 193   * @param BP_Activity_Activity $activity_comment The parent activity.
 194   * @param int                  $comment_id       ID for the newly received comment.
 195   * @param int                  $commenter_id     ID of the user who made the comment.
 196   */
 197  function bp_activity_comment_reply_add_notification( $activity_comment, $comment_id, $commenter_id ) {
 198      bp_notifications_add_notification( array(
 199          'user_id'           => $activity_comment->user_id,
 200          'item_id'           => $comment_id,
 201          'secondary_item_id' => $commenter_id,
 202          'component_name'    => buddypress()->activity->id,
 203          'component_action'  => 'comment_reply',
 204          'date_notified'     => bp_core_current_time(),
 205          'is_new'            => 1,
 206      ) );
 207  }
 208  add_action( 'bp_activity_sent_reply_to_reply_notification', 'bp_activity_comment_reply_add_notification', 10, 3 );
 209  
 210  /**
 211   * Mark at-mention notifications as read when users visit their Mentions page.
 212   *
 213   * @since 1.5.0
 214   * @since 2.5.0 Add the $user_id parameter
 215   *
 216   * @param int $user_id The id of the user whose notifications are marked as read.
 217   */
 218  function bp_activity_remove_screen_notifications( $user_id = 0 ) {
 219      // Only mark read if the current user is looking at his own mentions.
 220      if ( empty( $user_id ) || (int) $user_id !== (int) bp_loggedin_user_id() ) {
 221          return;
 222      }
 223  
 224      bp_notifications_mark_notifications_by_type( $user_id, buddypress()->activity->id, 'new_at_mention' );
 225  }
 226  add_action( 'bp_activity_clear_new_mentions', 'bp_activity_remove_screen_notifications', 10, 1 );
 227  
 228  /**
 229   * Mark notifications as read when a user visits an activity permalink.
 230   *
 231   * @since 2.0.0
 232   * @since 3.2.0 Marks replies to parent update and replies to an activity comment as read.
 233   *
 234   * @param BP_Activity_Activity $activity Activity object.
 235   */
 236  function bp_activity_remove_screen_notifications_single_activity_permalink( $activity ) {
 237      if ( ! is_user_logged_in() ) {
 238          return;
 239      }
 240  
 241      // Mark as read any notifications for the current user related to this activity item.
 242      bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $activity->id, buddypress()->activity->id, 'new_at_mention' );
 243  
 244      $comment_id = 0;
 245      // For replies to a parent update.
 246      if ( ! empty( $_GET['rid'] ) ) {
 247          $comment_id = (int) $_GET['rid'];
 248  
 249      // For replies to an activity comment.
 250      } elseif ( ! empty( $_GET['crid'] ) ) {
 251          $comment_id = (int) $_GET['crid'];
 252      }
 253  
 254      // Mark individual activity reply notification as read.
 255      if ( ! empty( $comment_id ) ) {
 256          BP_Notifications_Notification::update(
 257              array(
 258                  'is_new' => false
 259              ),
 260              array(
 261                  'user_id' => bp_loggedin_user_id(),
 262                  'id'      => $comment_id
 263              )
 264          );
 265      }
 266  }
 267  add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications_single_activity_permalink' );
 268  
 269  /**
 270   * Mark non-mention notifications as read when user visits our read permalink.
 271   *
 272   * In particular, 'update_reply' and 'comment_reply' notifications are handled
 273   * here. See {@link bp_activity_format_notifications()} for more info.
 274   *
 275   * @since 2.6.0
 276   */
 277  function bp_activity_remove_screen_notifications_for_non_mentions() {
 278      if ( false === is_singular() || false === is_user_logged_in() || empty( $_GET['nid'] ) ) {
 279          return;
 280      }
 281  
 282      // Mark notification as read.
 283      BP_Notifications_Notification::update(
 284          array(
 285              'is_new'  => false
 286          ),
 287          array(
 288              'user_id' => bp_loggedin_user_id(),
 289              'id'      => (int) $_GET['nid']
 290          )
 291      );
 292  }
 293  add_action( 'bp_screens', 'bp_activity_remove_screen_notifications_for_non_mentions' );
 294  
 295  /**
 296   * Delete at-mention notifications when the corresponding activity item is deleted.
 297   *
 298   * @since 2.0.0
 299   *
 300   * @param array $activity_ids_deleted IDs of deleted activity items.
 301   */
 302  function bp_activity_at_mention_delete_notification( $activity_ids_deleted = array() ) {
 303      // Let's delete all without checking if content contains any mentions
 304      // to avoid a query to get the activity.
 305      if ( ! empty( $activity_ids_deleted ) ) {
 306          foreach ( $activity_ids_deleted as $activity_id ) {
 307              bp_notifications_delete_all_notifications_by_type( $activity_id, buddypress()->activity->id );
 308          }
 309      }
 310  }
 311  add_action( 'bp_activity_deleted_activities', 'bp_activity_at_mention_delete_notification', 10 );
 312  
 313  /**
 314   * Add a notification for post comments to the post author or post commenter.
 315   *
 316   * Requires "activity stream commenting on posts and comments" to be enabled.
 317   *
 318   * @since 2.6.0
 319   *
 320   * @param int        $activity_id          The activity comment ID.
 321   * @param WP_Comment $post_type_comment    WP Comment object.
 322   * @param array      $activity_args        Activity comment arguments.
 323   * @param object     $activity_post_object The post type tracking args object.
 324   */
 325  function bp_activity_add_notification_for_synced_blog_comment( $activity_id, $post_type_comment, $activity_args, $activity_post_object ) {
 326      // If activity comments are disabled for WP posts, stop now!
 327      if ( bp_disable_blogforum_comments() || empty( $activity_id ) ) {
 328          return;
 329      }
 330  
 331      // Send a notification to the blog post author.
 332      if ( (int) $post_type_comment->post->post_author !== (int) $activity_args['user_id'] ) {
 333          // Only add a notification if comment author is a registered user.
 334          // @todo Should we remove this restriction?
 335          if ( ! empty( $post_type_comment->user_id ) ) {
 336              bp_notifications_add_notification( array(
 337                  'user_id'           => $post_type_comment->post->post_author,
 338                  'item_id'           => $activity_id,
 339                  'secondary_item_id' => $post_type_comment->user_id,
 340                  'component_name'    => buddypress()->activity->id,
 341                  'component_action'  => 'update_reply',
 342                  'date_notified'     => $post_type_comment->comment_date_gmt,
 343                  'is_new'            => 1,
 344              ) );
 345          }
 346      }
 347  
 348      // Send a notification to the parent comment author for follow-up comments.
 349      if ( ! empty( $post_type_comment->comment_parent ) ) {
 350          $parent_comment = get_comment( $post_type_comment->comment_parent );
 351  
 352          if ( ! empty( $parent_comment->user_id ) && (int) $parent_comment->user_id !== (int) $activity_args['user_id'] ) {
 353              bp_notifications_add_notification( array(
 354                  'user_id'           => $parent_comment->user_id,
 355                  'item_id'           => $activity_id,
 356                  'secondary_item_id' => $post_type_comment->user_id,
 357                  'component_name'    => buddypress()->activity->id,
 358                  'component_action'  => 'comment_reply',
 359                  'date_notified'     => $post_type_comment->comment_date_gmt,
 360                  'is_new'            => 1,
 361              ) );
 362          }
 363      }
 364  }
 365  add_action( 'bp_blogs_comment_sync_activity_comment', 'bp_activity_add_notification_for_synced_blog_comment', 10, 4 );
 366  
 367  /**
 368   * Add activity notifications settings to the notifications settings page.
 369   *
 370   * @since 1.2.0
 371   */
 372  function bp_activity_screen_notification_settings() {
 373      if ( bp_activity_do_mentions() ) {
 374          if ( ! $mention = bp_get_user_meta( bp_displayed_user_id(), 'notification_activity_new_mention', true ) ) {
 375              $mention = 'yes';
 376          }
 377      }
 378  
 379      if ( ! $reply = bp_get_user_meta( bp_displayed_user_id(), 'notification_activity_new_reply', true ) ) {
 380          $reply = 'yes';
 381      }
 382  
 383      ?>
 384  
 385      <table class="notification-settings" id="activity-notification-settings">
 386          <thead>
 387              <tr>
 388                  <th class="icon">&nbsp;</th>
 389                  <th class="title"><?php _e( 'Activity', 'buddypress' ) ?></th>
 390                  <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
 391                  <th class="no"><?php _e( 'No', 'buddypress' )?></th>
 392              </tr>
 393          </thead>
 394  
 395          <tbody>
 396              <?php if ( bp_activity_do_mentions() ) : ?>
 397                  <tr id="activity-notification-settings-mentions">
 398                      <td>&nbsp;</td>
 399                      <td><?php printf( __( 'A member mentions you in an update using "@%s"', 'buddypress' ), bp_core_get_username( bp_displayed_user_id() ) ) ?></td>
 400                      <td class="yes"><input type="radio" name="notifications[notification_activity_new_mention]" id="notification-activity-new-mention-yes" value="yes" <?php checked( $mention, 'yes', true ) ?>/><label for="notification-activity-new-mention-yes" class="bp-screen-reader-text"><?php
 401                          /* translators: accessibility text */
 402                          _e( 'Yes, send email', 'buddypress' );
 403                      ?></label></td>
 404                      <td class="no"><input type="radio" name="notifications[notification_activity_new_mention]" id="notification-activity-new-mention-no" value="no" <?php checked( $mention, 'no', true ) ?>/><label for="notification-activity-new-mention-no" class="bp-screen-reader-text"><?php
 405                          /* translators: accessibility text */
 406                          _e( 'No, do not send email', 'buddypress' );
 407                      ?></label></td>
 408                  </tr>
 409              <?php endif; ?>
 410  
 411              <tr id="activity-notification-settings-replies">
 412                  <td>&nbsp;</td>
 413                  <td><?php _e( "A member replies to an update or comment you've posted", 'buddypress' ) ?></td>
 414                  <td class="yes"><input type="radio" name="notifications[notification_activity_new_reply]" id="notification-activity-new-reply-yes" value="yes" <?php checked( $reply, 'yes', true ) ?>/><label for="notification-activity-new-reply-yes" class="bp-screen-reader-text"><?php
 415                      /* translators: accessibility text */
 416                      _e( 'Yes, send email', 'buddypress' );
 417                  ?></label></td>
 418                  <td class="no"><input type="radio" name="notifications[notification_activity_new_reply]" id="notification-activity-new-reply-no" value="no" <?php checked( $reply, 'no', true ) ?>/><label for="notification-activity-new-reply-no" class="bp-screen-reader-text"><?php
 419                      /* translators: accessibility text */
 420                      _e( 'No, do not send email', 'buddypress' );
 421                  ?></label></td>
 422              </tr>
 423  
 424              <?php
 425  
 426              /**
 427               * Fires inside the closing </tbody> tag for activity screen notification settings.
 428               *
 429               * @since 1.2.0
 430               */
 431              do_action( 'bp_activity_screen_notification_settings' ) ?>
 432          </tbody>
 433      </table>
 434  
 435  <?php
 436  }
 437  add_action( 'bp_notification_settings', 'bp_activity_screen_notification_settings', 1 );


Generated: Thu Nov 21 01:01:38 2019 Cross-referenced by PHPXref 0.7.1