Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/22/2016 08:46:36 PM (10 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-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 );
Note: See TracChangeset for help on using the changeset viewer.