Changeset 7995
- Timestamp:
- 02/26/2014 09:48:25 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-messages/bp-messages-classes.php
r7994 r7995 11 11 if ( !defined( 'ABSPATH' ) ) exit; 12 12 13 /** 14 * BuddyPress Message Thread class. 15 * 16 * @since BuddyPress (1.0.0) 17 */ 13 18 class BP_Messages_Thread { 19 /** 20 * The message thread ID. 21 * 22 * @since BuddyPress (1.0.0) 23 * @var int 24 */ 14 25 public $thread_id; 26 27 /** 28 * The current messages. 29 * 30 * @since BuddyPress (1.0.0) 31 * @var object 32 */ 15 33 public $messages; 34 35 /** 36 * The current recipients in the message thread. 37 * 38 * @since BuddyPress (1.0.0) 39 * @var object 40 */ 16 41 public $recipients; 42 43 /** 44 * The user IDs of all messages in the message thread. 45 * 46 * @since BuddyPress (1.2.0) 47 * @var array 48 */ 17 49 public $sender_ids; 18 50 51 /** 52 * The unread count for the logged-in user. 53 * 54 * @since BuddyPress (1.2.0) 55 * @var int 56 */ 19 57 public $unread_count; 20 58 … … 22 60 * The content of the last message in this thread 23 61 * 24 * @since BuddyPress (1.2 )62 * @since BuddyPress (1.2.0) 25 63 * @var string 26 64 */ … … 30 68 * The date of the last message in this thread 31 69 * 32 * @since BuddyPress (1.2 )70 * @since BuddyPress (1.2.0) 33 71 * @var string 34 72 */ … … 38 76 * The ID of the last message in this thread 39 77 * 40 * @since BuddyPress (1.2 )78 * @since BuddyPress (1.2.0) 41 79 * @var int 42 80 */ … … 46 84 * The subject of the last message in this thread 47 85 * 48 * @since BuddyPress (1.2 )86 * @since BuddyPress (1.2.0) 49 87 * @var string 50 88 */ … … 54 92 * The user ID of the author of the last message in this thread 55 93 * 56 * @since BuddyPress (1.2 )94 * @since BuddyPress (1.2.0) 57 95 * @var int 58 96 */ … … 62 100 * Sort order of the messages in this thread (ASC or DESC). 63 101 * 64 * @since BuddyPress (1.5 )102 * @since BuddyPress (1.5.0) 65 103 * @var string 66 104 */ 67 105 public $messages_order; 68 106 107 /** 108 * Constructor. 109 * 110 * @since BuddyPress (1.0.0) 111 * 112 * @param int $thread_id The message thread ID. 113 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 114 */ 69 115 public function __construct( $thread_id = false, $order = 'ASC' ) { 70 if ( $thread_id ) 116 if ( $thread_id ) { 71 117 $this->populate( $thread_id, $order ); 72 } 73 118 } 119 } 120 121 /** 122 * Populate method. 123 * 124 * Used in constructor. 125 * 126 * @since BuddyPress (1.0.0) 127 * 128 * @param int $thread_id The message thread ID. 129 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 130 */ 74 131 public function populate( $thread_id, $order ) { 75 132 global $wpdb, $bp; 76 133 77 if( 'ASC' != $order && 'DESC' != $order ) 134 if( 'ASC' != $order && 'DESC' != $order ) { 78 135 $order= 'ASC'; 136 } 79 137 80 138 $this->messages_order = $order; 81 139 $this->thread_id = $thread_id; 82 140 83 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 ) ) ) 84 return false; 85 86 foreach ( (array) $this->messages as $key => $message ) 141 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 ) ) ) { 142 return false; 143 } 144 145 foreach ( (array) $this->messages as $key => $message ) { 87 146 $this->sender_ids[$message->sender_id] = $message->sender_id; 147 } 88 148 89 149 // Fetch the recipients … … 91 151 92 152 // Get the unread count for the logged in user 93 if ( isset( $this->recipients[bp_loggedin_user_id()] ) ) 153 if ( isset( $this->recipients[bp_loggedin_user_id()] ) ) { 94 154 $this->unread_count = $this->recipients[bp_loggedin_user_id()]->unread_count; 95 } 96 155 } 156 } 157 158 /** 159 * Mark a thread initialized in this class as read. 160 * 161 * @since BuddyPress (1.0.0) 162 * 163 * @see BP_Messages_Thread::mark_as_read() 164 */ 97 165 public function mark_read() { 98 166 BP_Messages_Thread::mark_as_read( $this->thread_id ); 99 167 } 100 168 169 /** 170 * Mark a thread initialized in this class as unread. 171 * 172 * @since BuddyPress (1.0.0) 173 * 174 * @see BP_Messages_Thread::mark_as_unread() 175 */ 101 176 public function mark_unread() { 102 177 BP_Messages_Thread::mark_as_unread( $this->thread_id ); 103 178 } 104 179 180 /** 181 * Returns recipients for a message thread. 182 * 183 * @since BuddyPress (1.0.0) 184 * 185 * @return object 186 */ 105 187 public function get_recipients() { 106 188 global $wpdb, $bp; … … 117 199 /** Static Functions ******************************************************/ 118 200 201 /** 202 * Delete a message thread. 203 * 204 * @since BuddyPress (1.0.0) 205 * 206 * @param int $thread_id The message thread ID 207 * @return bool 208 */ 119 209 public static function delete( $thread_id ) { 120 210 global $wpdb, $bp; … … 138 228 } 139 229 230 /** 231 * Get current message threads for a user. 232 * 233 * @since BuddyPress (1.0.0) 234 * 235 * @param int $user_id The user ID. 236 * @param string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 237 * Defaults to 'inbox'. 238 * @param string $type The type of messages to get. Either 'all' or 'unread' 239 * or 'read'. Defaults to 'all'. 240 * @param int $limit The number of messages to get. Defaults to null. 241 * @param int $page The page number to get. Defaults to null. 242 * @param string $search_terms The search term to use. Defaults to ''. 243 * @return array|bool Array on success. Boolean false on failure. 244 */ 140 245 public static function get_current_threads_for_user( $user_id, $box = 'inbox', $type = 'all', $limit = null, $page = null, $search_terms = '' ) { 141 246 global $wpdb, $bp; 142 247 143 248 $pag_sql = $type_sql = $search_sql = ''; 144 if ( $limit && $page ) 249 if ( $limit && $page ) { 145 250 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 146 147 if ( $type == 'unread' ) 251 } 252 253 if ( $type == 'unread' ) { 148 254 $type_sql = " AND r.unread_count != 0 "; 149 elseif ( $type == 'read' )255 } elseif ( $type == 'read' ) { 150 256 $type_sql = " AND r.unread_count = 0 "; 151 152 if ( !empty( $search_terms ) ) { 257 } 258 259 if ( ! empty( $search_terms ) ) { 153 260 $search_terms = like_escape( esc_sql( $search_terms ) ); 154 261 $search_sql = "AND ( subject LIKE '%%$search_terms%%' OR message LIKE '%%$search_terms%%' )"; … … 163 270 } 164 271 165 if ( empty( $thread_ids ) ) 166 return false; 272 if ( empty( $thread_ids ) ) { 273 return false; 274 } 167 275 168 276 // Sort threads by date_sent 169 foreach( (array) $thread_ids as $thread ) 277 foreach( (array) $thread_ids as $thread ) { 170 278 $sorted_threads[$thread->thread_id] = strtotime( $thread->date_sent ); 279 } 171 280 172 281 arsort( $sorted_threads ); 173 282 174 283 $threads = false; 175 foreach ( (array) $sorted_threads as $thread_id => $date_sent ) 284 foreach ( (array) $sorted_threads as $thread_id => $date_sent ) { 176 285 $threads[] = new BP_Messages_Thread( $thread_id ); 286 } 177 287 178 288 return array( 'threads' => &$threads, 'total' => (int) $total_threads ); 179 289 } 180 290 291 /** 292 * Mark a thread as read. 293 * 294 * @since BuddyPress (1.0.0) 295 * 296 * @param int $thread_id The message thread ID. 297 */ 181 298 public static function mark_as_read( $thread_id ) { 182 299 global $wpdb, $bp; … … 186 303 } 187 304 305 /** 306 * Mark a thread as unread. 307 * 308 * @since BuddyPress (1.0.0) 309 * 310 * @param int $thread_id The message thread ID. 311 */ 188 312 public static function mark_as_unread( $thread_id ) { 189 313 global $wpdb, $bp; … … 193 317 } 194 318 319 /** 320 * Returns the total number of message threads for a user. 321 * 322 * @since BuddyPress (1.0.0) 323 * 324 * @param int $user_id The user ID. 325 * @param string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 326 * Defaults to 'inbox'. 327 * @param string $type The type of messages to get. Either 'all' or 'unread' 328 * or 'read'. Defaults to 'all'. 329 * @return int 330 */ 195 331 public static function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) { 196 332 global $wpdb, $bp; … … 208 344 } 209 345 346 /** 347 * Determine if the logged-in user is a sender of any message in a thread. 348 * 349 * @since BuddyPress (1.0.0) 350 * 351 * @param int $thread_id The message thread ID. 352 * @param bool 353 */ 210 354 public static function user_is_sender( $thread_id ) { 211 355 global $wpdb, $bp; … … 213 357 $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); 214 358 215 if ( !$sender_ids ) 216 return false; 359 if ( ! $sender_ids ) { 360 return false; 361 } 217 362 218 363 return in_array( bp_loggedin_user_id(), $sender_ids ); 219 364 } 220 365 366 /** 367 * Returns the userlink of the last sender in a message thread. 368 * 369 * @since BuddyPress (1.0.0) 370 * 371 * @param int $thread_id The message thread ID. 372 * @return string|bool The user link on success. Boolean false on failure. 373 */ 221 374 public static function get_last_sender( $thread_id ) { 222 375 global $wpdb, $bp; 223 376 224 if ( !$sender_id = $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d GROUP BY sender_id ORDER BY date_sent LIMIT 1", $thread_id ) ) ) 225 return false; 377 if ( ! $sender_id = $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d GROUP BY sender_id ORDER BY date_sent LIMIT 1", $thread_id ) ) ) { 378 return false; 379 } 226 380 227 381 return bp_core_get_userlink( $sender_id, true ); 228 382 } 229 383 384 /** 385 * Gets the inbox message count for a user. 386 * 387 * @since BuddyPress (1.0.0) 388 * 389 * @param int $user_id The user ID. 390 * @return int 391 */ 230 392 public static function get_inbox_count( $user_id = 0 ) { 393 global $wpdb, $bp; 394 395 if ( empty( $user_id ) ) { 396 $user_id = bp_loggedin_user_id(); 397 } 398 399 $sql = $wpdb->prepare( "SELECT SUM(unread_count) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 AND sender_only = 0", $user_id ); 400 $unread_count = $wpdb->get_var( $sql ); 401 402 if ( empty( $unread_count ) || is_wp_error( $unread_count ) ) { 403 return 0; 404 } 405 406 return (int) $unread_count; 407 } 408 409 /** 410 * Checks whether a user is a part of a message thread discussion. 411 * 412 * @since BuddyPress (1.0.0) 413 * 414 * @param int $thread_id The message thread ID. 415 * @param int $user_id The user ID. 416 * @return int The message ID on success. 417 */ 418 public static function check_access( $thread_id, $user_id = 0 ) { 231 419 global $wpdb, $bp; 232 420 … … 234 422 $user_id = bp_loggedin_user_id(); 235 423 236 $sql = $wpdb->prepare( "SELECT SUM(unread_count) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 AND sender_only = 0", $user_id );237 $unread_count = $wpdb->get_var( $sql );238 239 if ( empty( $unread_count ) || is_wp_error( $unread_count ) )240 return 0;241 242 return (int) $unread_count;243 }244 245 public static function check_access( $thread_id, $user_id = 0 ) {246 global $wpdb, $bp;247 248 if ( empty( $user_id ) )249 $user_id = bp_loggedin_user_id();250 251 424 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) ); 252 425 } 253 426 427 /** 428 * Checks whether a message thread exists. 429 * 430 * @since BuddyPress (1.0.0) 431 * 432 * @param int $thread_id The message thread ID. 433 * @return int The message thread ID on success. 434 */ 254 435 public static function is_valid( $thread_id ) { 255 436 global $wpdb, $bp; … … 258 439 } 259 440 441 /** 442 * Returns a string containing all the message recipient userlinks. 443 * 444 * String is comma-delimited. 445 * 446 * If a message thread has more than four users, the returned string is simply 447 * "X Recipients" where "X" is the number of recipients in the message thread. 448 * 449 * @since BuddyPress (1.0.0) 450 * 451 * @param object $recipients Object containing the message recipients. 452 * @return string 453 */ 260 454 public static function get_recipient_links( $recipients ) { 261 455 if ( count( $recipients ) >= 5 ) … … 277 471 } 278 472 473 /** 474 * Upgrade method for the older BP message thread DB table. 475 * 476 * @since BuddyPress (1.2.0) 477 * 478 * @todo We should remove this. No one is going to upgrade from v1.1, right? 479 * @return bool 480 */ 279 481 public static function update_tables() { 280 482 global $wpdb, $bp; … … 285 487 286 488 // Nothing to update, just return true to remove the table 287 if ( empty( $threads ) ) 489 if ( empty( $threads ) ) { 288 490 return true; 491 } 289 492 290 493 foreach( (array) $threads as $thread ) { … … 295 498 296 499 // Add the thread_id to the messages table 297 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) )500 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) 298 501 $errors = true; 299 502 } 300 503 } 301 504 302 if ( $errors ) 303 return false; 505 if ( $errors ) { 506 return false; 507 } 304 508 305 509 return true;
Note: See TracChangeset
for help on using the changeset viewer.