BP_REST_XProfile_Repeater_Endpoint::bp_profile_repeaters_update_field_data( integer $user_id, array $posted_field_ids, array $repeater_set_sequence, array $deleted_field_ids, integer $field_group_id )

Update Field data with delete.

Description

Parameters

$user_id

(Required) The ID of the user.

$posted_field_ids

(Required) The remaining field ids after delete.

$repeater_set_sequence

(Required) The repeater sequence array.

$deleted_field_ids

(Required) Fields IDs to delete.

$field_group_id

(Required) The ID of the field group.

Source

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

	protected function bp_profile_repeaters_update_field_data( $user_id, $posted_field_ids, $repeater_set_sequence, $deleted_field_ids, $field_group_id ) {
		global $wpdb;
		$bp = buddypress();

		if ( ! empty( $errors ) ) {
			return;
		}

		$is_repeater_enabled = 'on' === bp_xprofile_get_meta( $field_group_id, 'group', 'is_repeater_enabled', true ) ? true : false;
		if ( ! $is_repeater_enabled ) {
			return;
		}

		// First, clear the data for deleted fields, if any.
		if ( isset( $deleted_field_ids ) && ! empty( $deleted_field_ids ) ) {
			$deleted_field_ids = wp_parse_id_list( $deleted_field_ids );
			foreach ( $deleted_field_ids as $deleted_field_id ) {
				xprofile_delete_field_data( $deleted_field_id, $user_id );
			}
		}

		$field_set_sequence = wp_parse_id_list( $repeater_set_sequence );

		// We'll take the data from all clone fields and dump it into the main/template field.
		// This is done to ensure that search etc, work smoothly.
		$main_field_data = array();

		$counter            = 1;
		$field_set_sequence = (array) $field_set_sequence;
		if ( ! empty( $field_set_sequence ) ) {
			foreach ( (array) $field_set_sequence as $field_set_number ) {

				// phpcs:ignore
				$fields_of_current_set = $wpdb->get_col("SELECT object_id FROM {$bp->profile->table_name_meta} WHERE meta_key = '_clone_number' AND meta_value = {$field_set_number} " . ' AND object_id IN (' . implode( ',', $posted_field_ids ) . ") and object_type = 'field' " );

				if ( ! empty( $fields_of_current_set ) && ! is_wp_error( $fields_of_current_set ) ) {
					foreach ( $fields_of_current_set as $field_of_current_set ) {
						// phpcs:ignore
						$cloned_from = $wpdb->get_var( "SELECT meta_value FROM {$bp->profile->table_name_meta} WHERE object_id = {$field_of_current_set} AND meta_key = '_cloned_from' " );

						$sql  = "SELECT m1.object_id FROM {$bp->profile->table_name_meta} AS m1 JOIN {$bp->profile->table_name_meta} AS m2 ON m1.object_id = m2.object_id ";
						$sql .= " WHERE m1.object_type = 'field' AND m1.meta_key = '_cloned_from' AND m1.meta_value = {$cloned_from} ";
						$sql .= " AND m2.meta_key = '_clone_number' AND m2.meta_value = {$counter} ";

						// phpcs:ignore
						$corresponding_field_id = $wpdb->get_var( $sql );
						if ( ! empty( $corresponding_field_id ) ) {
							$new_data             = xprofile_get_field_data( $field_of_current_set, $user_id );
							$new_visibility_level = xprofile_get_field_visibility_level( $field_of_current_set, $user_id );
							xprofile_set_field_visibility_level( $corresponding_field_id, $user_id, $new_visibility_level );

							// phpcs:ignore
							$type = $wpdb->get_var( $wpdb->prepare( "SELECT `type` FROM {$bp->table_prefix}bp_xprofile_fields WHERE id = %d", $corresponding_field_id ) );

							if ( 'datebox' === $type ) {
								$new_data = $new_data . ' 00:00:00';
							}
							xprofile_set_field_data( $corresponding_field_id, $user_id, $new_data );

							if ( ! isset( $main_field_data[ $cloned_from ] ) ) {
								$main_field_data[ $cloned_from ] = array();
							}

							$main_field_data[ $cloned_from ][] = is_array( $new_data ) ? implode( ' ', $new_data ) : $new_data;
						}
					}
				}

				$counter ++;
			}
		}

		if ( ! empty( $main_field_data ) ) {
			foreach ( $main_field_data as $main_field_id => $values ) {
				$values_str = implode( ' ', $values );
				xprofile_set_field_data( $main_field_id, $user_id, $values_str );
			}
		}

		bp_set_profile_field_set_count( $field_group_id, $user_id, count( $field_set_sequence ) );
	}

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.