BP_REST_Invites_Endpoint::create_item( WP_REST_Request $request )

Create an Invites/Send Invites.

Description

Parameters

$request

(Required) Full data about the request.

Return

(WP_REST_Response) | WP_Error

Source

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

	public function create_item( $request ) {

		$fields = $request->get_param( 'fields' );

		if ( empty( $fields ) ) {
			return new WP_Error(
				'bp_rest_required_fields',
				__( 'Sorry, you need to set the fields parameter.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		$invite_exists_array = array();
		$failed_invite       = array();

		$bp = buddypress();

		if ( ! empty( $fields ) ) {
			foreach ( $fields as $k => $field ) {
				if (
					isset( $field['name'] )
					&& isset( $field['email_id'] )
					&& '' !== $field['name']
					&& '' !== $field['name']
					&& '' !== $field['email_id']
					&& is_email( $field['email_id'] )
				) {
					if ( email_exists( (string) $field['email_id'] ) ) {
						$invite_exists_array[] = $field['email_id'];
					} else {
						$invite_correct_array[] = array(
							'name'        => $field['name'],
							'email'       => $field['email_id'],
							'member_type' => ( isset( $field['profile_type'] ) && ! empty( $field['profile_type'] ) ) ? $field['profile_type'] : '',
						);
					}
				} else {
					$invite_wrong_array[] = array(
						'name'        => ( isset( $field['name'] ) ? $field['name'] : '' ),
						'email'       => ( isset( $field['email_id'] ) ? $field['email_id'] : '' ),
						'member_type' => ( isset( $field['profile_type'] ) ? $field['profile_type'] : '' ),
					);
				}
			}
		}

		$invitations_ids = array();

		$query_string = array();
		if ( ! empty( $invite_correct_array ) ) {

			require trailingslashit( buddypress()->plugin_dir . 'bp-invites/actions' ) . '/invites.php';

			foreach ( $invite_correct_array as $key => $value ) {

				$_POST = array();

				$email          = $value['email'];
				$name           = $value['name'];
				$member_type    = $value['member_type'];
				$query_string[] = $email;
				$inviter_name   = bp_core_get_user_displayname( bp_loggedin_user_id() );

				if ( true === bp_disable_invite_member_email_subject() ) {
					$subject = $request->get_param( 'email_subject' );
					if ( empty( $subject ) ) {
						$subject = stripslashes( wp_strip_all_tags( bp_get_member_invitation_subject() ) );
					} else {
						$_POST['bp_member_invites_custom_subject'] = $subject;
					}
				} else {
					$subject = stripslashes( wp_strip_all_tags( bp_get_member_invitation_subject() ) );
				}

				if ( true === bp_disable_invite_member_email_content() ) {
					$message = $request->get_param( 'email_content' );
					if ( empty( $message ) ) {
						$message = stripslashes( wp_strip_all_tags( bp_get_member_invitation_message() ) );
					} else {
						$_POST['bp_member_invites_custom_content'] = $message;
					}
				} else {
					$message = stripslashes( wp_strip_all_tags( bp_get_member_invitation_message() ) );
				}

				$message .= '

' . bp_get_member_invites_wildcard_replace( stripslashes( wp_strip_all_tags( bp_get_invites_member_invite_url() ) ), $email );

				$inviter_name = bp_core_get_user_displayname( bp_loggedin_user_id() );
				$site_name    = get_bloginfo( 'name' );
				$inviter_url  = bp_loggedin_user_domain();

				$email_encode = rawurlencode( $email );

				// set post variable.
				$_POST['custom_user_email'] = $email;

				// Set both variable which will use in email.
				$_POST['custom_user_name']   = $name;
				$_POST['custom_user_avatar'] = apply_filters( 'bp_sent_invite_email_avatar', buddypress()->plugin_url . 'bp-core/images/mystery-man.jpg' );

				$accept_link = add_query_arg(
					array(
						'bp-invites' => 'accept-member-invitation',
						'email'      => $email_encode,
						// phpcs:ignore
						'inviter'    => base64_encode( bp_loggedin_user_id() ),
					),
					bp_get_root_domain() . '/' . bp_get_signup_slug() . '/'
				);
				$accept_link = apply_filters( 'bp_member_invitation_accept_url', $accept_link );
				$args        = array(
					'tokens' => array(
						'inviter.name' => $inviter_name,
						'inviter.url'  => $inviter_url,
						'invitee.url'  => $accept_link,
					),
				);

				/**
				 * Remove Recipients avatar and name.
				 *
				 * T:1602 - https://trello.com/c/p2VKGMHs/1602-recipients-name-and-avatar-should-not-be-showing-on-email-invite-template
				 */
				add_filter( 'bp_email_get_salutation', '__return_false' );

				$insert_post_args = array(
					'post_author'  => $bp->loggedin_user->id,
					'post_content' => $message,
					'post_title'   => $subject,
					'post_status'  => 'publish',
					'post_type'    => bp_get_invite_post_type(),
				);

				$post_id = wp_insert_post( $insert_post_args );

				if ( ! empty( $post_id ) || ! is_wp_error( $post_id ) ) {
					$invitations_ids[] = $post_id;

					// Send invitation email.
					bp_send_email( 'invites-member-invite', $email, $args );

					// Save a blank bp_ia_accepted post_meta.
					update_post_meta( $post_id, 'bp_member_invites_accepted', '' );
					update_post_meta( $post_id, '_bp_invitee_email', $email );
					update_post_meta( $post_id, '_bp_invitee_name', $name );
					update_post_meta( $post_id, '_bp_inviter_name', $inviter_name );
					update_post_meta( $post_id, '_bp_invitee_status', 0 );
					update_post_meta( $post_id, '_bp_invitee_member_type', $member_type );
				} else {
					$failed_invite[] = $value;
				}
			}
		}

		if ( empty( $invitations_ids ) ) {
			return new WP_Error(
				'bp_rest_no_invitation_send',
				__( 'Sorry, No any invitation has been send.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		$send_invitations = get_posts(
			array(
				'post_type' => bp_get_invite_post_type(),
				'include'   => $invitations_ids,
			)
		);

		$retval = array(
			'data'   => array(),
			'exists' => $invite_exists_array,
			'failed' => wp_list_pluck( array_filter( $failed_invite ), 'email' ),
		);

		foreach ( $send_invitations as $invite ) {
			$retval['data'][] = $this->prepare_response_for_collection(
				$this->prepare_item_for_response( $invite, $request )
			);
		}

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a list of invites has been send via the REST API.
		 *
		 * @param WP_REST_Response $response The response data.
		 * @param WP_REST_Request  $request  The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		do_action( 'bp_rest_invites_get_items', $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.