bp_dd_import_users_messages()

Import private messages between users.

Description

Return

(array)

Source

File: bp-core/bp-core-tools-default-data.php

function bp_dd_import_users_messages() {
	$messages            = array();
	$random_thread_reply = array();

	if ( ! bp_is_active( 'messages' ) ) {
		return $messages;
	}

	/** @var $messages_subjects array */
	/** @var $messages_content array */
	require( BP_DEFAULT_DATA_DIR . 'data/messages.php' );

	$users_data = require( BP_DEFAULT_DATA_DIR . 'data/users.php' );
	$users_data = wp_list_pluck( $users_data, 'login', 'refer_id' );
	foreach ( $messages as $message ) {
		$user_object = get_user_by( 'login', $users_data[ $message['sender_refer_id'] ] );

		$user_id            = empty( $user_object->ID ) ? bp_dd_get_random_users_ids( 1, 'string' ) : $user_object->ID;
		$recipients_numbers = empty( $message['recipients'] ) ? 1 : $message['recipients'];
		$recipients         = bp_dd_get_random_users_ids( absint( $recipients_numbers ), 'array' );

		$thread_reply = empty( $message['thread_reply'] ) ? 1 : $message['thread_reply'];

		$message_id = messages_new_message( array(
			'sender_id'  => $user_id,
			'recipients' => $recipients,
			'subject'    => $message['content'],
			'content'    => $message['content'],
			'date_sent'  => bp_dd_get_random_date( 29, 16 ),
		) );

		$messages[] = $message_id;

		$all_recipients = array_merge( $recipients, (array) $user_id );

		// first level messages
		for ( $i = 0; $i <= $thread_reply; $i ++ ) {
			$replying_sender_id     = $all_recipients[ array_rand( $all_recipients ) ];
			$replying_recipients_id = array_diff( $all_recipients, (array) $replying_sender_id );

			$messages[] = messages_new_message( array(
				'sender_id'  => $replying_sender_id,
				'recipients' => $replying_recipients_id,
				'thread_id'  => $message_id,
				'subject'    => $message['content'],
				'content'    => $random_thread_reply[ array_rand( $random_thread_reply ) ],
				'date_sent'  => bp_dd_get_random_date( 15, 1 ),
			) );
		}
	}

	if ( ! empty( $messages ) ) {
		/** @noinspection PhpParamsInspection */
		bp_update_option( 'bp_dd_imported_user_messages_ids', $messages );
	}

	return $messages;
}

Changelog

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