[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/classes/ -> class-bp-messages-thread.php (source)

   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          // Backward compatibility with old method of passing arguments.
 449          if ( ! is_array( $args ) || func_num_args() > 1 ) {
 450              _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__ ) );
 451  
 452              $old_args_keys = array(
 453                  0 => 'user_id',
 454                  1 => 'box',
 455                  2 => 'type',
 456                  3 => 'limit',
 457                  4 => 'page',
 458                  5 => 'search_terms',
 459              );
 460  
 461              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 462          }
 463  
 464          $r = bp_parse_args( $args, array(
 465              'user_id'      => false,
 466              'box'          => 'inbox',
 467              'type'         => 'all',
 468              'limit'        => null,
 469              'page'         => null,
 470              'search_terms' => '',
 471              'meta_query'   => array()
 472          ) );
 473  
 474          $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
 475          $meta_query_sql = array(
 476              'join'  => '',
 477              'where' => ''
 478          );
 479  
 480          if ( $r['limit'] && $r['page'] ) {
 481              $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
 482          }
 483  
 484          if ( $r['type'] == 'unread' ) {
 485              $type_sql = " AND r.unread_count != 0 ";
 486          } elseif ( $r['type'] == 'read' ) {
 487              $type_sql = " AND r.unread_count = 0 ";
 488          }
 489  
 490          if ( ! empty( $r['search_terms'] ) ) {
 491              $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
 492              $search_sql        = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like );
 493          }
 494  
 495          $r['user_id'] = (int) $r['user_id'];
 496  
 497          // Default deleted SQL.
 498          $deleted_sql = 'r.is_deleted = 0';
 499  
 500          switch ( $r['box'] ) {
 501              case 'sentbox' :
 502                  $user_id_sql = 'AND ' . $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] );
 503                  $sender_sql  = 'AND m.sender_id = r.user_id';
 504                  break;
 505  
 506              case 'inbox' :
 507                  $user_id_sql = 'AND ' . $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
 508                  $sender_sql  = 'AND r.sender_only = 0';
 509                  break;
 510  
 511              default :
 512                  // Omit user-deleted threads from all other custom message boxes.
 513                  $deleted_sql = $wpdb->prepare( '( r.user_id = %d AND r.is_deleted = 0 )', $r['user_id'] );
 514                  break;
 515          }
 516  
 517          // Process meta query into SQL.
 518          $meta_query = self::get_meta_query_sql( $r['meta_query'] );
 519          if ( ! empty( $meta_query['join'] ) ) {
 520              $meta_query_sql['join'] = $meta_query['join'];
 521          }
 522          if ( ! empty( $meta_query['where'] ) ) {
 523              $meta_query_sql['where'] = $meta_query['where'];
 524          }
 525  
 526          $bp = buddypress();
 527  
 528          // Set up SQL array.
 529          $sql = array();
 530          $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent';
 531          $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']}";
 532          $sql['where']  = "WHERE {$deleted_sql} {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql} {$meta_query_sql['where']}";
 533          $sql['misc']   = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}";
 534  
 535          // Get thread IDs.
 536          $thread_ids = $wpdb->get_results( implode( ' ', $sql ) );
 537          if ( empty( $thread_ids ) ) {
 538              return false;
 539          }
 540  
 541          // Adjust $sql to work for thread total.
 542          $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )';
 543          unset( $sql['misc'] );
 544          $total_threads = $wpdb->get_var( implode( ' ', $sql ) );
 545  
 546          // Sort threads by date_sent.
 547          foreach( (array) $thread_ids as $thread ) {
 548              $sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent );
 549          }
 550  
 551          arsort( $sorted_threads );
 552  
 553          $threads = array();
 554          foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
 555              $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
 556                  'update_meta_cache' => false
 557              ) );
 558          }
 559  
 560          /**
 561           * Filters the results of the query for a user's message threads.
 562           *
 563           * @since 2.2.0
 564           *
 565           * @param array $value {
 566           *     @type array $threads       Array of threads. Passed by reference.
 567           *     @type int   $total_threads Number of threads found by the query.
 568           * }
 569           */
 570          return apply_filters( 'bp_messages_thread_current_threads', array(
 571              'threads' => &$threads,
 572              'total'   => (int) $total_threads
 573          ) );
 574      }
 575  
 576      /**
 577       * Get the SQL for the 'meta_query' param in BP_Messages_Thread::get_current_threads_for_user().
 578       *
 579       * We use WP_Meta_Query to do the heavy lifting of parsing the meta_query array
 580       * and creating the necessary SQL clauses.
 581       *
 582       * @since 2.2.0
 583       *
 584       * @param array $meta_query An array of meta_query filters. See the
 585       *                          documentation for WP_Meta_Query for details.
 586       * @return array $sql_array 'join' and 'where' clauses.
 587       */
 588  	public static function get_meta_query_sql( $meta_query = array() ) {
 589          global $wpdb;
 590  
 591          $sql_array = array(
 592              'join'  => '',
 593              'where' => '',
 594          );
 595  
 596          if ( ! empty( $meta_query ) ) {
 597              $meta_query = new WP_Meta_Query( $meta_query );
 598  
 599              // WP_Meta_Query expects the table name at
 600              // $wpdb->messagemeta.
 601              $wpdb->messagemeta = buddypress()->messages->table_name_meta;
 602  
 603              return $meta_query->get_sql( 'message', 'm', 'id' );
 604          }
 605  
 606          return $sql_array;
 607      }
 608  
 609      /**
 610       * Mark a thread as read.
 611       *
 612       * @since 1.0.0
 613       *
 614       * @param int $thread_id The message thread ID.
 615       *
 616       * @return false|int Number of threads marked as read or false on error.
 617       */
 618  	public static function mark_as_read( $thread_id = 0 ) {
 619          global $wpdb;
 620  
 621          $user_id =
 622              bp_displayed_user_id() ?
 623              bp_displayed_user_id() :
 624              bp_loggedin_user_id();
 625  
 626          $bp     = buddypress();
 627          $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 ) );
 628  
 629          wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
 630          wp_cache_delete( $user_id, 'bp_messages_unread_count' );
 631  
 632          /**
 633           * Fires when messages thread was marked as read.
 634           *
 635           * @since 2.8.0
 636           *
 637           * @param int $thread_id The message thread ID.
 638           */
 639          do_action( 'messages_thread_mark_as_read', $thread_id );
 640  
 641          return $retval;
 642      }
 643  
 644      /**
 645       * Mark a thread as unread.
 646       *
 647       * @since 1.0.0
 648       *
 649       * @param int $thread_id The message thread ID.
 650       *
 651       * @return false|int Number of threads marked as unread or false on error.
 652       */
 653  	public static function mark_as_unread( $thread_id = 0 ) {
 654          global $wpdb;
 655  
 656          $user_id =
 657              bp_displayed_user_id() ?
 658              bp_displayed_user_id() :
 659              bp_loggedin_user_id();
 660  
 661          $bp     = buddypress();
 662          $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 ) );
 663  
 664          wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
 665          wp_cache_delete( $user_id, 'bp_messages_unread_count' );
 666  
 667          /**
 668           * Fires when messages thread was marked as unread.
 669           *
 670           * @since 2.8.0
 671           *
 672           * @param int $thread_id The message thread ID.
 673           */
 674          do_action( 'messages_thread_mark_as_unread', $thread_id );
 675  
 676          return $retval;
 677      }
 678  
 679      /**
 680       * Returns the total number of message threads for a user.
 681       *
 682       * @since 1.0.0
 683       *
 684       * @param int    $user_id The user ID.
 685       * @param string $box     The type of mailbox to get. Either 'inbox' or 'sentbox'.
 686       *                        Defaults to 'inbox'.
 687       * @param string $type    The type of messages to get. Either 'all' or 'unread'.
 688       *                        or 'read'. Defaults to 'all'.
 689       * @return int $value Total thread count for the provided user.
 690       */
 691  	public static function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) {
 692          global $wpdb;
 693  
 694          $exclude_sender = $type_sql = '';
 695          if ( $box !== 'sentbox' ) {
 696              $exclude_sender = 'AND sender_only != 1';
 697          }
 698  
 699          if ( $type === 'unread' ) {
 700              $type_sql = 'AND unread_count != 0';
 701          } elseif ( $type === 'read' ) {
 702              $type_sql = 'AND unread_count = 0';
 703          }
 704  
 705          $bp = buddypress();
 706  
 707          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 ) );
 708      }
 709  
 710      /**
 711       * Determine if the logged-in user is a sender of any message in a thread.
 712       *
 713       * @since 1.0.0
 714       *
 715       * @param int $thread_id The message thread ID.
 716       * @return bool
 717       */
 718  	public static function user_is_sender( $thread_id ) {
 719          global $wpdb;
 720  
 721          $bp = buddypress();
 722  
 723          $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) );
 724  
 725          if ( empty( $sender_ids ) ) {
 726              return false;
 727          }
 728  
 729          return in_array( bp_loggedin_user_id(), $sender_ids );
 730      }
 731  
 732      /**
 733       * Returns the userlink of the last sender in a message thread.
 734       *
 735       * @since 1.0.0
 736       *
 737       * @param int $thread_id The message thread ID.
 738       * @return string|bool The user link on success. Boolean false on failure.
 739       */
 740  	public static function get_last_sender( $thread_id ) {
 741          global $wpdb;
 742  
 743          $bp = buddypress();
 744  
 745          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 ) ) ) {
 746              return false;
 747          }
 748  
 749          return bp_core_get_userlink( $sender_id, true );
 750      }
 751  
 752      /**
 753       * Gets the unread message count for a user.
 754       *
 755       * @since 1.0.0
 756       *
 757       * @param int $user_id The user ID.
 758       * @return int $unread_count Total inbox unread count for user.
 759       */
 760  	public static function get_inbox_count( $user_id = 0 ) {
 761          global $wpdb;
 762  
 763          if ( empty( $user_id ) ) {
 764              $user_id = bp_loggedin_user_id();
 765          }
 766  
 767          $unread_count = wp_cache_get( $user_id, 'bp_messages_unread_count' );
 768  
 769          if ( false === $unread_count ) {
 770              $bp = buddypress();
 771  
 772              $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 ) );
 773  
 774              wp_cache_set( $user_id, $unread_count, 'bp_messages_unread_count' );
 775          }
 776  
 777          /**
 778           * Filters a user's unread message count.
 779           *
 780           * @since 2.2.0
 781           *
 782           * @param int $unread_count Unread message count.
 783           * @param int $user_id      ID of the user.
 784           */
 785          return apply_filters( 'messages_thread_get_inbox_count', (int) $unread_count, $user_id );
 786      }
 787  
 788      /**
 789       * Checks whether a user is a part of a message thread discussion.
 790       *
 791       * @since 1.0.0
 792       *
 793       * @param int $thread_id The message thread ID.
 794       * @param int $user_id   The user ID.
 795       * @return int|null The recorded recipient ID on success, null on failure.
 796       */
 797  	public static function check_access( $thread_id, $user_id = 0 ) {
 798  
 799          if ( empty( $user_id ) ) {
 800              $user_id = bp_loggedin_user_id();
 801          }
 802  
 803          $recipients = self::get_recipients_for_thread( $thread_id );
 804  
 805          if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) {
 806              return $recipients[ $user_id ]->id;
 807          } else {
 808              return null;
 809          }
 810      }
 811  
 812      /**
 813       * Checks whether a message thread exists.
 814       *
 815       * @since 1.0.0
 816       *
 817       * @param int $thread_id The message thread ID.
 818       * @return false|int|null The message thread ID on success, null on failure.
 819       */
 820  	public static function is_valid( $thread_id = 0 ) {
 821  
 822          // Bail if no thread ID is passed.
 823          if ( empty( $thread_id ) ) {
 824              return false;
 825          }
 826  
 827          $thread = self::get_messages( $thread_id );
 828  
 829          if ( ! empty( $thread ) ) {
 830              return $thread_id;
 831          } else {
 832              return null;
 833          }
 834      }
 835  
 836      /**
 837       * Returns a string containing all the message recipient userlinks.
 838       *
 839       * String is comma-delimited.
 840       *
 841       * If a message thread has more than four users, the returned string is simply
 842       * "X Recipients" where "X" is the number of recipients in the message thread.
 843       *
 844       * @since 1.0.0
 845       *
 846       * @param array $recipients Array containing the message recipients (array of objects).
 847       * @return string $value String of message recipent userlinks.
 848       */
 849  	public static function get_recipient_links( $recipients ) {
 850  
 851          if ( count( $recipients ) >= 5 ) {
 852              return sprintf( __( '%s Recipients', 'buddypress' ), number_format_i18n( count( $recipients ) ) );
 853          }
 854  
 855          $recipient_links = array();
 856  
 857          foreach ( (array) $recipients as $recipient ) {
 858              $recipient_link = bp_core_get_userlink( $recipient->user_id );
 859  
 860              if ( empty( $recipient_link ) ) {
 861                  $recipient_link = __( 'Deleted User', 'buddypress' );
 862              }
 863  
 864              $recipient_links[] = $recipient_link;
 865          }
 866  
 867          return implode( ', ', (array) $recipient_links );
 868      }
 869  
 870      /**
 871       * Upgrade method for the older BP message thread DB table.
 872       *
 873       * @since 1.2.0
 874       *
 875       * @todo We should remove this.  No one is going to upgrade from v1.1, right?
 876       * @return bool
 877       */
 878  	public static function update_tables() {
 879          global $wpdb;
 880  
 881          $bp_prefix = bp_core_get_table_prefix();
 882          $errors    = false;
 883          $threads   = $wpdb->get_results( "SELECT * FROM {$bp_prefix}bp_messages_threads" );
 884  
 885          // Nothing to update, just return true to remove the table.
 886          if ( empty( $threads ) ) {
 887              return true;
 888          }
 889  
 890          $bp = buddypress();
 891  
 892          foreach( (array) $threads as $thread ) {
 893              $message_ids = maybe_unserialize( $thread->message_ids );
 894  
 895              if ( ! empty( $message_ids ) ) {
 896                  $message_ids = implode( ',', $message_ids );
 897  
 898                  // Add the thread_id to the messages table.
 899                  if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) {
 900                      $errors = true;
 901                  }
 902              }
 903          }
 904  
 905          return (bool) ! $errors;
 906      }
 907  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1