load batch of new users
Showing
1 changed file
with
73 additions
and
0 deletions
| ... | @@ -7,6 +7,79 @@ if ( class_exists( 'WP_CLI' ) ) { | ... | @@ -7,6 +7,79 @@ if ( class_exists( 'WP_CLI' ) ) { |
| 7 | campaign_Monitor_LoadData(); | 7 | campaign_Monitor_LoadData(); |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | function import_brokers_from_csv() { | ||
| 11 | $data = []; | ||
| 12 | $row = 0; | ||
| 13 | if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . "broker-import-sheet.csv", "r")) !== FALSE) { | ||
| 14 | while (($rowData = fgetcsv($handle, 1000, ",")) !== FALSE) { | ||
| 15 | if ($row == 0) { | ||
| 16 | ++$row; | ||
| 17 | continue; | ||
| 18 | } | ||
| 19 | $data[] = ['firstname'=>$rowData[0],'lastname'=>$rowData[1],'email'=>$rowData[2],'broker_id'=>$rowData[3]]; | ||
| 20 | } | ||
| 21 | fclose($handle); | ||
| 22 | } | ||
| 23 | |||
| 24 | $brokerages = getBrokerageList(); | ||
| 25 | |||
| 26 | |||
| 27 | foreach($data as $rd) { | ||
| 28 | |||
| 29 | |||
| 30 | // Create a WordPress user | ||
| 31 | $password = wp_generate_password(); | ||
| 32 | $email = sanitize_email($rd['email']); | ||
| 33 | $role = 'broker'; // Set the desired role here | ||
| 34 | $user_name = sanitize_user(substr($email, 0, strpos($email, '@'))); | ||
| 35 | |||
| 36 | $userdata = array( | ||
| 37 | 'user_login' => $user_name, | ||
| 38 | 'first_name' => $rd['firstname'], | ||
| 39 | 'last_name' => $rd['lastname'], | ||
| 40 | 'user_pass' => $password, | ||
| 41 | 'user_email' => $email, | ||
| 42 | 'role' => $role, | ||
| 43 | ); | ||
| 44 | |||
| 45 | $user_id = wp_insert_user($userdata); | ||
| 46 | |||
| 47 | if (!is_wp_error($user_id)) { | ||
| 48 | // User created successfully | ||
| 49 | echo 'User '.$email.' created successfully.'; | ||
| 50 | |||
| 51 | $brokerage = ""; | ||
| 52 | |||
| 53 | foreach($brokerages as $broker) { | ||
| 54 | if($broker['broker_id'] == $rd['broker_id']) { | ||
| 55 | $brokerage = $broker['brokerage']; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | if(empty($brokerage)) { | ||
| 60 | echo 'Brokerage not found for '.$email.'.'; | ||
| 61 | } | ||
| 62 | |||
| 63 | update_user_meta($user_id, 'broker_id', $rd['broker_id']); | ||
| 64 | update_user_meta($user_id, 'brokerage', $brokerage); | ||
| 65 | update_user_meta($user_id, 'wp_capabilities','a:1:{s:6:"broker";b:1;}'); | ||
| 66 | update_user_meta($user_id, 'wp_user_level', 0); | ||
| 67 | update_user_meta($user_id, 'course_points', 0); | ||
| 68 | |||
| 69 | global $wpdb; | ||
| 70 | $wpdb->insert('wp_uam_accessgroup_to_object', ['object_id'=>$user_id,'object_type'=>'_user_','general_object_type'=>'_user_','group_id'=>1,'group_type'=>'UserGroup']); | ||
| 71 | |||
| 72 | } else { | ||
| 73 | // Error creating user | ||
| 74 | echo 'Error creating user: ' . $user_id->get_error_message(); | ||
| 75 | } | ||
| 76 | //s:23:"a:1:{s:6:"broker";b:1;}"; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 10 | //collect rows of a csv file and store them in an associative array | 83 | //collect rows of a csv file and store them in an associative array |
| 11 | function sync_broker_teams_assignment() { | 84 | function sync_broker_teams_assignment() { |
| 12 | 85 | ... | ... |
-
Please register or sign in to post a comment