Skip to:
Content

BuddyPress.org

Changeset 7621


Ignore:
Timestamp:
11/30/2013 04:44:58 AM (8 years ago)
Author:
johnjamesjacoby
Message:

Refactor Messages component's approach to Notifications integration:

  • Introduce helper functions for handling the adding/marking/deleting of notifications. Hook these new functions into their respective actions rather than have them hardcoded and interspersed amongst the first-class code.
  • See #5266.
Location:
trunk/bp-messages
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-messages/bp-messages-functions.php

    r7613 r7621  
    115115
    116116    if ( $message->send() ) {
    117 
    118         // Send screen notifications to the recipients
    119         foreach ( (array) $message->recipients as $recipient ) {
    120             bp_core_add_notification( $message->id, $recipient->user_id, 'messages', 'new_message', $message->sender_id );
    121         }
    122 
    123         // Send email notifications to the recipients
    124         messages_notification_new_message( array( 'message_id' => $message->id, 'sender_id' => $message->sender_id, 'subject' => $message->subject, 'content' => $message->message, 'recipients' => $message->recipients, 'thread_id' => $message->thread_id) );
    125 
    126117        do_action_ref_array( 'messages_message_sent', array( &$message ) );
    127118
     
    224215    return BP_Messages_Thread::is_valid( $thread_id );
    225216}
    226 
    227 /**
    228  * Format the BuddyBar/Toolbar notifications for the Messages component
    229  *
    230  * @package BuddyPress
    231  *
    232  * @param string $action The kind of notification being rendered
    233  * @param int $item_id The primary item id
    234  * @param int $secondary_item_id The secondary item id
    235  * @param int $total_items The total number of messaging-related notifications waiting for the user
    236  * @param string $format 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar
    237  */
    238 function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    239 
    240     if ( 'new_message' == $action ) {
    241         $link  = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
    242         $title = __( 'Inbox', 'buddypress' );
    243 
    244         if ( (int) $total_items > 1 ) {
    245             $text   = sprintf( __('You have %d new messages', 'buddypress' ), (int) $total_items );
    246             $filter = 'bp_messages_multiple_new_message_notification';
    247         } else {
    248             if ( !empty( $secondary_item_id ) ) {
    249                 $text = sprintf( __('You have %d new message from %s', 'buddypress' ), (int) $total_items, bp_core_get_user_displayname( $secondary_item_id ) );
    250             } else {
    251                 $text = sprintf( __('You have %d new message',         'buddypress' ), (int) $total_items );
    252             }
    253             $filter = 'bp_messages_single_new_message_notification';
    254         }
    255     }
    256 
    257     if ( 'string' == $format ) {
    258         $return = apply_filters( $filter, '<a href="' . $link . '" title="' . $title . '">' . $text . '</a>', (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    259     } else {
    260         $return = apply_filters( $filter, array(
    261             'text' => $text,
    262             'link' => $link
    263         ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    264     }
    265 
    266     do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    267 
    268     return $return;
    269 }
  • trunk/bp-messages/bp-messages-notifications.php

    r6917 r7621  
    88 */
    99
    10 
    1110// Exit if accessed directly
    1211if ( !defined( 'ABSPATH' ) ) exit;
    1312
     13/** Email *********************************************************************/
     14
     15/**
     16 * Email message recipients to alert them of a new unread private message
     17 *
     18 * @since BuddyPress (1.0)
     19 * @param array $args
     20 */
    1421function messages_notification_new_message( $args = array() ) {
     22
     23    // Cast possible $message object as an array
     24    if ( is_object( $args ) ) {
     25        $args = (array) $args;
     26    }
    1527
    1628    // These should be extracted below
    1729    $recipients    = array();
    1830    $email_subject = $email_content = '';
     31    $sender_id     = 0;
    1932
     33    // Barf
    2034    extract( $args );
    2135
     36    // Get the sender display name
    2237    $sender_name = bp_core_get_user_displayname( $sender_id );
    2338
     
    2540    if ( ! empty( $recipients ) ) {
    2641
    27         foreach( $recipients as $recipient ) {
     42        foreach ( $recipients as $recipient ) {
    2843
    29             if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) )
     44            if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
    3045                continue;
     46            }
    3147
    3248            // User data and links
    33             $ud            = get_userdata( $recipient->user_id );
     49            $ud = get_userdata( $recipient->user_id );
    3450
    3551            // Bail if user cannot be found
    36             if ( empty( $ud ) )
     52            if ( empty( $ud ) ) {
    3753                continue;
     54            }
    3855
    3956            $message_link  = bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() .'/';
     
    7895    do_action( 'bp_messages_sent_notification_email', $recipients, $email_subject, $email_content, $args );
    7996}
     97add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
     98
     99/** Notifications *************************************************************/
     100
     101/**
     102 * Format the BuddyBar/Toolbar notifications for the Messages component
     103 *
     104 * @since BuddyPress (1.0)
     105 * @param string $action The kind of notification being rendered
     106 * @param int $item_id The primary item id
     107 * @param int $secondary_item_id The secondary item id
     108 * @param int $total_items The total number of messaging-related notifications waiting for the user
     109 * @param string $format 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar
     110 */
     111function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     112
     113    if ( 'new_message' === $action ) {
     114        $link  = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
     115        $title = __( 'Inbox', 'buddypress' );
     116
     117        if ( (int) $total_items > 1 ) {
     118            $text   = sprintf( __('You have %d new messages', 'buddypress' ), (int) $total_items );
     119            $filter = 'bp_messages_multiple_new_message_notification';
     120        } else {
     121            if ( !empty( $secondary_item_id ) ) {
     122                $text = sprintf( __('You have %d new message from %s', 'buddypress' ), (int) $total_items, bp_core_get_user_displayname( $secondary_item_id ) );
     123            } else {
     124                $text = sprintf( __('You have %d new message',         'buddypress' ), (int) $total_items );
     125            }
     126            $filter = 'bp_messages_single_new_message_notification';
     127        }
     128    }
     129
     130    if ( 'string' === $format ) {
     131        $return = apply_filters( $filter, '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     132    } else {
     133        $return = apply_filters( $filter, array(
     134            'text' => $text,
     135            'link' => $link
     136        ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     137    }
     138
     139    do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     140
     141    return $return;
     142}
     143
     144/**
     145 * Send notifications to message recipients
     146 *
     147 * @since BuddyPress (1.9.0)
     148 * @param obj $message
     149 */
     150function bp_messages_message_sent_add_notification( $message ) {
     151    if ( bp_is_active( 'notifications' ) && ! empty( $message->recipients ) ) {
     152        foreach ( (array) $message->recipients as $recipient ) {
     153            bp_notifications_add_notification( array(
     154                'user_id'           => $recipient->user_id,
     155                'item_id'           => $message->id,
     156                'secondary_item_id' => $message->sender_id,
     157                'component_name'    => buddypress()->messages->id,
     158                'component_action'  => 'new_message',
     159                'date_notified'     => bp_core_current_time(),
     160                'is_new'            => 1,
     161            ) );
     162        }
     163    }
     164}
     165add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
     166
     167/**
     168 * Mark new message notifications when member views their inbox.
     169 *
     170 * @since BuddyPress (1.9.0)
     171 */
     172function bp_messages_screen_inbox_mark_notifications() {
     173    if ( bp_is_active( 'notifications' ) ) {
     174        bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->messages->id, 'new_message' );
     175    }
     176}
     177add_action( 'messages_screen_inbox', 'bp_messages_screen_inbox_mark_notifications', 10 );
     178
     179/**
     180 * Mark new message notification when member reads a message thread directly.
     181 *
     182 * @since BuddyPress (1.9.0)
     183 */
     184function bp_messages_screen_conversation_mark_notifications() {
     185    if ( bp_is_active( 'notifications' ) ) {
     186        bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) bp_action_variable( 0 ), buddypress()->messages->id, 'new_message' );
     187    }
     188}
     189add_action( 'messages_screen_conversation', 'bp_messages_screen_inbox_mark_notifications', 10 );
  • trunk/bp-messages/bp-messages-template.php

    r7584 r7621  
    185185        wp_redirect( bp_displayed_user_id() );
    186186    } else {
    187         if ( bp_is_current_action( 'inbox' ) ) {
    188             bp_core_mark_notifications_by_type( bp_loggedin_user_id(), $bp->messages->id, 'new_message' );
    189         }
    190 
    191187        if ( bp_is_current_action( 'sentbox' ) ) {
    192188            $box = 'sentbox';
Note: See TracChangeset for help on using the changeset viewer.