BP_Messages_Thread::populate( int $thread_id, string $order = 'ASC', array $args = array() )

Populate method.


Description Description

Used in constructor.


Parameters Parameters

$thread_id

(Required) The message thread ID.

$order

(Optional) The order to sort the messages. Either 'ASC' or 'DESC'.

Default value: 'ASC'

$args

(Optional) Array of arguments.

  • 'update_meta_cache'
    (bool) Whether to pre-fetch metadata for queried message items. Default: true.

Default value: array()


Top ↑

Return Return

(bool) False on failure.


Top ↑

Source Source

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

	public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) {

		if ( 'ASC' !== $order && 'DESC' !== $order ) {
			$order = 'ASC';
		}

		$user_id =
			bp_displayed_user_id() ?
			bp_displayed_user_id() :
			bp_loggedin_user_id();

		// Merge $args with our defaults.
		$r = wp_parse_args( $args, array(
			'user_id'           => $user_id,
			'update_meta_cache' => true
		) );

		$this->messages_order = $order;
		$this->thread_id      = (int) $thread_id;

		// Get messages for thread.
		$this->messages = self::get_messages( $this->thread_id );

		if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) {
			return false;
		}

		// Flip if order is DESC.
		if ( 'DESC' === $order ) {
			$this->messages = array_reverse( $this->messages );
		}

		$last_message_index         = count( $this->messages ) - 1;
		$this->last_message_id      = $this->messages[ $last_message_index ]->id;
		$this->last_message_date    = $this->messages[ $last_message_index ]->date_sent;
		$this->last_sender_id       = $this->messages[ $last_message_index ]->sender_id;
		$this->last_message_subject = $this->messages[ $last_message_index ]->subject;
		$this->last_message_content = $this->messages[ $last_message_index ]->message;

		foreach ( (array) $this->messages as $key => $message ) {
			$this->sender_ids[ $message->sender_id ] = $message->sender_id;
		}

		// Fetch the recipients.
		$this->recipients = $this->get_recipients();

		// Get the unread count for the logged in user.
		if ( isset( $this->recipients[ $r['user_id'] ] ) ) {
			$this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count;
		}

		// Grab all message meta.
		if ( true === (bool) $r['update_meta_cache'] ) {
			bp_messages_update_meta_cache( wp_list_pluck( $this->messages, 'id' ) );
		}

		/**
		 * Fires after a BP_Messages_Thread object has been populated.
		 *
		 * @since 2.2.0
		 *
		 * @param BP_Messages_Thread $this Message thread object.
		 */
		do_action( 'bp_messages_thread_post_populate', $this );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.