BP_Messages_Thread::get_current_threads_for_user( array $args = array() )

Get current message threads for a user.

Description

Parameters

$args

(Optional) Array of arguments

Default value: array()

Return

(array|bool) Array on success. Boolean false on failure.

Source

File: bp-messages/classes/class-bp-messages-thread.php

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
public static function get_current_threads_for_user( $args = array() ) {
    global $wpdb;
 
    $bp = buddypress();
 
    // Backward compatibility with old method of passing arguments.
    if ( ! is_array( $args ) || func_num_args() > 1 ) {
        _deprecated_argument( __METHOD__, '2.2.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddyboss' ), __METHOD__, __FILE__ ) );
 
        $old_args_keys = array(
            0 => 'user_id',
            1 => 'box',
            2 => 'type',
            3 => 'limit',
            4 => 'page',
            5 => 'search_terms',
        );
 
        $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
    }
 
    $r = bp_parse_args( $args, array(
        'user_id'      => false,
        'box'          => 'inbox',
        'type'         => 'all',
        'limit'        => null,
        'page'         => null,
        'search_terms' => '',
        'include'      => false,
        'meta_query'   => array()
    ) );
 
    $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = $having_sql = '';
    $current_user_participants_ids = [];
    $meta_query_sql = array(
        'join'  => '',
        'where' => ''
    );
 
    if ( $r['limit'] && $r['page'] ) {
        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
    }
 
    $r['user_id'] = (int) $r['user_id'];
    $where_sql = '1 = 1';
 
    if ( ! empty( $r['include'] ) ) {
        $user_threads_query = $r['include'];
    } else {
        $user_threads_query = $wpdb->prepare( "
        SELECT DISTINCT(thread_id)
        FROM {$bp->messages->table_name_recipients}
        WHERE user_id = %d
        AND is_deleted = 0
    ", $r['user_id'] );
    }
 
    if ( ! empty( $r['search_terms'] ) ) {
        $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
        $where_sql = $wpdb->prepare( "m.message LIKE %s", $search_terms_like );
 
        $current_user_participants = $wpdb->get_results( $q = $wpdb->prepare( "
            SELECT DISTINCT(r.user_id), u.display_name
            FROM {$bp->messages->table_name_recipients} r
            LEFT JOIN {$wpdb->users} u ON r.user_id = u.ID
            WHERE r.thread_id IN ($user_threads_query) AND
            ( u.display_name LIKE %s OR u.user_login LIKE %s OR u.user_nicename LIKE %s )
        ", $search_terms_like, $search_terms_like, $search_terms_like ) );
 
        $current_user_participants_ids = array_map( 'intval', wp_list_pluck( $current_user_participants, 'user_id' ) );
        $current_user_participants_ids = array_diff( $current_user_participants_ids, [ bp_loggedin_user_id() ] );
 
        if( $current_user_participants_ids ) {
            $user_ids = implode(',', array_unique($current_user_participants_ids));
            $where_sql = $wpdb->prepare( "
                (m.message LIKE %s OR r.user_id IN ({$user_ids}))
            ", $search_terms_like );
        }
    }
 
    $where_sql .= " AND r.thread_id IN ($user_threads_query)";
 
    // Process meta query into SQL.
    $meta_query = self::get_meta_query_sql( $r['meta_query'] );
    if ( ! empty( $meta_query['join'] ) ) {
        $meta_query_sql['join'] = $meta_query['join'];
    }
    if ( ! empty( $meta_query['where'] ) ) {
        $meta_query_sql['where'] = $meta_query['where'];
    }
 
    // Set up SQL array.
    $sql = array();
    $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent, GROUP_CONCAT(DISTINCT r.user_id ORDER BY r.user_id separator \',\' ) as recipient_list';
    $sql['from']   = "FROM {$bp->messages->table_name_recipients} r INNER JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id {$meta_query_sql['join']}";
    $sql['where']  = "WHERE {$where_sql} {$meta_query_sql['where']}";
    $sql['misc']   = "GROUP BY m.thread_id {$having_sql} ORDER BY date_sent DESC {$pag_sql}";
 
    // Get thread IDs.
    $thread_ids = $wpdb->get_results( $qq = implode( ' ', $sql ) );
    // print_r($qq);die();
    if ( empty( $thread_ids ) ) {
        return false;
    }
 
    // Adjust $sql to work for thread total.
    $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )';
    unset( $sql['misc'] );
    $total_threads = $wpdb->get_var( implode( ' ', $sql ) );
 
    // Sort threads by date_sent.
    foreach( (array) $thread_ids as $thread ) {
        $sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent );
    }
 
    arsort( $sorted_threads );
 
    $threads = array();
    foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
        $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
            'update_meta_cache' => false
        ) );
    }
 
    /**
     * Filters the results of the query for a user's message threads.
     *
     * @since BuddyPress 2.2.0
     *
     * @param array $value {
     *     @type array $threads       Array of threads. Passed by reference.
     *     @type int   $total_threads Number of threads found by the query.
     * }
     */
    return apply_filters( 'bp_messages_thread_current_threads', array(
        'threads' => &$threads,
        'total'   => (int) $total_threads
    ) );
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 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.