BP_REST_XProfile_Field_Groups_Endpoint::create_item( WP_REST_Request $request )

Create a XProfile field 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-field-groups-endpoint.php

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

		$args = array(
			'name'        => $request['name'],
			'description' => $request['description'],
			'can_delete'  => $request['can_delete'],
		);

		/**
		 * Filter the query arguments for the request.
		 *
		 * @param array $args Key value array of query var to query value.
		 * @param WP_REST_Request $request The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		$args = apply_filters( 'bp_rest_xprofile_field_groups_create_item_query_args', $args, $request );

		if ( empty( $args['name'] ) ) {
			return new WP_Error(
				'bp_rest_required_param_missing',
				__( 'Required param missing.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		$group_id = xprofile_insert_field_group( $args );

		if ( ! $group_id ) {
			return new WP_Error(
				'bp_rest_user_cannot_create_xprofile_field_group',
				__( 'Cannot create new XProfile field group.', 'buddyboss' ),
				array(
					'status' => 500,
				)
			);
		}

		$field_group = $this->get_xprofile_field_group_object( $group_id );

		// Create Additional fields.
		$fields_update = $this->update_additional_fields_for_object( $field_group, $request );

		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

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

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a XProfile field group is created via the REST API.
		 *
		 * @param BP_XProfile_Group $field_group Created field group object.
		 * @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_xprofile_field_groups_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.