- Timestamp:
- 04/15/2015 10:44:55 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/classes/class-bp_messages-thread.php
r9752 r9753 148 148 $this->thread_id = $thread_id; 149 149 150 $bp = buddypress(); 151 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 ) ) ) { 150 // get messages for thread 151 $this->messages = self::get_messages( $thread_id ); 152 153 if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) { 153 154 return false; 155 } 156 157 // flip if order is DESC 158 if ( 'DESC' === $order ) { 159 $this->messages = array_reverse( $this->messages ); 154 160 } 155 161 … … 245 251 246 252 /** Static Functions ******************************************************/ 253 254 /** 255 * Get all messages associated with a thread. 256 * 257 * @since BuddyPress (2.3.0) 258 * 259 * @param int $thread_id The message thread ID 260 * @return array 261 */ 262 public static function get_messages( $thread_id = 0 ) { 263 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 264 265 if ( false === $messages ) { 266 global $wpdb; 267 268 $bp = buddypress(); 269 270 // always sort by ASC by default 271 $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ) ); 272 273 wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' ); 274 } 275 276 return $messages; 277 } 247 278 248 279 /** … … 664 695 * @param int $thread_id The message thread ID. 665 696 * @param int $user_id The user ID. 666 * @return int The message ID on success.697 * @return int|null The recorded recipient ID on success, null on failure 667 698 */ 668 699 public static function check_access( $thread_id, $user_id = 0 ) { 669 global $wpdb; 670 671 if ( empty( $user_id ) ) 700 if ( empty( $user_id ) ) { 672 701 $user_id = bp_loggedin_user_id(); 673 674 $bp = buddypress(); 675 676 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 ) ); 702 } 703 704 $recipients = self::get_recipients_for_thread( $thread_id ); 705 706 if ( isset( $recipients[$user_id] ) && 0 == $recipients[$user_id]->is_deleted ) { 707 return $recipients[$user_id]->id; 708 } else { 709 return null; 710 } 677 711 } 678 712 … … 683 717 * 684 718 * @param int $thread_id The message thread ID. 685 * @return int The message thread ID on success.719 * @return int|null The message thread ID on success, null on failure 686 720 */ 687 721 public static function is_valid( $thread_id = 0 ) { 688 global $wpdb;689 690 722 // Bail if no thread ID is passed 691 723 if ( empty( $thread_id ) ) { … … 693 725 } 694 726 695 $bp = buddypress(); 696 697 return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) ); 727 $thread = self::get_messages( $thread_id ); 728 729 if ( ! empty( $thread ) ) { 730 return $thread_id; 731 } else { 732 return null; 733 } 698 734 } 699 735
Note: See TracChangeset
for help on using the changeset viewer.