BP_REST_Group_Invites_Endpoint::create_multiple_item( WP_REST_Request $request )

Invite multiple member to a group.

Description

Parameters

$request

(WP_REST_Request) (Required) Full data about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-groups/classes/class-bp-rest-group-invites-endpoint.php

	public function create_multiple_item( $request ) {
		$inviter_id_arg = $request['inviter_id'] ? $request['inviter_id'] : bp_loggedin_user_id();
		$group          = $this->groups_endpoint->get_group_object( $request['group_id'] );
		$user_ids       = (array) $request['user_id'];
		$inviter        = bp_rest_get_user( $inviter_id_arg );
		$users          = array();
		$retval         = array();

		if ( ! empty( $user_ids ) ) {
			foreach ( $user_ids as $user_id ) {
				$users[] = bp_rest_get_user( $user_id );
			}
		}

		$invited = array();
		foreach ( $users as $user ) {
			$invited[ $user->ID ] = groups_invite_user(
				array(
					'user_id'     => $user->ID,
					'group_id'    => $group->id,
					'inviter_id'  => $inviter->ID,
					'send_invite' => isset( $request['invite_sent'] ) ? (bool) $request['invite_sent'] : 1,
					'content'     => $request['message'],
				)
			);
		}

		$invited = array_filter( $invited );

		if ( ! $invited ) {
			return new WP_Error(
				'bp_rest_group_invite_cannot_create_item',
				__( 'Could not invite member to the group.', 'buddyboss' ),
				array(
					'status' => 500,
				)
			);
		}

		foreach ( $invited as $invite_id ) {

			$invite = new BP_Invitation( $invite_id );

			// Set context.
			$request->set_param( 'context', 'edit' );

			$retval[] = $this->prepare_response_for_collection(
				$this->prepare_item_for_response( $invite, $request )
			);
		}

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a member is invited to a group via the REST API.
		 *
		 * @since 0.1.0
		 *
		 * @param array            $users    The invited user.
		 * @param WP_User          $inviter  The inviter user.
		 * @param BP_Groups_Group  $group    The group object.
		 * @param WP_REST_Response $response The response data.
		 * @param WP_REST_Request  $request  The request sent to the API.
		 *
		 * @param array            $invited  The invitation object.
		 */
		do_action( 'bp_rest_group_multiple_invites_create_item', $invited, $users, $inviter, $group, $response, $request );

		return $response;
	}

Changelog

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