bp_dd_import_users_profile()

Import extended profile fields.

Description

Return

(int)

Source

File: bp-core/bp-core-tools-default-data.php

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
function bp_dd_import_users_profile() {
    $count = 0;
 
    if ( ! bp_is_active( 'xprofile' ) ) {
        return $count;
    }
 
    $data = array();
 
    $xprofile_structure = require_once( BP_DEFAULT_DATA_DIR . 'data/xprofile_structure.php' );
 
    // Firstly, import profile groups.
    foreach ( $xprofile_structure as $group_type => $group_data ) {
        $group_id = xprofile_insert_field_group( array(
            'name'        => $group_data['name'],
            'description' => $group_data['desc'],
        ) );
        $groups[] = $group_id;
 
        // Then import fields.
        foreach ( $group_data['fields'] as $field_type => $field_data ) {
            $field_id = xprofile_insert_field( array(
                'field_group_id' => $group_id,
                'parent_id'      => 0,
                'type'           => $field_type,
                'name'           => $field_data['name'],
                'description'    => $field_data['desc'],
                'is_required'    => $field_data['required'],
                'order_by'       => 'custom',
            ) );
 
            if ( $field_id ) {
                bp_xprofile_update_field_meta( $field_id, 'default_visibility', $field_data['default-visibility'] );
 
                bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $field_data['allow-custom-visibility'] );
 
                $data[ $field_id ]['type'] = $field_type;
 
                // finally import options
                if ( ! empty( $field_data['options'] ) ) {
                    foreach ( $field_data['options'] as $option ) {
                        $option_id = xprofile_insert_field( array(
                            'field_group_id'    => $group_id,
                            'parent_id'         => $field_id,
                            'type'              => 'option',
                            'name'              => $option['name'],
                            'can_delete'        => true,
                            'is_default_option' => $option['is_default_option'],
                            'option_order'      => $option['option_order'],
                        ) );
 
                        $data[ $field_id ]['options'][ $option_id ] = $option['name'];
                    }
                } else {
                    $data[ $field_id ]['options'] = array();
                }
            }
        }
    }
 
    $xprofile_data = require_once( BP_DEFAULT_DATA_DIR . 'data/xprofile_data.php' );
    $users         = bp_dd_get_random_users_ids( 0 );
 
    // Now import profile fields data for all fields for each user.
    foreach ( $users as $user_id ) {
        foreach ( $data as $field_id => $field_data ) {
            switch ( $field_data['type'] ) {
                case 'datebox':
                case 'textarea':
                case 'number':
                case 'textbox':
                case 'url':
                case 'selectbox':
                case 'radio':
                    if ( xprofile_set_field_data( $field_id, $user_id, $xprofile_data[ $field_data['type'] ][ array_rand( $xprofile_data[ $field_data['type'] ] ) ] ) ) {
                        $count ++;
                    }
                    break;
 
                case 'checkbox':
                case 'multiselectbox':
                    if ( xprofile_set_field_data( $field_id, $user_id, explode( ',', $xprofile_data[ $field_data['type'] ][ array_rand( $xprofile_data[ $field_data['type'] ] ) ] ) ) ) {
                        $count ++;
                    }
                    break;
            }
        }
    }
 
    if ( ! empty( $groups ) ) {
        /** @noinspection PhpParamsInspection */
        bp_update_option( 'bp_dd_imported_user_xprofile_ids', $groups );
    }
 
    return $count;
}

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.