Changeset 14069
- Timestamp:
- 11/03/2024 05:55:47 PM (5 months ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/bp-messages-template.php
r14061 r14069 19 19 * 20 20 * @since 1.0.0 21 * @since 15.0.0 Added the `$includes` parameter. 21 22 * 22 23 * @global BP_Messages_Box_Template $messages_template The message box template loop class. … … 32 33 * @type string $type Type of messages to return. Values: 'all', 'read', 'unread' 33 34 * Default: 'all' 35 * @type array $includes Filter threads by recipient IDs. 34 36 * @type string $search_terms Terms to which to limit results. Default: 35 37 * the value of $_REQUEST['s']. … … 80 82 'page_arg' => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679. 81 83 'meta_query' => array(), 84 'includes' => array(), 82 85 'recipients_page' => null, 83 86 'recipients_per_page' => null, … … 96 99 * @since 1.1.0 97 100 * 98 * @param bool $ valueWhether or not the message has threads.101 * @param bool $has_threads Whether or not the message has threads. 99 102 * @param BP_Messages_Box_Template $messages_template Current message box template object. 100 103 * @param array $r Array of parsed arguments passed into function. -
trunk/src/bp-messages/classes/class-bp-messages-box-template.php
r14061 r14069 169 169 'max' => false, 170 170 'search_terms' => '', 171 'includes' => array(), 171 172 'meta_query' => array(), 172 173 'recipients_page' => null, … … 201 202 'page' => $this->pag_page, 202 203 'search_terms' => $this->search_terms, 204 'includes' => $r['includes'], 203 205 'meta_query' => $r['meta_query'], 204 206 'recipients_page' => $r['recipients_page'], -
trunk/src/bp-messages/classes/class-bp-messages-rest-controller.php
r14061 r14069 125 125 'type' => $request->get_param( 'type' ), 126 126 'page' => $request->get_param( 'page' ), 127 'includes' => $request->get_param( 'includes' ), 127 128 'per_page' => $request->get_param( 'per_page' ), 128 129 'search_terms' => $request->get_param( 'search' ), … … 1540 1541 1541 1542 $params['box'] = array( 1542 'description' => __( 'Filter th e result by box.', 'buddypress' ),1543 'description' => __( 'Filter threads by the mailbox type.', 'buddypress' ), 1543 1544 'default' => 'inbox', 1544 1545 'type' => 'string', … … 1549 1550 1550 1551 $params['type'] = array( 1551 'description' => __( 'Filter th e result by threadstatus.', 'buddypress' ),1552 'description' => __( 'Filter threads by the status.', 'buddypress' ), 1552 1553 'default' => 'all', 1553 1554 'type' => 'string', … … 1563 1564 'required' => true, 1564 1565 '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', 1565 1574 'validate_callback' => 'rest_validate_request_arg', 1566 1575 ); -
trunk/src/bp-messages/classes/class-bp-messages-thread.php
r14061 r14069 673 673 * 674 674 * @since 1.0.0 675 * @since 15.0.0 Added the `$includes` parameter. 675 676 * 676 677 * @global wpdb $wpdb WordPress database object. … … 685 686 * @type int $limit The number of messages to get. Defaults to null. 686 687 * @type int $page The page number to get. Defaults to null. 688 * @type array $includes Filter threads by recipient IDs. 687 689 * @type string $search_terms The search term to use. Defaults to ''. 688 690 * @type array $meta_query Meta query arguments. See WP_Meta_Query for more details. … … 731 733 'type' => 'all', 732 734 'limit' => null, 735 'includes' => array(), 733 736 'page' => null, 734 737 'recipients_page' => null, … … 741 744 ); 742 745 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 = ''; 744 747 $meta_query_sql = array( 745 748 'join' => '', … … 795 798 $bp = buddypress(); 796 799 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 797 815 // Set up SQL array. 798 816 $sql = array(); 799 817 $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent'; 800 818 $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']}"; 802 820 $sql['misc'] = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}"; 803 821 … … 811 829 $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )'; 812 830 unset( $sql['misc'] ); 831 813 832 $total_threads = $wpdb->get_var( implode( ' ', $sql ) ); 814 833 … … 841 860 * @since 2.2.0 842 861 * 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. 846 865 * } 847 * @param array $r 866 * @param array $r Array of parameters. 848 867 */ 849 868 return apply_filters( -
trunk/tests/phpunit/testcases/messages/test-controller.php
r14037 r14069 72 72 $this->assertCount( 1, $data[0]['messages'] ); 73 73 74 // Check the thread data for the requested user id => `$u1`.75 74 $this->check_thread_data( $this->endpoint->get_thread_object( $data[0]['id'], $u1 ), $data[0] ); 76 75 } … … 143 142 $this->assertCount( 1, $all_data[0]['recipients'] ); 144 143 $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] ); 145 205 } 146 206
Note: See TracChangeset
for help on using the changeset viewer.