bp_profile_repeaters_update_field_data( $user_id,  $posted_field_ids,  $errors,  $old_values,  $new_values )

Update/Sort repeater fields when profile data is updated.

Description

Source

File: bp-xprofile/bp-xprofile-repeaters.php

function bp_profile_repeaters_update_field_data ( $user_id, $posted_field_ids, $errors, $old_values, $new_values ) {
    global $wpdb;
    $bp = buddypress();
    
    if ( !empty( $errors ) ) {
        return;
    }
    
    $field_group_id = $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$bp->profile->table_name_fields} WHERE id = %d", $posted_field_ids[ 0 ] ) );
    
    $is_repeater_enabled = 'on' == BP_XProfile_Group::get_group_meta( $field_group_id, 'is_repeater_enabled' ) ? true : false;
    if ( !$is_repeater_enabled ) {
        return;
    }
    
    // First, clear the data for deleted fields, if any
    if ( isset( $_POST['deleted_field_ids'] ) && !empty( $_POST['deleted_field_ids'] ) ) {
        $deleted_field_ids = wp_parse_id_list( $_POST['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( $_POST['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;
    foreach( (array) $field_set_sequence as $field_set_number ) {
        //Find all fields in this set, take their values and update the data for corresponding fields in set number $counter
        $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 ) {
                $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 " 
                    . " WHERE m1.object_type = 'field' AND m1.meta_key = '_cloned_from' AND m1.meta_value = {$cloned_from} "
                    . " AND m2.meta_key = '_clone_number' AND m2.meta_value = {$counter} ";
                $corresponding_field_id = $wpdb->get_var( $sql );
                if ( !empty( $corresponding_field_id ) ) {
                    $new_data = isset( $new_values[ $field_of_current_set ][ 'value' ] ) ? $new_values[ $field_of_current_set ][ 'value' ] : '';
                    $new_visibility_level = isset( $new_values[ $field_of_current_set ][ 'visibility' ] ) ? $new_values[ $field_of_current_set ][ 'visibility' ] : '';
                    xprofile_set_field_visibility_level( $corresponding_field_id, $user_id, $new_visibility_level );

	                $type = $wpdb->get_var( $wpdb->prepare( "SELECT `type` FROM {$wpdb->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_values[ $field_of_current_set ][ 'value' ] ) ? implode( ' ', $new_values[ $field_of_current_set ][ 'value' ] ) : $new_values[ $field_of_current_set ][ 'value' ];
                }
            }
        }
    
        $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 ) );
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.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.