Skip to:
Content

BuddyPress.org

Ticket #8426: 8426.patch

File 8426.patch, 1.7 KB (added by oztaser, 4 years ago)
  • src/bp-messages/bp-messages-notifications.php

     
    217217 * @param int $thread_id ID of the thread being marked as read.
    218218 */
    219219function bp_messages_mark_notification_on_mark_thread( $thread_id ) {
    220         $thread_messages = BP_Messages_Thread::get_messages( $thread_id );
     220        // Get unread PM notifications for the user.
     221        $new_pm_notifications = BP_Notifications_Notification::get(
     222                array(
     223                        'user_id'          => bp_loggedin_user_id(),
     224                        'component_name'   => buddypress()->messages->id,
     225                        'component_action' => 'new_message',
     226                        'is_new'           => 1,
     227                )
     228        );
    221229
    222         foreach ( $thread_messages as $thread_message ) {
    223                 bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $thread_message->id, buddypress()->messages->id, 'new_message' );
     230        // Bail if there are no unread PM notifications for the user.
     231        if ( true === empty( $new_pm_notifications ) ) {
     232                return;
    224233        }
     234
     235        $unread_message_ids = wp_list_pluck( $new_pm_notifications, 'item_id' );
     236        $thread_messages    = BP_Messages_Thread::get_messages( $thread_id );
     237
     238        // Get the unread message ids for this thread only.
     239        $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_messages, 'id' ) );
     240
     241        foreach ( $message_ids as $message_id ) {
     242                bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $message_id, buddypress()->messages->id, 'new_message' );
     243        }
    225244}
    226245add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' );
    227246