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


Generated: Fri Nov 27 01:01:40 2020 Cross-referenced by PHPXref 0.7.1