BP_REST_XProfile_Repeater_Endpoint::delete_item( WP_REST_Request $request )

Delete a XProfile Repeater fields.

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 delete_item( $request ) {
		// Setting context.
		// Get the field group before it's deleted.
		$field_group = xprofile_get_field_group( (int) $request['id'] );

		if ( ! isset( $request['fields'] ) || empty( $request['fields'] ) ) {
			$request['fields'] = array();
		}

		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 delete your profile repeater fields.', 'buddyboss' ),
				array(
					'status' => rest_authorization_required_code(),
				)
			);
		}

		$group_fields = bp_get_repeater_template_field_ids( $field_group->id );

		$required           = false;
		$result             = true;
		$field_set_sequence = array();
		$deleted_field_ids  = array();
		if ( ! empty( $group_fields ) ) {
			foreach ( $group_fields as $field_id ) {
				$field_set_sequence += $this->get_repeater_fields( $field_id, $user_id, $field_group->id );
				$is_required         = xprofile_check_is_required_field( $field_id );
				if ( true === $is_required ) {
					$required = $is_required;
				}
			}
		}

		$posted_field_ids = array_keys( $field_set_sequence );

		$fields = $request['fields'];
		$count  = bp_get_profile_field_set_count( $field_group->id, $user_id );

		if (
			( isset( $fields ) && ! empty( $fields ) )
			&& (
				( true === $required && $count > 1 )
				|| ( true !== $required && $count >= 1 )
			)
		) {
			$deleted_field_ids = wp_parse_id_list( $fields );
			if ( ! empty( $deleted_field_ids ) ) {
				foreach ( $deleted_field_ids as $deleted_field_id ) {
					if ( array_key_exists( $deleted_field_id, $field_set_sequence ) ) {
						unset( $field_set_sequence[ $deleted_field_id ] );
					}
				}
			}
			--$count;
			if ( 0 === $count ) {
				$count        = 1;
				$clone_fields = bp_get_repeater_clone_field_ids_subset( $field_group->id, $user_id );
				if ( ! empty( $clone_fields ) ) {
					foreach ( $clone_fields as $deleted_field_id ) {
						if ( array_key_exists( $deleted_field_id, $field_set_sequence ) ) {
							unset( $field_set_sequence[ $deleted_field_id ] );
						}
					}
				}
			}
		}

		$field_set_sequence    = array_values( $field_set_sequence );
		$repeater_set_sequence = array_unique( $field_set_sequence );

		$this->bp_profile_repeaters_update_field_data( $user_id, $posted_field_ids, $repeater_set_sequence, $deleted_field_ids, $field_group->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(
				'deleted' => $result,
				'data'    => $retval,
			)
		);

		/**
		 * Fires after a XProfile repeater field is deleted 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_delete_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.