BP_REST_XProfile_Repeater_Endpoint::create_item( WP_REST_Request $request )

Create a new Repeater Group.

Description

Parameters

$request

(Required) Full data about the request.

Return

(WP_REST_Response|WP_Error)

Source

File: bp-xprofile/classes/class-bp-rest-xprofile-repeater-endpoint.php

	public function create_item( $request ) {
		// Setting context.
		$request->set_param( 'context', 'edit' );

		// Get the field group before it's deleted.
		$field_group = xprofile_get_field_group( (int) $request['id'] );

		if ( empty( $field_group->id ) ) {
			return new WP_Error(
				'bp_rest_invalid_id',
				__( 'Invalid field group ID.', 'buddyboss' ),
				array(
					'status' => 404,
				)
			);
		}

		$user_id = get_current_user_id();
		if ( ! $user_id ) {
			new WP_Error(
				'bp_rest_authorization_required',
				__( 'Sorry, you are not allowed to update your profile repeater fields.', 'buddyboss' ),
				array(
					'status' => rest_authorization_required_code(),
				)
			);
		}

		$count = bp_get_profile_field_set_count( $field_group->id, $user_id );
		$count++;
		bp_set_profile_field_set_count( $field_group->id, $user_id, $count );

		$clone_fields = bp_get_repeater_clone_field_ids_subset( $field_group->id, $user_id );
		if ( empty( $clone_fields ) ) {
			$group_fields = bp_get_repeater_template_field_ids( $field_group->id );
			if ( ! empty( $group_fields ) ) {
				foreach ( $group_fields as $field_id ) {
					bp_clone_field_for_repeater_sets( $field_id );
				}
			}
		}

		$field_group = $this->group_fields_endpoint->get_xprofile_field_group_object( $request );

		$retval = $this->group_fields_endpoint->prepare_response_for_collection(
			$this->group_fields_endpoint->prepare_item_for_response( $field_group, $request )
		);

		$response = new WP_REST_Response();
		$response->set_data(
			array(
				'added' => true,
				'data'  => $retval,
			)
		);

		/**
		 * Fires after a XProfile repeater fields created via the REST API.
		 *
		 * @since 0.1.0
		 *
		 * @param BP_XProfile_Group $field_group Deleted field group.
		 * @param WP_REST_Response  $response  The response data.
		 * @param WP_REST_Request   $request   The request sent to the API.
		 */
		do_action( 'bp_rest_xprofile_repeater_fields_create_item', $field_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.