bp_clone_field_for_repeater_sets( type $field_id )

Copy form fields for field sets that repeat.

Description

Parameters

$field_id

(Required)

Return

(boolean)

Source

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

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
function bp_clone_field_for_repeater_sets ( $field_id ) {
    global $wpdb;
    $bp = buddypress();
     
    $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 ) ) {
        $template_field_id = $db_row['id'];
 
        $new_field_column_names = array(
            'group_id',
            'parent_id',
            'type',
            'name',
            'description',
            'is_required',
            'is_default_option',
            'field_order',
            'option_order',
            'order_by',
            'can_delete',
        );
 
        $new_field_column_data_types = array( '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%s', '%d' );
 
        $new_field_column_data = array(
            'group_id'          => $db_row['group_id'],
            'parent_id'         => $db_row['parent_id'],
            'type'              => $db_row['type'],
            'name'              => $db_row['name'],
            'description'       => $db_row['description'],
            'is_required'       => $db_row['is_required'],
            'is_default_option' => $db_row['is_default_option'],
            'field_order'       => $db_row['field_order'],
            'option_order'      => $db_row['option_order'],
            'order_by'          => $db_row['order_by'],
            'can_delete'        => $db_row['can_delete'],
        );
         
        $inserted = $wpdb->insert(
            $bp->profile->table_name_fields,
            $new_field_column_data,
            $new_field_column_data_types
        );
         
        if ( $inserted ) {
            $new_field_id = $wpdb->insert_id;
            $metas = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_meta} WHERE object_id = {$template_field_id} AND object_type = 'field'", ARRAY_A );
            if ( !empty( $metas ) && !is_wp_error( $metas ) ) {
                foreach ( $metas as $meta ) {
                    bp_xprofile_update_meta( $new_field_id, 'field', $meta['meta_key'], $meta['meta_value'] );
                }
            }
             
            $current_clone_number = 1;
             
            //get all clones of the template field
            $all_clones = $wpdb->get_col( "SELECT object_id FROM {$bp->profile->table_name_meta} WHERE meta_key = '_cloned_from' AND meta_value = {$template_field_id}" );
            if ( !empty( $all_clones ) && !is_wp_error( $all_clones ) ) {
                $last_max_clone_number = $wpdb->get_var(
                    "SELECT MAX( meta_value ) FROM {$bp->profile->table_name_meta} WHERE meta_key = '_clone_number' AND object_id IN (". implode( ',', $all_clones ) .")"
                );
                     
                $last_max_clone_number = !empty( $last_max_clone_number ) ? absint( $last_max_clone_number ) : 0;
                $current_clone_number = $last_max_clone_number + 1;
            }
             
            bp_xprofile_update_meta( $new_field_id, 'field', '_is_repeater_clone', true );
            bp_xprofile_update_meta( $new_field_id, 'field', '_cloned_from', $template_field_id );
            bp_xprofile_update_meta( $new_field_id, 'field', '_clone_number', $current_clone_number );
             
            //fix field order
            $field_order = ( $current_clone_number * bp_profile_field_set_max_cap() ) + $db_row[ 'field_order' ];
            $wpdb->update(
                $bp->profile->table_name_fields,
                array( 'field_order' => $field_order ),
                array( 'id' => $new_field_id ),
                array( '%d' ),
                array( '%d' )
            );
                 
            return $new_field_id;
        }
    }
     
    return false;
}

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.