[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Messages Classes. 4 * 5 * @package BuddyPress 6 * @subpackage MessagesClasses 7 * @since 1.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * BuddyPress Message Thread class. 15 * 16 * @since 1.0.0 17 */ 18 class BP_Messages_Thread { 19 20 /** 21 * The message thread ID. 22 * 23 * @since 1.0.0 24 * @var int 25 */ 26 public $thread_id; 27 28 /** 29 * The current messages. 30 * 31 * @since 1.0.0 32 * @var array 33 */ 34 public $messages; 35 36 /** 37 * The current recipients in the message thread. 38 * 39 * @since 1.0.0 40 * @var array 41 */ 42 public $recipients; 43 44 /** 45 * The user IDs of all messages in the message thread. 46 * 47 * @since 1.2.0 48 * @var array 49 */ 50 public $sender_ids; 51 52 /** 53 * The unread count for the logged-in user. 54 * 55 * @since 1.2.0 56 * @var int 57 */ 58 public $unread_count; 59 60 /** 61 * The content of the last message in this thread. 62 * 63 * @since 1.2.0 64 * @var string 65 */ 66 public $last_message_content; 67 68 /** 69 * The date of the last message in this thread. 70 * 71 * @since 1.2.0 72 * @var string 73 */ 74 public $last_message_date; 75 76 /** 77 * The ID of the last message in this thread. 78 * 79 * @since 1.2.0 80 * @var int 81 */ 82 public $last_message_id; 83 84 /** 85 * The subject of the last message in this thread. 86 * 87 * @since 1.2.0 88 * @var string 89 */ 90 public $last_message_subject; 91 92 /** 93 * The user ID of the author of the last message in this thread. 94 * 95 * @since 1.2.0 96 * @var int 97 */ 98 public $last_sender_id; 99 100 /** 101 * Sort order of the messages in this thread (ASC or DESC). 102 * 103 * @since 1.5.0 104 * @var string 105 */ 106 public $messages_order; 107 108 /** 109 * Constructor. 110 * 111 * @since 1.0.0 112 * 113 * @see BP_Messages_Thread::populate() for full description of parameters. 114 * 115 * @param bool $thread_id ID for the message thread. 116 * @param string $order Order to display the messages in. 117 * @param array $args Array of arguments for thread querying. 118 */ 119 public function __construct( $thread_id = false, $order = 'ASC', $args = array() ) { 120 if ( $thread_id ) { 121 $this->populate( $thread_id, $order, $args ); 122 } 123 } 124 125 /** 126 * Populate method. 127 * 128 * Used in constructor. 129 * 130 * @since 1.0.0 131 * 132 * @param int $thread_id The message thread ID. 133 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 134 * @param array $args { 135 * Array of arguments. 136 * @type bool $update_meta_cache Whether to pre-fetch metadata for 137 * queried message items. Default: true. 138 * } 139 * @return bool False on failure. 140 */ 141 public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) { 142 143 if ( 'ASC' !== $order && 'DESC' !== $order ) { 144 $order = 'ASC'; 145 } 146 147 $user_id = 148 bp_displayed_user_id() ? 149 bp_displayed_user_id() : 150 bp_loggedin_user_id(); 151 152 // Merge $args with our defaults. 153 $r = wp_parse_args( $args, array( 154 'user_id' => $user_id, 155 'update_meta_cache' => true 156 ) ); 157 158 $this->messages_order = $order; 159 $this->thread_id = (int) $thread_id; 160 161 // Get messages for thread. 162 $this->messages = self::get_messages( $this->thread_id ); 163 164 if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) { 165 return false; 166 } 167 168 // Flip if order is DESC. 169 if ( 'DESC' === $order ) { 170 $this->messages = array_reverse( $this->messages ); 171 } 172 173 $last_message_index = count( $this->messages ) - 1; 174 $this->last_message_id = $this->messages[ $last_message_index ]->id; 175 $this->last_message_date = $this->messages[ $last_message_index ]->date_sent; 176 $this->last_sender_id = $this->messages[ $last_message_index ]->sender_id; 177 $this->last_message_subject = $this->messages[ $last_message_index ]->subject; 178 $this->last_message_content = $this->messages[ $last_message_index ]->message; 179 180 foreach ( (array) $this->messages as $key => $message ) { 181 $this->sender_ids[ $message->sender_id ] = $message->sender_id; 182 } 183 184 // Fetch the recipients. 185 $this->recipients = $this->get_recipients(); 186 187 // Get the unread count for the logged in user. 188 if ( isset( $this->recipients[ $r['user_id'] ] ) ) { 189 $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count; 190 } 191 192 // Grab all message meta. 193 if ( true === (bool) $r['update_meta_cache'] ) { 194 bp_messages_update_meta_cache( wp_list_pluck( $this->messages, 'id' ) ); 195 } 196 197 /** 198 * Fires after a BP_Messages_Thread object has been populated. 199 * 200 * @since 2.2.0 201 * 202 * @param BP_Messages_Thread $this Message thread object. 203 */ 204 do_action( 'bp_messages_thread_post_populate', $this ); 205 } 206 207 /** 208 * Mark a thread initialized in this class as read. 209 * 210 * @since 1.0.0 211 * 212 * @see BP_Messages_Thread::mark_as_read() 213 */ 214 public function mark_read() { 215 BP_Messages_Thread::mark_as_read( $this->thread_id ); 216 } 217 218 /** 219 * Mark a thread initialized in this class as unread. 220 * 221 * @since 1.0.0 222 * 223 * @see BP_Messages_Thread::mark_as_unread() 224 */ 225 public function mark_unread() { 226 BP_Messages_Thread::mark_as_unread( $this->thread_id ); 227 } 228 229 /** 230 * Returns recipients for a message thread. 231 * 232 * @since 1.0.0 233 * @since 2.3.0 Added $thread_id as a parameter. 234 * 235 * @param int $thread_id The thread ID. 236 * @return array 237 */ 238 public function get_recipients( $thread_id = 0 ) { 239 global $wpdb; 240 241 if ( empty( $thread_id ) ) { 242 $thread_id = $this->thread_id; 243 } 244 245 $thread_id = (int) $thread_id; 246 247 $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ); 248 if ( false === $recipients ) { 249 $bp = buddypress(); 250 251 $recipients = array(); 252 $sql = $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ); 253 $results = $wpdb->get_results( $sql ); 254 255 foreach ( (array) $results as $recipient ) { 256 $recipients[ $recipient->user_id ] = $recipient; 257 } 258 259 wp_cache_set( 'thread_recipients_' . $thread_id, $recipients, 'bp_messages' ); 260 } 261 262 // Cast all items from the messages DB table as integers. 263 foreach ( (array) $recipients as $key => $data ) { 264 $recipients[ $key ] = (object) array_map( 'intval', (array) $data ); 265 } 266 267 /** 268 * Filters the recipients of a message thread. 269 * 270 * @since 2.2.0 271 * 272 * @param array $recipients Array of recipient objects. 273 * @param int $thread_id ID of the current thread. 274 */ 275 return apply_filters( 'bp_messages_thread_get_recipients', $recipients, $thread_id ); 276 } 277 278 /** Static Functions ******************************************************/ 279 280 /** 281 * Get all messages associated with a thread. 282 * 283 * @since 2.3.0 284 * 285 * @param int $thread_id The message thread ID. 286 * 287 * @return object List of messages associated with a thread. 288 */ 289 public static function get_messages( $thread_id = 0 ) { 290 $thread_id = (int) $thread_id; 291 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 292 293 if ( false === $messages ) { 294 global $wpdb; 295 296 $bp = buddypress(); 297 298 // Always sort by ASC by default. 299 $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ) ); 300 301 wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' ); 302 } 303 304 // Integer casting. 305 foreach ( $messages as $key => $data ) { 306 $messages[ $key ]->id = (int) $messages[ $key ]->id; 307 $messages[ $key ]->thread_id = (int) $messages[ $key ]->thread_id; 308 $messages[ $key ]->sender_id = (int) $messages[ $key ]->sender_id; 309 } 310 311 return $messages; 312 } 313 314 /** 315 * Static method to get message recipients by thread ID. 316 * 317 * @since 2.3.0 318 * 319 * @param int $thread_id The thread ID. 320 * @return array 321 */ 322 public static function get_recipients_for_thread( $thread_id = 0 ) { 323 $thread = new self( false ); 324 return $thread->get_recipients( $thread_id ); 325 } 326 327 /** 328 * Mark messages in a thread as deleted or delete all messages in a thread. 329 * 330 * Note: All messages in a thread are deleted once every recipient in a thread 331 * has marked the thread as deleted. 332 * 333 * @since 1.0.0 334 * @since 2.7.0 The $user_id parameter was added. Previously the current user 335 * was always assumed. 336 * 337 * @param int $thread_id The message thread ID. 338 * @param int $user_id The ID of the user in the thread to mark messages as 339 * deleted for. Defaults to the current logged-in user. 340 * 341 * @return bool 342 */ 343 public static function delete( $thread_id = 0, $user_id = 0 ) { 344 global $wpdb; 345 346 $thread_id = (int) $thread_id; 347 $user_id = (int) $user_id; 348 349 if ( empty( $user_id ) ) { 350 $user_id = bp_loggedin_user_id(); 351 } 352 353 /** 354 * Fires before a message thread is marked as deleted. 355 * 356 * @since 2.2.0 357 * @since 2.7.0 The $user_id parameter was added. 358 * 359 * @param int $thread_id ID of the thread being deleted. 360 * @param int $user_id ID of the user that the thread is being deleted for. 361 */ 362 do_action( 'bp_messages_thread_before_mark_delete', $thread_id, $user_id ); 363 364 $bp = buddypress(); 365 366 // Mark messages as deleted 367 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) ); 368 369 // Get the message ids in order to pass to the action. 370 $message_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); 371 372 // Check to see if any more recipients remain for this message. 373 $recipients = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0", $thread_id ) ); 374 375 // No more recipients so delete all messages associated with the thread. 376 if ( empty( $recipients ) ) { 377 378 /** 379 * Fires before an entire message thread is deleted. 380 * 381 * @since 2.2.0 382 * 383 * @param int $thread_id ID of the thread being deleted. 384 * @param array $message_ids IDs of messages being deleted. 385 */ 386 do_action( 'bp_messages_thread_before_delete', $thread_id, $message_ids ); 387 388 // Delete all the messages. 389 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); 390 391 // Do something for each message ID. 392 foreach ( $message_ids as $message_id ) { 393 394 // Delete message meta. 395 bp_messages_delete_meta( $message_id ); 396 397 /** 398 * Fires after a message is deleted. This hook is poorly named. 399 * 400 * @since 1.0.0 401 * 402 * @param int $message_id ID of the message. 403 */ 404 do_action( 'messages_thread_deleted_thread', $message_id ); 405 } 406 407 // Delete all the recipients. 408 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) ); 409 } 410 411 /** 412 * Fires after a message thread is either marked as deleted or deleted. 413 * 414 * @since 2.2.0 415 * @since 2.7.0 The $user_id parameter was added. 416 * 417 * @param int $thread_id ID of the thread being deleted. 418 * @param array $message_ids IDs of messages being deleted. 419 * @param int $user_id ID of the user the threads were deleted for. 420 */ 421 do_action( 'bp_messages_thread_after_delete', $thread_id, $message_ids, $user_id ); 422 423 return true; 424 } 425 426 /** 427 * Get current message threads for a user. 428 * 429 * @since 1.0.0 430 * 431 * @param array $args { 432 * Array of arguments. 433 * @type int $user_id The user ID. 434 * @type string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 435 * Defaults to 'inbox'. 436 * @type string $type The type of messages to get. Either 'all' or 'unread' 437 * or 'read'. Defaults to 'all'. 438 * @type int $limit The number of messages to get. Defaults to null. 439 * @type int $page The page number to get. Defaults to null. 440 * @type string $search_terms The search term to use. Defaults to ''. 441 * @type array $meta_query Meta query arguments. See WP_Meta_Query for more details. 442 * } 443 * @return array|bool Array on success. Boolean false on failure. 444 */ 445 public static function get_current_threads_for_user( $args = array() ) { 446 global $wpdb; 447 448 $function_args = func_get_args(); 449 450 // Backward compatibility with old method of passing arguments. 451 if ( ! is_array( $args ) || count( $function_args ) > 1 ) { 452 _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__ ) ); 453 454 $old_args_keys = array( 455 0 => 'user_id', 456 1 => 'box', 457 2 => 'type', 458 3 => 'limit', 459 4 => 'page', 460 5 => 'search_terms', 461 ); 462 463 $args = bp_core_parse_args_array( $old_args_keys, $function_args ); 464 } 465 466 $r = bp_parse_args( $args, array( 467 'user_id' => false, 468 'box' => 'inbox', 469 'type' => 'all', 470 'limit' => null, 471 'page' => null, 472 'search_terms' => '', 473 'meta_query' => array() 474 ) ); 475 476 $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = ''; 477 $meta_query_sql = array( 478 'join' => '', 479 'where' => '' 480 ); 481 482 if ( $r['limit'] && $r['page'] ) { 483 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) ); 484 } 485 486 if ( $r['type'] == 'unread' ) { 487 $type_sql = " AND r.unread_count != 0 "; 488 } elseif ( $r['type'] == 'read' ) { 489 $type_sql = " AND r.unread_count = 0 "; 490 } 491 492 if ( ! empty( $r['search_terms'] ) ) { 493 $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%'; 494 $search_sql = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like ); 495 } 496 497 $r['user_id'] = (int) $r['user_id']; 498 499 // Default deleted SQL. 500 $deleted_sql = 'r.is_deleted = 0'; 501 502 switch ( $r['box'] ) { 503 case 'sentbox' : 504 $user_id_sql = 'AND ' . $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] ); 505 $sender_sql = 'AND m.sender_id = r.user_id'; 506 break; 507 508 case 'inbox' : 509 $user_id_sql = 'AND ' . $wpdb->prepare( 'r.user_id = %d', $r['user_id'] ); 510 $sender_sql = 'AND r.sender_only = 0'; 511 break; 512 513 default : 514 // Omit user-deleted threads from all other custom message boxes. 515 $deleted_sql = $wpdb->prepare( '( r.user_id = %d AND r.is_deleted = 0 )', $r['user_id'] ); 516 break; 517 } 518 519 // Process meta query into SQL. 520 $meta_query = self::get_meta_query_sql( $r['meta_query'] ); 521 if ( ! empty( $meta_query['join'] ) ) { 522 $meta_query_sql['join'] = $meta_query['join']; 523 } 524 if ( ! empty( $meta_query['where'] ) ) { 525 $meta_query_sql['where'] = $meta_query['where']; 526 } 527 528 $bp = buddypress(); 529 530 // Set up SQL array. 531 $sql = array(); 532 $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent'; 533 $sql['from'] = "FROM {$bp->messages->table_name_recipients} r INNER JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id {$meta_query_sql['join']}"; 534 $sql['where'] = "WHERE {$deleted_sql} {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql} {$meta_query_sql['where']}"; 535 $sql['misc'] = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}"; 536 537 // Get thread IDs. 538 $thread_ids = $wpdb->get_results( implode( ' ', $sql ) ); 539 if ( empty( $thread_ids ) ) { 540 return false; 541 } 542 543 // Adjust $sql to work for thread total. 544 $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )'; 545 unset( $sql['misc'] ); 546 $total_threads = $wpdb->get_var( implode( ' ', $sql ) ); 547 548 // Sort threads by date_sent. 549 foreach( (array) $thread_ids as $thread ) { 550 $sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent ); 551 } 552 553 arsort( $sorted_threads ); 554 555 $threads = array(); 556 foreach ( (array) $sorted_threads as $thread_id => $date_sent ) { 557 $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array( 558 'update_meta_cache' => false 559 ) ); 560 } 561 562 /** 563 * Filters the results of the query for a user's message threads. 564 * 565 * @since 2.2.0 566 * 567 * @param array $value { 568 * @type array $threads Array of threads. Passed by reference. 569 * @type int $total_threads Number of threads found by the query. 570 * } 571 */ 572 return apply_filters( 'bp_messages_thread_current_threads', array( 573 'threads' => &$threads, 574 'total' => (int) $total_threads 575 ) ); 576 } 577 578 /** 579 * Get the SQL for the 'meta_query' param in BP_Messages_Thread::get_current_threads_for_user(). 580 * 581 * We use WP_Meta_Query to do the heavy lifting of parsing the meta_query array 582 * and creating the necessary SQL clauses. 583 * 584 * @since 2.2.0 585 * 586 * @param array $meta_query An array of meta_query filters. See the 587 * documentation for WP_Meta_Query for details. 588 * @return array $sql_array 'join' and 'where' clauses. 589 */ 590 public static function get_meta_query_sql( $meta_query = array() ) { 591 global $wpdb; 592 593 $sql_array = array( 594 'join' => '', 595 'where' => '', 596 ); 597 598 if ( ! empty( $meta_query ) ) { 599 $meta_query = new WP_Meta_Query( $meta_query ); 600 601 // WP_Meta_Query expects the table name at 602 // $wpdb->messagemeta. 603 $wpdb->messagemeta = buddypress()->messages->table_name_meta; 604 605 return $meta_query->get_sql( 'message', 'm', 'id' ); 606 } 607 608 return $sql_array; 609 } 610 611 /** 612 * Mark a thread as read. 613 * 614 * @since 1.0.0 615 * 616 * @param int $thread_id The message thread ID. 617 * 618 * @return false|int Number of threads marked as read or false on error. 619 */ 620 public static function mark_as_read( $thread_id = 0 ) { 621 global $wpdb; 622 623 $user_id = 624 bp_displayed_user_id() ? 625 bp_displayed_user_id() : 626 bp_loggedin_user_id(); 627 628 $bp = buddypress(); 629 $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) ); 630 631 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 632 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); 633 634 /** 635 * Fires when messages thread was marked as read. 636 * 637 * @since 2.8.0 638 * 639 * @param int $thread_id The message thread ID. 640 */ 641 do_action( 'messages_thread_mark_as_read', $thread_id ); 642 643 return $retval; 644 } 645 646 /** 647 * Mark a thread as unread. 648 * 649 * @since 1.0.0 650 * 651 * @param int $thread_id The message thread ID. 652 * 653 * @return false|int Number of threads marked as unread or false on error. 654 */ 655 public static function mark_as_unread( $thread_id = 0 ) { 656 global $wpdb; 657 658 $user_id = 659 bp_displayed_user_id() ? 660 bp_displayed_user_id() : 661 bp_loggedin_user_id(); 662 663 $bp = buddypress(); 664 $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) ); 665 666 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 667 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); 668 669 /** 670 * Fires when messages thread was marked as unread. 671 * 672 * @since 2.8.0 673 * 674 * @param int $thread_id The message thread ID. 675 */ 676 do_action( 'messages_thread_mark_as_unread', $thread_id ); 677 678 return $retval; 679 } 680 681 /** 682 * Returns the total number of message threads for a user. 683 * 684 * @since 1.0.0 685 * 686 * @param int $user_id The user ID. 687 * @param string $box The type of mailbox to get. Either 'inbox' or 'sentbox'. 688 * Defaults to 'inbox'. 689 * @param string $type The type of messages to get. Either 'all' or 'unread'. 690 * or 'read'. Defaults to 'all'. 691 * @return int $value Total thread count for the provided user. 692 */ 693 public static function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) { 694 global $wpdb; 695 696 $exclude_sender = $type_sql = ''; 697 if ( $box !== 'sentbox' ) { 698 $exclude_sender = 'AND sender_only != 1'; 699 } 700 701 if ( $type === 'unread' ) { 702 $type_sql = 'AND unread_count != 0'; 703 } elseif ( $type === 'read' ) { 704 $type_sql = 'AND unread_count = 0'; 705 } 706 707 $bp = buddypress(); 708 709 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 ) ); 710 } 711 712 /** 713 * Determine if the logged-in user is a sender of any message in a thread. 714 * 715 * @since 1.0.0 716 * 717 * @param int $thread_id The message thread ID. 718 * @return bool 719 */ 720 public static function user_is_sender( $thread_id ) { 721 global $wpdb; 722 723 $bp = buddypress(); 724 725 $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); 726 727 if ( empty( $sender_ids ) ) { 728 return false; 729 } 730 731 return in_array( bp_loggedin_user_id(), $sender_ids ); 732 } 733 734 /** 735 * Returns the userlink of the last sender in a message thread. 736 * 737 * @since 1.0.0 738 * 739 * @param int $thread_id The message thread ID. 740 * @return string|bool The user link on success. Boolean false on failure. 741 */ 742 public static function get_last_sender( $thread_id ) { 743 global $wpdb; 744 745 $bp = buddypress(); 746 747 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 ) ) ) { 748 return false; 749 } 750 751 return bp_core_get_userlink( $sender_id, true ); 752 } 753 754 /** 755 * Gets the unread message count for a user. 756 * 757 * @since 1.0.0 758 * 759 * @param int $user_id The user ID. 760 * @return int $unread_count Total inbox unread count for user. 761 */ 762 public static function get_inbox_count( $user_id = 0 ) { 763 global $wpdb; 764 765 if ( empty( $user_id ) ) { 766 $user_id = bp_loggedin_user_id(); 767 } 768 769 $unread_count = wp_cache_get( $user_id, 'bp_messages_unread_count' ); 770 771 if ( false === $unread_count ) { 772 $bp = buddypress(); 773 774 $unread_count = (int) $wpdb->get_var( $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 ) ); 775 776 wp_cache_set( $user_id, $unread_count, 'bp_messages_unread_count' ); 777 } 778 779 /** 780 * Filters a user's unread message count. 781 * 782 * @since 2.2.0 783 * 784 * @param int $unread_count Unread message count. 785 * @param int $user_id ID of the user. 786 */ 787 return apply_filters( 'messages_thread_get_inbox_count', (int) $unread_count, $user_id ); 788 } 789 790 /** 791 * Checks whether a user is a part of a message thread discussion. 792 * 793 * @since 1.0.0 794 * 795 * @param int $thread_id The message thread ID. 796 * @param int $user_id The user ID. 797 * @return int|null The recorded recipient ID on success, null on failure. 798 */ 799 public static function check_access( $thread_id, $user_id = 0 ) { 800 801 if ( empty( $user_id ) ) { 802 $user_id = bp_loggedin_user_id(); 803 } 804 805 $recipients = self::get_recipients_for_thread( $thread_id ); 806 807 if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) { 808 return $recipients[ $user_id ]->id; 809 } else { 810 return null; 811 } 812 } 813 814 /** 815 * Checks whether a message thread exists. 816 * 817 * @since 1.0.0 818 * 819 * @param int $thread_id The message thread ID. 820 * @return false|int|null The message thread ID on success, null on failure. 821 */ 822 public static function is_valid( $thread_id = 0 ) { 823 824 // Bail if no thread ID is passed. 825 if ( empty( $thread_id ) ) { 826 return false; 827 } 828 829 $thread = self::get_messages( $thread_id ); 830 831 if ( ! empty( $thread ) ) { 832 return $thread_id; 833 } else { 834 return null; 835 } 836 } 837 838 /** 839 * Returns a string containing all the message recipient userlinks. 840 * 841 * String is comma-delimited. 842 * 843 * If a message thread has more than four users, the returned string is simply 844 * "X Recipients" where "X" is the number of recipients in the message thread. 845 * 846 * @since 1.0.0 847 * 848 * @param array $recipients Array containing the message recipients (array of objects). 849 * @return string $value String of message recipent userlinks. 850 */ 851 public static function get_recipient_links( $recipients ) { 852 853 if ( count( $recipients ) >= 5 ) { 854 /* translators: %s: number of message recipients */ 855 return sprintf( __( '%s Recipients', 'buddypress' ), number_format_i18n( count( $recipients ) ) ); 856 } 857 858 $recipient_links = array(); 859 860 foreach ( (array) $recipients as $recipient ) { 861 $recipient_link = bp_core_get_userlink( $recipient->user_id ); 862 863 if ( empty( $recipient_link ) ) { 864 $recipient_link = __( 'Deleted User', 'buddypress' ); 865 } 866 867 $recipient_links[] = $recipient_link; 868 } 869 870 return implode( ', ', (array) $recipient_links ); 871 } 872 873 /** 874 * Upgrade method for the older BP message thread DB table. 875 * 876 * @since 1.2.0 877 * 878 * @todo We should remove this. No one is going to upgrade from v1.1, right? 879 * @return bool 880 */ 881 public static function update_tables() { 882 global $wpdb; 883 884 $bp_prefix = bp_core_get_table_prefix(); 885 $errors = false; 886 $threads = $wpdb->get_results( "SELECT * FROM {$bp_prefix}bp_messages_threads" ); 887 888 // Nothing to update, just return true to remove the table. 889 if ( empty( $threads ) ) { 890 return true; 891 } 892 893 $bp = buddypress(); 894 895 foreach( (array) $threads as $thread ) { 896 $message_ids = maybe_unserialize( $thread->message_ids ); 897 898 if ( ! empty( $message_ids ) ) { 899 $message_ids = implode( ',', $message_ids ); 900 901 // Add the thread_id to the messages table. 902 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) { 903 $errors = true; 904 } 905 } 906 } 907 908 return (bool) ! $errors; 909 } 910 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 9 01:01:43 2021 | Cross-referenced by PHPXref 0.7.1 |