Ticket #6221: 6221.02.patch
File 6221.02.patch, 12.2 KB (added by , 10 years ago) |
---|
-
src/bp-messages/bp-messages-cache.php
46 46 add_action( 'messages_screen_inbox', 'bp_core_clear_cache' ); 47 47 48 48 /** 49 * Clear unread count cache for each recipient after a message is sent.49 * Clear message cache after a message is saved. 50 50 * 51 51 * @since BuddyPress (2.0.0) 52 52 * 53 53 * @param BP_Messages_Message $message 54 54 */ 55 function bp_messages_clear_unread_count_cache_on_message_save( BP_Messages_Message $message ) { 55 function bp_messages_clear_cache_on_message_save( BP_Messages_Message $message ) { 56 // Delete thread cache 57 wp_cache_delete( $message->thread_id, 'bp_messages_threads' ); 58 59 // Delete unread count for each recipient 56 60 foreach ( (array) $message->recipients as $recipient ) { 57 61 wp_cache_delete( $recipient->user_id, 'bp_messages_unread_count' ); 58 62 } 63 64 // Delete thread recipient cache 65 wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' ); 59 66 } 60 add_action( 'messages_message_after_save', 'bp_messages_clear_ unread_count_cache_on_message_save' );67 add_action( 'messages_message_after_save', 'bp_messages_clear_cache_on_message_save' ); 61 68 62 69 /** 63 * Clear unread count cache for the logged-in user after a messageis deleted.70 * Clear message cache after a message thread is deleted. 64 71 * 65 72 * @since BuddyPress (2.0.0) 66 73 * 67 74 * @param int|array $thread_ids If single thread, the thread ID. Otherwise, an 68 75 * array of thread IDs 69 76 */ 70 function bp_messages_clear_unread_count_cache_on_message_delete( $thread_ids ) { 77 function bp_messages_clear_cache_on_message_delete( $thread_ids ) { 78 // Delete thread and thread recipient cache 79 foreach( (array) $thread_ids as $thread_id ) { 80 wp_cache_delete( $thread_id, 'bp_messages_threads' ); 81 wp_cache_delete( "thread_recipients_{$thread_id}", 'bp_messages' ); 82 } 83 84 // Delete unread count for logged-in user 71 85 wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' ); 72 86 } 73 add_action( 'messages_ before_delete_thread', 'bp_messages_clear_unread_count_cache_on_message_delete' );87 add_action( 'messages_delete_thread', 'bp_messages_clear_cache_on_message_delete' ); 74 88 75 89 /** 76 90 * Invalidate cache for notices. … … 84 98 } 85 99 add_action( 'messages_notice_after_save', 'bp_notices_clear_cache' ); 86 100 add_action( 'messages_notice_before_delete', 'bp_notices_clear_cache' ); 87 88 /**89 * Invalidate thread recipient cache on message update.90 *91 * @since BuddyPress (2.3.0)92 *93 * @param BP_Messages_Message $message Message object.94 */95 function bp_messages_clear_message_thread_recipient_cache_on_message_sent( BP_Messages_Message $message ) {96 wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' );97 }98 add_action( 'messages_message_sent', 'bp_messages_clear_message_thread_recipient_cache_on_message_sent' );99 100 /**101 * Invalidate thread recipient cache on thread deletion.102 *103 * @since BuddyPress (2.3.0)104 *105 * @param int|array $thread_ids IDs of deleted threads.106 */107 function bp_messages_clear_message_thread_recipient_cache_on_thread_delete( $thread_ids ) {108 foreach ( (array) $thread_ids as $thread_id ) {109 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );110 }111 }112 add_action( 'messages_delete_thread', 'bp_messages_clear_message_thread_recipient_cache_on_thread_delete' ); -
src/bp-messages/bp-messages-loader.php
303 303 // Global groups 304 304 wp_cache_add_global_groups( array( 305 305 'bp_messages', 306 'bp_messages_threads', 306 307 'bp_messages_unread_count', 307 308 'message_meta' 308 309 ) ); -
src/bp-messages/classes/class-bp_messages-thread.php
147 147 $this->messages_order = $order; 148 148 $this->thread_id = $thread_id; 149 149 150 $bp = buddypress(); 150 // get messages for thread 151 $this->messages = self::get_messages( $thread_id ); 151 152 152 if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id )) ) {153 if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) { 153 154 return false; 154 155 } 155 156 157 // flip if order is DESC 158 if ( 'DESC' === $order ) { 159 $this->messages = array_reverse( $this->messages ); 160 } 161 156 162 foreach ( (array) $this->messages as $key => $message ) { 157 163 $this->sender_ids[$message->sender_id] = $message->sender_id; 158 164 } … … 207 213 * 208 214 * @since BuddyPress (1.0.0) 209 215 * 216 * @param int $thread_id The thread ID 210 217 * @return array 211 218 */ 212 public function get_recipients( ) {219 public function get_recipients( $thread_id = 0 ) { 213 220 global $wpdb; 214 221 215 $recipients = wp_cache_get( 'thread_recipients_' . $this->thread_id, 'bp_messages' ); 222 if ( empty( $thread_id ) ) { 223 $thread_id = $this->thread_id; 224 } 225 226 $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ); 216 227 if ( false === $recipients ) { 217 228 $bp = buddypress(); 218 229 219 230 $recipients = array(); 220 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $th is->thread_id ) );231 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) ); 221 232 222 233 foreach ( (array) $results as $recipient ) { 223 234 $recipients[ $recipient->user_id ] = $recipient; 224 235 } 225 236 226 wp_cache_set( 'thread_recipients_' . $th is->thread_id, $recipients, 'bp_messages' );237 wp_cache_set( 'thread_recipients_' . $thread_id, $recipients, 'bp_messages' ); 227 238 } 228 239 229 240 /** … … 234 245 * @param array $recipients Array of recipient objects. 235 246 * @param int $thread_id ID of the current thread. 236 247 */ 237 return apply_filters( 'bp_messages_thread_get_recipients', $recipients, $th is->thread_id );248 return apply_filters( 'bp_messages_thread_get_recipients', $recipients, $thread_id ); 238 249 } 239 250 240 251 /** Static Functions ******************************************************/ 241 252 242 253 /** 254 * Get all messages associated with a thread. 255 * 256 * @since BuddyPress (2.3.0) 257 * 258 * @param int $thread_id The message thread ID 259 * @return array 260 */ 261 public static function get_messages( $thread_id = 0 ) { 262 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 263 264 if ( false === $messages ) { 265 global $wpdb; 266 267 $bp = buddypress(); 268 269 // always sort by ASC by default 270 $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ) ); 271 272 wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' ); 273 } 274 275 return $messages; 276 } 277 278 /** 279 * Static method to get message recipients by thread ID. 280 * 281 * @since BuddyPress (2.3.0) 282 * 283 * @param int $thread_id The thread ID 284 * @return array 285 */ 286 public static function get_recipients_for_thread( $thread_id = 0 ) { 287 $thread = new self( false ); 288 return $thread->get_recipients( $thread_id ); 289 } 290 291 /** 243 292 * Mark messages in a thread as deleted or delete all messages in a thread. 244 293 * 245 294 * Note: All messages in a thread are deleted once every recipient in a thread … … 644 693 * 645 694 * @param int $thread_id The message thread ID. 646 695 * @param int $user_id The user ID. 647 * @return int The message ID on success.696 * @return int|null The recorded recipient ID on success, null on failure 648 697 */ 649 698 public static function check_access( $thread_id, $user_id = 0 ) { 650 global $wpdb; 651 652 if ( empty( $user_id ) ) 699 if ( empty( $user_id ) ) { 653 700 $user_id = bp_loggedin_user_id(); 701 } 654 702 655 $ bp = buddypress();703 $recipients = self::get_recipients_for_thread( $thread_id ); 656 704 657 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0 AND user_id = %d", $thread_id, $user_id ) ); 705 if ( isset( $recipients[$user_id] ) && 0 == $recipients[$user_id]->is_deleted ) { 706 return $recipients[$user_id]->id; 707 } else { 708 return null; 709 } 658 710 } 659 711 660 712 /** … … 663 715 * @since BuddyPress (1.0.0) 664 716 * 665 717 * @param int $thread_id The message thread ID. 666 * @return int The message thread ID on success.718 * @return int|null The message thread ID on success, null on failure 667 719 */ 668 720 public static function is_valid( $thread_id = 0 ) { 669 global $wpdb;670 671 721 // Bail if no thread ID is passed 672 722 if ( empty( $thread_id ) ) { 673 723 return false; 674 724 } 675 725 676 $ bp = buddypress();726 $thread = self::get_messages( $thread_id ); 677 727 678 return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) ); 728 if ( ! empty( $thread ) ) { 729 return $thread_id; 730 } else { 731 return null; 732 } 679 733 } 680 734 681 735 /** -
tests/phpunit/testcases/messages/class.bp-messages-thread.php
4 4 * @group BP_Messages_Thread 5 5 */ 6 6 class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 7 8 /** 9 * @group cache 10 */ 11 public function test_construct_cache() { 12 $u1 = $this->factory->user->create(); 13 $u2 = $this->factory->user->create(); 14 15 $t1 = $this->factory->message->create( array( 16 'sender_id' => $u1, 17 'recipients' => array( $u2 ), 18 'subject' => 'Foo', 19 ) ); 20 21 // prime cache 22 new BP_Messages_Thread( $t1 ); 23 24 // Cache should exist 25 $this->assertThat( 26 wp_cache_get( $t1, 'bp_messages_threads' ), 27 $this->logicalNot( $this->equalTo( false ) ), 28 'Message thread cache should exist.' 29 ); 30 } 31 32 /** 33 * @group order 34 */ 35 public function test_construct_order_desc() { 36 $u1 = $this->factory->user->create(); 37 $u2 = $this->factory->user->create(); 38 39 // create thread 40 $t1 = $this->factory->message->create( array( 41 'sender_id' => $u1, 42 'recipients' => array( $u2 ), 43 'subject' => 'Foo', 44 ) ); 45 // save message ID 46 $thread = new BP_Messages_Thread( $t1 ); 47 $m1 = wp_list_pluck( $thread->messages, 'id' ); 48 $m1 = array_pop( $m1 ); 49 50 // create reply 51 $t2 = $this->factory->message->create( array( 52 'thread_id' => $t1, 53 'sender_id' => $u1, 54 'recipients' => array( $u2 ), 55 'content' => 'Bar' 56 ) ); 57 // save message ID 58 $thread = new BP_Messages_Thread( $t1 ); 59 $m2 = wp_list_pluck( $thread->messages, 'id' ); 60 $m2 = array_pop( $m2 ); 61 62 // now get thread by DESC 63 $thread = new BP_Messages_Thread( $t1, 'DESC' ); 64 65 // assert! 66 $this->assertEquals( 67 array( $m2, $m1 ), 68 wp_list_pluck( $thread->messages, 'id' ) 69 ); 70 } 71 7 72 /** 8 73 * @group get_current_threads_for_user 9 74 */ … … 286 351 287 352 $this->set_current_user( $current_user ); 288 353 } 354 355 /** 356 * @group check_access 357 */ 358 public function test_check_access_valid_thread() { 359 $u1 = $this->factory->user->create(); 360 $u2 = $this->factory->user->create(); 361 362 $t1 = $this->factory->message->create( array( 363 'sender_id' => $u1, 364 'recipients' => array( $u2 ), 365 'subject' => 'Foo', 366 ) ); 367 368 // save recipient ID 369 $thread = new BP_Messages_Thread( $t1 ); 370 $r1 = wp_list_pluck( $thread->recipients, 'id' ); 371 $r1 = array_pop( $r1 ); 372 373 $this->assertEquals( $r1, BP_Messages_Thread::check_access( $t1, $u1 ) ); 374 } 375 376 /** 377 * @group check_access 378 */ 379 public function test_check_access_invalid_thread() { 380 $this->assertEquals( null, BP_Messages_Thread::check_access( 999, 1 ) ); 381 } 382 383 /** 384 * @group is_valid 385 */ 386 public function test_is_valid_valid_thread() { 387 $u1 = $this->factory->user->create(); 388 $u2 = $this->factory->user->create(); 389 390 $t1 = $this->factory->message->create( array( 391 'sender_id' => $u1, 392 'recipients' => array( $u2 ), 393 'subject' => 'Foo', 394 ) ); 395 396 $this->assertEquals( $t1, BP_Messages_Thread::is_valid( $t1 ) ); 397 } 398 399 /** 400 * @group is_valid 401 */ 402 public function test_is_valid_invalid_thread() { 403 $this->assertEquals( null, BP_Messages_Thread::is_valid( 999 ) ); 404 } 289 405 }