BP_REST_Group_Membership_Request_Endpoint::create_item( WP_REST_Request $request )

Request membership to a group.


Parameters Parameters

$request

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


Top ↑

Return Return

(WP_REST_Response|WP_Error)


Source Source

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

	public function create_item( $request ) {
		$user_id_arg = $request['user_id'] ? $request['user_id'] : bp_loggedin_user_id();
		$user        = bp_rest_get_user( $user_id_arg );
		$group       = $this->groups_endpoint->get_group_object( $request['group_id'] );

		// Avoid duplicate requests.
		if ( groups_check_for_membership_request( $user->ID, $group->id ) ) {
			return new WP_Error(
				'bp_rest_group_membership_requests_duplicate_request',
				__( 'There is already a request to this member.', 'buddypress' ),
				array(
					'status' => 500,
				)
			);
		}

		$request_id = groups_send_membership_request(
			array(
				'group_id' => $group->id,
				'user_id'  => $user->ID,
				'content'  => $request['message'],
			)
		);

		if ( ! $request_id ) {
			return new WP_Error(
				'bp_rest_group_membership_requests_cannot_create_item',
				__( 'Could not send membership request to this group.', 'buddypress' ),
				array(
					'status' => 500,
				)
			);
		}

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

		$invite = new BP_Invitation( $request_id );

		$retval = array(
			$this->prepare_response_for_collection(
				$this->prepare_item_for_response( $invite, $request )
			),
		);

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a group membership request is made via the REST API.
		 *
		 * @since 5.0.0
		 *
		 * @param WP_User          $user     The user.
		 * @param BP_Invitation    $invite   The invitation object.
		 * @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.
		 */
		do_action( 'bp_rest_group_membership_requests_create_item', $user, $invite, $group, $response, $request );

		return $response;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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