Last active
January 16, 2024 07:36
-
-
Save webtoffee-git/4e26e049b9bfbd9e023cc8209b10bc7b to your computer and use it in GitHub Desktop.
Import/Export BuddyPress Users-WordPress Users/Customers Import/Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Do not copy this line of code | |
add_filter('wt_user_alter_csv_header', 'wt_user_alter_csv_header'); | |
add_filter('hf_customer_csv_export_data', 'hf_customer_csv_export_data'); | |
add_action('wt_customer_csv_import_data', 'wt_customer_csv_import_data',10,2); | |
add_filter('wt_iew_alter_export_data', 'wt_iew_alter_export_data',10,6); | |
add_filter('wt_user_importer_pre_parse_data', 'wt_user_importer_pre_parse_data'); | |
function wt_user_alter_csv_header($row) { | |
global $wpdb; | |
$field_id = $wpdb->get_col('SELECT id FROM ' . $wpdb->prefix . "bp_xprofile_fields where parent_id= 0"); | |
foreach ($field_id as $id) { | |
$obj_fname = xprofile_get_field($id); | |
$fname = $obj_fname->name; | |
$row['bp_xprofile:' . $fname] = 'meta:bp_xprofile:' . $fname; | |
} | |
$row['medlemstyp'] = 'tax:medlemstyp'; | |
$row['bp_xprofile_group_data'] = 'meta:bp_xprofilegroup'; | |
$row['bp_group_data'] = 'meta:bp_group_data'; | |
$row['membership'] = 'meta:membership_wt'; | |
return $row; | |
} | |
function hf_customer_csv_export_data($customer_data) { | |
$id = $customer_data['customer_id']; | |
$membership = bp_get_member_type($id,false); | |
$user = get_user_by('id', $id); | |
global $wpdb; | |
$field_id = $wpdb->get_col('SELECT id FROM ' . $wpdb->prefix . "bp_xprofile_fields where parent_id= 0"); | |
foreach ($field_id as $id) { | |
$obj_fname = xprofile_get_field($id); | |
$fname = $obj_fname->name; | |
// $customer_data['bp_xprofile:' . $fname] = xprofile_get_field_data($id, $user->ID); | |
if (is_array(xprofile_get_field_data($id, $user->ID))) { | |
$array_data = xprofile_get_field_data($id, $user->ID); | |
$seriali_data = serialize($array_data); | |
$customer_data['bp_xprofile:' . $fname] = $seriali_data; | |
} else { | |
$customer_data['bp_xprofile:' . $fname] = xprofile_get_field_data($id, $user->ID); | |
} | |
} | |
$term_name = array(); | |
foreach ($membership as $key => $mvalue) { | |
$term_data = get_term_by('slug', $mvalue, 'bp_member_type'); | |
if($term_data->term_id){ | |
$term_name[] = get_term_meta( $term_data->term_id, 'bp_type_singular_name', true); | |
} | |
} | |
$customer_data['medlemstyp'] = $term_name ? implode(",",$term_name) : $membership; | |
$xprof_groups = $wpdb->get_results('SELECT xg.ID,xf.id as act_parent,xg.name as gname,xf.name as fname,xf.parent_id,xf.type FROM ' . $wpdb->prefix . "bp_xprofile_groups xg INNER JOIN " . $wpdb->prefix . "bp_xprofile_fields xf ON xg.id = xf.group_id", ARRAY_A); | |
$bp_group_data_flag = get_option('Wt_bp_gp_data'); | |
if ($bp_group_data_flag != 'set') { | |
$customer_data['bp_xprofile_group_data'] = serialize($xprof_groups); | |
add_option('Wt_bp_gp_data', 'set'); | |
} | |
$bp_group_data = $wpdb->get_results('SELECT u.group_id,m.name,m.slug,m.description,m.status FROM ' . $wpdb->prefix . "bp_groups_members u INNER JOIN " . $wpdb->prefix . "bp_groups m ON m.id = u.group_id WHERE u.user_id =" . $user->ID, ARRAY_A); | |
$customer_data['bp_group_data'] = $bp_group_data ? serialize($bp_group_data) : ''; | |
$customer_data['membership'] = implode(",", $membership); | |
return $customer_data; | |
} | |
function wt_customer_csv_import_data($parsed_item, $user_id) { | |
$data = get_user_meta($user_id, 'membership_wt', true); | |
if (!empty($data)) { | |
$membership = explode(",", $data); | |
foreach ($membership as $key => $mvalue) { | |
$term_id = term_exists($mvalue); | |
if ($term_id) { | |
bp_set_member_type($user_id, $mvalue, false); | |
//wp_set_post_terms($user_id, array($mvalue), 'bp_member_type', false); | |
} | |
} | |
} | |
@delete_user_meta($user_id, 'membership_wt'); | |
foreach ($parsed_item['meta_mapping_fields']['meta'] as $xp_key => $xp_value) { | |
if (strstr($xp_key, 'meta:bp_xprofile:') || $xp_key == 'meta:bp_xprofilegroup' || $xp_key == 'meta:bp_group_data') { | |
$xp_array[$xp_key] = $xp_value; | |
} | |
} | |
foreach ($xp_array as $item_keys => $item_values) { | |
if (strstr($item_keys, 'meta:bp_')) { | |
$bp_data[ltrim($item_keys, 'meta:')] = $item_values; | |
} | |
} | |
foreach ($bp_data as $key => $data) { | |
if (strstr($key, 'bp_xprofilegroup')) { | |
$xprofilegroup = unserialize($data); | |
} | |
} | |
global $wpdb; | |
foreach ($xprofilegroup as $gf_key => $gf_value) { | |
$gname = $gf_value['gname']; | |
$xp_group_data = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_groups where name=\"$gname\""); | |
if ($xp_group_data) { | |
$metakey = 'wt_xprofile_connect_parent'; | |
$fname = $gf_value['fname']; | |
$ftype = $gf_value['type']; | |
$act_parent = $gf_value['act_parent']; | |
$parent_id_xp = $gf_value['parent_id']; | |
$gid = $xp_group_data[0]; | |
$xp_field_data = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_fields where name=\"$fname\" and group_id=$xp_group_data[0]"); | |
if (!$xp_field_data) { | |
if ($act_parent > 0) { | |
$xp_field_data_parent = $wpdb->get_col("SELECT object_id FROM {$wpdb->prefix}bp_xprofile_meta where meta_key=\"$metakey\" and meta_value=\"$parent_id_xp\""); | |
$act_parents = $xp_field_data_parent[0] ? $xp_field_data_parent[0] : 0; | |
} | |
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_fields (name,group_id,parent_id,type) VALUES ('$fname',$gid,$act_parents,'$ftype')"); | |
$fid = $wpdb->insert_id; | |
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_meta (object_id,meta_key,meta_value) VALUES ($fid,'$metakey',$act_parent)"); | |
//create | |
} | |
} else { | |
$id = $wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_groups (name) VALUES ('$gname')"); | |
$lastid = $wpdb->insert_id; | |
$fname = $gf_value['fname']; | |
$ftype = $gf_value['type']; | |
$act_parent = $gf_value['act_parent']; | |
$parent_id_xp = $gf_value['parent_id']; | |
$metakey = 'wt_xprofile_connect_parent'; | |
$gid = $lastid; | |
if ($act_parent > 0) { | |
$xp_field_data_parent = $wpdb->get_col("SELECT object_id FROM {$wpdb->prefix}bp_xprofile_meta where meta_key=\"$metakey\" and meta_value=\"$parent_id_xp\""); | |
$act_parents = $xp_field_data_parent[0] ? $xp_field_data_parent[0] : 0; | |
} | |
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_fields (name,group_id,parent_id,type) VALUES ('$fname',$gid,$act_parents,'$ftype')"); | |
$fid = $wpdb->insert_id; | |
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_meta (object_id,meta_key,meta_value) VALUES ($fid,'$metakey',$act_parent)"); | |
//create | |
} | |
} | |
$wpdb->query("DELETE FROM {$wpdb->prefix}bp_xprofile_meta WHERE meta_key=\"$metakey\" "); | |
foreach ($bp_data as $xpro_key => $xpro_value) { | |
if (strstr($xpro_key, 'bp_xprofile:')) { | |
$dp_xfield = explode(":", $xpro_key); | |
$key = $dp_xfield[1]; | |
$xp_field_ids = $wpdb->get_col("SELECT id FROM {$wpdb->prefix}bp_xprofile_fields where name=\"$key\""); | |
$xp_field_id = $xp_field_ids[0]; | |
$wpdb->query("INSERT INTO {$wpdb->prefix}bp_xprofile_data (field_id,user_id,value) VALUES ($xp_field_id,$user_id,'$xpro_value')"); | |
} | |
} | |
return $parsed_item; | |
} | |
function wt_iew_alter_export_data($export_data, $offset, $is_last_offset, $file_as, $to_export, $csv_delimiter) { | |
if ($is_last_offset) { | |
delete_option('Wt_bp_gp_data'); | |
} | |
return $export_data; | |
} | |
function wt_user_importer_pre_parse_data($data) { | |
foreach ($data['meta_mapping_fields']['meta'] as $x_key => $x_value) { | |
if (strstr($x_key, 'meta:bp_xprofile:') || $x_key == 'meta:bp_xprofilegroup' || $x_key == 'meta:bp_group_data') { | |
$x_array[$x_key] = $x_value; | |
unset($data['meta_mapping_fields']['meta'][$x_key]); | |
} | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment