Skip to:
Content

BuddyPress.org

Changeset 14069


Ignore:
Timestamp:
11/03/2024 05:55:47 PM (5 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

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-template.php

    r14061 r14069  
    1919 *
    2020 * @since 1.0.0
     21 * @since 15.0.0 Added the `$includes` parameter.
    2122 *
    2223 * @global BP_Messages_Box_Template $messages_template The message box template loop class.
     
    3233 *     @type string   $type                Type of messages to return. Values: 'all', 'read', 'unread'
    3334 *                                         Default: 'all'
     35 *     @type array    $includes            Filter threads by recipient IDs.
    3436 *     @type string   $search_terms        Terms to which to limit results. Default:
    3537 *                                         the value of $_REQUEST['s'].
     
    8082            'page_arg'            => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
    8183            'meta_query'          => array(),
     84            'includes'            => array(),
    8285            'recipients_page'     => null,
    8386            'recipients_per_page' => null,
     
    9699     * @since 1.1.0
    97100     *
    98      * @param bool                     $value             Whether or not the message has threads.
     101     * @param bool                     $has_threads       Whether or not the message has threads.
    99102     * @param BP_Messages_Box_Template $messages_template Current message box template object.
    100103     * @param array                    $r                 Array of parsed arguments passed into function.
  • trunk/src/bp-messages/classes/class-bp-messages-box-template.php

    r14061 r14069  
    169169                'max'                 => false,
    170170                'search_terms'        => '',
     171                'includes'            => array(),
    171172                'meta_query'          => array(),
    172173                'recipients_page'     => null,
     
    201202                    'page'                => $this->pag_page,
    202203                    'search_terms'        => $this->search_terms,
     204                    'includes'            => $r['includes'],
    203205                    'meta_query'          => $r['meta_query'],
    204206                    'recipients_page'     => $r['recipients_page'],
  • trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php

    r14061 r14069  
    125125            'type'                => $request->get_param( 'type' ),
    126126            'page'                => $request->get_param( 'page' ),
     127            'includes'            => $request->get_param( 'includes' ),
    127128            'per_page'            => $request->get_param( 'per_page' ),
    128129            'search_terms'        => $request->get_param( 'search' ),
     
    15401541
    15411542        $params['box'] = array(
    1542             'description'       => __( 'Filter the result by box.', 'buddypress' ),
     1543            'description'       => __( 'Filter threads by the mailbox type.', 'buddypress' ),
    15431544            'default'           => 'inbox',
    15441545            'type'              => 'string',
     
    15491550
    15501551        $params['type'] = array(
    1551             'description'       => __( 'Filter the result by thread status.', 'buddypress' ),
     1552            'description'       => __( 'Filter threads by the status.', 'buddypress' ),
    15521553            'default'           => 'all',
    15531554            'type'              => 'string',
     
    15631564            'required'          => true,
    15641565            'sanitize_callback' => 'absint',
     1566            'validate_callback' => 'rest_validate_request_arg',
     1567        );
     1568
     1569        $params['includes'] = array(
     1570            'description'       => __( 'Filter threads by recipient IDs.', 'buddypress' ),
     1571            'default'           => array(),
     1572            'type'              => 'array',
     1573            'sanitize_callback' => 'wp_parse_id_list',
    15651574            'validate_callback' => 'rest_validate_request_arg',
    15661575        );
  • 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(
  • trunk/tests/phpunit/testcases/messages/test-controller.php

    r14037 r14069  
    7272        $this->assertCount( 1, $data[0]['messages'] );
    7373
    74         // Check the thread data for the requested user id => `$u1`.
    7574        $this->check_thread_data( $this->endpoint->get_thread_object( $data[0]['id'], $u1 ), $data[0] );
    7675    }
     
    143142        $this->assertCount( 1, $all_data[0]['recipients'] );
    144143        $this->assertCount( 1, $all_data[1]['recipients'] );
     144    }
     145
     146    /**
     147     * @BP9157
     148     * @group get_items
     149     */
     150    public function test_filter_threads_by_recipients() {
     151        $u1 = static::factory()->user->create();
     152        $u2 = static::factory()->user->create();
     153        $u3 = static::factory()->user->create();
     154
     155        $thread = $this->bp::factory()->message->create_and_get(
     156            array(
     157                'sender_id'  => $u1,
     158                'recipients' => array( $u2 ),
     159                'subject'    => 'First Thread',
     160            )
     161        );
     162
     163        $this->bp::factory()->message->create(
     164            array(
     165                'thread_id'  => $thread->thread_id,
     166                'sender_id'  => $u2,
     167                'recipients' => array( $u1 ),
     168            )
     169        );
     170
     171        $thread2 = $this->bp::factory()->message->create_and_get(
     172            array(
     173                'sender_id'  => $u1,
     174                'recipients' => array( $u3 ),
     175                'subject'    => 'Second Thread',
     176            )
     177        );
     178
     179        $this->bp::factory()->message->create(
     180            array(
     181                'thread_id'  => $thread2->thread_id,
     182                'sender_id'  => $u3,
     183                'recipients' => array( $u1 ),
     184            )
     185        );
     186
     187        $this->bp::set_current_user( $this->user );
     188
     189        $request = new WP_REST_Request( 'GET', $this->endpoint_url );
     190        $request->set_param( 'context', 'view' );
     191        $request->set_param( 'user_id', $u1 );
     192        $request->set_param( 'includes', array( $u3 ) );
     193        $response = $this->server->dispatch( $request );
     194
     195        $this->assertEquals( 200, $response->get_status() );
     196
     197        $data = $response->get_data();
     198
     199        $this->assertNotEmpty( $data );
     200
     201        $a_ids = wp_list_pluck( $data, 'id' );
     202
     203        $this->assertCount( 1, $a_ids );
     204        $this->assertSame( $thread2->thread_id, $a_ids[0] );
    145205    }
    146206
Note: See TracChangeset for help on using the changeset viewer.