Skip to:
Content

BuddyPress.org

Changeset 11022


Ignore:
Timestamp:
08/22/2016 08:46:36 PM (3 years ago)
Author:
r-a-y
Message:

Load all notification-related code conditionally for each component.

If the notifications component isn't active, there is no need to load all
notification code.

See #6712.

Location:
trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-functions.php

    r10894 r11022  
    34183418}
    34193419
     3420/* Emails *********************************************************************/
     3421
     3422/**
     3423 * Send email and BP notifications when a user is mentioned in an update.
     3424 *
     3425 * @since 1.2.0
     3426 *
     3427 * @param int $activity_id      The ID of the activity update.
     3428 * @param int $receiver_user_id The ID of the user who is receiving the update.
     3429 */
     3430function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
     3431    $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
     3432
     3433    // Don't leave multiple notifications for the same activity item.
     3434    foreach( $notifications as $notification ) {
     3435        if ( $activity_id == $notification->item_id ) {
     3436            return;
     3437        }
     3438    }
     3439
     3440    $activity     = new BP_Activity_Activity( $activity_id );
     3441    $email_type   = 'activity-at-message';
     3442    $group_name   = '';
     3443    $message_link = bp_activity_get_permalink( $activity_id );
     3444    $poster_name  = bp_core_get_user_displayname( $activity->user_id );
     3445
     3446    remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3447    remove_filter( 'bp_get_activity_content_body', 'wpautop' );
     3448    remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3449
     3450    /** This filter is documented in bp-activity/bp-activity-template.php */
     3451    $content = apply_filters( 'bp_get_activity_content_body', $activity->content );
     3452
     3453    add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3454    add_filter( 'bp_get_activity_content_body', 'wpautop' );
     3455    add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3456
     3457    // Now email the user with the contents of the message (if they have enabled email notifications).
     3458    if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
     3459        if ( bp_is_active( 'groups' ) && bp_is_group() ) {
     3460            $email_type = 'groups-at-message';
     3461            $group_name = bp_get_current_group_name();
     3462        }
     3463
     3464        $unsubscribe_args = array(
     3465            'user_id'           => $receiver_user_id,
     3466            'notification_type' => $email_type,
     3467        );
     3468
     3469        $args = array(
     3470            'tokens' => array(
     3471                'activity'         => $activity,
     3472                'usermessage'      => wp_strip_all_tags( $content ),
     3473                'group.name'       => $group_name,
     3474                'mentioned.url'    => $message_link,
     3475                'poster.name'      => $poster_name,
     3476                'receiver-user.id' => $receiver_user_id,
     3477                'unsubscribe'      => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     3478            ),
     3479        );
     3480
     3481        bp_send_email( $email_type, $receiver_user_id, $args );
     3482    }
     3483
     3484    /**
     3485     * Fires after the sending of an @mention email notification.
     3486     *
     3487     * @since 1.5.0
     3488     * @since 2.5.0 $subject, $message, $content arguments unset and deprecated.
     3489     *
     3490     * @param BP_Activity_Activity $activity         Activity Item object.
     3491     * @param string               $deprecated       Removed in 2.5; now an empty string.
     3492     * @param string               $deprecated       Removed in 2.5; now an empty string.
     3493     * @param string               $deprecated       Removed in 2.5; now an empty string.
     3494     * @param int                  $receiver_user_id The ID of the user who is receiving the update.
     3495     */
     3496    do_action( 'bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id );
     3497}
     3498
     3499/**
     3500 * Send email and BP notifications when an activity item receives a comment.
     3501 *
     3502 * @since 1.2.0
     3503 * @since 2.5.0 Updated to use new email APIs.
     3504 *
     3505 * @param int   $comment_id   The comment id.
     3506 * @param int   $commenter_id The ID of the user who posted the comment.
     3507 * @param array $params       {@link bp_activity_new_comment()}.
     3508 */
     3509function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
     3510    $original_activity = new BP_Activity_Activity( $params['activity_id'] );
     3511    $poster_name       = bp_core_get_user_displayname( $commenter_id );
     3512    $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
     3513
     3514    remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3515    remove_filter( 'bp_get_activity_content_body', 'wpautop' );
     3516    remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3517
     3518    /** This filter is documented in bp-activity/bp-activity-template.php */
     3519    $content = apply_filters( 'bp_get_activity_content_body', $params['content'] );
     3520
     3521    add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     3522    add_filter( 'bp_get_activity_content_body', 'wpautop' );
     3523    add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     3524
     3525    if ( $original_activity->user_id != $commenter_id ) {
     3526
     3527        // Send an email if the user hasn't opted-out.
     3528        if ( 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
     3529
     3530            $unsubscribe_args = array(
     3531                'user_id'           => $original_activity->user_id,
     3532                'notification_type' => 'activity-comment',
     3533            );
     3534
     3535            $args = array(
     3536                'tokens' => array(
     3537                    'comment.id'                => $comment_id,
     3538                    'commenter.id'              => $commenter_id,
     3539                    'usermessage'               => wp_strip_all_tags( $content ),
     3540                    'original_activity.user_id' => $original_activity->user_id,
     3541                    'poster.name'               => $poster_name,
     3542                    'thread.url'                => esc_url( $thread_link ),
     3543                    'unsubscribe'               => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     3544                ),
     3545            );
     3546
     3547            bp_send_email( 'activity-comment', $original_activity->user_id, $args );
     3548        }
     3549
     3550        /**
     3551         * Fires at the point that notifications should be sent for activity comments.
     3552         *
     3553         * @since 2.6.0
     3554         *
     3555         * @param BP_Activity_Activity $original_activity The original activity.
     3556         * @param int                  $comment_id        ID for the newly received comment.
     3557         * @param int                  $commenter_id      ID of the user who made the comment.
     3558         * @param array                $params            Arguments used with the original activity comment.
     3559         */
     3560        do_action( 'bp_activity_sent_reply_to_update_notification', $original_activity, $comment_id, $commenter_id, $params );
     3561    }
     3562
     3563
     3564    /*
     3565     * If this is a reply to another comment, send an email notification to the
     3566     * author of the immediate parent comment.
     3567     */
     3568    if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
     3569        return;
     3570    }
     3571
     3572    $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
     3573
     3574    if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id ) {
     3575
     3576        // Send an email if the user hasn't opted-out.
     3577        if ( 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
     3578
     3579            $unsubscribe_args = array(
     3580                'user_id'           => $parent_comment->user_id,
     3581                'notification_type' => 'activity-comment-author',
     3582            );
     3583
     3584            $args = array(
     3585                'tokens' => array(
     3586                    'comment.id'             => $comment_id,
     3587                    'commenter.id'           => $commenter_id,
     3588                    'usermessage'            => wp_strip_all_tags( $content ),
     3589                    'parent-comment-user.id' => $parent_comment->user_id,
     3590                    'poster.name'            => $poster_name,
     3591                    'thread.url'             => esc_url( $thread_link ),
     3592                    'unsubscribe'            => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     3593                ),
     3594            );
     3595
     3596            bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
     3597        }
     3598
     3599        /**
     3600         * Fires at the point that notifications should be sent for comments on activity replies.
     3601         *
     3602         * @since 2.6.0
     3603         *
     3604         * @param BP_Activity_Activity $parent_comment The parent activity.
     3605         * @param int                  $comment_id     ID for the newly received comment.
     3606         * @param int                  $commenter_id   ID of the user who made the comment.
     3607         * @param array                $params         Arguments used with the original activity comment.
     3608         */
     3609        do_action( 'bp_activity_sent_reply_to_reply_notification', $parent_comment, $comment_id, $commenter_id, $params );
     3610    }
     3611}
     3612
     3613/**
     3614 * Helper method to map action arguments to function parameters.
     3615 *
     3616 * @since 1.9.0
     3617 *
     3618 * @param int   $comment_id ID of the comment being notified about.
     3619 * @param array $params     Parameters to use with notification.
     3620 */
     3621function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
     3622    bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
     3623}
     3624add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
    34203625
    34213626/** Embeds *******************************************************************/
  • trunk/src/bp-activity/bp-activity-notifications.php

    r10941 r11022  
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    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  * @param int $activity_id      The ID of the activity update.
    21  * @param int $receiver_user_id The ID of the user who is receiving the update.
    22  */
    23 function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
    24     $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
    25 
    26     // Don't leave multiple notifications for the same activity item.
    27     foreach( $notifications as $notification ) {
    28         if ( $activity_id == $notification->item_id ) {
    29             return;
    30         }
    31     }
    32 
    33     $activity     = new BP_Activity_Activity( $activity_id );
    34     $email_type   = 'activity-at-message';
    35     $group_name   = '';
    36     $message_link = bp_activity_get_permalink( $activity_id );
    37     $poster_name  = bp_core_get_user_displayname( $activity->user_id );
    38 
    39     remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    40     remove_filter( 'bp_get_activity_content_body', 'wpautop' );
    41     remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    42 
    43     /** This filter is documented in bp-activity/bp-activity-template.php */
    44     $content = apply_filters( 'bp_get_activity_content_body', $activity->content );
    45 
    46     add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    47     add_filter( 'bp_get_activity_content_body', 'wpautop' );
    48     add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    49 
    50     // Now email the user with the contents of the message (if they have enabled email notifications).
    51     if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
    52         if ( bp_is_active( 'groups' ) && bp_is_group() ) {
    53             $email_type = 'groups-at-message';
    54             $group_name = bp_get_current_group_name();
    55         }
    56 
    57         $unsubscribe_args = array(
    58             'user_id'           => $receiver_user_id,
    59             'notification_type' => $email_type,
    60         );
    61 
    62         $args = array(
    63             'tokens' => array(
    64                 'activity'         => $activity,
    65                 'usermessage'      => wp_strip_all_tags( $content ),
    66                 'group.name'       => $group_name,
    67                 'mentioned.url'    => $message_link,
    68                 'poster.name'      => $poster_name,
    69                 'receiver-user.id' => $receiver_user_id,
    70                 'unsubscribe'      => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    71             ),
    72         );
    73 
    74         bp_send_email( $email_type, $receiver_user_id, $args );
    75     }
    76 
    77     /**
    78      * Fires after the sending of an @mention email notification.
    79      *
    80      * @since 1.5.0
    81      * @since 2.5.0 $subject, $message, $content arguments unset and deprecated.
    82      *
    83      * @param BP_Activity_Activity $activity         Activity Item object.
    84      * @param string               $deprecated       Removed in 2.5; now an empty string.
    85      * @param string               $deprecated       Removed in 2.5; now an empty string.
    86      * @param string               $deprecated       Removed in 2.5; now an empty string.
    87      * @param int                  $receiver_user_id The ID of the user who is receiving the update.
    88      */
    89     do_action( 'bp_activity_sent_mention_email', $activity, '', '', '', $receiver_user_id );
    90 }
    91 
    92 /**
    93  * Send email and BP notifications when an activity item receives a comment.
    94  *
    95  * @since 1.2.0
    96  * @since 2.5.0 Updated to use new email APIs.
    97  *
    98  * @param int   $comment_id   The comment id.
    99  * @param int   $commenter_id The ID of the user who posted the comment.
    100  * @param array $params       {@link bp_activity_new_comment()}.
    101  */
    102 function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
    103     $original_activity = new BP_Activity_Activity( $params['activity_id'] );
    104     $poster_name       = bp_core_get_user_displayname( $commenter_id );
    105     $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
    106 
    107     remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    108     remove_filter( 'bp_get_activity_content_body', 'wpautop' );
    109     remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    110 
    111     /** This filter is documented in bp-activity/bp-activity-template.php */
    112     $content = apply_filters( 'bp_get_activity_content_body', $params['content'] );
    113 
    114     add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
    115     add_filter( 'bp_get_activity_content_body', 'wpautop' );
    116     add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    117 
    118     if ( $original_activity->user_id != $commenter_id ) {
    119 
    120         // Send an email if the user hasn't opted-out.
    121         if ( 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
    122 
    123             $unsubscribe_args = array(
    124                 'user_id'           => $original_activity->user_id,
    125                 'notification_type' => 'activity-comment',
    126             );
    127 
    128             $args = array(
    129                 'tokens' => array(
    130                     'comment.id'                => $comment_id,
    131                     'commenter.id'              => $commenter_id,
    132                     'usermessage'               => wp_strip_all_tags( $content ),
    133                     'original_activity.user_id' => $original_activity->user_id,
    134                     'poster.name'               => $poster_name,
    135                     'thread.url'                => esc_url( $thread_link ),
    136                     'unsubscribe'               => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    137                 ),
    138             );
    139 
    140             bp_send_email( 'activity-comment', $original_activity->user_id, $args );
    141         }
    142 
    143         /**
    144          * Fires at the point that notifications should be sent for activity comments.
    145          *
    146          * @since 2.6.0
    147          *
    148          * @param BP_Activity_Activity $original_activity The original activity.
    149          * @param int                  $comment_id        ID for the newly received comment.
    150          * @param int                  $commenter_id      ID of the user who made the comment.
    151          * @param array                $params            Arguments used with the original activity comment.
    152          */
    153         do_action( 'bp_activity_sent_reply_to_update_notification', $original_activity, $comment_id, $commenter_id, $params );
    154     }
    155 
    156 
    157     /*
    158      * If this is a reply to another comment, send an email notification to the
    159      * author of the immediate parent comment.
    160      */
    161     if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
    162         return;
    163     }
    164 
    165     $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
    166 
    167     if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id ) {
    168 
    169         // Send an email if the user hasn't opted-out.
    170         if ( 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
    171 
    172             $unsubscribe_args = array(
    173                 'user_id'           => $parent_comment->user_id,
    174                 'notification_type' => 'activity-comment-author',
    175             );
    176 
    177             $args = array(
    178                 'tokens' => array(
    179                     'comment.id'             => $comment_id,
    180                     'commenter.id'           => $commenter_id,
    181                     'usermessage'            => wp_strip_all_tags( $content ),
    182                     'parent-comment-user.id' => $parent_comment->user_id,
    183                     'poster.name'            => $poster_name,
    184                     'thread.url'             => esc_url( $thread_link ),
    185                     'unsubscribe'            => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    186                 ),
    187             );
    188 
    189             bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
    190         }
    191 
    192         /**
    193          * Fires at the point that notifications should be sent for comments on activity replies.
    194          *
    195          * @since 2.6.0
    196          *
    197          * @param BP_Activity_Activity $parent_comment The parent activity.
    198          * @param int                  $comment_id     ID for the newly received comment.
    199          * @param int                  $commenter_id   ID of the user who made the comment.
    200          * @param array                $params         Arguments used with the original activity comment.
    201          */
    202         do_action( 'bp_activity_sent_reply_to_reply_notification', $parent_comment, $comment_id, $commenter_id, $params );
    203     }
    204 }
    205 
    206 /**
    207  * Helper method to map action arguments to function parameters.
    208  *
    209  * @since 1.9.0
    210  *
    211  * @param int   $comment_id ID of the comment being notified about.
    212  * @param array $params     Parameters to use with notification.
    213  */
    214 function bp_activity_new_comment_notification_helper( $comment_id, $params ) {
    215     bp_activity_new_comment_notification( $comment_id, $params['user_id'], $params );
    216 }
    217 add_action( 'bp_activity_comment_posted', 'bp_activity_new_comment_notification_helper', 10, 2 );
    218 
    219 /** Notifications *************************************************************/
    22012
    22113/**
     
    360152 */
    361153function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
    362     if ( bp_is_active( 'notifications' ) ) {
    363         bp_notifications_add_notification( array(
     154    bp_notifications_add_notification( array(
    364155            'user_id'           => $receiver_user_id,
    365156            'item_id'           => $activity->id,
     
    369160            'date_notified'     => bp_core_current_time(),
    370161            'is_new'            => 1,
    371         ) );
    372     }
     162    ) );
    373163}
    374164add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );
     
    384174 */
    385175function bp_activity_update_reply_add_notification( $activity, $comment_id, $commenter_id ) {
    386     if ( bp_is_active( 'notifications' ) ) {
    387         bp_notifications_add_notification( array(
    388             'user_id'           => $activity->user_id,
    389             'item_id'           => $comment_id,
    390             'secondary_item_id' => $commenter_id,
    391             'component_name'    => buddypress()->activity->id,
    392             'component_action'  => 'update_reply',
    393             'date_notified'     => bp_core_current_time(),
    394             'is_new'            => 1,
    395         ) );
    396     }
     176    bp_notifications_add_notification( array(
     177        'user_id'           => $activity->user_id,
     178        'item_id'           => $comment_id,
     179        'secondary_item_id' => $commenter_id,
     180        'component_name'    => buddypress()->activity->id,
     181        'component_action'  => 'update_reply',
     182        'date_notified'     => bp_core_current_time(),
     183        'is_new'            => 1,
     184    ) );
    397185}
    398186add_action( 'bp_activity_sent_reply_to_update_notification', 'bp_activity_update_reply_add_notification', 10, 3 );
     
    408196 */
    409197function bp_activity_comment_reply_add_notification( $activity_comment, $comment_id, $commenter_id ) {
    410     if ( bp_is_active( 'notifications' ) ) {
    411         bp_notifications_add_notification( array(
    412             'user_id'           => $activity_comment->user_id,
    413             'item_id'           => $comment_id,
    414             'secondary_item_id' => $commenter_id,
    415             'component_name'    => buddypress()->activity->id,
    416             'component_action'  => 'comment_reply',
    417             'date_notified'     => bp_core_current_time(),
    418             'is_new'            => 1,
    419         ) );
    420     }
     198    bp_notifications_add_notification( array(
     199        'user_id'           => $activity_comment->user_id,
     200        'item_id'           => $comment_id,
     201        'secondary_item_id' => $commenter_id,
     202        'component_name'    => buddypress()->activity->id,
     203        'component_action'  => 'comment_reply',
     204        'date_notified'     => bp_core_current_time(),
     205        'is_new'            => 1,
     206    ) );
    421207}
    422208add_action( 'bp_activity_sent_reply_to_reply_notification', 'bp_activity_comment_reply_add_notification', 10, 3 );
     
    431217 */
    432218function bp_activity_remove_screen_notifications( $user_id = 0 ) {
    433     if ( ! bp_is_active( 'notifications' ) ) {
    434         return;
    435     }
    436 
    437219    // Only mark read if the current user is looking at his own mentions.
    438220    if ( empty( $user_id ) || (int) $user_id !== (int) bp_loggedin_user_id() ) {
     
    452234 */
    453235function bp_activity_remove_screen_notifications_single_activity_permalink( $activity ) {
    454     if ( ! bp_is_active( 'notifications' ) ) {
    455         return;
    456     }
    457 
    458236    if ( ! is_user_logged_in() ) {
    459237        return;
     
    474252 */
    475253function bp_activity_remove_screen_notifications_for_non_mentions() {
    476     if ( false === bp_is_active( 'notifications' ) || false === is_singular() || false === is_user_logged_in() || empty( $_GET['nid'] ) ) {
     254    if ( false === is_singular() || false === is_user_logged_in() || empty( $_GET['nid'] ) ) {
    477255        return;
    478256    }
     
    501279    // Let's delete all without checking if content contains any mentions
    502280    // to avoid a query to get the activity.
    503     if ( bp_is_active( 'notifications' ) && ! empty( $activity_ids_deleted ) ) {
     281    if ( ! empty( $activity_ids_deleted ) ) {
    504282        foreach ( $activity_ids_deleted as $activity_id ) {
    505283            bp_notifications_delete_all_notifications_by_type( $activity_id, buddypress()->activity->id );
     
    523301function bp_activity_add_notification_for_synced_blog_comment( $activity_id, $post_type_comment, $activity_args, $activity_post_object ) {
    524302    // If activity comments are disabled for WP posts, stop now!
    525     if ( bp_disable_blogforum_comments() || empty( $activity_id ) || false === bp_is_active( 'notifications' ) ) {
     303    if ( bp_disable_blogforum_comments() || empty( $activity_id ) ) {
    526304        return;
    527305    }
  • trunk/src/bp-activity/classes/class-bp-activity-component.php

    r10851 r11022  
    5858            'template',
    5959            'functions',
    60             'notifications',
    6160            'cache'
    6261        );
     62
     63        // Notifications support.
     64        if ( bp_is_active( 'notifications' ) ) {
     65            $includes[] = 'notifications';
     66        }
    6367
    6468        if ( ! buddypress()->do_autoload ) {
  • trunk/src/bp-friends/bp-friends-functions.php

    r10587 r11022  
    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    $unsubscribe_args = array(
     819        'user_id'           => $friend_id,
     820        'notification_type' => 'friends-request',
     821    );
     822
     823    $args = array(
     824        'tokens' => array(
     825            'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
     826            'friend.id'           => $friend_id,
     827            'friendship.id'       => $friendship_id,
     828            'initiator.id'        => $initiator_id,
     829            'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
     830            'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
     831            'unsubscribe'         => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     832        ),
     833    );
     834    bp_send_email( 'friends-request', $friend_id, $args );
     835}
     836add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
     837
     838/**
     839 * Send notifications related to the acceptance of a friendship request.
     840 *
     841 * When a friendship request is accepted, an email and a BP notification are
     842 * sent to the user who requested the friendship ($initiator_id).
     843 *
     844 * @since 1.0.0
     845 *
     846 * @param int $friendship_id ID of the friendship object.
     847 * @param int $initiator_id  ID of the user who initiated the request.
     848 * @param int $friend_id     ID of the request recipient.
     849 */
     850function friends_notification_accepted_request( $friendship_id, $initiator_id, $friend_id ) {
     851    if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) ) {
     852        return;
     853    }
     854
     855    $unsubscribe_args = array(
     856        'user_id'           => $initiator_id,
     857        'notification_type' => 'friends-request-accepted',
     858    );
     859
     860    $args = array(
     861        'tokens' => array(
     862            'friend.id'      => $friend_id,
     863            'friendship.url' => esc_url( bp_core_get_user_domain( $friend_id ) ),
     864            'friend.name'    => bp_core_get_user_displayname( $friend_id ),
     865            'friendship.id'  => $friendship_id,
     866            'initiator.id'   => $initiator_id,
     867            'unsubscribe'      => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     868        ),
     869    );
     870    bp_send_email( 'friends-request-accepted', $initiator_id, $args );
     871}
     872add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
  • trunk/src/bp-friends/bp-friends-notifications.php

    r10941 r11022  
    1313// Exit if accessed directly.
    1414defined( 'ABSPATH' ) || exit;
    15 
    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     $unsubscribe_args = array(
    36         'user_id'           => $friend_id,
    37         'notification_type' => 'friends-request',
    38     );
    39 
    40     $args = array(
    41         'tokens' => array(
    42             'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
    43             'friend.id'           => $friend_id,
    44             'friendship.id'       => $friendship_id,
    45             'initiator.id'        => $initiator_id,
    46             'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
    47             'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
    48             'unsubscribe'         => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    49         ),
    50     );
    51     bp_send_email( 'friends-request', $friend_id, $args );
    52 }
    53 add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
    54 
    55 /**
    56  * Send notifications related to the acceptance of a friendship request.
    57  *
    58  * When a friendship request is accepted, an email and a BP notification are
    59  * sent to the user who requested the friendship ($initiator_id).
    60  *
    61  * @since 1.0.0
    62  *
    63  * @param int $friendship_id ID of the friendship object.
    64  * @param int $initiator_id  ID of the user who initiated the request.
    65  * @param int $friend_id     ID of the request recipient.
    66  */
    67 function friends_notification_accepted_request( $friendship_id, $initiator_id, $friend_id ) {
    68     if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) ) {
    69         return;
    70     }
    71 
    72     $unsubscribe_args = array(
    73         'user_id'           => $initiator_id,
    74         'notification_type' => 'friends-request-accepted',
    75     );
    76 
    77     $args = array(
    78         'tokens' => array(
    79             'friend.id'      => $friend_id,
    80             'friendship.url' => esc_url( bp_core_get_user_domain( $friend_id ) ),
    81             'friend.name'    => bp_core_get_user_displayname( $friend_id ),
    82             'friendship.id'  => $friendship_id,
    83             'initiator.id'   => $initiator_id,
    84             'unsubscribe'      => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    85         ),
    86     );
    87     bp_send_email( 'friends-request-accepted', $initiator_id, $args );
    88 }
    89 add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
    90 
    91 /** Notifications *************************************************************/
    9215
    9316/**
     
    195118 */
    196119function friends_clear_friend_notifications() {
    197     if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     120    if ( isset( $_GET['new'] ) ) {
    198121        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    199122    }
     
    207130 */
    208131function bp_friends_mark_friendship_request_notifications_by_type() {
    209     if ( isset( $_GET['new'] ) && bp_is_active( 'notifications' ) ) {
     132    if ( isset( $_GET['new'] ) ) {
    210133        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_request' );
    211134    }
     
    219142 */
    220143function bp_friends_mark_friendship_accepted_notifications_by_type() {
    221     if ( bp_is_active( 'notifications' ) ) {
    222         bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    223     }
     144    bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->friends->id, 'friendship_accepted' );
    224145}
    225146add_action( 'friends_screen_my_friends', 'bp_friends_mark_friendship_accepted_notifications_by_type' );
     
    235156 */
    236157function bp_friends_friendship_requested_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
    237     if ( bp_is_active( 'notifications' ) ) {
    238         bp_notifications_add_notification( array(
    239             'user_id'           => $friend_user_id,
    240             'item_id'           => $initiator_user_id,
    241             'secondary_item_id' => $friendship_id,
    242             'component_name'    => buddypress()->friends->id,
    243             'component_action'  => 'friendship_request',
    244             'date_notified'     => bp_core_current_time(),
    245             'is_new'            => 1,
    246         ) );
    247     }
     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    ) );
    248167}
    249168add_action( 'friends_friendship_requested', 'bp_friends_friendship_requested_notification', 10, 3 );
     
    258177 */
    259178function bp_friends_mark_friendship_rejected_notifications_by_item_id( $friendship_id, $friendship ) {
    260     if ( bp_is_active( 'notifications' ) ) {
    261         bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    262     }
     179    bp_notifications_mark_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    263180}
    264181add_action( 'friends_friendship_rejected', 'bp_friends_mark_friendship_rejected_notifications_by_item_id', 10, 2 );
     
    274191 */
    275192function bp_friends_add_friendship_accepted_notification( $friendship_id, $initiator_user_id, $friend_user_id ) {
    276 
    277     // Bail if notifications is not active.
    278     if ( ! bp_is_active( 'notifications' ) ) {
    279         return;
    280     }
    281 
    282193    // Remove the friend request notice.
    283194    bp_notifications_mark_notifications_by_item_id( $friend_user_id, $initiator_user_id, buddypress()->friends->id, 'friendship_request' );
     
    305216 */
    306217function bp_friends_mark_friendship_withdrawn_notifications_by_item_id( $friendship_id, $friendship ) {
    307     if ( bp_is_active( 'notifications' ) ) {
    308         bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    309     }
     218    bp_notifications_delete_notifications_by_item_id( $friendship->friend_user_id, $friendship->initiator_user_id, buddypress()->friends->id, 'friendship_request' );
    310219}
    311220add_action( 'friends_friendship_withdrawn', 'bp_friends_mark_friendship_withdrawn_notifications_by_item_id', 10, 2 );
     
    319228 */
    320229function bp_friends_remove_notifications_data( $user_id = 0 ) {
    321     if ( bp_is_active( 'notifications' ) ) {
    322         bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
    323     }
     230    bp_notifications_delete_notifications_from_user( $user_id, buddypress()->friends->id, 'friendship_request' );
    324231}
    325232add_action( 'friends_remove_data', 'bp_friends_remove_notifications_data', 10, 1 );
  • trunk/src/bp-friends/classes/class-bp-friends-component.php

    r10676 r11022  
    5454            'template',
    5555            'functions',
    56             'notifications',
    5756            'widgets',
    5857        );
     58
     59        // Conditional includes.
     60        if ( bp_is_active( 'notifications' ) ) {
     61            $includes[] = 'notifications';
     62        }
    5963
    6064        if ( ! buddypress()->do_autoload ) {
  • trunk/src/bp-messages/bp-messages-functions.php

    r10654 r11022  
    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        $unsubscribe_args = array(
     577            'user_id'           => $recipient->user_id,
     578            'notification_type' => 'messages-unread',
     579        );
     580
     581        $args = array(
     582            'tokens' => array(
     583                'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
     584                'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
     585                'sender.name' => $sender_name,
     586                'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
     587                'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
     588            ),
     589        );
     590        bp_send_email( 'messages-unread', $ud, $args );
     591    }
     592
     593    /**
     594     * Fires after the sending of a new message email notification.
     595     *
     596     * @since 1.5.0
     597     * @deprecated 2.5.0 Use the filters in BP_Email.
     598     *                   $email_subject and $email_content arguments unset and deprecated.
     599     *
     600     * @param array  $recipients    User IDs of recipients.
     601     * @param string $email_subject Deprecated in 2.5; now an empty string.
     602     * @param string $email_content Deprecated in 2.5; now an empty string.
     603     * @param array  $args          Array of originally provided arguments.
     604     */
     605    do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
     606}
     607add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
  • trunk/src/bp-messages/bp-messages-notifications.php

    r10941 r11022  
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    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         $unsubscribe_args = array(
    62             'user_id'           => $recipient->user_id,
    63             'notification_type' => 'messages-unread',
    64         );
    65 
    66         $args = array(
    67             'tokens' => array(
    68                 'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
    69                 'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
    70                 'sender.name' => $sender_name,
    71                 'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
    72                 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
    73             ),
    74         );
    75         bp_send_email( 'messages-unread', $ud, $args );
    76     }
    77 
    78     /**
    79      * Fires after the sending of a new message email notification.
    80      *
    81      * @since 1.5.0
    82      * @deprecated 2.5.0 Use the filters in BP_Email.
    83      *                   $email_subject and $email_content arguments unset and deprecated.
    84      *
    85      * @param array  $recipients    User IDs of recipients.
    86      * @param string $email_subject Deprecated in 2.5; now an empty string.
    87      * @param string $email_content Deprecated in 2.5; now an empty string.
    88      * @param array  $args          Array of originally provided arguments.
    89      */
    90     do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
    91 }
    92 add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
    93 
    94 /** Notifications *************************************************************/
    9512
    9613/**
     
    231148 */
    232149function bp_messages_message_sent_add_notification( $message ) {
    233     if ( bp_is_active( 'notifications' ) && ! empty( $message->recipients ) ) {
     150    if ( ! empty( $message->recipients ) ) {
    234151        foreach ( (array) $message->recipients as $recipient ) {
    235152            bp_notifications_add_notification( array(
     
    253170 */
    254171function bp_messages_screen_conversation_mark_notifications() {
    255     if ( bp_is_active( 'notifications' ) ) {
    256         global $thread_template;
    257 
    258         // Get unread PM notifications for the user.
    259         $new_pm_notifications = BP_Notifications_Notification::get( array(
    260             'user_id'           => bp_loggedin_user_id(),
    261             'component_name'    => buddypress()->messages->id,
    262             'component_action'  => 'new_message',
    263             'is_new'            => 1,
    264         ) );
    265         $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
    266 
    267         // No unread PMs, so stop!
    268         if ( empty( $unread_message_ids ) ) {
    269             return;
    270         }
    271 
    272         // Get the unread message ids for this thread only.
    273         $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) );
    274 
    275         // Mark each notification for each PM message as read.
    276         foreach ( $message_ids as $message_id ) {
    277             bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
    278         }
     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    }
     187
     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' ) );
     190
     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' );
    279194    }
    280195}
     
    290205 */
    291206function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
    292     if ( ! bp_is_active( 'notifications' ) ) {
    293         return;
    294     }
    295 
    296207    // For each recipient, delete notifications corresponding to each message.
    297208    $thread = new BP_Messages_Thread( $thread_id );
  • trunk/src/bp-messages/classes/class-bp-messages-component.php

    r10678 r11022  
    6464            'template',
    6565            'functions',
    66             'notifications',
    6766            'widgets',
    6867        );
     
    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';
Note: See TracChangeset for help on using the changeset viewer.