bp_messages_remove_user_to_group_message_thread( $group_id,  $user_id )

Add member to Group thread when h/she joined the group.

Description

Parameters

$group_id

(Required)

$user_id

(Required)

Source

File: bp-messages/bp-messages-filters.php

function bp_messages_remove_user_to_group_message_thread( $group_id, $user_id ) {

	global $wpdb, $bp, $messages_template;

	if ( true === bp_disable_group_messages() && bp_is_active( 'messages' ) ) {

		$group_thread = (int) groups_get_groupmeta( (int) $group_id, 'group_message_thread' );
		$recipients   = BP_Messages_Thread::get_recipients_for_thread( (int) $group_thread );
		$recipients   = wp_list_pluck( $recipients, 'user_id' );

		if ( $group_thread > 0 && in_array( (int) $user_id, $recipients, true ) ) {

			$first_message     = BP_Messages_Thread::get_first_message( $group_thread );
			$message_users_ids = bp_messages_get_meta( $first_message->id, 'message_users_ids', true ); // users list
			$message_users_ids = explode( ',', $message_users_ids );
			if ( ( $key = array_search( $user_id, $message_users_ids ) ) !== false ) {
				unset( $message_users_ids[ $key ] );
			}

			bp_messages_update_meta( $first_message->id, 'message_users_ids', implode( ',', $message_users_ids ) );

			remove_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
			remove_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
			$new_reply = messages_new_message( array(
				'sender_id'  => $user_id,
				'thread_id'  => $group_thread,
				'subject'    => '',
				'content'    => '<p> </p>',
				'date_sent'  => $date_sent = bp_core_current_time(),
				'error_type' => 'wp_error',
			) );
			add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
			add_action( 'messages_message_sent', 'bp_messages_message_sent_add_notification', 10 );
			if ( ! is_wp_error( $new_reply ) && true === is_int( ( int ) $new_reply ) ) {
				if ( bp_has_message_threads( array( 'include' => $new_reply ) ) ) {
					while ( bp_message_threads() ) {
						bp_message_thread();
						$last_message_id = (int) $messages_template->thread->last_message_id;
						bp_messages_update_meta( $last_message_id, 'group_message_group_left', 'yes' );
						bp_messages_update_meta( $last_message_id, 'group_id', (int) $group_id );
					}
				}
			}
			$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND thread_id = %d", $user_id, (int) $group_thread ) );
		}
	}
}

Changelog

Changelog
Version Description
BuddyBoss 1.3.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.