From ceaed255c0be76334b77492ceba828f37cf0fb00 Mon Sep 17 00:00:00 2001 From: Kartik Suthar Date: Thu, 2 Jan 2025 13:22:26 +0530 Subject: [PATCH] WIP - Messages --- src/bp-messages/bp-messages-functions.php | 4 +-- .../classes/class-bp-messages-message.php | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/bp-messages/bp-messages-functions.php b/src/bp-messages/bp-messages-functions.php index 9315a25229..2219e9fbcc 100644 --- a/src/bp-messages/bp-messages-functions.php +++ b/src/bp-messages/bp-messages-functions.php @@ -1288,9 +1288,7 @@ function bp_messages_get_avatars( $thread_id, $user_id ) { $avatar_urls = array( $group_avatar ); } } else { - // Get the last two not deleted messages and message is not from a current user. - $qyery = "SELECT DISTINCT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d AND sender_id != %d AND is_deleted = 0 ORDER BY id DESC LIMIT 2"; - $avatars_user_ids = $wpdb->get_col( $wpdb->prepare( $qyery, $thread_id, $user_id ) ); + $avatars_user_ids = BP_Messages_Message::avatar_recipients( $thread_id, $user_id ); if ( empty( $avatars_user_ids ) ) { $avatars_user_ids = array( $user_id ); diff --git a/src/bp-messages/classes/class-bp-messages-message.php b/src/bp-messages/classes/class-bp-messages-message.php index 782118762a..ebe4f223a0 100644 --- a/src/bp-messages/classes/class-bp-messages-message.php +++ b/src/bp-messages/classes/class-bp-messages-message.php @@ -912,4 +912,35 @@ protected static function convert_orderby_to_order_by_term( $orderby, $order = ' protected static function strip_leading_and( $s ) { return preg_replace( '/^\s*AND\s*/', '', $s ); } + + /** + * Get the avatar recipient IDs for a given thread. + * + * @param int $thread_id Thread id. + * @param int $user_id User id. + * + * @return array + */ + public static function avatar_recipients( $thread_id, $user_id = 0 ) { + global $wpdb, $bp; + static $cache = array(); + + if ( empty( $user_id ) ) { + $user_id = bp_loggedin_user_id(); + } + + $key = 'thread_avatar_' . $thread_id . '_' . $user_id; + + if ( isset( $cache[ $key ] ) ) { + return $cache[ $key ]; + } + + // Get the last two not deleted messages and message is not from a current user. + $query = "SELECT DISTINCT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d AND sender_id != %d AND is_deleted = 0 ORDER BY id DESC LIMIT 2"; + $retval = $wpdb->get_col( $wpdb->prepare( $query, $thread_id, $user_id ) ); + + $cache[ $key ] = $retval; + + return $retval; + } }