xprofile_update_clone_positions_on_template_position_update( int $template_field_id, int $new_position, int $template_field_group_id )

Update position and group_id for all clone fields when a template/main field’s order is changed.

Description

Parameters

$template_field_id

(int) (Required)

$new_position

(int) (Required)

$template_field_group_id

(int) (Required)

Return

(type)

Source

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

function xprofile_update_clone_positions_on_template_position_update ( $template_field_id, $new_position, $template_field_group_id ) {
	
    //
    // Check if template field is now moved to another field set
    // If so
    //  - If the new field set is also a repeater
    //      - Update clone fields, update their group id and sorting numbers
    // If not
    //  - Update clone fields, update their sorting numbers
    //
    
    global $wpdb;
    $bp = buddypress();
    
    $clone_field_ids = $wpdb->get_col( $wpdb->prepare( 
        "SELECT object_id FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND meta_key = '_cloned_from' AND meta_value = %d",
        $template_field_id
    ) );
        
    if ( empty( $clone_field_ids ) || is_wp_error( $clone_field_ids ) ) {
        return;
    }
    
    //get the old group id
    //since all clone fields have same group id, we can simply get the group id for the first one
    $old_group_id = $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$bp->profile->table_name_fields} WHERE id = %d", $clone_field_ids[ 0 ] ) );
    if ( empty( $old_group_id ) || is_wp_error( $old_group_id ) ) {
        return;//something's not right
    }
    
    $update_field_orders = false;
    
    if ( $old_group_id != $template_field_group_id ) {
        //tempalte field has been moved to a new field group
        $is_repeater_enabled = 'on' == bp_xprofile_get_meta( $template_field_group_id, 'group', 'is_repeater_enabled' ) ? true : false;
		
        if ( $is_repeater_enabled ) {
            //update group id for all clone fields
            $sql = $wpdb->prepare( 
                "UPDATE {$bp->profile->table_name_fields} SET group_id = %d WHERE id IN ( ". implode( ',', $clone_field_ids ) ." )", 
                $template_field_group_id 
            );
            
            $wpdb->query( $sql );
                
            $update_field_orders = true;
        }
    } else {
        //tempalte field is still in same field group
        $update_field_orders = true;
    }
    
    if ( $update_field_orders ) {
        $max_cap = bp_profile_field_set_max_cap();
        
        foreach ( $clone_field_ids as $clone_field_id ) {
            $clone_number = (int) bp_xprofile_get_meta( $clone_field_id, 'field', '_clone_number', true );
            $field_order = ( $clone_number * $max_cap ) + $new_position;
            $wpdb->update(
                $bp->profile->table_name_fields,
                array( 'field_order' => $field_order ),
                array( 'id' => $clone_field_id ),
                array( '%d' ),
                array( '%d' )
            );
        }
    }
}

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.