BP_Messages_Thread::get_current_threads_for_user( array $args = array() )

Get current message threads for a user.

Description

Parameters

$args

(array) (Optional) Array of arguments

Default value: array()

Return

(array|bool) Array on success. Boolean false on failure.

Source

File: bp-messages/classes/class-bp-messages-thread.php

	public static function get_current_threads_for_user( $args = array() ) {
		global $wpdb;

		$bp = buddypress();

		// Backward compatibility with old method of passing arguments.
		if ( ! is_array( $args ) || func_num_args() > 1 ) {
			_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.', 'buddyboss' ), __METHOD__, __FILE__ ) );

			$old_args_keys = array(
				0 => 'user_id',
				1 => 'box',
				2 => 'type',
				3 => 'limit',
				4 => 'page',
				5 => 'search_terms',
			);

			$args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
		}

		$r = bp_parse_args( $args, array(
			'user_id'      => false,
			'box'          => 'inbox',
			'type'         => 'all',
			'limit'        => null,
			'page'         => null,
			'search_terms' => '',
			'include'      => false,
			'meta_query'   => array()
		) );

		$pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = $having_sql = '';
		$current_user_participants_ids = [];
		$meta_query_sql = array(
			'join'  => '',
			'where' => ''
		);

		if ( $r['limit'] && $r['page'] ) {
			$pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
		}

		$r['user_id'] = (int) $r['user_id'];
		$where_sql = '1 = 1';

		if ( ! empty( $r['include'] ) ) {
			$user_threads_query = $r['include'];
		} else {
			$user_threads_query = $wpdb->prepare( "
			SELECT DISTINCT(thread_id)
			FROM {$bp->messages->table_name_recipients}
			WHERE user_id = %d
			AND is_deleted = 0
		", $r['user_id'] );
		}

		if ( ! empty( $r['search_terms'] ) ) {
			$search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
			$where_sql = $wpdb->prepare( "m.message LIKE %s", $search_terms_like );

			$current_user_participants = $wpdb->get_results( $q = $wpdb->prepare( "
				SELECT DISTINCT(r.user_id), u.display_name
				FROM {$bp->messages->table_name_recipients} r
				LEFT JOIN {$wpdb->users} u ON r.user_id = u.ID
				WHERE r.thread_id IN ($user_threads_query) AND
				( u.display_name LIKE %s OR u.user_login LIKE %s OR u.user_nicename LIKE %s )
			", $search_terms_like, $search_terms_like, $search_terms_like ) );

			$current_user_participants_ids = array_map( 'intval', wp_list_pluck( $current_user_participants, 'user_id' ) );
			$current_user_participants_ids = array_diff( $current_user_participants_ids, [ bp_loggedin_user_id() ] );

			if( $current_user_participants_ids ) {
				$user_ids = implode(',', array_unique($current_user_participants_ids));
				$where_sql = $wpdb->prepare( "
					(m.message LIKE %s OR r.user_id IN ({$user_ids}))
				", $search_terms_like );
			}
		}

		$where_sql .= " AND r.thread_id IN ($user_threads_query)";

		// Process meta query into SQL.
		$meta_query = self::get_meta_query_sql( $r['meta_query'] );
		if ( ! empty( $meta_query['join'] ) ) {
			$meta_query_sql['join'] = $meta_query['join'];
		}
		if ( ! empty( $meta_query['where'] ) ) {
			$meta_query_sql['where'] = $meta_query['where'];
		}

		// Set up SQL array.
		$sql = array();
		$sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent, GROUP_CONCAT(DISTINCT r.user_id ORDER BY r.user_id separator \',\' ) as recipient_list';
		$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']}";
		$sql['where']  = "WHERE {$where_sql} {$meta_query_sql['where']}";
		$sql['misc']   = "GROUP BY m.thread_id {$having_sql} ORDER BY date_sent DESC {$pag_sql}";

		// Get thread IDs.
		$thread_ids = $wpdb->get_results( $qq = implode( ' ', $sql ) );
		// print_r($qq);die();
		if ( empty( $thread_ids ) ) {
			return false;
		}

		// Adjust $sql to work for thread total.
		$sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )';
		unset( $sql['misc'] );
		$total_threads = $wpdb->get_var( implode( ' ', $sql ) );

		// Sort threads by date_sent.
		foreach( (array) $thread_ids as $thread ) {
			$sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent );
		}

		arsort( $sorted_threads );

		$threads = array();
		foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
			$threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
				'update_meta_cache' => false
			) );
		}

		/**
		 * Filters the results of the query for a user's message threads.
		 *
		 * @since BuddyPress 2.2.0
		 *
		 * @param array $value {
		 *     @type array $threads       Array of threads. Passed by reference.
		 *     @type int   $total_threads Number of threads found by the query.
		 * }
		 */
		return apply_filters( 'bp_messages_thread_current_threads', array(
			'threads' => &$threads,
			'total'   => (int) $total_threads
		) );
	}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.