Changeset 9925
- Timestamp:
- 06/11/2015 02:18:26 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/classes/class-bp_messages-thread.php
r9862 r9925 16 16 */ 17 17 class BP_Messages_Thread { 18 18 19 /** 19 20 * The message thread ID. … … 138 139 */ 139 140 public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) { 140 global $wpdb; 141 142 if( 'ASC' != $order && 'DESC' != $order ) { 141 142 if ( 'ASC' !== $order && 'DESC' !== $order ) { 143 143 $order = 'ASC'; 144 144 } … … 146 146 // merge $args with our defaults 147 147 $r = wp_parse_args( $args, array( 148 'user_id' => bp_loggedin_user_id(), 148 149 'update_meta_cache' => true 149 150 ) ); 150 151 151 152 $this->messages_order = $order; 152 $this->thread_id = $thread_id;153 $this->thread_id = (int) $thread_id; 153 154 154 155 // get messages for thread 155 $this->messages = self::get_messages( $th read_id );156 $this->messages = self::get_messages( $this->thread_id ); 156 157 157 158 if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) { … … 165 166 166 167 foreach ( (array) $this->messages as $key => $message ) { 167 $this->sender_ids[ $message->sender_id] = $message->sender_id;168 $this->sender_ids[ $message->sender_id ] = $message->sender_id; 168 169 } 169 170 … … 172 173 173 174 // Get the unread count for the logged in user 174 if ( isset( $this->recipients[ bp_loggedin_user_id()] ) ) {175 $this->unread_count = $this->recipients[ bp_loggedin_user_id()]->unread_count;175 if ( isset( $this->recipients[ $r['user_id'] ] ) ) { 176 $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count; 176 177 } 177 178 … … 230 231 } 231 232 233 $thread_id = (int) $thread_id; 234 232 235 $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ); 233 236 if ( false === $recipients ) { … … 235 238 236 239 $recipients = array(); 237 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) ); 240 $sql = $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ); 241 $results = $wpdb->get_results( $sql ); 238 242 239 243 foreach ( (array) $results as $recipient ) { … … 267 271 */ 268 272 public static function get_messages( $thread_id = 0 ) { 269 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 273 $thread_id = (int) $thread_id; 274 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 270 275 271 276 if ( false === $messages ) { … … 309 314 * @return bool 310 315 */ 311 public static function delete( $thread_id ) { 312 global $wpdb; 316 public static function delete( $thread_id = 0 ) { 317 global $wpdb; 318 319 $thread_id = (int) $thread_id; 313 320 314 321 /** … … 353 360 // Do something for each message ID 354 361 foreach ( $message_ids as $message_id ) { 362 355 363 // Delete message meta 356 364 bp_messages_delete_meta( $message_id ); … … 422 430 } 423 431 424 $ defaults =array(432 $r = bp_parse_args( $args, array( 425 433 'user_id' => false, 426 434 'box' => 'inbox', … … 430 438 'search_terms' => '', 431 439 'meta_query' => array() 432 ); 433 $r = wp_parse_args( $args, $defaults ); 440 ) ); 434 441 435 442 $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = ''; … … 495 502 // Sort threads by date_sent 496 503 foreach( (array) $thread_ids as $thread ) { 497 $sorted_threads[ $thread->thread_id] = strtotime( $thread->date_sent );504 $sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent ); 498 505 } 499 506 500 507 arsort( $sorted_threads ); 501 508 502 $threads = false;509 $threads = array(); 503 510 foreach ( (array) $sorted_threads as $thread_id => $date_sent ) { 504 511 $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array( … … 517 524 * } 518 525 */ 519 return apply_filters( 'bp_messages_thread_current_threads', array( 'threads' => &$threads, 'total' => (int) $total_threads ) ); 526 return apply_filters( 'bp_messages_thread_current_threads', array( 527 'threads' => &$threads, 528 'total' => (int) $total_threads 529 ) ); 520 530 } 521 531 … … 561 571 * @param int $thread_id The message thread ID. 562 572 */ 563 public static function mark_as_read( $thread_id ) {573 public static function mark_as_read( $thread_id = 0 ) { 564 574 global $wpdb; 565 575 566 576 $bp = buddypress(); 567 577 $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id ); 568 $wpdb->query( $sql);578 $wpdb->query( $sql ); 569 579 570 580 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); … … 579 589 * @param int $thread_id The message thread ID. 580 590 */ 581 public static function mark_as_unread( $thread_id ) {591 public static function mark_as_unread( $thread_id = 0 ) { 582 592 global $wpdb; 583 593 584 594 $bp = buddypress(); 585 595 $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id ); 586 $wpdb->query( $sql);596 $wpdb->query( $sql ); 587 597 588 598 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); … … 605 615 global $wpdb; 606 616 607 $exclude_sender = ''; 608 if ( $box != 'sentbox' ) 609 $exclude_sender = ' AND sender_only != 1'; 610 611 if ( $type == 'unread' ) 612 $type_sql = " AND unread_count != 0 "; 613 elseif ( $type == 'read' ) 614 $type_sql = " AND unread_count = 0 "; 617 $exclude_sender = $type_sql = ''; 618 if ( $box !== 'sentbox' ) { 619 $exclude_sender = 'AND sender_only != 1'; 620 } 621 622 if ( $type === 'unread' ) { 623 $type_sql = 'AND unread_count != 0'; 624 } elseif ( $type === 'read' ) { 625 $type_sql = 'AND unread_count = 0'; 626 } 615 627 616 628 $bp = buddypress(); 617 629 618 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 {$exclude_sender} {$type_sql}", $user_id ) );630 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 {$exclude_sender} {$type_sql}", $user_id ) ); 619 631 } 620 632 … … 635 647 $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); 636 648 637 if ( ! $sender_ids) {649 if ( empty( $sender_ids ) ) { 638 650 return false; 639 651 } … … 711 723 */ 712 724 public static function check_access( $thread_id, $user_id = 0 ) { 725 713 726 if ( empty( $user_id ) ) { 714 727 $user_id = bp_loggedin_user_id(); … … 717 730 $recipients = self::get_recipients_for_thread( $thread_id ); 718 731 719 if ( isset( $recipients[ $user_id] ) && 0 == $recipients[$user_id]->is_deleted ) {720 return $recipients[ $user_id]->id;732 if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) { 733 return $recipients[ $user_id ]->id; 721 734 } else { 722 735 return null; … … 734 747 */ 735 748 public static function is_valid( $thread_id = 0 ) { 749 736 750 // Bail if no thread ID is passed 737 751 if ( empty( $thread_id ) ) { … … 763 777 */ 764 778 public static function get_recipient_links( $recipients ) { 765 if ( count( $recipients ) >= 5 ) 779 780 if ( count( $recipients ) >= 5 ) { 766 781 return sprintf( __( '%s Recipients', 'buddypress' ), number_format_i18n( count( $recipients ) ) ); 782 } 767 783 768 784 $recipient_links = array(); … … 806 822 $message_ids = maybe_unserialize( $thread->message_ids ); 807 823 808 if ( ! empty( $message_ids ) ) {824 if ( ! empty( $message_ids ) ) { 809 825 $message_ids = implode( ',', $message_ids ); 810 826 811 827 // Add the thread_id to the messages table 812 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) 828 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) { 813 829 $errors = true; 830 } 814 831 } 815 832 } 816 833 817 if ( $errors ) { 818 return false; 819 } 820 821 return true; 834 return (bool) ! $errors; 822 835 } 823 836 }
Note: See TracChangeset
for help on using the changeset viewer.