Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/27/2016 09:15:07 PM (10 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-messages/bp-messages-notifications.php

    r10417 r10479  
    2727 */
    2828function messages_notification_new_message( $raw_args = array() ) {
    29 
    30     // Cast possible $message object as an array.
    3129    if ( is_object( $raw_args ) ) {
    3230        $args = (array) $raw_args;
     
    4341    extract( $args );
    4442
    45     // Get the sender display name.
     43    if ( empty( $recipients ) ) {
     44        return;
     45    }
     46
    4647    $sender_name = bp_core_get_user_displayname( $sender_id );
    4748
    48     // Bail if no recipients.
    49     if ( ! empty( $recipients ) ) {
    50 
    51         foreach ( $recipients as $recipient ) {
    52 
    53             if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
    54                 continue;
    55             }
    56 
    57             // User data and links.
    58             $ud = get_userdata( $recipient->user_id );
    59 
    60             // Bail if user cannot be found.
    61             if ( empty( $ud ) ) {
    62                 continue;
    63             }
    64 
    65             $message_link  = bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() .'/';
    66             $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    67             $settings_link = bp_core_get_user_domain( $recipient->user_id ) . $settings_slug . '/notifications/';
    68 
    69             // Sender info.
    70             $sender_name   = stripslashes( $sender_name );
    71             $subject       = stripslashes( wp_filter_kses( $subject ) );
    72             $content       = stripslashes( wp_filter_kses( $message ) );
    73 
    74             // Set up and send the message.
    75             $email_to      = $ud->user_email;
    76             $email_subject = bp_get_email_subject( array( 'text' => sprintf( __( 'New message from %s', 'buddypress' ), $sender_name ) ) );
    77 
    78             $email_content = sprintf( __(
    79 '%1$s sent you a new message:
    80 
    81 Subject: %2$s
    82 
    83 "%3$s"
    84 
    85 To view and read your messages please log in and visit: %4$s
    86 
    87 ---------------------
    88 ', 'buddypress' ), $sender_name, $subject, $content, $message_link );
    89 
    90             // Only show the disable notifications line if the settings component is enabled.
    91             if ( bp_is_active( 'settings' ) ) {
    92                 $email_content .= sprintf( __( 'To disable these notifications, please log in and go to: %s', 'buddypress' ), $settings_link );
    93             }
    94 
    95             /**
    96              * Filters the user email that the message notification will be sent to.
    97              *
    98              * @since 1.2.0
    99              *
    100              * @param string  $email_to User email the notification is being sent to.
    101              * @param WP_User $ud       WP_User object of who is receiving the message.
    102              */
    103             $email_to      = apply_filters( 'messages_notification_new_message_to',      $email_to, $ud );
    104 
    105             /**
    106              * Filters the message notification subject that will be sent to user.
    107              *
    108              * @since 1.2.0
    109              *
    110              * @param string  $email_subject Email notification subject text.
    111              * @param string  $sender_name   Name of the person who sent the message.
    112              * @param WP_User $ud            WP_User object of who is receiving the message.
    113              */
    114             $email_subject = apply_filters( 'messages_notification_new_message_subject', $email_subject, $sender_name, $ud );
    115 
    116             /**
    117              * Filters the message notification message that will be sent to user.
    118              *
    119              * @since 1.2.0
    120              *
    121              * @param string  $email_content Email notification message text.
    122              * @param string  $sender_name   Name of the person who sent the message.
    123              * @param string  $subject       Email notification subject text.
    124              * @param string  $content       Content of the message.
    125              * @param string  $message_link  URL permalink for the message.
    126              * @param string  $settings_link URL permalink for the user's notification settings area.
    127              * @param WP_User $ud            WP_User object of who is receiving the message.
    128              */
    129             $email_content = apply_filters( 'messages_notification_new_message_message', $email_content, $sender_name, $subject, $content, $message_link, $settings_link, $ud );
    130 
    131             wp_mail( $email_to, $email_subject, $email_content );
    132         }
     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( $message ),
     64                'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/' ),
     65                'sender.name' => $sender_name,
     66                'usersubject' => sanitize_text_field( $subject ),
     67            ),
     68        );
     69        bp_send_email( 'messages-unread', $ud, $args );
    13370    }
    13471
     
    13774     *
    13875     * @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.
    13978     *
    14079     * @param array  $recipients    User IDs of recipients.
    141      * @param string $email_subject Email notification subject text.
    142      * @param string $email_content Email notification message text.
    143      * @param array  $$args         Array of originally provided arguments.
     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.
    14483     */
    145     do_action( 'bp_messages_sent_notification_email', $recipients, $email_subject, $email_content, $args );
     84    do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
    14685}
    14786add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
Note: See TracChangeset for help on using the changeset viewer.