Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/22/2016 08:46:36 PM (5 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.