Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/03/2024 05:55:47 PM (8 months ago)
Author:
espellcaste
Message:

Messages: filter threads by recipient IDs.

bp_has_message_threads (and the BP REST API) can filter thread(s) messages by recipient IDs. Very useful when trying to identify if a member already messaged another.

Props imath and slaFFik.

See https://github.com/buddypress/BP-REST/issues/455
Closes https://github.com/buddypress/buddypress/pull/394
See #9229 and #9145
Fixes #9157

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r14061 r14069  
    673673     *
    674674     * @since 1.0.0
     675     * @since 15.0.0 Added the `$includes` parameter.
    675676     *
    676677     * @global wpdb $wpdb WordPress database object.
     
    685686     *     @type int      $limit               The number of messages to get. Defaults to null.
    686687     *     @type int      $page                The page number to get. Defaults to null.
     688     *     @type array    $includes            Filter threads by recipient IDs.
    687689     *     @type string   $search_terms        The search term to use. Defaults to ''.
    688690     *     @type array    $meta_query          Meta query arguments. See WP_Meta_Query for more details.
     
    731733                'type'                => 'all',
    732734                'limit'               => null,
     735                'includes'            => array(),
    733736                'page'                => null,
    734737                'recipients_page'     => null,
     
    741744        );
    742745
    743         $pag_sql        = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
     746        $pag_sql        = $type_sql = $search_sql = $user_id_sql = $includes_sql = $sender_sql = '';
    744747        $meta_query_sql = array(
    745748            'join'  => '',
     
    795798        $bp = buddypress();
    796799
     800        if ( ! empty( $r['includes'] ) && is_array( $r['includes'] ) ) {
     801            $includes_ids = array_filter(
     802                wp_parse_id_list( $r['includes'] ),
     803                function ( $recipient_id ) use ( $r ) {
     804                    // Filter out the current user ID, if available.
     805                    return $recipient_id !== $r['user_id'];
     806                }
     807            );
     808
     809            if ( ! empty( $includes_ids ) ) {
     810                $includes_ids = implode( ',', $includes_ids );
     811                $includes_sql = "AND r.thread_id IN (SELECT thread_id FROM {$bp->messages->table_name_recipients} WHERE user_id in ({$includes_ids}))";
     812            }
     813        }
     814
    797815        // Set up SQL array.
    798816        $sql           = array();
    799817        $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent';
    800818        $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']}";
    801         $sql['where']  = "WHERE {$deleted_sql} {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql} {$meta_query_sql['where']}";
     819        $sql['where']  = "WHERE {$deleted_sql} {$user_id_sql} {$sender_sql} {$includes_sql} {$type_sql} {$search_sql} {$meta_query_sql['where']}";
    802820        $sql['misc']   = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}";
    803821
     
    811829        $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )';
    812830        unset( $sql['misc'] );
     831
    813832        $total_threads = $wpdb->get_var( implode( ' ', $sql ) );
    814833
     
    841860         * @since 2.2.0
    842861         *
    843          * @param array $value {
    844          *     @type array $threads       Array of threads. Passed by reference.
    845          *     @type int   $total_threads Number of threads found by the query.
     862         * @param array $results {
     863         *     @type BP_Messages_Thread[] $threads       Array of threads. Passed by reference.
     864         *     @type int                  $total_threads Number of threads found by the query.
    846865         * }
    847          *  @param array $r    Array of parameters.
     866         *  @param array $r Array of parameters.
    848867         */
    849868        return apply_filters(
Note: See TracChangeset for help on using the changeset viewer.