bp_messages_get_avatars( integer $thread_id, integer $user_id )

Get Message thread avatars by thread id.

Description

Parameters

$thread_id

(Required) Message thread id.

$user_id

(Required) user id.

Return

(array)

Source

File: bp-messages/bp-messages-functions.php

934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
function bp_messages_get_avatars( $thread_id, $user_id ) {
    global $wpdb;
 
    if ( empty( $user_id ) ) {
        $user_id = bp_loggedin_user_id();
    }
 
    $avatar_urls      = array();
    $avatars_user_ids = array();
    $thread_messages  = BP_Messages_Thread::get_messages( $thread_id, null, 99999999 );
    $recepients       = BP_Messages_Thread::get_recipients_for_thread( $thread_id );
 
    if ( count( $recepients ) > 2 ) {
        foreach ( $thread_messages as $message ) {
            if ( $message->sender_id !== $user_id ) {
 
                if ( count( $avatars_user_ids ) >= 2 ) {
                    continue;
                }
 
                if ( ! in_array( $message->sender_id, $avatars_user_ids ) ) {
                    $avatars_user_ids[] = $message->sender_id;
                }
            }
        }
    } else {
        unset( $recepients[ $user_id ] );
        $avatars_user_ids[] = current( $recepients )->user_id;
    }
 
    if ( count( $recepients ) > 2 && count( $avatars_user_ids ) < 2 ) {
        unset( $recepients[ $user_id ] );
        if ( count( $avatars_user_ids ) === 0 ) {
            $avatars_user_ids = array_slice( array_keys( $recepients ), 0, 2 );
        } else {
            unset( $recepients[ $avatars_user_ids[0] ] );
            $avatars_user_ids = array_merge( $avatars_user_ids, array_slice( array_keys( $recepients ), 0, 1 ) );
        }
    }
 
    if ( ! empty( $avatars_user_ids ) ) {
        $avatars_user_ids = array_reverse( $avatars_user_ids );
        foreach ( (array) $avatars_user_ids as $avatar_user_id ) {
            $avatar_urls[] = array(
                'url'  => esc_url(
                    bp_core_fetch_avatar(
                        array(
                            'item_id' => $avatar_user_id,
                            'object'  => 'user',
                            'type'    => 'thumb',
                            'width'   => BP_AVATAR_THUMB_WIDTH,
                            'height'  => BP_AVATAR_THUMB_HEIGHT,
                            'html'    => false,
                        )
                    )
                ),
                'name' => esc_attr( bp_core_get_user_displayname( $avatar_user_id ) )
            );
        }
    }
 
 
    $first_message    = end( $thread_messages );
    $first_message_id = ( ! empty( $first_message ) ? $first_message->id : false );
    $group_id         = ( isset( $first_message_id ) ) ? (int) bp_messages_get_meta( $first_message_id, 'group_id', true ) : 0;
    if ( ! empty( $first_message_id ) && ! empty( $group_id ) ) {
        $message_from  = bp_messages_get_meta( $first_message_id, 'message_from', true ); // group
        $message_users = bp_messages_get_meta( $first_message_id, 'group_message_users', true ); // all - individual
        $message_type  = bp_messages_get_meta( $first_message_id, 'group_message_type', true ); // open - private
 
        if ( 'group' === $message_from && 'all' === $message_users && 'open' === $message_type ) {
            if ( bp_is_active( 'groups' ) ) {
                $group_name   = bp_get_group_name( groups_get_group( $group_id ) );
                $group_avatar = array(
                    'url'  => bp_core_fetch_avatar(
                        array(
                            'item_id'    => $group_id,
                            'object'     => 'group',
                            'type'       => 'full',
                            'avatar_dir' => 'group-avatars',
                            'alt'        => sprintf( __( 'Group logo of %s', 'buddyboss' ), $group_name ),
                            'title'      => $group_name,
                            'html'       => false,
                        )
                    ),
                    'name' => $group_name
                );
            } else {
                 
                /**
                *
                * Filters table prefix.
                *
                * @param int $wpdb->base_prefix table prefix
                *
                * @since BuddyBoss 1.4.7
                */
                $prefix                   = apply_filters( 'bp_core_get_table_prefix', $wpdb->base_prefix );
                $groups_table             = $prefix . 'bp_groups';
                $group_name               = $wpdb->get_var( "SELECT `name` FROM `{$groups_table}` WHERE `id` = '{$group_id}';" ); // db call ok; no-cache ok;
                $group_avatar             = buddypress()->plugin_url . 'bp-core/images/mystery-group.png';
                $legacy_group_avatar_name = '-groupavatar-full';
                $legacy_user_avatar_name  = '-avatar2';
 
                if ( ! empty( $group_name ) ) {
                    $directory         = 'group-avatars';
                    $avatar_size       = '-bpfull';
                    $avatar_folder_dir = bp_core_avatar_upload_path() . '/' . $directory . '/' . $group_id;
                    $avatar_folder_url = bp_core_avatar_url() . '/' . $directory . '/' . $group_id;
 
                    $avatar = bp_core_get_group_avatar( $legacy_user_avatar_name, $legacy_group_avatar_name, $avatar_size, $avatar_folder_dir, $avatar_folder_url );
                    if ( '' !== $avatar ) {
                        $group_avatar = array(
                            'url'  => $avatar,
                            'name' => $group_name
                        );
                    }
                }
            }
 
            if ( ! empty( $group_avatar ) ) {
                $avatar_urls = array( $group_avatar );
            }
        }
    }
     
    /**
     *
     * Filters the avatar url array to be applied in message thread.
     *
     * @param array $avatar_urls avatar urls in
     * @param int $thread_id Message thread id
     * @param int $user_id user id
     *
     * @since BuddyBoss 1.4.7
     */
    return apply_filters( 'bp_messages_get_avatars', $avatar_urls, $thread_id, $user_id );
}

Changelog

Changelog
Version Description
BuddyBoss 1.4.7 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.