Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/27/2016 09:15:07 PM (9 years ago)
Author:
djpaul
Message:

Emails: refactor notification functions and use bp_send_email to send email.

Backwards compatibility is mostly maintained, though there are a few unavoidable instances where some data passed to filters is no longer sent (an empty string or equivalent is provided to avoid PHP Notices). These will be detailed in a post on bpdevel.wordpress.com in the feature to alert plugin developers.

See #6592. Props timersys, mercime, boonebgorges, hnla, DJPaul.

File:
1 edited

Legend:

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

    r10457 r10479  
    4141 */
    4242function bp_activity_at_message_notification( $activity_id, $receiver_user_id ) {
     43    $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
    4344
    4445    // Don't leave multiple notifications for the same activity item.
    45     $notifications = BP_Core_Notification::get_all_for_user( $receiver_user_id, 'all' );
    46 
    4746    foreach( $notifications as $notification ) {
    4847        if ( $activity_id == $notification->item_id ) {
     
    5150    }
    5251
    53     $activity = new BP_Activity_Activity( $activity_id );
    54 
    55     $subject = '';
    56     $message = '';
    57     $content = '';
     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 );
    5868
    5969    // Now email the user with the contents of the message (if they have enabled email notifications).
    6070    if ( 'no' != bp_get_user_meta( $receiver_user_id, 'notification_activity_new_mention', true ) ) {
    61         $poster_name = bp_core_get_user_displayname( $activity->user_id );
    62 
    63         $message_link  = bp_activity_get_permalink( $activity_id );
    64         $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    65         $settings_link = bp_core_get_user_domain( $receiver_user_id ) . $settings_slug . '/notifications/';
    66 
    67         $poster_name = stripslashes( $poster_name );
    68         $content = bp_activity_filter_kses( strip_tags( stripslashes( $activity->content ) ) );
    69 
    70         // Set up and send the message.
    71         $ud       = bp_core_get_core_userdata( $receiver_user_id );
    72         $to       = $ud->user_email;
    73         $subject  = bp_get_email_subject( array( 'text' => sprintf( __( '%s mentioned you in an update', 'buddypress' ), $poster_name ) ) );
    74 
    7571        if ( bp_is_active( 'groups' ) && bp_is_group() ) {
    76             $message = sprintf( __(
    77 '%1$s mentioned you in the group "%2$s":
    78 
    79 "%3$s"
    80 
    81 To view and respond to the message, log in and visit: %4$s
    82 
    83 ---------------------
    84 ', 'buddypress' ), $poster_name, bp_get_current_group_name(), $content, $message_link );
    85         } else {
    86             $message = sprintf( __(
    87 '%1$s mentioned you in an update:
    88 
    89 "%2$s"
    90 
    91 To view and respond to the message, log in and visit: %3$s
    92 
    93 ---------------------
    94 ', 'buddypress' ), $poster_name, $content, $message_link );
     72            $email_type = 'groups-at-message';
     73            $group_name = bp_get_current_group_name();
    9574        }
    9675
    97         // Only show the disable notifications line if the settings component is enabled.
    98         if ( bp_is_active( 'settings' ) ) {
    99             $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    100         }
    101 
    102         /**
    103          * Filters the user email that the @mention notification will be sent to.
    104          *
    105          * @since 1.2.0
    106          *
    107          * @param string $to User email the notification is being sent to.
    108          */
    109         $to      = apply_filters( 'bp_activity_at_message_notification_to', $to );
    110 
    111         /**
    112          * Filters the @mention notification subject that will be sent to user.
    113          *
    114          * @since 1.2.0
    115          *
    116          * @param string $subject     Email notification subject text.
    117          * @param string $poster_name Name of the person who made the @mention.
    118          */
    119         $subject = apply_filters( 'bp_activity_at_message_notification_subject', $subject, $poster_name );
    120 
    121         /**
    122          * Filters the @mention notification message that will be sent to user.
    123          *
    124          * @since 1.2.0
    125          *
    126          * @param string $message       Email notification message text.
    127          * @param string $poster_name   Name of the person who made the @mention.
    128          * @param string $content       Content of the @mention.
    129          * @param string $message_link  URL permalink for the activity message.
    130          * @param string $settings_link URL permalink for the user's notification settings area.
    131          */
    132         $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link );
    133 
    134         wp_mail( $to, $subject, $message );
    135     }
    136 
    137     /**
    138      * Fires after the sending of an @mention email notification.
    139      *
    140      * @since 1.5.0
    141      *
    142      * @param BP_Activity_Activity $activity         Activity Item object.
    143      * @param string               $subject          Email notification subject text.
    144      * @param string               $message          Email notification message text.
    145      * @param string               $content          Content of the @mention.
    146      * @param int                  $receiver_user_id The ID of the user who is receiving the update.
    147      */
    148     do_action( 'bp_activity_sent_mention_email', $activity, $subject, $message, $content, $receiver_user_id );
     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    }
    14989}
    15090
     
    15393 *
    15494 * @since 1.2.0
     95 * @since 2.5.0 Updated to use new email APIs.
    15596 *
    15697 * @uses bp_get_user_meta()
     
    177118 * @param int   $commenter_id The ID of the user who posted the comment.
    178119 * @param array $params       {@link bp_activity_new_comment()}.
    179  * @return bool
    180120 */
    181121function bp_activity_new_comment_notification( $comment_id = 0, $commenter_id = 0, $params = array() ) {
    182 
    183     // Set some default parameters.
    184     $activity_id = 0;
    185     $parent_id   = 0;
    186 
    187     extract( $params );
    188 
    189     $original_activity = new BP_Activity_Activity( $activity_id );
     122    $original_activity = new BP_Activity_Activity( $params['activity_id'] );
     123    $poster_name       = bp_core_get_user_displayname( $commenter_id );
     124    $thread_link       = bp_activity_get_permalink( $params['activity_id'] );
     125
     126    remove_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     127    remove_filter( 'bp_get_activity_content_body', 'wpautop' );
     128    remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
     129
     130    /** This filter is documented in bp-activity/bp-activity-template.php */
     131    $content = apply_filters( 'bp_get_activity_content_body', $params['content'] );
     132
     133    add_filter( 'bp_get_activity_content_body', 'convert_smilies' );
     134    add_filter( 'bp_get_activity_content_body', 'wpautop' );
     135    add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
    190136
    191137    if ( $original_activity->user_id != $commenter_id && 'no' != bp_get_user_meta( $original_activity->user_id, 'notification_activity_new_reply', true ) ) {
    192         $poster_name   = bp_core_get_user_displayname( $commenter_id );
    193         $thread_link   = bp_activity_get_permalink( $activity_id );
    194         $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    195         $settings_link = bp_core_get_user_domain( $original_activity->user_id ) . $settings_slug . '/notifications/';
    196 
    197         $poster_name = stripslashes( $poster_name );
    198         $content = bp_activity_filter_kses( stripslashes($content) );
    199 
    200         // Set up and send the message.
    201         $ud      = bp_core_get_core_userdata( $original_activity->user_id );
    202         $to      = $ud->user_email;
    203         $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your updates', 'buddypress' ), $poster_name ) ) );
    204         $message = sprintf( __(
    205 '%1$s replied to one of your updates:
    206 
    207 "%2$s"
    208 
    209 To view your original update and all comments, log in and visit: %3$s
    210 
    211 ---------------------
    212 ', 'buddypress' ), $poster_name, $content, $thread_link );
    213 
    214         // Only show the disable notifications line if the settings component is enabled.
    215         if ( bp_is_active( 'settings' ) ) {
    216             $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    217         }
    218 
    219         /**
    220          * Filters the user email that the new comment notification will be sent to.
    221          *
    222          * @since 1.2.0
    223          *
    224          * @param string $to User email the notification is being sent to.
    225          */
    226         $to = apply_filters( 'bp_activity_new_comment_notification_to', $to );
    227 
    228         /**
    229          * Filters the new comment notification subject that will be sent to user.
    230          *
    231          * @since 1.2.0
    232          *
    233          * @param string $subject     Email notification subject text.
    234          * @param string $poster_name Name of the person who made the comment.
    235          */
    236         $subject = apply_filters( 'bp_activity_new_comment_notification_subject', $subject, $poster_name );
    237 
    238         /**
    239          * Filters the new comment notification message that will be sent to user.
    240          *
    241          * @since 1.2.0
    242          *
    243          * @param string $message       Email notification message text.
    244          * @param string $poster_name   Name of the person who made the comment.
    245          * @param string $content       Content of the comment.
    246          * @param string $thread_link   URL permalink for the activity thread.
    247          * @param string $settings_link URL permalink for the user's notification settings area.
    248          */
    249         $message = apply_filters( 'bp_activity_new_comment_notification_message', $message, $poster_name, $content, $thread_link, $settings_link );
    250 
    251         wp_mail( $to, $subject, $message );
    252 
    253         /**
    254          * Fires after the sending of a reply to an update email notification.
    255          *
    256          * @since 1.5.0
    257          *
    258          * @param int    $user_id      ID of the original activity item author.
    259          * @param string $subject      Email notification subject text.
    260          * @param string $message      Email notification message text.
    261          * @param int    $comment_id   ID for the newly received comment.
    262          * @param int    $commenter_id ID of the user who made the comment.
    263          * @param array  $params       Arguments used with the original activity comment.
    264          */
    265         do_action( 'bp_activity_sent_reply_to_update_email', $original_activity->user_id, $subject, $message, $comment_id, $commenter_id, $params );
    266     }
     138        $args = array(
     139            'tokens' => array(
     140                'comment.id'                => $comment_id,
     141                'commenter.id'              => $commenter_id,
     142                'usermessage'               => wp_strip_all_tags( $content ),
     143                'original_activity.user_id' => $original_activity->user_id,
     144                'poster.name'               => $poster_name,
     145                'thread.url'                => esc_url( $thread_link ),
     146            ),
     147        );
     148
     149        bp_send_email( 'activity-comment', $original_activity->user_id, $args );
     150    }
     151
    267152
    268153    /*
     
    270155     * author of the immediate parent comment.
    271156     */
    272     if ( empty( $parent_id ) || ( $activity_id == $parent_id ) ) {
    273         return false;
    274     }
    275 
    276     $parent_comment = new BP_Activity_Activity( $parent_id );
     157    if ( empty( $params['parent_id'] ) || ( $params['activity_id'] == $params['parent_id'] ) ) {
     158        return;
     159    }
     160
     161    $parent_comment = new BP_Activity_Activity( $params['parent_id'] );
    277162
    278163    if ( $parent_comment->user_id != $commenter_id && $original_activity->user_id != $parent_comment->user_id && 'no' != bp_get_user_meta( $parent_comment->user_id, 'notification_activity_new_reply', true ) ) {
    279         $poster_name   = bp_core_get_user_displayname( $commenter_id );
    280         $thread_link   = bp_activity_get_permalink( $activity_id );
    281         $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    282         $settings_link = bp_core_get_user_domain( $parent_comment->user_id ) . $settings_slug . '/notifications/';
    283 
    284         // Set up and send the message.
    285         $ud       = bp_core_get_core_userdata( $parent_comment->user_id );
    286         $to       = $ud->user_email;
    287         $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s replied to one of your comments', 'buddypress' ), $poster_name ) ) );
    288 
    289         $poster_name = stripslashes( $poster_name );
    290         $content = bp_activity_filter_kses( stripslashes( $content ) );
    291 
    292 $message = sprintf( __(
    293 '%1$s replied to one of your comments:
    294 
    295 "%2$s"
    296 
    297 To view the original activity, your comment and all replies, log in and visit: %3$s
    298 
    299 ---------------------
    300 ', 'buddypress' ), $poster_name, $content, $thread_link );
    301 
    302         // Only show the disable notifications line if the settings component is enabled.
    303         if ( bp_is_active( 'settings' ) ) {
    304             $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    305         }
    306 
    307         /**
    308          * Filters the user email that the new comment reply notification will be sent to.
    309          *
    310          * @since 1.2.0
    311          *
    312          * @param string $to User email the notification is being sent to.
    313          */
    314         $to = apply_filters( 'bp_activity_new_comment_notification_comment_author_to', $to );
    315 
    316         /**
    317          * Filters the new comment reply notification subject that will be sent to user.
    318          *
    319          * @since 1.2.0
    320          *
    321          * @param string $subject     Email notification subject text.
    322          * @param string $poster_name Name of the person who made the comment reply.
    323          */
    324         $subject = apply_filters( 'bp_activity_new_comment_notification_comment_author_subject', $subject, $poster_name );
    325 
    326         /**
    327          * Filters the new comment reply notification message that will be sent to user.
    328          *
    329          * @since 1.2.0
    330          *
    331          * @param string $message       Email notification message text.
    332          * @param string $poster_name   Name of the person who made the comment reply.
    333          * @param string $content       Content of the comment reply.
    334          * @param string $settings_link URL permalink for the user's notification settings area.
    335          * @param string $thread_link   URL permalink for the activity thread.
    336          */
    337         $message = apply_filters( 'bp_activity_new_comment_notification_comment_author_message', $message, $poster_name, $content, $settings_link, $thread_link );
    338 
    339         wp_mail( $to, $subject, $message );
    340 
    341         /**
    342          * Fires after the sending of a reply to a reply email notification.
    343          *
    344          * @since 1.5.0
    345          *
    346          * @param int    $user_id      ID of the parent activity item author.
    347          * @param string $subject      Email notification subject text.
    348          * @param string $message      Email notification message text.
    349          * @param int    $comment_id   ID for the newly received comment.
    350          * @param int    $commenter_id ID of the user who made the comment.
    351          * @param array  $params       Arguments used with the original activity comment.
    352          */
    353         do_action( 'bp_activity_sent_reply_to_reply_email', $parent_comment->user_id, $subject, $message, $comment_id, $commenter_id, $params );
     164        $args = array(
     165            'tokens' => array(
     166                'comment.id'             => $comment_id,
     167                'commenter.id'           => $commenter_id,
     168                'usermessage'            => wp_strip_all_tags( $content ),
     169                'parent-comment-user.id' => $parent_comment->user_id,
     170                'poster.name'            => $poster_name,
     171                'thread.url'             => esc_url( $thread_link ),
     172            ),
     173        );
     174
     175        bp_send_email( 'activity-comment-author', $parent_comment->user_id, $args );
    354176    }
    355177}
Note: See TracChangeset for help on using the changeset viewer.