xprofile_update_clones_on_template_update( BP_XProfile_Field $field )

Update repeater/clone fields when the main/template field is updated.

Description

Parameters

$field

(Required) Description

Source

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

function xprofile_update_clones_on_template_update ( $field ) {
    global $wpdb;
    $bp = buddypress();
    
    //get all clone field ids
    $clone_ids = $wpdb->get_col( $wpdb->prepare(
        "SELECT f.id FROM {$bp->profile->table_name_fields} AS f JOIN {$bp->profile->table_name_meta} AS fm ON f.id = fm.object_id "
        . " WHERE f.parent_id = 0 AND fm.meta_key = '_cloned_from' AND fm.meta_value = %d ",
        $field->id
    ) );
        
    if ( empty( $clone_ids ) || is_wp_error( $clone_ids ) ) {
        return;
    }
    
    $db_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $field->id ), ARRAY_A );
    
    if ( !empty( $db_row ) && !is_wp_error( $db_row ) ) {
        $sql = $wpdb->prepare(
            "UPDATE {$bp->profile->table_name_fields} SET "
            . " group_id = %d, parent_id = %d, type = %s, name = %s, description = %s, is_required = %d, "
            . " is_default_option = %d, option_order = %d, order_by = %d, can_delete = %d "
            . " WHERE id IN ( ". implode( ',', $clone_ids ) ." )",
            $db_row[ 'group_id' ], $db_row[ 'parent_id' ], $db_row[ 'type' ], $db_row[ 'name' ], $db_row[ 'description' ], $db_row[ 'is_required' ],
            $db_row[ 'is_default_option' ], $db_row[ 'option_order' ], $db_row[ 'order_by' ], $db_row[ 'can_delete' ]
        );
            
        $wpdb->query( $sql );
        
        $metas = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_meta} WHERE object_id = {$field->id} AND object_type = 'field'", ARRAY_A );
        
        if ( !empty( $metas ) && !is_wp_error( $metas ) ) {
            foreach ( $clone_ids as $clone_id ) {
                foreach ( $metas as $meta ) {
                    bp_xprofile_update_meta( $clone_id, 'field', $meta['meta_key'], $meta['meta_value'] );
                }
            }
        }
    }
}

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.