Ticket #8597: 8597.diff
File 8597.diff, 20.2 KB (added by , 3 years ago) |
---|
-
src/bp-messages/bp-messages-template.php
diff --git src/bp-messages/bp-messages-template.php src/bp-messages/bp-messages-template.php index b5441b294..66f4ec342 100644
defined( 'ABSPATH' ) || exit; 23 23 * 24 24 * @param array|string $args { 25 25 * Array of arguments. All are optional. 26 * @type int $user_id ID of the user whose threads are being loaded. 27 * Default: ID of the logged-in user. 28 * @type string $box Current "box" view. If not provided here, the current 29 * view will be inferred from the URL. 30 * @type int $per_page Number of results to return per page. Default: 10. 31 * @type int $max Max results to return. Default: false. 32 * @type string $type Type of messages to return. Values: 'all', 'read', 'unread' 33 * Default: 'all' 34 * @type string $search_terms Terms to which to limit results. Default: 35 * the value of $_REQUEST['s']. 36 * @type string $page_arg URL argument used for the pagination param. 37 * Default: 'mpage'. 38 * @type array $meta_query Meta query arguments. Only applicable if $box is 39 * not 'notices'. See WP_Meta_Query more details. 26 * @type int $user_id ID of the user whose threads are being loaded. 27 * Default: ID of the logged-in user. 28 * @type string $box Current "box" view. If not provided here, the current 29 * view will be inferred from the URL. 30 * @type int $per_page Number of results to return per page. Default: 10. 31 * @type int $max Max results to return. Default: false. 32 * @type string $type Type of messages to return. Values: 'all', 'read', 'unread' 33 * Default: 'all' 34 * @type string $search_terms Terms to which to limit results. Default: 35 * the value of $_REQUEST['s']. 36 * @type string $page_arg URL argument used for the pagination param. 37 * Default: 'mpage'. 38 * @type array $meta_query Meta query arguments. Only applicable if $box is 39 * not 'notices'. See WP_Meta_Query more details. 40 * @type int|null $recipients_page Page of recipients being requested. Default to null, meaning all. 41 * @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all. 42 * @type int|null $messages_page Page of messages being requested. Default to null, meaning all. 43 * @type int|null $messages_per_page Messages to return per page. Defaults to null, meaning all 40 44 * } 41 45 * @return bool True if there are threads to display, otherwise false. 42 46 */ … … function bp_has_message_threads( $args = array() ) { 46 50 // The default box the user is looking at. 47 51 $current_action = bp_current_action(); 48 52 switch ( $current_action ) { 49 case 'sentbox' 50 case 'notices' 51 case 'inbox' 53 case 'sentbox': 54 case 'notices': 55 case 'inbox': 52 56 $default_box = $current_action; 53 57 break; 54 default 58 default: 55 59 $default_box = 'inbox'; 56 60 break; 57 61 } … … function bp_has_message_threads( $args = array() ) { 67 71 $r = bp_parse_args( 68 72 $args, 69 73 array( 70 'user_id' => $user_id, 71 'box' => $default_box, 72 'per_page' => 10, 73 'max' => false, 74 'type' => 'all', 75 'search_terms' => $search_terms, 76 'page_arg' => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679. 77 'meta_query' => array(), 74 'user_id' => $user_id, 75 'box' => $default_box, 76 'per_page' => 10, 77 'max' => false, 78 'type' => 'all', 79 'search_terms' => $search_terms, 80 'page_arg' => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679. 81 'meta_query' => array(), 82 'recipients_page' => null, 83 'recipients_per_page' => null, 84 'messages_page' => null, 85 'messages_per_page' => null, 78 86 ), 79 87 'has_message_threads' 80 88 ); -
src/bp-messages/classes/class-bp-messages-box-template.php
diff --git src/bp-messages/classes/class-bp-messages-box-template.php src/bp-messages/classes/class-bp-messages-box-template.php index 99e20f77c..fbf175e80 100644
class BP_Messages_Box_Template { 79 79 public $pag_page = 1; 80 80 81 81 /** 82 * The number of items being requested per page.82 * The number of items (threads) being requested per page. 83 83 * 84 84 * @var int 85 85 */ … … class BP_Messages_Box_Template { 111 111 112 112 // Backward compatibility with old method of passing arguments. 113 113 if ( ! is_array( $args ) || count( $function_args ) > 1 ) { 114 _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.', 'buddypress' ), __METHOD__, __FILE__ ) ); 114 _deprecated_argument( 115 __METHOD__, 116 '2.2.0', 117 sprintf( 118 /* translators: 1: the name of the method. 2: the name of the file. */ 119 esc_html__( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), 120 __METHOD__, 121 __FILE__ 122 ) 123 ); 115 124 116 125 $old_args_keys = array( 117 126 0 => 'user_id', … … class BP_Messages_Box_Template { 120 129 3 => 'max', 121 130 4 => 'type', 122 131 5 => 'search_terms', 123 6 => 'page_arg' 132 6 => 'page_arg', 124 133 ); 125 134 126 135 $args = bp_core_parse_args_array( $old_args_keys, $function_args ); … … class BP_Messages_Box_Template { 129 138 $r = bp_parse_args( 130 139 $args, 131 140 array( 132 'page' => 1, 133 'per_page' => 10, 134 'page_arg' => 'mpage', 135 'box' => 'inbox', 136 'type' => 'all', 137 'user_id' => bp_loggedin_user_id(), 138 'max' => false, 139 'search_terms' => '', 140 'meta_query' => array(), 141 'page' => 1, 142 'per_page' => 10, 143 'page_arg' => 'mpage', 144 'box' => 'inbox', 145 'type' => 'all', 146 'user_id' => bp_loggedin_user_id(), 147 'max' => false, 148 'search_terms' => '', 149 'meta_query' => array(), 150 'recipients_page' => null, 151 'recipients_per_page' => null, 152 'messages_page' => null, 153 'messages_per_page' => null, 141 154 ) 142 155 ); 143 156 144 157 $this->pag_arg = sanitize_key( $r['page_arg'] ); 145 $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] 146 $this->pag_num = bp_sanitize_pagination_arg( 'num', 158 $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 159 $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 147 160 $this->user_id = $r['user_id']; 148 161 $this->box = $r['box']; 149 162 $this->type = $r['type']; 150 163 $this->search_terms = $r['search_terms']; 151 164 152 165 if ( 'notices' === $this->box ) { 153 $this->threads = BP_Messages_Notice::get_notices( array( 154 'pag_num' => $this->pag_num, 155 'pag_page' => $this->pag_page 156 ) ); 166 $this->threads = BP_Messages_Notice::get_notices( 167 array( 168 'pag_num' => $this->pag_num, 169 'pag_page' => $this->pag_page, 170 ) 171 ); 157 172 } else { 158 $threads = BP_Messages_Thread::get_current_threads_for_user( array( 159 'user_id' => $this->user_id, 160 'box' => $this->box, 161 'type' => $this->type, 162 'limit' => $this->pag_num, 163 'page' => $this->pag_page, 164 'search_terms' => $this->search_terms, 165 'meta_query' => $r['meta_query'], 166 ) ); 173 $threads = BP_Messages_Thread::get_current_threads_for_user( 174 array( 175 'user_id' => $this->user_id, 176 'box' => $this->box, 177 'type' => $this->type, 178 'limit' => $this->pag_num, 179 'page' => $this->pag_page, 180 'search_terms' => $this->search_terms, 181 'meta_query' => $r['meta_query'], 182 'recipients_page' => $r['recipients_page'], 183 'recipients_per_page' => $r['recipients_per_page'], 184 'messages_page' => $r['messages_page'], 185 'messages_per_page' => $r['messages_per_page'], 186 ) 187 ); 167 188 168 189 $this->threads = isset( $threads['threads'] ) ? $threads['threads'] : array(); 169 190 $this->total_thread_count = isset( $threads['total'] ) ? $threads['total'] : 0; 170 191 } 171 192 172 if ( ! $this->threads ) {193 if ( ! $this->threads ) { 173 194 $this->thread_count = 0; 174 195 $this->total_thread_count = 0; 175 196 } else { … … class BP_Messages_Box_Template { 211 232 $add_args['s'] = $this->search_terms; 212 233 } 213 234 214 $this->pag_links = paginate_links( array( 215 'base' => add_query_arg( $pag_args, $base ), 216 'format' => '', 217 'total' => ceil( (int) $this->total_thread_count / (int) $this->pag_num ), 218 'current' => $this->pag_page, 219 'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ), 220 'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ), 221 'mid_size' => 1, 222 'add_args' => $add_args, 223 ) ); 235 $this->pag_links = paginate_links( 236 array( 237 'base' => add_query_arg( $pag_args, $base ), 238 'format' => '', 239 'total' => ceil( (int) $this->total_thread_count / (int) $this->pag_num ), 240 'current' => $this->pag_page, 241 'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ), 242 'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ), 243 'mid_size' => 1, 244 'add_args' => $add_args, 245 ) 246 ); 224 247 } 225 248 } 226 249 … … class BP_Messages_Box_Template { 307 330 308 331 // Set up the last message data. 309 332 if ( count( $this->thread->messages ) > 1 ) { 310 if ( 'inbox' == $this->box ) {333 if ( 'inbox' === $this->box ) { 311 334 foreach ( (array) $this->thread->messages as $key => $message ) { 312 if ( bp_loggedin_user_id() != $message->sender_id ) {335 if ( bp_loggedin_user_id() !== $message->sender_id ) { 313 336 $last_message_index = $key; 314 337 break; 315 338 } 316 339 } 317 318 } elseif ( 'sentbox' == $this->box ) { 340 } elseif ( 'sentbox' === $this->box ) { 319 341 foreach ( (array) $this->thread->messages as $key => $message ) { 320 if ( bp_loggedin_user_id() == $message->sender_id ) {342 if ( bp_loggedin_user_id() === $message->sender_id ) { 321 343 $last_message_index = $key; 322 344 break; 323 345 } -
src/bp-messages/classes/class-bp-messages-thread.php
diff --git src/bp-messages/classes/class-bp-messages-thread.php src/bp-messages/classes/class-bp-messages-thread.php index e1a7f1891..dacc8cd95 100644
class BP_Messages_Thread { 550 550 * 551 551 * @param array $args { 552 552 * Array of arguments. 553 * @type int $user_id The user ID. 554 * @type string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 555 * Defaults to 'inbox'. 556 * @type string $type The type of messages to get. Either 'all' or 'unread' 557 * or 'read'. Defaults to 'all'. 558 * @type int $limit The number of messages to get. Defaults to null. 559 * @type int $page The page number to get. Defaults to null. 560 * @type string $search_terms The search term to use. Defaults to ''. 561 * @type array $meta_query Meta query arguments. See WP_Meta_Query for more details. 553 * @type int $user_id The user ID. 554 * @type string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 555 * Defaults to 'inbox'. 556 * @type string $type The type of messages to get. Either 'all' or 'unread' 557 * or 'read'. Defaults to 'all'. 558 * @type int $limit The number of messages to get. Defaults to null. 559 * @type int $page The page number to get. Defaults to null. 560 * @type string $search_terms The search term to use. Defaults to ''. 561 * @type array $meta_query Meta query arguments. See WP_Meta_Query for more details. 562 * @type int|null $recipients_page Page of recipients being requested. Default to null, meaning all. 563 * @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all. 564 * @type int|null $messages_page Page of messages being requested. Default to null, meaning all. 565 * @type int|null $messages_per_page Messages to return per page. Defaults to null, meaning all. 562 566 * } 563 * @return array|bool Array on success. Boolean false on failure.567 * @return array|bool Array on success. False on failure. 564 568 */ 565 569 public static function get_current_threads_for_user( $args = array() ) { 566 570 global $wpdb; … … class BP_Messages_Thread { 595 599 $r = bp_parse_args( 596 600 $args, 597 601 array( 598 'user_id' => false, 599 'box' => 'inbox', 600 'type' => 'all', 601 'limit' => null, 602 'page' => null, 603 'search_terms' => '', 604 'meta_query' => array(), 602 'user_id' => false, 603 'box' => 'inbox', 604 'type' => 'all', 605 'limit' => null, 606 'page' => null, 607 'recipients_page' => null, 608 'recipients_per_page' => null, 609 'messages_page' => null, 610 'messages_per_page' => null, 611 'search_terms' => '', 612 'meta_query' => array(), 605 613 ) 606 614 ); 607 615 608 616 $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = ''; 609 617 $meta_query_sql = array( 610 618 'join' => '', 611 'where' => '' 619 'where' => '', 612 620 ); 613 621 614 622 if ( $r['limit'] && $r['page'] ) { … … class BP_Messages_Thread { 632 640 $deleted_sql = 'r.is_deleted = 0'; 633 641 634 642 switch ( $r['box'] ) { 635 case 'sentbox' 643 case 'sentbox': 636 644 $user_id_sql = 'AND ' . $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] ); 637 645 $sender_sql = 'AND m.sender_id = r.user_id'; 638 646 break; 639 647 640 case 'inbox' 648 case 'inbox': 641 649 $user_id_sql = 'AND ' . $wpdb->prepare( 'r.user_id = %d', $r['user_id'] ); 642 650 $sender_sql = 'AND r.sender_only = 0'; 643 651 break; 644 652 645 default 653 default: 646 654 // Omit user-deleted threads from all other custom message boxes. 647 655 $deleted_sql = $wpdb->prepare( '( r.user_id = %d AND r.is_deleted = 0 )', $r['user_id'] ); 648 656 break; … … class BP_Messages_Thread { 686 694 687 695 $threads = array(); 688 696 foreach ( (array) $sorted_threads as $thread_id => $date_sent ) { 689 $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array( 690 'update_meta_cache' => false 691 ) ); 697 $threads[] = new BP_Messages_Thread( 698 $thread_id, 699 'ASC', 700 array( 701 'update_meta_cache' => false, 702 'recipients_page' => $r['recipients_page'], 703 'recipients_per_page' => $r['recipients_per_page'], 704 'page' => $r['messages_page'], 705 'per_page' => $r['messages_per_page'], 706 ) 707 ); 692 708 } 693 709 694 710 /** … … class BP_Messages_Thread { 700 716 * @type array $threads Array of threads. Passed by reference. 701 717 * @type int $total_threads Number of threads found by the query. 702 718 * } 719 * @param array $r Array of paremeters. 703 720 */ 704 return apply_filters( 'bp_messages_thread_current_threads', array( 705 'threads' => &$threads, 706 'total' => (int) $total_threads 707 ) ); 721 return apply_filters( 722 'bp_messages_thread_current_threads', 723 array( 724 'threads' => &$threads, 725 'total' => (int) $total_threads, 726 ), 727 $r 728 ); 708 729 } 709 730 710 731 /** -
tests/phpunit/testcases/messages/class.bp-messages-thread.php
diff --git tests/phpunit/testcases/messages/class.bp-messages-thread.php tests/phpunit/testcases/messages/class.bp-messages-thread.php index 946a87edb..7c32e3858 100644
class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 237 237 $this->assertSame( $expected, $found ); 238 238 } 239 239 240 /** 241 * @group get_current_threads_for_user 242 */ 243 public function test_get_current_threads_setting_per_page_messages_and_recipients() { 244 $u1 = self::factory()->user->create(); 245 $u2 = self::factory()->user->create(); 246 247 // create initial thread 248 $message_1 = self::factory()->message->create_and_get( 249 array( 250 'sender_id' => $u1, 251 'recipients' => array( $u2 ), 252 ) 253 ); 254 255 // create some replies to thread 256 self::factory()->message->create_and_get( 257 array( 258 'thread_id' => $message_1->thread_id, 259 'sender_id' => $u2, 260 'recipients' => array( $u1 ), 261 ) 262 ); 263 264 self::factory()->message->create_and_get( 265 array( 266 'thread_id' => $message_1->thread_id, 267 'sender_id' => $u2, 268 'recipients' => array( $u1 ), 269 ) 270 ); 271 272 self::factory()->message->create_and_get( 273 array( 274 'thread_id' => $message_1->thread_id, 275 'sender_id' => $u1, 276 'recipients' => array( $u2 ), 277 ) 278 ); 279 280 $threads = BP_Messages_Thread::get_current_threads_for_user( 281 array( 'user_id' => $u1 ) 282 )['threads']; 283 284 $this->assertCount( 4, $threads[0]->messages ); 285 $this->assertCount( 2, $threads[0]->recipients ); 286 287 $threads = BP_Messages_Thread::get_current_threads_for_user( 288 array( 289 'user_id' => $u1, 290 'messages_page' => 1, 291 'messages_per_page' => 2, 292 'recipients_page' => 1, 293 'recipients_per_page' => 1, 294 ) 295 )['threads']; 296 297 $this->assertCount( 2, $threads[0]->messages ); 298 $this->assertNotCount( 2, $threads[0]->recipients ); 299 $this->assertCount( 1, $threads[0]->recipients ); 300 } 301 240 302 /** 241 303 * @group get_recipients 242 304 */ -
tests/phpunit/testcases/messages/template.php
diff --git tests/phpunit/testcases/messages/template.php tests/phpunit/testcases/messages/template.php index a3334320b..cd405ffb3 100644
5 5 * @group template 6 6 */ 7 7 class BP_Tests_Messages_Template extends BP_UnitTestCase { 8 8 9 /** 9 10 * @group bp_has_message_threads 10 11 */ … … class BP_Tests_Messages_Template extends BP_UnitTestCase { 359 360 360 361 $_REQUEST = $request; 361 362 } 363 364 /** 365 * @group pagination 366 * @group BP_Messages_Box_Template 367 */ 368 public function test_setting_per_page_messages_and_recipients() { 369 $u1 = self::factory()->user->create(); 370 $u2 = self::factory()->user->create(); 371 372 // create initial thread 373 $message_1 = self::factory()->message->create_and_get( 374 array( 375 'sender_id' => $u1, 376 'recipients' => array( $u2 ), 377 ) 378 ); 379 380 // create some replies to thread 381 self::factory()->message->create_and_get( 382 array( 383 'thread_id' => $message_1->thread_id, 384 'sender_id' => $u2, 385 'recipients' => array( $u1 ), 386 ) 387 ); 388 389 self::factory()->message->create_and_get( 390 array( 391 'thread_id' => $message_1->thread_id, 392 'sender_id' => $u2, 393 'recipients' => array( $u1 ), 394 ) 395 ); 396 397 self::factory()->message->create_and_get( 398 array( 399 'thread_id' => $message_1->thread_id, 400 'sender_id' => $u1, 401 'recipients' => array( $u2 ), 402 ) 403 ); 404 405 $messages_template = new BP_Messages_Box_Template( 406 array( 'user_id' => $u1 ) 407 ); 408 409 $this->assertSame( array( $message_1->thread_id ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); 410 $this->assertCount( 4, $messages_template->threads[0]->messages ); 411 $this->assertCount( 2, $messages_template->threads[0]->recipients ); 412 413 $messages_template = new BP_Messages_Box_Template( 414 array( 415 'user_id' => $u1, 416 'messages_page' => 1, 417 'messages_per_page' => 2, 418 'recipients_page' => 1, 419 'recipients_per_page' => 1, 420 ) 421 ); 422 423 $this->assertCount( 2, $messages_template->threads[0]->messages ); 424 $this->assertNotCount( 2, $messages_template->threads[0]->recipients ); 425 $this->assertCount( 1, $messages_template->threads[0]->recipients ); 426 } 362 427 }