Skip to:
Content

BuddyPress.org

Ticket #6057: 6057.02.patch

File 6057.02.patch, 34.6 KB (added by imath, 9 years ago)
  • src/bp-activity/bp-activity-notifications.php

    diff --git src/bp-activity/bp-activity-notifications.php src/bp-activity/bp-activity-notifications.php
    index 7fb5e92..be15f5b 100644
    To view and respond to the message, log in and visit: %3$s 
    181181function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
    182182
    183183        // Set some default parameters.
    184         $activity_id = 0;
    185         $parent_id   = 0;
    186 
    187         extract( $params );
    188 
    189         $original_activity = new BP_Activity_Activity( $activity_id );
    190 
    191         if ( $original_activity->user_id != $commenter_id && 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
    192                 $poster_name   = bp_core_get_user_displayname( $commenter_id );
    193                 $thread_link   = bp_activity_get_permalink( $activity_id );
    194                 $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    195                 $settings_link = bp_core_get_user_domain( $original_activity->user_id ) . $settings_slug . '/notifications/';
    196 
    197                 $poster_name = stripslashes( $poster_name );
    198                 $content = bp_activity_filter_kses( stripslashes($content) );
    199 
    200                 // Set up and send the message.
    201                 $ud      = bp_core_get_core_userdata( $original_activity->user_id );
    202                 $to      = $ud->user_email;
    203                 $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
    204                 $message = sprintf( __(
     184        $reply = (object) wp_parse_args( $params, array(
     185                'activity_id' => 0,
     186                'parent_id'   => 0,
     187        ) );
     188
     189        $original_activity = new BP_Activity_Activity( $reply->activity_id );
     190
     191        if ( $original_activity->user_id != $commenter_id ) {
     192                // Send an email if the user has not disabled it
     193                if ( 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
     194                        $poster_name   = bp_core_get_user_displayname( $commenter_id );
     195                        $thread_link   = bp_activity_get_permalink( $reply->activity_id );
     196                        $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
     197                        $settings_link = bp_core_get_user_domain( $original_activity->user_id ) . $settings_slug . '/notifications/';
     198
     199                        $poster_name = stripslashes( $poster_name );
     200                        $content = bp_activity_filter_kses( stripslashes( $reply->content ) );
     201
     202                        // Set up and send the message
     203                        $ud      = bp_core_get_core_userdata( $original_activity->user_id );
     204                        $to      = $ud->user_email;
     205                        $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
     206                        $message = sprintf( __(
    205207'%1$s replied to one of your updates:
    206208
    207209"%2$s"
    To view your original update and all comments, log in and visit: %3$s 
    211213---------------------
    212214', 'buddypress' ), $poster_name, $content, $thread_link );
    213215
    214                 // Only show the disable notifications line if the settings component is enabled.
    215                 if ( bp_is_active( 'settings' ) ) {
    216                         $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    217                 }
    218 
    219                 /**
    220                  * Filters the user email that the new comment notification will be sent to.
    221                  *
    222                  * @since 1.2.0
    223                  *
    224                  * @param string $to User email the notification is being sent to.
    225                  */
    226                 $to = apply_filters( 'bp_activity_new_comment_notification_to', $to );
    227 
    228                 /**
    229                  * Filters the new comment notification subject that will be sent to user.
    230                  *
    231                  * @since 1.2.0
    232                  *
    233                  * @param string $subject     Email notification subject text.
    234                  * @param string $poster_name Name of the person who made the comment.
    235                  */
    236                 $subject = apply_filters( 'bp_activity_new_comment_notification_subject', $subject, $poster_name );
    237 
    238                 /**
    239                  * Filters the new comment notification message that will be sent to user.
    240                  *
    241                  * @since 1.2.0
    242                  *
    243                  * @param string $message       Email notification message text.
    244                  * @param string $poster_name   Name of the person who made the comment.
    245                  * @param string $content       Content of the comment.
    246                  * @param string $thread_link   URL permalink for the activity thread.
    247                  * @param string $settings_link URL permalink for the user's notification settings area.
    248                  */
    249                 $message = apply_filters( 'bp_activity_new_comment_notification_message', $message, $poster_name, $content, $thread_link, $settings_link );
     216                        // Only show the disable notifications line if the settings component is enabled.
     217                        if ( bp_is_active( 'settings' ) ) {
     218                                $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
     219                        }
    250220
    251                 wp_mail( $to, $subject, $message );
     221                        /**
     222                         * Filters the user email that the new comment notification will be sent to.
     223                         *
     224                         * @since 1.2.0
     225                         *
     226                         * @param string $to User email the notification is being sent to.
     227                         */
     228                        $to = apply_filters( 'bp_activity_new_comment_notification_to', $to );
     229
     230                        /**
     231                         * Filters the new comment notification subject that will be sent to user.
     232                         *
     233                         * @since 1.2.0
     234                         *
     235                         * @param string $subject     Email notification subject text.
     236                         * @param string $poster_name Name of the person who made the comment.
     237                         */
     238                        $subject = apply_filters( 'bp_activity_new_comment_notification_subject', $subject, $poster_name );
     239
     240                        /**
     241                         * Filters the new comment notification message that will be sent to user.
     242                         *
     243                         * @since 1.2.0
     244                         *
     245                         * @param string $message       Email notification message text.
     246                         * @param string $poster_name   Name of the person who made the comment.
     247                         * @param string $content       Content of the comment.
     248                         * @param string $thread_link   URL permalink for the activity thread.
     249                         * @param string $settings_link URL permalink for the user's notification settings area.
     250                         */
     251                        $message = apply_filters( 'bp_activity_new_comment_notification_message', $message, $poster_name, $content, $thread_link, $settings_link );
     252
     253                        wp_mail( $to, $subject, $message );
     254
     255                        /**
     256                         * Fires after the sending of a reply to an update email notification.
     257                         *
     258                         * @since 1.5.0
     259                         *
     260                         * @param int    $user_id      ID of the original activity item author.
     261                         * @param string $subject      Email notification subject text.
     262                         * @param string $message      Email notification message text.
     263                         * @param int    $comment_id   ID for the newly received comment.
     264                         * @param int    $commenter_id ID of the user who made the comment.
     265                         * @param array  $params       Arguments used with the original activity comment.
     266                         */
     267                        do_action( 'bp_activity_sent_reply_to_update_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
     268                }
    252269
    253270                /**
    254                  * Fires after the sending of a reply to an update email notification.
    255                  *
    256                  * @since 1.5.0
    257                  *
    258                  * @param int    $user_id      ID of the original activity item author.
    259                  * @param string $subject      Email notification subject text.
    260                  * @param string $message      Email notification message text.
    261                  * @param int    $comment_id   ID for the newly received comment.
    262                  * @param int    $commenter_id ID of the user who made the comment.
    263                  * @param array  $params       Arguments used with the original activity comment.
    264                  */
    265                 do_action( 'bp_activity_sent_reply_to_update_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
     271                 * Trigger the 'update_reply' notification.
     272                 *
     273                 * @since 2.5.0
     274                 *
     275                 * @param BP_Activity_Activity $original_activity the original activity.
     276                 * @param int                  $comment_id ID for the newly received comment.
     277                 * @param int                  $commenter_id ID of the user who made the comment.
     278                 * @param object               $reply Arguments used with the original activity comment.
     279                 */
     280                do_action( 'bp_activity_sent_reply_to_update_notification', $original_activity, $comment_id, $commenter_id, $reply );
    266281        }
    267282
    268283        /*
    269284         * If this is a reply to another comment, send an email notification to the
    270285         * author of the immediate parent comment.
    271286         */
    272         if ( empty( $parent_id ) || ( $activity_id == $parent_id ) ) {
     287        if ( empty( $reply->parent_id ) || ( $reply->activity_id == $reply->parent_id ) ) {
    273288                return false;
    274289        }
    275290
    276         $parent_comment = new BP_Activity_Activity( $parent_id );
     291        $parent_comment = new BP_Activity_Activity( $reply->parent_id );
    277292
    278         if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id && 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
    279                 $poster_name   = bp_core_get_user_displayname( $commenter_id );
    280                 $thread_link   = bp_activity_get_permalink( $activity_id );
    281                 $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    282                 $settings_link = bp_core_get_user_domain( $parent_comment->user_id ) . $settings_slug . '/notifications/';
     293        if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id ) {
     294                // Send an email if the user has not disabled it
     295                if ( 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
     296                        $poster_name   = bp_core_get_user_displayname( $commenter_id );
     297                        $thread_link   = bp_activity_get_permalink( $reply->activity_id );
     298                        $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
     299                        $settings_link = bp_core_get_user_domain( $parent_comment->user_id ) . $settings_slug . '/notifications/';
    283300
    284                 // Set up and send the message.
    285                 $ud       = bp_core_get_core_userdata( $parent_comment->user_id );
    286                 $to       = $ud->user_email;
    287                 $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
     301                        // Set up and send the message
     302                        $ud      = bp_core_get_core_userdata( $parent_comment->user_id );
     303                        $to      = $ud->user_email;
     304                        $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
    288305
    289                 $poster_name = stripslashes( $poster_name );
    290                 $content = bp_activity_filter_kses( stripslashes( $content ) );
     306                        $poster_name = stripslashes( $poster_name );
     307                        $content = bp_activity_filter_kses( stripslashes( $reply->content ) );
    291308
    292309$message = sprintf( __(
    293310'%1$s replied to one of your comments:
    To view the original activity, your comment and all replies, log in and visit: % 
    299316---------------------
    300317', 'buddypress' ), $poster_name, $content, $thread_link );
    301318
    302                 // Only show the disable notifications line if the settings component is enabled.
    303                 if ( bp_is_active( 'settings' ) ) {
    304                         $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    305                 }
    306 
    307                 /**
    308                  * Filters the user email that the new comment reply notification will be sent to.
    309                  *
    310                  * @since 1.2.0
    311                  *
    312                  * @param string $to User email the notification is being sent to.
    313                  */
    314                 $to = apply_filters( 'bp_activity_new_comment_notification_comment_author_to', $to );
    315 
    316                 /**
    317                  * Filters the new comment reply notification subject that will be sent to user.
    318                  *
    319                  * @since 1.2.0
    320                  *
    321                  * @param string $subject     Email notification subject text.
    322                  * @param string $poster_name Name of the person who made the comment reply.
    323                  */
    324                 $subject = apply_filters( 'bp_activity_new_comment_notification_comment_author_subject', $subject, $poster_name );
    325 
    326                 /**
    327                  * Filters the new comment reply notification message that will be sent to user.
    328                  *
    329                  * @since 1.2.0
    330                  *
    331                  * @param string $message       Email notification message text.
    332                  * @param string $poster_name   Name of the person who made the comment reply.
    333                  * @param string $content       Content of the comment reply.
    334                  * @param string $settings_link URL permalink for the user's notification settings area.
    335                  * @param string $thread_link   URL permalink for the activity thread.
    336                  */
    337                 $message = apply_filters( 'bp_activity_new_comment_notification_comment_author_message', $message, $poster_name, $content, $settings_link, $thread_link );
     319                        // Only show the disable notifications line if the settings component is enabled.
     320                        if ( bp_is_active( 'settings' ) ) {
     321                                $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
     322                        }
    338323
    339                 wp_mail( $to, $subject, $message );
     324                        /**
     325                         * Filters the user email that the new comment reply notification will be sent to.
     326                         *
     327                         * @since 1.2.0
     328                         *
     329                         * @param string $to User email the notification is being sent to.
     330                         */
     331                        $to = apply_filters( 'bp_activity_new_comment_notification_comment_author_to', $to );
     332
     333                        /**
     334                         * Filters the new comment reply notification subject that will be sent to user.
     335                         *
     336                         * @since 1.2.0
     337                         *
     338                         * @param string $subject     Email notification subject text.
     339                         * @param string $poster_name Name of the person who made the comment reply.
     340                         */
     341                        $subject = apply_filters( 'bp_activity_new_comment_notification_comment_author_subject', $subject, $poster_name );
     342
     343                        /**
     344                         * Filters the new comment reply notification message that will be sent to user.
     345                         *
     346                         * @since 1.2.0
     347                         *
     348                         * @param string $message       Email notification message text.
     349                         * @param string $poster_name   Name of the person who made the comment reply.
     350                         * @param string $content       Content of the comment reply.
     351                         * @param string $settings_link URL permalink for the user's notification settings area.
     352                         * @param string $thread_link   URL permalink for the activity thread.
     353                         */
     354                        $message = apply_filters( 'bp_activity_new_comment_notification_comment_author_message', $message, $poster_name, $content, $settings_link, $thread_link );
     355
     356                        wp_mail( $to, $subject, $message );
     357
     358                        /**
     359                         * Fires after the sending of a reply to a reply email notification.
     360                         *
     361                         * @since 1.5.0
     362                         *
     363                         * @param int    $user_id      ID of the parent activity item author.
     364                         * @param string $subject      Email notification subject text.
     365                         * @param string $message      Email notification message text.
     366                         * @param int    $comment_id   ID for the newly received comment.
     367                         * @param int    $commenter_id ID of the user who made the comment.
     368                         * @param array  $params       Arguments used with the original activity comment.
     369                         */
     370                        do_action( 'bp_activity_sent_reply_to_reply_email', $parent_comment->user_id, $subject, $message, $comment_id, $commenter_id, $params );
     371                }
    340372
    341373                /**
    342                  * Fires after the sending of a reply to a reply email notification.
    343                  *
    344                  * @since 1.5.0
    345                  *
    346                  * @param int    $user_id      ID of the parent activity item author.
    347                  * @param string $subject      Email notification subject text.
    348                  * @param string $message      Email notification message text.
    349                  * @param int    $comment_id   ID for the newly received comment.
    350                  * @param int    $commenter_id ID of the user who made the comment.
    351                  * @param array  $params       Arguments used with the original activity comment.
    352                  */
    353                 do_action( 'bp_activity_sent_reply_to_reply_email', $parent_comment->user_id, $subject, $message, $comment_id, $commenter_id, $params );
     374                 * Trigger the comment_reply notification.
     375                 *
     376                 * @since 2.5.0
     377                 *
     378                 * @param BP_Activity_Activity $parent_comment the parent activity.
     379                 * @param int                  $comment_id ID for the newly received comment.
     380                 * @param int                  $commenter_id ID of the user who made the comment.
     381                 * @param object               $reply Arguments used with the original activity comment.
     382                 */
     383                do_action( 'bp_activity_sent_reply_to_reply_notification', $parent_comment, $comment_id, $commenter_id, $reply );
    354384        }
    355385}
    356386
    add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_ 
    389419 * @return string $return Formatted @mention notification.
    390420 */
    391421function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     422        $action_filter = $action;
     423        $return        = false;
     424        $activity_id   = $item_id;
     425        $user_id       = $secondary_item_id;
     426        $user_fullname = bp_core_get_user_displayname( $user_id );
    392427
    393428        switch ( $action ) {
    394429                case 'new_at_mention':
    395                         $activity_id      = $item_id;
    396                         $poster_user_id   = $secondary_item_id;
    397                         $at_mention_link  = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
    398                         $at_mention_title = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
    399                         $amount = 'single';
     430                        $action_filter = 'at_mentions';
     431                        $link          = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/';
     432                        $title         = sprintf( __( '@%s Mentions', 'buddypress' ), bp_get_loggedin_user_username() );
     433                        $amount        = 'single';
    400434
    401435                        if ( (int) $total_items > 1 ) {
    402436                                $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
    403437                                $amount = 'multiple';
    404438                        } else {
    405                                 $user_fullname = bp_core_get_user_displayname( $poster_user_id );
    406439                                $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
    407440                        }
    408441                break;
     442
     443                case 'update_reply':
     444                        $link   = bp_get_notifications_permalink();
     445                        $title  = __( 'New Activity reply', 'buddypress' );
     446                        $amount = 'single';
     447
     448                        if ( (int) $total_items > 1 ) {
     449                                $link   = add_query_arg( 'type', $action, $link );
     450                                $text   = sprintf( __( 'You have %1$d new replies', 'buddypress' ), (int) $total_items );
     451                                $amount = 'multiple';
     452                        } else {
     453                                $link   = bp_activity_get_permalink( $activity_id );
     454                                $text   =  sprintf( __( '%1$s commented one of your updates', 'buddypress' ), $user_fullname );
     455                        }
     456                break;
     457
     458                case 'comment_reply':
     459                        $link   = bp_get_notifications_permalink();
     460                        $title  = __( 'New Activity comment reply', 'buddypress' );
     461                        $amount = 'single';
     462
     463                        if ( (int) $total_items > 1 ) {
     464                                $link   = add_query_arg( 'type', $action, $link );
     465                                $text   = sprintf( __( 'You have %1$d new comment replies', 'buddypress' ), (int) $total_items );
     466                                $amount = 'multiple';
     467                        } else {
     468                                $link   = bp_activity_get_permalink( $activity_id );
     469                                $text   =  sprintf( __( '%1$s replied to one your activity comments', 'buddypress' ), $user_fullname );
     470                        }
     471                break;
    409472        }
    410473
    411474        if ( 'string' == $format ) {
    412475
    413476                /**
    414                  * Filters the @mention notification for the string format.
     477                 * Filters the activity notification for the string format.
    415478                 *
    416479                 * This is a variable filter that is dependent on how many items
    417480                 * need notified about. The two possible hooks are bp_activity_single_at_mentions_notification
    418481                 * or bp_activity_multiple_at_mentions_notification.
    419482                 *
    420483                 * @since 1.5.0
     484                 * @since 2.5.0 use the $action_filter as a new dynamic portion of the filter name.
    421485                 *
    422                  * @param string $string          HTML anchor tag for the mention.
    423                  * @param string $at_mention_link The permalink for the mention.
     486                 * @param string $string          HTML anchor tag for the interaction.
     487                 * @param string $link            The permalink for the interaction.
    424488                 * @param int    $total_items     How many items being notified about.
    425489                 * @param int    $activity_id     ID of the activity item being formatted.
    426                  * @param int    $poster_user_id  ID of the user posting the mention.
     490                 * @param int    $user_id         ID of the user who inited the interaction.
    427491                 */
    428                 $return = apply_filters( 'bp_activity_' . $amount . '_at_mentions_notification', '<a href="' . esc_url( $at_mention_link ) . '" title="' . esc_attr( $at_mention_title ) . '">' . esc_html( $text ) . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
     492                $return = apply_filters( 'bp_activity_' . $amount . '_' . $action_filter . '_notification', '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>', $link, (int) $total_items, $activity_id, $user_id );
    429493        } else {
    430494
    431495                /**
    432                  * Filters the @mention notification for any non-string format.
     496                 * Filters the activity notification for any non-string format.
    433497                 *
    434498                 * This is a variable filter that is dependent on how many items need notified about.
    435499                 * The two possible hooks are bp_activity_single_at_mentions_notification
    436500                 * or bp_activity_multiple_at_mentions_notification.
    437501                 *
    438502                 * @since 1.5.0
     503                 * @since 2.5.0 use the $action_filter as a new dynamic portion of the filter name.
    439504                 *
    440                  * @param array  $array           Array holding the content and permalink for the mention notification.
    441                  * @param string $at_mention_link The permalink for the mention.
     505                 * @param array  $array           Array holding the content and permalink for the interaction notification.
     506                 * @param string $link            The permalink for the interaction.
    442507                 * @param int    $total_items     How many items being notified about.
    443508                 * @param int    $activity_id     ID of the activity item being formatted.
    444                  * @param int    $poster_user_id  ID of the user posting the mention.
     509                 * @param int    $user_id         ID of the user who inited the interaction.
    445510                 */
    446                 $return = apply_filters( 'bp_activity_' . $amount . '_at_mentions_notification', array(
     511                $return = apply_filters( 'bp_activity_' . $amount . '_' . $action_filter . '_notification', array(
    447512                        'text' => $text,
    448                         'link' => $at_mention_link
    449                 ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id );
     513                        'link' => $link
     514                ), $link, (int) $total_items, $activity_id, $user_id );
    450515        }
    451516
    452517        /**
    function bp_activity_format_notifications( $action, $item_id, $secondary_item_id 
    456521         *
    457522         * @param string $action            The type of activity item.
    458523         * @param int    $item_id           The activity ID.
    459          * @param int    $secondary_item_id @mention mentioner ID.
     524         * @param int    $secondary_item_id The user ID who inited the interaction.
    460525         * @param int    $total_items       Total amount of items to format.
    461526         */
    462527        do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    function bp_activity_at_mention_add_notification( $activity, $subject, $message, 
    496561add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
    497562
    498563/**
     564 * Notify a member one of their activity received a reply.
     565 *
     566 * @since 2.5.0
     567 *
     568 * @param BP_Activity_Activity $activity the original activity.
     569 * @param int                  $comment_id ID for the newly received comment.
     570 * @param int                  $commenter_id ID of the user who made the comment.
     571 * @param object               $reply Arguments used with the original activity comment.
     572 */
     573function bp_activity_update_reply_add_notification( $activity, $comment_id, $commenter_id ) {
     574        if ( bp_is_active( 'notifications' ) ) {
     575                bp_notifications_add_notification( array(
     576                        'user_id'           => $activity->user_id,
     577                        'item_id'           => $activity->id,
     578                        'secondary_item_id' => $commenter_id,
     579                        'component_name'    => buddypress()->activity->id,
     580                        'component_action'  => 'update_reply',
     581                        'date_notified'     => bp_core_current_time(),
     582                        'is_new'            => 1,
     583                ) );
     584        }
     585}
     586add_action( 'bp_activity_sent_reply_to_update_notification', 'bp_activity_update_reply_add_notification', 10, 3 );
     587
     588/**
     589 * Notify a member one of their activity comment received a reply.
     590 *
     591 * @since 2.5.0
     592 *
     593 * @param BP_Activity_Activity $activity_comment the parent activity.
     594 * @param int                  $comment_id ID for the newly received comment.
     595 * @param int                  $commenter_id ID of the user who made the comment.
     596 * @param object               $reply Arguments used with the original activity comment.
     597 */
     598function bp_activity_comment_reply_add_notification( $activity_comment, $comment_id, $commenter_id ) {
     599        if ( bp_is_active( 'notifications' ) ) {
     600                bp_notifications_add_notification( array(
     601                        'user_id'           => $activity_comment->user_id,
     602                        'item_id'           => $activity_comment->item_id,
     603                        'secondary_item_id' => $commenter_id,
     604                        'component_name'    => buddypress()->activity->id,
     605                        'component_action'  => 'comment_reply',
     606                        'date_notified'     => bp_core_current_time(),
     607                        'is_new'            => 1,
     608                ) );
     609        }
     610}
     611add_action( 'bp_activity_sent_reply_to_reply_notification', 'bp_activity_comment_reply_add_notification', 10, 3 );
     612
     613/**
    499614 * Mark at-mention notifications as read when users visit their Mentions page.
    500615 *
    501616 * @since 1.5.0
    add_action( 'bp_activity_screen_mentions', 'bp_activity_remove_screen_notificati 
    520635 * Mark at-mention notification as read when user visits the activity with the mention.
    521636 *
    522637 * @since 2.0.0
     638 * @since 2.5.0 Mark notifications for 'update_reply' and 'comment_reply' actions
    523639 *
    524640 * @param BP_Activity_Activity $activity Activity object.
    525641 */
    function bp_activity_remove_screen_notifications_single_activity_permalink( $act 
    532648                return;
    533649        }
    534650
    535         // Mark as read any notifications for the current user related to this activity item.
    536         bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $activity->id, buddypress()->activity->id, 'new_at_mention' );
     651        /**
     652         * Filter here to add the notification actions to mark as read
     653         * when the single activity is displayed.
     654         *
     655         * @since 2.5.0
     656         *
     657         * @param array $value List of notification actions to mark as read.
     658         */
     659        $notification_actions = apply_filters( 'bp_activity_notification_actions_single_activity', array(
     660                'new_at_mention',
     661                'update_reply',
     662                'comment_reply',
     663        ) );
     664
     665        $user_id   = bp_loggedin_user_id();
     666        $component = buddypress()->activity->id;
     667
     668        foreach ( $notification_actions as $action ) {
     669                // Mark as read any notifications for the current user related to this activity item.
     670                bp_notifications_mark_notifications_by_item_id( $user_id, $activity->id, $component, $action );
     671        }
    537672}
    538673add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications_single_activity_permalink' );
    539674
  • src/bp-notifications/bp-notifications-template.php

    diff --git src/bp-notifications/bp-notifications-template.php src/bp-notifications/bp-notifications-template.php
    index f03f4f8..98c4f3f 100644
    function bp_has_notifications( $args = '' ) { 
    512512                $user_id = bp_loggedin_user_id();
    513513        }
    514514
     515        // Set the component action (by default false to get all actions)
     516        $component_action = false;
     517
     518        if ( isset( $_REQUEST['type'] ) ) {
     519                $component_action = sanitize_key( $_REQUEST['type'] );
     520        }
     521
     522        // Set the search terms (by default an empty string to get all notifications)
     523        $search_terms = '';
     524
     525        if ( isset( $_REQUEST['s'] ) ) {
     526                $search_terms = stripslashes( $_REQUEST['s'] );
     527        }
     528
    515529        // Parse the args.
    516530        $r = bp_parse_args( $args, array(
    517531                'id'                => false,
    518532                'user_id'           => $user_id,
    519533                'secondary_item_id' => false,
    520534                'component_name'    => bp_notifications_get_registered_components(),
    521                 'component_action'  => false,
     535                'component_action'  => $component_action,
    522536                'is_new'            => $is_new,
    523                 'search_terms'      => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '',
     537                'search_terms'      => $search_terms,
    524538                'order_by'          => 'date_notified',
    525539                'sort_order'        => 'DESC',
    526540                'meta_query'        => false,
  • tests/phpunit/testcases/activity/notifications.php

    diff --git tests/phpunit/testcases/activity/notifications.php tests/phpunit/testcases/activity/notifications.php
    index 43d22f8..4ed0068 100644
    class BP_Tests_Activity_Notifications extends BP_UnitTestCase { 
    260260                        'content' => sprintf( 'Hello! @%s', $u2_mentionname ),
    261261                ) );
    262262        }
     263
     264        /**
     265         * @group bp_activity_new_comment_notification
     266         */
     267        public function test_bp_activity_new_comment_notification() {
     268                $this->activity_c_or_r_data = array();
     269
     270                $a = $this->factory->activity->create( array(
     271                        'user_id' => $this->u1,
     272                        'component' => buddypress()->activity->id,
     273                        'type' => 'activity_update',
     274                        'content' => 'Please comment this activity.',
     275                ) );
     276
     277                add_action( 'bp_activity_sent_reply_to_update_email', array( $this, 'new_activity_comment_get_data' ), 10, 2 );
     278                add_action( 'bp_activity_sent_reply_to_reply_email', array( $this, 'new_activity_comment_get_data' ), 10, 2 );
     279
     280                $c = bp_activity_new_comment( array(
     281                        'content'     => 'this is the comment',
     282                        'user_id'     => $this->u2,
     283                        'activity_id' => $a, // ID of the root activity item.
     284                        'parent_id'   => false  // ID of a parent comment (optional).
     285                ) );
     286
     287                $poster_name = bp_core_get_user_displayname( $this->u2 );
     288                $expected_c  = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
     289
     290                // Activity author should be notified
     291                $this->assertTrue( 1 === count( $this->activity_c_or_r_data ) );
     292                $this->assertEquals( $expected_c, $this->activity_c_or_r_data['bp_activity_sent_reply_to_update_email'][1] );
     293                $this->assertEquals( $this->u1, $this->activity_c_or_r_data['bp_activity_sent_reply_to_update_email'][0] );
     294
     295                // reset
     296                $this->activity_c_or_r_data = array();
     297
     298                $r = bp_activity_new_comment( array(
     299                        'content'     => 'this is the comment',
     300                        'user_id'     => $this->u1,
     301                        'activity_id' => $a, // ID of the root activity item.
     302                        'parent_id'   => $c  // ID of a parent comment (optional).
     303                ) );
     304
     305                $poster_name = bp_core_get_user_displayname( $this->u1 );
     306                $expected_r  = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
     307
     308                // Comment author should be notified
     309                $this->assertTrue( 1 === count( $this->activity_c_or_r_data ) );
     310                $this->assertEquals( $expected_r, $this->activity_c_or_r_data['bp_activity_sent_reply_to_reply_email'][1] );
     311                $this->assertEquals( $this->u2, $this->activity_c_or_r_data['bp_activity_sent_reply_to_reply_email'][0] );
     312
     313                $u3 = $this->factory->user->create();
     314
     315                // reset
     316                $this->activity_c_or_r_data = array();
     317
     318                $r3 = bp_activity_new_comment( array(
     319                        'content'     => 'this is a reply to a comment',
     320                        'user_id'     => $u3,
     321                        'activity_id' => $a, // ID of the root activity item.
     322                        'parent_id'   => $c  // ID of a parent comment (optional).
     323                ) );
     324
     325                $poster_name = bp_core_get_user_displayname( $u3 );
     326                $expected_c3 = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
     327                $expected_r3 = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
     328
     329                // Activity & Comment authors should be notified
     330                $this->assertTrue( 2 === count( $this->activity_c_or_r_data ) );
     331                $this->assertEquals( $expected_c3, $this->activity_c_or_r_data['bp_activity_sent_reply_to_update_email'][1] );
     332                $this->assertEquals( $this->u1, $this->activity_c_or_r_data['bp_activity_sent_reply_to_update_email'][0] );
     333                $this->assertEquals( $expected_r3, $this->activity_c_or_r_data['bp_activity_sent_reply_to_reply_email'][1] );
     334                $this->assertEquals( $this->u2, $this->activity_c_or_r_data['bp_activity_sent_reply_to_reply_email'][0] );
     335
     336                remove_action( 'bp_activity_sent_reply_to_reply_email', array( $this, 'new_activity_comment_get_data' ), 10, 2 );
     337                remove_action( 'bp_activity_sent_reply_to_update_email', array( $this, 'new_activity_comment_get_data' ), 10, 2 );
     338        }
     339
     340        public function new_activity_comment_get_data() {
     341                $this->activity_c_or_r_data[ current_filter() ] = func_get_args();
     342        }
     343
     344        /**
     345         * @group bp_activity_format_notifications
     346         */
     347        public function test_bp_activity_format_notifications_new_at_mention() {
     348                $this->test_format_filter = array();
     349
     350                // Current user is $this->u1, so $this->u2 posted the mention
     351                $a = $this->factory->activity->create( array(
     352                        'user_id' => $this->u2,
     353                        'component' => buddypress()->activity->id,
     354                        'type' => 'activity_update',
     355                        'content' => 'fake new_at_mention activity',
     356                ) );
     357
     358                add_filter( 'bp_activity_single_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
     359                add_filter( 'bp_activity_multiple_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
     360
     361                $format_tests = array(
     362                        'array_single'    => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 1, 'array' ),
     363                        'string_single'   => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 1 ),
     364                        'array_multiple'  => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 2, 'array' ),
     365                        'string_multiple' => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 2 ),
     366                );
     367
     368                remove_filter( 'bp_activity_single_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
     369                remove_filter( 'bp_activity_multiple_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
     370
     371                $single = sprintf( __( '%1$s mentioned you', 'buddypress' ), bp_core_get_user_displayname( $this->u2 ) );
     372                $multiple = 'You have 2 new mentions';
     373
     374                $this->assertContains( $single, $format_tests['string_single'] );
     375                $this->assertContains( $single, $format_tests['array_single']['text'] );
     376                $this->assertContains( $multiple, $format_tests['string_multiple'] );
     377                $this->assertContains( $multiple, $format_tests['array_multiple']['text'] );
     378
     379                // Check filters
     380                $this->assertTrue( 4 === count( $this->test_format_filter ) );
     381        }
     382
     383        public function format_notification_filter( $return ) {
     384                $this->test_format_filter[] = current_filter();
     385                return $return;
     386        }
     387
     388        /**
     389         * @group bp_activity_update_reply_add_notification
     390         * @group bp_activity_comment_reply_add_notification
     391         */
     392        public function test_bp_activity_comment_add_notification() {
     393                $a = $this->factory->activity->create( array(
     394                        'user_id' => $this->u1,
     395                        'component' => buddypress()->activity->id,
     396                        'type' => 'activity_update',
     397                        'content' => 'Please comment this activity.',
     398                ) );
     399
     400                $c = bp_activity_new_comment( array(
     401                        'content'     => 'this is the comment',
     402                        'user_id'     => $this->u2,
     403                        'activity_id' => $a, // ID of the root activity item.
     404                        'parent_id'   => false  // ID of a parent comment (optional).
     405                ) );
     406
     407                $u3 = $this->factory->user->create();
     408
     409                $r3 = bp_activity_new_comment( array(
     410                        'content'     => 'this is a reply to a comment',
     411                        'user_id'     => $u3,
     412                        'activity_id' => $a, // ID of the root activity item.
     413                        'parent_id'   => $c  // ID of a parent comment (optional).
     414                ) );
     415
     416                $u1_notifications = BP_Notifications_Notification::get( array(
     417                        'user_id' => $this->u1,
     418                ) );
     419
     420                $expected_commenters = array( $this->u2, $u3 );
     421                $this->assertEquals( $expected_commenters, wp_list_pluck( $u1_notifications, 'secondary_item_id' ) );
     422
     423                $u2_notifications = BP_Notifications_Notification::get( array(
     424                        'user_id' => $this->u2,
     425                ) );
     426
     427                $expected_commenter = array( $u3 );
     428                $this->assertEquals( $expected_commenter, wp_list_pluck( $u2_notifications, 'secondary_item_id' ) );
     429        }
    263430}