Skip to:
Content

BuddyPress.org


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

    r10417 r10479  
    2222 * the user of whom friendship has been requested ($friend_id).
    2323 *
     24 * @since 1.0
     25 *
    2426 * @param int $friendship_id ID of the friendship object.
    2527 * @param int $initiator_id  ID of the user who initiated the request.
    2628 * @param int $friend_id     ID of the request recipient.
    27  * @return bool
    2829 */
    2930function friends_notification_new_request( $friendship_id, $initiator_id, $friend_id ) {
    30 
    31         $initiator_name = bp_core_get_user_displayname( $initiator_id );
    32 
    33         if ( 'no' == bp_get_user_meta( (int) $friend_id, 'notification_friends_friendship_request', true ) )
    34                 return false;
    35 
    36         $ud                = get_userdata( $friend_id );
    37         $all_requests_link = bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/';
    38         $settings_slug     = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    39         $settings_link     = trailingslashit( bp_core_get_user_domain( $friend_id ) .  $settings_slug . '/notifications' );
    40         $initiator_link    = bp_core_get_user_domain( $initiator_id );
    41 
    42         // Set up and send the message.
    43         $to       = $ud->user_email;
    44         $subject  = bp_get_email_subject( array( 'text' => sprintf( __( 'New friendship request from %s', 'buddypress' ), $initiator_name ) ) );
    45         $message  = sprintf( __(
    46 '%1$s wants to add you as a friend.
    47 
    48 To view all of your pending friendship requests: %2$s
    49 
    50 To view %3$s\'s profile: %4$s
    51 
    52 ---------------------
    53 ', 'buddypress' ), $initiator_name, $all_requests_link, $initiator_name, $initiator_link );
    54 
    55         // Only show the disable notifications line if the settings component is enabled.
    56         if ( bp_is_active( 'settings' ) ) {
    57                 $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    58         }
    59 
    60         /**
    61          * Filters the email address for who is getting the friend request.
    62          *
    63          * @since 1.2.0
    64          *
    65          * @param string $to Email address for who is getting the friend request.
    66          */
    67         $to      = apply_filters( 'friends_notification_new_request_to', $to );
    68 
    69         /**
    70          * Filters the subject for the friend request email.
    71          *
    72          * @since 1.2.0
    73          *
    74          * @param string $subject        Subject line to be used in friend request email.
    75          * @param string $initiator_name Name of the person requesting friendship.
    76          */
    77         $subject = apply_filters( 'friends_notification_new_request_subject', $subject, $initiator_name );
    78 
    79         /**
    80          * Filters the message for the friend request email.
    81          *
    82          * @since 1.2.0
    83          *
    84          * @param string $message           Message to be used in friend request email.
    85          * @param string $initiator_name    Name of the person requesting friendship.
    86          * @param string $initiator_link    Profile link of person requesting friendship.
    87          * @param string $all_requests_link User's friends request management link.
    88          * @param string $settings_link     Email recipient's settings management link.
    89          */
    90         $message = apply_filters( 'friends_notification_new_request_message', $message, $initiator_name, $initiator_link, $all_requests_link, $settings_link );
    91 
    92         wp_mail( $to, $subject, $message );
    93 
    94         /**
    95          * Fires after the new friend request email is sent.
    96          *
    97          * @since 1.5.0
    98          *
    99          * @param int    $friend_id     ID of the request recipient.
    100          * @param string $subject       Text for the friend request subject field.
    101          * @param string $message       Text for the friend request message field.
    102          * @param int    $friendship_id ID of the friendship object.
    103          * @param int    $initiator_id  ID of the friendship requester.
    104          */
    105         do_action( 'bp_friends_sent_request_email', $friend_id, $subject, $message, $friendship_id, $initiator_id );
     31        if ( 'no' == bp_get_user_meta( (int) $friend_id, 'notification_friends_friendship_request', true ) ) {
     32                return;
     33        }
     34
     35        $args = array(
     36                'tokens' => array(
     37                        'friend-requests.url' => esc_url( bp_core_get_user_domain( $friend_id ) . bp_get_friends_slug() . '/requests/' ),
     38                        'friend.id'           => $friend_id,
     39                        'friendship.id'       => $friendship_id,
     40                        'initiator.id'        => $initiator_id,
     41                        'initiator.url'       => esc_url( bp_core_get_user_domain( $initiator_id ) ),
     42                        'initiator.name'      => bp_core_get_user_displayname( $initiator_id ),
     43                ),
     44        );
     45        bp_send_email( 'friends-request', $friend_id, $args );
    10646}
    10747add_action( 'friends_friendship_requested', 'friends_notification_new_request', 10, 3 );
     
    11252 * When a friendship request is accepted, an email and a BP notification are
    11353 * sent to the user who requested the friendship ($initiator_id).
     54 *
     55 * @since 1.0
    11456 *
    11557 * @param int $friendship_id ID of the friendship object.
    11658 * @param int $initiator_id  ID of the user who initiated the request.
    11759 * @param int $friend_id     ID of the request recipient.
    118  * @return bool
    11960 */
    12061function friends_notification_accepted_request( $friendship_id, $initiator_id, $friend_id ) {
    121 
    122         $friend_name = bp_core_get_user_displayname( $friend_id );
    123 
    124         if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) )
    125                 return false;
    126 
    127         $ud            = get_userdata( $initiator_id );
    128         $friend_link   = bp_core_get_user_domain( $friend_id );
    129         $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings';
    130         $settings_link = trailingslashit( bp_core_get_user_domain( $initiator_id ) . $settings_slug . '/notifications' );
    131 
    132         // Set up and send the message.
    133         $to       = $ud->user_email;
    134         $subject  = bp_get_email_subject( array( 'text' => sprintf( __( '%s accepted your friendship request', 'buddypress' ), $friend_name ) ) );
    135         $message  = sprintf( __(
    136 '%1$s accepted your friend request.
    137 
    138 To view %2$s\'s profile: %3$s
    139 
    140 ---------------------
    141 ', 'buddypress' ), $friend_name, $friend_name, $friend_link );
    142 
    143         // Only show the disable notifications line if the settings component is enabled.
    144         if ( bp_is_active( 'settings' ) ) {
    145                 $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link );
    146         }
    147 
    148         /**
    149          * Filters the email address for whose friend request got accepted.
    150          *
    151          * @since 1.2.0
    152          *
    153          * @param string $to Email address for whose friend request got accepted.
    154          */
    155         $to      = apply_filters( 'friends_notification_accepted_request_to', $to );
    156 
    157         /**
    158          * Filters the subject for the friend request accepted email.
    159          *
    160          * @since 1.2.0
    161          *
    162          * @param string $subject     Subject line to be used in friend request accepted email.
    163          * @param string $friend_name Name of the person who accepted the friendship request.
    164          */
    165         $subject = apply_filters( 'friends_notification_accepted_request_subject', $subject, $friend_name );
    166 
    167         /**
    168          * Filters the message for the friend request accepted email.
    169          *
    170          * @since 1.2.0
    171          *
    172          * @param string $message       Message to be used in friend request email.
    173          * @param string $friend_name   Name of the person who accepted the friendship request.
    174          * @param string $friend_link   Profile link of person who accepted the friendship request.
    175          * @param string $settings_link Email recipient's settings management link.
    176          */
    177         $message = apply_filters( 'friends_notification_accepted_request_message', $message, $friend_name, $friend_link, $settings_link );
    178 
    179         wp_mail( $to, $subject, $message );
    180 
    181         /**
    182          * Fires after the friend request accepted email is sent.
    183          *
    184          * @since 1.5.0
    185          *
    186          * @param int    $initiator_id  ID of the friendship requester.
    187          * @param string $subject       Text for the friend request subject field.
    188          * @param string $message       Text for the friend request message field.
    189          * @param int    $friendship_id ID of the friendship object.
    190          * @param int    $friend_id     ID of the request recipient.
    191          */
    192         do_action( 'bp_friends_sent_accepted_email', $initiator_id, $subject, $message, $friendship_id, $friend_id );
     62        if ( 'no' == bp_get_user_meta( (int) $initiator_id, 'notification_friends_friendship_accepted', true ) ) {
     63                return;
     64        }
     65
     66        $args = array(
     67                'tokens' => array(
     68                        'friend.id'      => $friend_id,
     69                        'friendship.url' => esc_url( bp_core_get_user_domain( $friend_id ) ),
     70                        'friend.name'    => bp_core_get_user_displayname( $friend_id ),
     71                        'friendship.id'  => $friendship_id,
     72                        'initiator.id'   => $initiator_id,
     73                ),
     74        );
     75        bp_send_email( 'friends-request-accepted', $initiator_id, $args );
    19376}
    19477add_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.