Skip to:
Content

BuddyPress.org

Ticket #6712: 6712.02.notifications-conditional-loading.patch

File 6712.02.notifications-conditional-loading.patch, 39.8 KB (added by r-a-y, 4 years ago)
  • src/bp-activity/bp-activity-actions.php

     
    746746}
    747747
    748748/**
     749 * Helper method to map action arguments to function parameters.
     750 *
     751 * @since 1.9.0
     752 *
     753 * @param int   $comment_id ID of the comment being notified about.
     754 * @param array $params     Parameters to use with notification.
     755 */
     756function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
     757        bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
     758}
     759add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
     760
     761/**
    749762 * AJAX endpoint for Suggestions API lookups.
    750763 *
    751764 * @since 2.1.0
  • src/bp-activity/bp-activity-functions.php

     
    34763476        do_action( 'bp_activity_mark_as_ham', $activity, $source );
    34773477}
    34783478
     3479/* Emails *********************************************************************/
     3480
     3481/**
     3482 * Send email and BP notifications when a user is mentioned in an update.
     3483 *
     3484 * @since 1.2.0
     3485 *
     3486 * @uses bp_notifications_add_notification()
     3487 * @uses bp_get_user_meta()
     3488 * @uses bp_core_get_user_displayname()
     3489 * @uses bp_activity_get_permalink()
     3490 * @uses bp_core_get_user_domain()
     3491 * @uses bp_get_settings_slug()
     3492 * @uses bp_activity_filter_kses()
     3493 * @uses bp_core_get_core_userdata()
     3494 * @uses wp_specialchars_decode()
     3495 * @uses get_blog_option()
     3496 * @uses bp_is_active()
     3497 * @uses bp_is_group()
     3498 * @uses bp_get_current_group_name()
     3499 * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook.
     3500 * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook.
     3501 * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook.
     3502 * @uses wp_mail()
     3503 * @uses do_action() To call the 'bp_activity_sent_mention_email' hook.
     3504 *
     3505 * @param int $activity_id      The ID of the activity update.
     3506 * @param int $receiver_user_id The ID of the user who is receiving the update.
     3507 */
     3508function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
     3509        $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
     3510
     3511        // Don't leave multiple notifications for the same activity item.
     3512        foreach( $notifications as $notification ) {
     3513                if ( $activity_id == $notification->item_id ) {
     3514                        return;
     3515                }
     3516        }
     3517
     3518        $activity     = new BP_Activity_Activity( $activity_id );
     3519        $email_type   = 'activity-at-message';
     3520        $group_name   = '';
     3521        $message_link = bp_activity_get_permalink( $activity_id );
     3522        $poster_name  = bp_core_get_user_displayname( $activity->user_id );
     3523
     3524        remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3525        remove_filter( 'bp_get_activity_content_body', 'wpautop' );
     3526        remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3527
     3528        /** This filter is documented in bp-activity/bp-activity-template.php */
     3529        $content = apply_filters( 'bp_get_activity_content_body', $activity->content );
     3530
     3531        add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3532        add_filter( 'bp_get_activity_content_body', 'wpautop' );
     3533        add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3534
     3535        // Now email the user with the contents of the message (if they have enabled email notifications).
     3536        if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
     3537                if ( bp_is_active( 'groups' ) && bp_is_group() ) {
     3538                        $email_type = 'groups-at-message';
     3539                        $group_name = bp_get_current_group_name();
     3540                }
     3541
     3542                $args = array(
     3543                        'tokens' => array(
     3544                                'activity'         => $activity,
     3545                                'usermessage'      => wp_strip_all_tags( $content ),
     3546                                'group.name'       => $group_name,
     3547                                'mentioned.url'    => $message_link,
     3548                                'poster.name'      => $poster_name,
     3549                                'receiver-user.id' => $receiver_user_id,
     3550                        ),
     3551                );
     3552
     3553                bp_send_email( $email_type, $receiver_user_id, $args );
     3554        }
     3555
     3556        /**
     3557         * Fires after the sending of an @mention email notification.
     3558         *
     3559         * @since 1.5.0
     3560         * @since 2.5.0 $subject, $message, $content arguments unset and deprecated.
     3561         *
     3562         * @param BP_Activity_Activity $activity         Activity Item object.
     3563         * @param string               $deprecated       Removed in 2.5; now an empty string.
     3564         * @param string               $deprecated       Removed in 2.5; now an empty string.
     3565         * @param string               $deprecated       Removed in 2.5; now an empty string.
     3566         * @param int                  $receiver_user_id The ID of the user who is receiving the update.
     3567         */
     3568        do_action( 'bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id );
     3569}
     3570
     3571/**
     3572 * Send email and BP notifications when an activity item receives a comment.
     3573 *
     3574 * @since 1.2.0
     3575 * @since 2.5.0 Updated to use new email APIs.
     3576 *
     3577 * @uses bp_get_user_meta()
     3578 * @uses bp_core_get_user_displayname()
     3579 * @uses bp_activity_get_permalink()
     3580 * @uses bp_core_get_user_domain()
     3581 * @uses bp_get_settings_slug()
     3582 * @uses bp_activity_filter_kses()
     3583 * @uses bp_core_get_core_userdata()
     3584 * @uses wp_specialchars_decode()
     3585 * @uses get_blog_option()
     3586 * @uses bp_get_root_blog_id()
     3587 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_to' hook.
     3588 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_subject' hook.
     3589 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_message' hook.
     3590 * @uses wp_mail()
     3591 * @uses do_action() To call the 'bp_activity_sent_reply_to_update_email' hook.
     3592 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_to' hook.
     3593 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_subject' hook.
     3594 * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_message' hook.
     3595 * @uses do_action() To call the 'bp_activity_sent_reply_to_reply_email' hook.
     3596 *
     3597 * @param int   $comment_id   The comment id.
     3598 * @param int   $commenter_id The ID of the user who posted the comment.
     3599 * @param array $params       {@link bp_activity_new_comment()}.
     3600 */
     3601function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
     3602        $original_activity = new BP_Activity_Activity( $params['activity_id'] );
     3603        $poster_name       = bp_core_get_user_displayname( $commenter_id );
     3604        $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
     3605
     3606        remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3607        remove_filter( 'bp_get_activity_content_body', 'wpautop' );
     3608        remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3609
     3610        /** This filter is documented in bp-activity/bp-activity-template.php */
     3611        $content = apply_filters( 'bp_get_activity_content_body', $params['content'] );
     3612
     3613        add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3614        add_filter( 'bp_get_activity_content_body', 'wpautop' );
     3615        add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3616
     3617        if ( $original_activity->user_id != $commenter_id && 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
     3618                $args = array(
     3619                        'tokens' => array(
     3620                                'comment.id'                => $comment_id,
     3621                                'commenter.id'              => $commenter_id,
     3622                                'usermessage'               => wp_strip_all_tags( $content ),
     3623                                'original_activity.user_id' => $original_activity->user_id,
     3624                                'poster.name'               => $poster_name,
     3625                                'thread.url'                => esc_url( $thread_link ),
     3626                        ),
     3627                );
     3628
     3629                bp_send_email( 'activity-comment', $original_activity->user_id, $args );
     3630        }
     3631
     3632
     3633        /*
     3634         * If this is a reply to another comment, send an email notification to the
     3635         * author of the immediate parent comment.
     3636         */
     3637        if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
     3638                return;
     3639        }
     3640
     3641        $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
     3642
     3643        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 ) ) {
     3644                $args = array(
     3645                        'tokens' => array(
     3646                                'comment.id'             => $comment_id,
     3647                                'commenter.id'           => $commenter_id,
     3648                                'usermessage'            => wp_strip_all_tags( $content ),
     3649                                'parent-comment-user.id' => $parent_comment->user_id,
     3650                                'poster.name'            => $poster_name,
     3651                                'thread.url'             => esc_url( $thread_link ),
     3652                        ),
     3653                );
     3654
     3655                bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
     3656        }
     3657}
    34793658
    34803659/** Embeds *******************************************************************/
    34813660
  • src/bp-activity/bp-activity-notifications.php

     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    1212
    13 /* Emails *********************************************************************/
    14 
    15 /**
    16  * Send email and BP notifications when a user is mentioned in an update.
    17  *
    18  * @since 1.2.0
    19  *
    20  * @uses bp_notifications_add_notification()
    21  * @uses bp_get_user_meta()
    22  * @uses bp_core_get_user_displayname()
    23  * @uses bp_activity_get_permalink()
    24  * @uses bp_core_get_user_domain()
    25  * @uses bp_get_settings_slug()
    26  * @uses bp_activity_filter_kses()
    27  * @uses bp_core_get_core_userdata()
    28  * @uses wp_specialchars_decode()
    29  * @uses get_blog_option()
    30  * @uses bp_is_active()
    31  * @uses bp_is_group()
    32  * @uses bp_get_current_group_name()
    33  * @uses apply_filters() To call the 'bp_activity_at_message_notification_to' hook.
    34  * @uses apply_filters() To call the 'bp_activity_at_message_notification_subject' hook.
    35  * @uses apply_filters() To call the 'bp_activity_at_message_notification_message' hook.
    36  * @uses wp_mail()
    37  * @uses do_action() To call the 'bp_activity_sent_mention_email' hook.
    38  *
    39  * @param int $activity_id      The ID of the activity update.
    40  * @param int $receiver_user_id The ID of the user who is receiving the update.
    41  */
    42 function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
    43         $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
    44 
    45         // Don't leave multiple notifications for the same activity item.
    46         foreach( $notifications as $notification ) {
    47                 if ( $activity_id == $notification->item_id ) {
    48                         return;
    49                 }
    50         }
    51 
    52         $activity     = new BP_Activity_Activity( $activity_id );
    53         $email_type   = 'activity-at-message';
    54         $group_name   = '';
    55         $message_link = bp_activity_get_permalink( $activity_id );
    56         $poster_name  = bp_core_get_user_displayname( $activity->user_id );
    57 
    58         remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    59         remove_filter( 'bp_get_activity_content_body', 'wpautop' );
    60         remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    61 
    62         /** This filter is documented in bp-activity/bp-activity-template.php */
    63         $content = apply_filters( 'bp_get_activity_content_body', $activity->content );
    64 
    65         add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    66         add_filter( 'bp_get_activity_content_body', 'wpautop' );
    67         add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    68 
    69         // Now email the user with the contents of the message (if they have enabled email notifications).
    70         if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
    71                 if ( bp_is_active( 'groups' ) && bp_is_group() ) {
    72                         $email_type = 'groups-at-message';
    73                         $group_name = bp_get_current_group_name();
    74                 }
    75 
    76                 $args = array(
    77                         'tokens' => array(
    78                                 'activity'         => $activity,
    79                                 'usermessage'      => wp_strip_all_tags( $content ),
    80                                 'group.name'       => $group_name,
    81                                 'mentioned.url'    => $message_link,
    82                                 'poster.name'      => $poster_name,
    83                                 'receiver-user.id' => $receiver_user_id,
    84                         ),
    85                 );
    86 
    87                 bp_send_email( $email_type, $receiver_user_id, $args );
    88         }
    89 
    90         /**
    91          * Fires after the sending of an @mention email notification.
    92          *
    93          * @since 1.5.0
    94          * @since 2.5.0 $subject, $message, $content arguments unset and deprecated.
    95          *
    96          * @param BP_Activity_Activity $activity         Activity Item object.
    97          * @param string               $deprecated       Removed in 2.5; now an empty string.
    98          * @param string               $deprecated       Removed in 2.5; now an empty string.
    99          * @param string               $deprecated       Removed in 2.5; now an empty string.
    100          * @param int                  $receiver_user_id The ID of the user who is receiving the update.
    101          */
    102         do_action( 'bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id );
    103 }
    104 
    105 /**
    106  * Send email and BP notifications when an activity item receives a comment.
    107  *
    108  * @since 1.2.0
    109  * @since 2.5.0 Updated to use new email APIs.
    110  *
    111  * @uses bp_get_user_meta()
    112  * @uses bp_core_get_user_displayname()
    113  * @uses bp_activity_get_permalink()
    114  * @uses bp_core_get_user_domain()
    115  * @uses bp_get_settings_slug()
    116  * @uses bp_activity_filter_kses()
    117  * @uses bp_core_get_core_userdata()
    118  * @uses wp_specialchars_decode()
    119  * @uses get_blog_option()
    120  * @uses bp_get_root_blog_id()
    121  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_to' hook.
    122  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_subject' hook.
    123  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_message' hook.
    124  * @uses wp_mail()
    125  * @uses do_action() To call the 'bp_activity_sent_reply_to_update_email' hook.
    126  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_to' hook.
    127  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_subject' hook.
    128  * @uses apply_filters() To call the 'bp_activity_new_comment_notification_comment_author_message' hook.
    129  * @uses do_action() To call the 'bp_activity_sent_reply_to_reply_email' hook.
    130  *
    131  * @param int   $comment_id   The comment id.
    132  * @param int   $commenter_id The ID of the user who posted the comment.
    133  * @param array $params       {@link bp_activity_new_comment()}.
    134  */
    135 function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
    136         $original_activity = new BP_Activity_Activity( $params['activity_id'] );
    137         $poster_name       = bp_core_get_user_displayname( $commenter_id );
    138         $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
    139 
    140         remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    141         remove_filter( 'bp_get_activity_content_body', 'wpautop' );
    142         remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    143 
    144         /** This filter is documented in bp-activity/bp-activity-template.php */
    145         $content = apply_filters( 'bp_get_activity_content_body', $params['content'] );
    146 
    147         add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    148         add_filter( 'bp_get_activity_content_body', 'wpautop' );
    149         add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    150 
    151         if ( $original_activity->user_id != $commenter_id && 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
    152                 $args = array(
    153                         'tokens' => array(
    154                                 'comment.id'                => $comment_id,
    155                                 'commenter.id'              => $commenter_id,
    156                                 'usermessage'               => wp_strip_all_tags( $content ),
    157                                 'original_activity.user_id' => $original_activity->user_id,
    158                                 'poster.name'               => $poster_name,
    159                                 'thread.url'                => esc_url( $thread_link ),
    160                         ),
    161                 );
    162 
    163                 bp_send_email( 'activity-comment', $original_activity->user_id, $args );
    164         }
    165 
    166 
    167         /*
    168          * If this is a reply to another comment, send an email notification to the
    169          * author of the immediate parent comment.
    170          */
    171         if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
    172                 return;
    173         }
    174 
    175         $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
    176 
    177         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 ) ) {
    178                 $args = array(
    179                         'tokens' => array(
    180                                 'comment.id'             => $comment_id,
    181                                 'commenter.id'           => $commenter_id,
    182                                 'usermessage'            => wp_strip_all_tags( $content ),
    183                                 'parent-comment-user.id' => $parent_comment->user_id,
    184                                 'poster.name'            => $poster_name,
    185                                 'thread.url'             => esc_url( $thread_link ),
    186                         ),
    187                 );
    188 
    189                 bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
    190         }
    191 }
    192 
    193 /**
    194  * Helper method to map action arguments to function parameters.
    195  *
    196  * @since 1.9.0
    197  *
    198  * @param int   $comment_id ID of the comment being notified about.
    199  * @param array $params     Parameters to use with notification.
    200  */
    201 function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
    202         bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
    203 }
    204 add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
    205 
    206 /** Notifications *************************************************************/
    207 
    20813/**
    20914 * Format notifications related to activity.
    21015 *
     
    317122 * @param int    $receiver_user_id   ID of user receiving notification.
    318123 */
    319124function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
    320         if ( bp_is_active( 'notifications' ) ) {
    321                 bp_notifications_add_notification( array(
    322                         'user_id'           => $receiver_user_id,
    323                         'item_id'           => $activity->id,
    324                         'secondary_item_id' => $activity->user_id,
    325                         'component_name'    => buddypress()->activity->id,
    326                         'component_action'  => 'new_at_mention',
    327                         'date_notified'     => bp_core_current_time(),
    328                         'is_new'            => 1,
    329                 ) );
    330         }
     125        bp_notifications_add_notification( array(
     126                'user_id'           => $receiver_user_id,
     127                'item_id'           => $activity->id,
     128                'secondary_item_id' => $activity->user_id,
     129                'component_name'    => buddypress()->activity->id,
     130                'component_action'  => 'new_at_mention',
     131                'date_notified'     => bp_core_current_time(),
     132                'is_new'            => 1,
     133        ) );
    331134}
    332135add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
    333136
     
    341144 * @uses bp_notifications_mark_all_notifications_by_type()
    342145 */
    343146function bp_activity_remove_screen_notifications( $user_id = 0 ) {
    344         if ( ! bp_is_active( 'notifications' ) ) {
    345                 return;
    346         }
    347 
    348147        // Only mark read if the current user is looking at his own mentions.
    349148        if ( empty( $user_id ) || (int) $user_id !== (int) bp_loggedin_user_id() ) {
    350149                return;
     
    362161 * @param BP_Activity_Activity $activity Activity object.
    363162 */
    364163function bp_activity_remove_screen_notifications_single_activity_permalink( $activity ) {
    365         if ( ! bp_is_active( 'notifications' ) ) {
    366                 return;
    367         }
    368 
    369164        if ( ! is_user_logged_in() ) {
    370165                return;
    371166        }
     
    385180function bp_activity_at_mention_delete_notification( $activity_ids_deleted = array() ) {
    386181        // Let's delete all without checking if content contains any mentions
    387182        // to avoid a query to get the activity.
    388         if ( bp_is_active( 'notifications' ) && ! empty( $activity_ids_deleted ) ) {
     183        if ( ! empty( $activity_ids_deleted ) ) {
    389184                foreach ( $activity_ids_deleted as $activity_id ) {
    390185                        bp_notifications_delete_all_notifications_by_type( $activity_id, buddypress()->activity->id );
    391186                }
  • src/bp-activity/classes/class-bp-activity-component.php

     
    5555                        'filters',
    5656                        'template',
    5757                        'functions',
    58                         'notifications',
    5958                        'cache'
    6059                );
    6160
     61                // Notifications support.
     62                if ( bp_is_active( 'notifications' ) ) {
     63                        $includes[] = 'notifications';
     64                }
     65
    6266                if ( ! buddypress()->do_autoload ) {
    6367                        $includes[] = 'classes';
    6468                }
  • src/bp-friends/bp-friends-functions.php

     
    795795        ) );
    796796}
    797797add_action( 'bp_activity_mentions_prime_results', 'bp_friends_prime_mentions_results' );
     798
     799/** Emails ********************************************************************/
     800
     801/**
     802 * Send notifications related to a new friendship request.
     803 *
     804 * When a friendship is requested, an email and a BP notification are sent to
     805 * the user of whom friendship has been requested ($friend_id).
     806 *
     807 * @since 1.0.0
     808 *
     809 * @param int $friendship_id ID of the friendship object.
     810 * @param int $initiator_id  ID of the user who initiated the request.
     811 * @param int $friend_id     ID of the request recipient.
     812 */
     813function friends_notification_new_request( $friendship_id, $initiator_id, $friend_id ) {
     814        if ( 'no' == bp_get_user_meta( (int) $friend_id, 'notification_friends_friendship_request', true ) ) {
     815                return;
     816        }
     817
     818        $args = array(
     819                'tokens' => array(
     820                        'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
     821                        'friend.id'           => $friend_id,
     822                        'friendship.id'       => $friendship_id,
     823                        'initiator.id'        => $initiator_id,
     824                        'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
     825                        'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
     826                ),
     827        );
     828        bp_send_email( 'friends-request', $friend_id, $args );
     829}
     830add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
     831
     832/**
     833 * Send notifications related to the acceptance of a friendship request.
     834 *
     835 * When a friendship request is accepted, an email and a BP notification are
     836 * sent to the user who requested the friendship ($initiator_id).
     837 *
     838 * @since 1.0.0
     839 *
     840 * @param int $friendship_id ID of the friendship object.
     841 * @param int $initiator_id  ID of the user who initiated the request.
     842 * @param int $friend_id     ID of the request recipient.
     843 */
     844function friends_notification_accepted_request( $friendship_id, $initiator_id, $friend_id ) {
     845        if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) ) {
     846                return;
     847        }
     848
     849        $args = array(
     850                'tokens' => array(
     851                        'friend.id'      => $friend_id,
     852                        'friendship.url' => esc_url( bp_core_get_user_domain( $friend_id ) ),
     853                        'friend.name'    => bp_core_get_user_displayname( $friend_id ),
     854                        'friendship.id'  => $friendship_id,
     855                        'initiator.id'   => $initiator_id,
     856                ),
     857        );
     858        bp_send_email( 'friends-request-accepted', $initiator_id, $args );
     859}
     860add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
     861 No newline at end of file
  • src/bp-friends/bp-friends-notifications.php

     
    1313// Exit if accessed directly.
    1414defined( 'ABSPATH' ) || exit;
    1515
    16 /** Emails ********************************************************************/
    17 
    18 /**
    19  * Send notifications related to a new friendship request.
    20  *
    21  * When a friendship is requested, an email and a BP notification are sent to
    22  * the user of whom friendship has been requested ($friend_id).
    23  *
    24  * @since 1.0.0
    25  *
    26  * @param int $friendship_id ID of the friendship object.
    27  * @param int $initiator_id  ID of the user who initiated the request.
    28  * @param int $friend_id     ID of the request recipient.
    29  */
    30 function friends_notification_new_request( $friendship_id, $initiator_id, $friend_id ) {
    31         if ( 'no' == bp_get_user_meta( (int) $friend_id, 'notification_friends_friendship_request', true ) ) {
    32                 return;
    33         }
    34 
    35         $args = array(
    36                 'tokens' => array(
    37                         'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
    38                         'friend.id'           => $friend_id,
    39                         'friendship.id'       => $friendship_id,
    40                         'initiator.id'        => $initiator_id,
    41                         'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
    42                         'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
    43                 ),
    44         );
    45         bp_send_email( 'friends-request', $friend_id, $args );
    46 }
    47 add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
    48 
    49 /**
    50  * Send notifications related to the acceptance of a friendship request.
    51  *
    52  * When a friendship request is accepted, an email and a BP notification are
    53  * sent to the user who requested the friendship ($initiator_id).
    54  *
    55  * @since 1.0.0
    56  *
    57  * @param int $friendship_id ID of the friendship object.
    58  * @param int $initiator_id  ID of the user who initiated the request.
    59  * @param int $friend_id     ID of the request recipient.
    60  */
    61 function friends_notification_accepted_request( $friendship_id, $initiator_id, $friend_id ) {
    62         if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) ) {
    63                 return;
    64         }
    65 
    66         $args = array(
    67                 'tokens' => array(
    68                         'friend.id'      => $friend_id,
    69                         'friendship.url' => esc_url( bp_core_get_user_domain( $friend_id ) ),
    70                         'friend.name'    => bp_core_get_user_displayname( $friend_id ),
    71                         'friendship.id'  => $friendship_id,
    72                         'initiator.id'   => $initiator_id,
    73                 ),
    74         );
    75         bp_send_email( 'friends-request-accepted', $initiator_id, $args );
    76 }
    77 add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
    78 
    79 /** Notifications *************************************************************/
    80 
    8116/**
    8217 * Notification formatting callback for bp-friends notifications.
    8318 *
     
    182117 * @since 1.2.0
    183118 */
    184119function friends_clear_friend_notifications() {
    185         if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     120        if ( isset( $_GET['new'] ) ) {
    186121                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    187122        }
    188123}
     
    194129 * @since 1.9.0
    195130 */
    196131function bp_friends_mark_friendship_request_notifications_by_type() {
    197         if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     132        if ( isset( $_GET['new'] ) ) {
    198133                bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
    199134        }
    200135}
     
    206141 * @since 1.9.0
    207142 */
    208143function bp_friends_mark_friendship_accepted_notifications_by_type() {
    209         if ( bp_is_active( 'notifications' ) ) {
    210                 bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    211         }
     144        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    212145}
    213146add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
    214147
     
    222155 * @param int $friend_user_id    The friendship request receiver user ID.
    223156 */
    224157function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
    225         if ( bp_is_active( 'notifications' ) ) {
    226                 bp_notifications_add_notification( array(
    227                         'user_id'           => $friend_user_id,
    228                         'item_id'           => $initiator_user_id,
    229                         'secondary_item_id' => $friendship_id,
    230                         'component_name'    => buddypress()->friends->id,
    231                         'component_action'  => 'friendship_request',
    232                         'date_notified'     => bp_core_current_time(),
    233                         'is_new'            => 1,
    234                 ) );
    235         }
     158        bp_notifications_add_notification( array(
     159                'user_id'           => $friend_user_id,
     160                'item_id'           => $initiator_user_id,
     161                'secondary_item_id' => $friendship_id,
     162                'component_name'    => buddypress()->friends->id,
     163                'component_action'  => 'friendship_request',
     164                'date_notified'     => bp_core_current_time(),
     165                'is_new'            => 1,
     166        ) );
    236167}
    237168add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 );
    238169
     
    245176 * @param object $friendship    Friendship object.
    246177 */
    247178function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
    248         if ( bp_is_active( 'notifications' ) ) {
    249                 bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    250         }
     179        bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    251180}
    252181add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
    253182
     
    261190 * @param int $friend_user_id    The friendship request receiver user ID.
    262191 */
    263192function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
    264 
    265         // Bail if notifications is not active.
    266         if ( ! bp_is_active( 'notifications' ) ) {
    267                 return;
    268         }
    269 
    270193        // Remove the friend request notice.
    271194        bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    272195
     
    292215 * @param object $friendship    Friendship Object.
    293216 */
    294217function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
    295         if ( bp_is_active( 'notifications' ) ) {
    296                 bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    297         }
     218        bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    298219}
    299220add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
    300221
     
    306227 * @param int $user_id ID of the user whose notifications are removed.
    307228 */
    308229function bp_friends_remove_notifications_data( $user_id = 0 ) {
    309         if ( bp_is_active( 'notifications' ) ) {
    310                 bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
    311         }
     230        bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
    312231}
    313232add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
  • src/bp-friends/classes/class-bp-friends-component.php

     
    5353                        'activity',
    5454                        'template',
    5555                        'functions',
    56                         'notifications',
    5756                        'widgets',
    5857                );
    5958
     59                // Conditional includes.
     60                if ( bp_is_active( 'notifications' ) ) {
     61                        $includes[] = 'notifications';
     62                }
     63
    6064                if ( ! buddypress()->do_autoload ) {
    6165                        $includes[] = 'classes';
    6266                }
  • src/bp-messages/bp-messages-functions.php

     
    524524
    525525        return $retval;
    526526}
     527
     528/** Email *********************************************************************/
     529
     530/**
     531 * Email message recipients to alert them of a new unread private message.
     532 *
     533 * @since 1.0.0
     534 *
     535 * @param array|BP_Messages_Message $raw_args {
     536 *     Array of arguments. Also accepts a BP_Messages_Message object.
     537 *     @type array  $recipients    User IDs of recipients.
     538 *     @type string $email_subject Subject line of message.
     539 *     @type string $email_content Content of message.
     540 *     @type int    $sender_id     User ID of sender.
     541 * }
     542 */
     543function messages_notification_new_message( $raw_args = array() ) {
     544        if ( is_object( $raw_args ) ) {
     545                $args = (array) $raw_args;
     546        } else {
     547                $args = $raw_args;
     548        }
     549
     550        // These should be extracted below.
     551        $recipients    = array();
     552        $email_subject = $email_content = '';
     553        $sender_id     = 0;
     554
     555        // Barf.
     556        extract( $args );
     557
     558        if ( empty( $recipients ) ) {
     559                return;
     560        }
     561
     562        $sender_name = bp_core_get_user_displayname( $sender_id );
     563
     564        // Send an email to each recipient.
     565        foreach ( $recipients as $recipient ) {
     566                if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
     567                        continue;
     568                }
     569
     570                // User data and links.
     571                $ud = get_userdata( $recipient->user_id );
     572                if ( empty( $ud ) ) {
     573                        continue;
     574                }
     575
     576                $args = array(
     577                        'tokens' => array(
     578                                'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
     579                                'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
     580                                'sender.name' => $sender_name,
     581                                'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
     582                        ),
     583                );
     584                bp_send_email( 'messages-unread', $ud, $args );
     585        }
     586
     587        /**
     588         * Fires after the sending of a new message email notification.
     589         *
     590         * @since 1.5.0
     591         * @deprecated 2.5.0 Use the filters in BP_Email.
     592         *                   $email_subject and $email_content arguments unset and deprecated.
     593         *
     594         * @param array  $recipients    User IDs of recipients.
     595         * @param string $email_subject Deprecated in 2.5; now an empty string.
     596         * @param string $email_content Deprecated in 2.5; now an empty string.
     597         * @param array  $args          Array of originally provided arguments.
     598         */
     599        do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
     600}
     601add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
     602 No newline at end of file
  • src/bp-messages/bp-messages-notifications.php

     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    1212
    13 /** Email *********************************************************************/
    14 
    15 /**
    16  * Email message recipients to alert them of a new unread private message.
    17  *
    18  * @since 1.0.0
    19  *
    20  * @param array|BP_Messages_Message $raw_args {
    21  *     Array of arguments. Also accepts a BP_Messages_Message object.
    22  *     @type array  $recipients    User IDs of recipients.
    23  *     @type string $email_subject Subject line of message.
    24  *     @type string $email_content Content of message.
    25  *     @type int    $sender_id     User ID of sender.
    26  * }
    27  */
    28 function messages_notification_new_message( $raw_args = array() ) {
    29         if ( is_object( $raw_args ) ) {
    30                 $args = (array) $raw_args;
    31         } else {
    32                 $args = $raw_args;
    33         }
    34 
    35         // These should be extracted below.
    36         $recipients    = array();
    37         $email_subject = $email_content = '';
    38         $sender_id     = 0;
    39 
    40         // Barf.
    41         extract( $args );
    42 
    43         if ( empty( $recipients ) ) {
    44                 return;
    45         }
    46 
    47         $sender_name = bp_core_get_user_displayname( $sender_id );
    48 
    49         // Send an email to each recipient.
    50         foreach ( $recipients as $recipient ) {
    51                 if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
    52                         continue;
    53                 }
    54 
    55                 // User data and links.
    56                 $ud = get_userdata( $recipient->user_id );
    57                 if ( empty( $ud ) ) {
    58                         continue;
    59                 }
    60 
    61                 $args = array(
    62                         'tokens' => array(
    63                                 'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
    64                                 'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
    65                                 'sender.name' => $sender_name,
    66                                 'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
    67                         ),
    68                 );
    69                 bp_send_email( 'messages-unread', $ud, $args );
    70         }
    71 
    72         /**
    73          * Fires after the sending of a new message email notification.
    74          *
    75          * @since 1.5.0
    76          * @deprecated 2.5.0 Use the filters in BP_Email.
    77          *                   $email_subject and $email_content arguments unset and deprecated.
    78          *
    79          * @param array  $recipients    User IDs of recipients.
    80          * @param string $email_subject Deprecated in 2.5; now an empty string.
    81          * @param string $email_content Deprecated in 2.5; now an empty string.
    82          * @param array  $args          Array of originally provided arguments.
    83          */
    84         do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
    85 }
    86 add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
    87 
    88 /** Notifications *************************************************************/
    89 
    9013/**
    9114 * Format notifications for the Messages component.
    9215 *
     
    224147 * @param BP_Messages_Message $message Message object.
    225148 */
    226149function bp_messages_message_sent_add_notification( $message ) {
    227         if ( bp_is_active( 'notifications' ) && ! empty( $message->recipients ) ) {
     150        if ( ! empty( $message->recipients ) ) {
    228151                foreach ( (array) $message->recipients as $recipient ) {
    229152                        bp_notifications_add_notification( array(
    230153                                'user_id'           => $recipient->user_id,
     
    246169 * @since 1.9.0
    247170 */
    248171function bp_messages_screen_conversation_mark_notifications() {
    249         if ( bp_is_active( 'notifications' ) ) {
    250                 global $thread_template;
    251 
    252                 // Get unread PM notifications for the user.
    253                 $new_pm_notifications = BP_Notifications_Notification::get( array(
    254                         'user_id'           => bp_loggedin_user_id(),
    255                         'component_name'    => buddypress()->messages->id,
    256                         'component_action'  => 'new_message',
    257                         'is_new'            => 1,
    258                 ) );
    259                 $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
    260 
    261                 // No unread PMs, so stop!
    262                 if ( empty( $unread_message_ids ) ) {
    263                         return;
    264                 }
     172        global $thread_template;
     173
     174        // Get unread PM notifications for the user.
     175        $new_pm_notifications = BP_Notifications_Notification::get( array(
     176                'user_id'           => bp_loggedin_user_id(),
     177                'component_name'    => buddypress()->messages->id,
     178                'component_action'  => 'new_message',
     179                'is_new'            => 1,
     180        ) );
     181        $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
     182
     183        // No unread PMs, so stop!
     184        if ( empty( $unread_message_ids ) ) {
     185                return;
     186        }
    265187
    266                 // Get the unread message ids for this thread only.
    267                 $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
     188        // Get the unread message ids for this thread only.
     189        $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
    268190
    269                 // Mark each notification for each PM message as read.
    270                 foreach ( $message_ids as $message_id ) {
    271                         bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
    272                 }
     191        // Mark each notification for each PM message as read.
     192        foreach ( $message_ids as $message_id ) {
     193                bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
    273194        }
    274195}
    275196add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 );
     
    283204 * @param array $message_ids IDs of the messages.
    284205 */
    285206function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
    286         if ( ! bp_is_active( 'notifications' ) ) {
    287                 return;
    288         }
    289 
    290207        // For each recipient, delete notifications corresponding to each message.
    291208        $thread = new BP_Messages_Thread( $thread_id );
    292209        foreach ( $thread->get_recipients() as $recipient ) {
  • src/bp-messages/classes/class-bp-messages-component.php

     
    6363                        'filters',
    6464                        'template',
    6565                        'functions',
    66                         'notifications',
    6766                        'widgets',
    6867                );
    6968
     
    7271                }
    7372
    7473                // Conditional includes.
     74                if ( bp_is_active( 'notifications' ) ) {
     75                        $includes[] = 'notifications';
     76                }
    7577                if ( bp_is_active( $this->id, 'star' ) ) {
    7678                        $includes[] = 'star';
    7779                }