bp_nouveau_ajax_get_user_message_threads()

AJAX get all user message threads.

Description

Source

File: bp-templates/bp-nouveau/includes/messages/ajax.php

function bp_nouveau_ajax_get_user_message_threads() {
	global $messages_template;

	if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
		wp_send_json_error( array(
			'feedback' => __( 'Unauthorized request.', 'buddyboss' ),
			'type'     => 'error'
		) );
	}

	$bp           = buddypress();
	$reset_action = $bp->current_action;

	// Override bp_current_action().
	if ( isset( $_POST['box'] ) ) {
		$bp->current_action = $_POST['box'];
	}

	// Add the message thread filter.
	if ( 'starred' === $bp->current_action ) {
		add_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
	}

	// Simulate the loop.
	if ( ! bp_has_message_threads( bp_ajax_querystring( 'messages' ) ) ) {
		// Remove the bp_current_action() override.
		$bp->current_action = $reset_action;

		wp_send_json_error( array(
			'feedback' => __( 'Sorry, no messages were found.', 'buddyboss' ),
			'type'     => 'info'
		) );
	}

	// remove the message thread filter.
	if ( 'starred' === $bp->current_action ) {
		remove_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
	}

	$threads       = new stdClass;
	$threads->meta = array(
		'total_page' => ceil( (int) $messages_template->total_thread_count / (int) $messages_template->pag_num ),
		'page'       => $messages_template->pag_page,
	);

	$threads->threads = array();
	$i                = 0;

	while ( bp_message_threads() ) : bp_message_thread();
		$last_message_id = (int) $messages_template->thread->last_message_id;

		$threads->threads[ $i ] = array(
			'id'            => bp_get_message_thread_id(),
			'message_id'    => (int) $last_message_id,
			'subject'       => strip_tags( bp_get_message_thread_subject() ),
			'excerpt'       => strip_tags( bp_get_message_thread_excerpt() ),
			'content'       => do_shortcode( bp_get_message_thread_content() ),
			'unread'        => bp_message_thread_has_unread(),
			'sender_name'   => bp_core_get_user_displayname( $messages_template->thread->last_sender_id ),
			'sender_is_you' => $messages_template->thread->last_sender_id == bp_loggedin_user_id(),
			'sender_link'   => bp_core_get_userlink( $messages_template->thread->last_sender_id, false, true ),
			'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
				'item_id' => $messages_template->thread->last_sender_id,
				'object'  => 'user',
				'type'    => 'thumb',
				'width'   => BP_AVATAR_THUMB_WIDTH,
				'height'  => BP_AVATAR_THUMB_HEIGHT,
				'html'    => false,
			) ) ),
			'count'         => bp_get_message_thread_total_count(),
			'date'          => strtotime( bp_get_message_thread_last_post_date_raw() ) * 1000,
			'display_date'  => bp_nouveau_get_message_date( bp_get_message_thread_last_post_date_raw() ),
			'started_date' => date(
				get_option('date_format'),
				strtotime($messages_template->thread->first_message_date)
			)
		);

		if ( is_array( $messages_template->thread->recipients ) ) {
			foreach ( $messages_template->thread->recipients as $recipient ) {
				if ( empty( $recipient->is_deleted ) ) {
					$threads->threads[ $i ]['recipients'][] = array(
						'avatar' => esc_url( bp_core_fetch_avatar( array(
							'item_id' => $recipient->user_id,
							'object'  => 'user',
							'type'    => 'thumb',
							'width'   => BP_AVATAR_THUMB_WIDTH,
							'height'  => BP_AVATAR_THUMB_HEIGHT,
							'html'    => false,
						) ) ),
						'user_link' => bp_core_get_userlink( $recipient->user_id, false, true ),
						'user_name' => bp_core_get_user_displayname( $recipient->user_id ),
						'is_you' => $recipient->user_id == bp_loggedin_user_id()
					);
				}
			}
		}

		if ( bp_is_active( 'messages', 'star' ) ) {
			$star_link = bp_get_the_message_star_action_link( array(
				'thread_id' => bp_get_message_thread_id(),
				'url_only'  => true,
			) );

			$threads->threads[ $i ]['star_link']  = $star_link;

			$star_link_data = explode( '/', $star_link );
			$threads->threads[ $i ]['is_starred'] = array_search( 'unstar', $star_link_data );

			// Defaults to last
			$sm_id = $last_message_id;

			if ( $threads->threads[ $i ]['is_starred'] ) {
				$sm_id = (int) $star_link_data[ $threads->threads[ $i ]['is_starred'] + 1 ];
			}

			$threads->threads[ $i ]['star_nonce'] = wp_create_nonce( 'bp-messages-star-' . $sm_id );
			$threads->threads[ $i ]['starred_id'] = $sm_id;
		}

		if ( bp_is_active( 'media' ) && bp_is_messages_media_support_enabled() ) {
			$media_ids = bp_messages_get_meta( $last_message_id, 'bp_media_ids', true );

			if ( ! empty( $media_ids ) ) {
				$media_ids = explode( ',', $media_ids );
				if ( sizeof( $media_ids ) < 2 ) {
					$threads->threads[ $i ]['excerpt'] = __( 'sent a photo', 'buddyboss' );
				} else {
					$threads->threads[ $i ]['excerpt'] = __( 'sent some photos', 'buddyboss' );
				}
			}
		} else if ( bp_is_active( 'media' ) && bp_is_messages_gif_support_enabled() ) {
			$gif_data = bp_messages_get_meta( $last_message_id, '_gif_data', true );

			if ( ! empty( $gif_data ) ) {
				$threads->threads[ $i ]['excerpt'] = __( 'sent a gif', 'buddyboss' );
			}
		}

		$thread_extra_content = bp_nouveau_messages_catch_hook_content( array(
			'inboxListItem' => 'bp_messages_inbox_list_item',
			'threadOptions' => 'bp_messages_thread_options',
		) );

		if ( array_filter( $thread_extra_content ) ) {
			$threads->threads[ $i ] = array_merge( $threads->threads[ $i ], $thread_extra_content );
		}

		$i += 1;
	endwhile;

	$threads->threads = array_filter( $threads->threads );

	$extra_content = bp_nouveau_messages_catch_hook_content( array(
		'beforeLoop' => 'bp_before_member_messages_loop',
		'afterLoop'  => 'bp_after_member_messages_loop',
	) );

	if ( array_filter( $extra_content ) ) {
		$threads->extraContent = $extra_content;
	}

	// Remove the bp_current_action() override.
	$bp->current_action = $reset_action;

	// Return the successfull reply.
	wp_send_json_success( $threads );
}

Changelog

Changelog
Version Description
BuddyPress 3.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.