PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.3
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.3
2.0 1.7.1 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.5 1.6 1.7 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8
import-users / importExtensions / UsersImport.php

UsersImport.php in Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on 1.3, at importExtensions/UsersImport.php

205 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Import Users plugin file.
4 *
5 * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com
6 */
7
8 namespace Smackcoders\SMUSERS;
9
10 if ( ! defined( 'ABSPATH' ) )
11 exit; // Exit if accessed directly
12
13 class UsersImport{
14 private static $users_instance = null,$send_user_password;
15
16 public static function getInstance() {
17 if (UsersImport::$users_instance == null) {
18 UsersImport::$users_instance = new UsersImport;
19 return UsersImport::$users_instance;
20 }
21 return UsersImport::$users_instance;
22 }
23
24 public function users_import_function ($data_array, $mode , $hash_key , $line_number) {
25
26 global $wpdb,$core_instance;
27 $helpers_instance = ImportHelpers::getInstance();
28 $returnArr = array();
29 $log_table_name = $wpdb->prefix ."import_detail_log";
30
31 $updated_row_counts = $helpers_instance->update_count($hash_key);
32 $created_count = $updated_row_counts['created'];
33 $updated_count = $updated_row_counts['updated'];
34 $skipped_count = $updated_row_counts['skipped'];
35
36 $data_array['role'] = trim(isset($data_array['role']));
37 if ( isset( $data_array['role'] ) && $data_array['role'] != '') {
38 $user_capability = '';
39 if ( !is_numeric( $data_array['role'] ) ) {
40 $roles = $this->getRoles();
41 if(array_key_exists($data_array['role'], $roles)) {
42 $user_capability = $data_array['role'];
43 }
44 } else {
45 for ( $i = 0; $i <= $data_array['role']; $i ++ ) {
46 $user_capability .= $i . ",";
47 }
48 $roles = $this->getRoles('cap');
49 if(in_array( $user_capability, $roles )) {
50 foreach ( $roles as $rkey => $rval ) {
51 if ( $rval == $user_capability ) {
52 $user_capability = $rkey;
53 }
54 }
55 } else {
56 $user_capability = ''; #TODO: Add log message for assigning the default role
57 }
58 }
59 if($user_capability != '')
60 $data_array['role'] = $user_capability;
61 else
62 $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
63 } else {
64 $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
65 }
66 $user_email = $data_array['user_email'];
67
68 //filter to change user meta values before import
69 $data_array = apply_filters('smack_csv_modify_userdata_filter', $data_array);
70
71 if ( $mode == 'Insert' ) {
72 $send_password = $data_array['user_pass'] ;
73 if ( empty( $data_array['user_pass'] ) ) {
74 $data_array['user_pass'] = wp_generate_password( 12, false );
75 $additional_meta_info = array(
76 'user_login' => $data_array['user_login'],
77 'user_pass' => $data_array['user_pass'],
78 'user_email' => $data_array['user_email'],
79 'role' => $data_array['role']
80 );
81 $data_array['smack_uci_import'] = $additional_meta_info;
82 }
83 else{
84 if (strlen($data_array['user_pass'])!== 34 && $data_array['user_pass'][0]!=='$'){
85 $data_array['user_pass']=wp_hash_password($data_array['user_pass']);
86 }
87 $additional_meta_info = array(
88 'user_login' => $data_array['user_login'],
89 'user_pass' => $data_array['user_pass'],
90 'user_email' => $data_array['user_email'],
91 'role' => $data_array['role']
92 );
93 $data_array['smack_uci_import'] = $additional_meta_info;
94 }
95 $retID = wp_insert_user($data_array);
96 update_user_meta($retID, 'sendPassword', $send_password);
97 if ( !is_wp_error($retID) && !empty( $data_array['user_pass'] ) ) {
98 $wpdb->get_results("UPDATE {$wpdb->prefix}users SET user_pass = '{$data_array['user_pass']}' WHERE ID = $retID");
99 }
100 $mode_of_affect = 'Inserted';
101
102 if ( is_wp_error( $retID ) ) {
103 $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
104 $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
105 return array('MODE' => $mode);
106 }
107 $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
108 $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
109
110 } else {
111 if ( $mode == 'Update') {
112
113 $update_query = $wpdb->prepare( "select ID from {$wpdb->prefix}users where user_email = %s order by ID DESC", $user_email );
114 $ID_result = $wpdb->get_results( $update_query );
115 if ( is_array( $ID_result ) && ! empty( $ID_result ) ) {
116 $retID = $ID_result[0]->ID;
117 $data_array['ID'] = $retID;
118 wp_update_user( $data_array );
119 $mode_of_affect = 'Updated';
120
121 $core_instance->detailed_log[$line_number]['Message'] = 'Updated User ID: ' . $retID;
122 $fields = $wpdb->get_results("UPDATE $log_table_name SET updated = $updated_count WHERE hash_key = '$hash_key'");
123
124 }else{
125 $retID = wp_insert_user($data_array);
126 $mode_of_affect = 'Inserted';
127
128 if ( is_wp_error( $retID ) ) {
129
130 $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
131 $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
132 return array('MODE' => $mode);
133 }
134 $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
135 $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
136 }
137 }
138 }
139 $metaData = array();
140 foreach ( $data_array as $daKey => $daVal ) {
141
142 switch ( $daKey ) {
143 case 'biographical_info' :
144 $metaData['description'] = $data_array[ $daKey ];
145 break;
146 case 'disable_visual_editor' :
147 $metaData['rich_editing'] = $data_array[ $daKey ];
148 break;
149 case 'enable_keyboard_shortcuts':
150 $metaData['comment_shortcuts'] = $data_array[ $daKey ];
151 break;
152 case 'admin_color':
153 $metaData['admin_color'] = $data_array[ $daKey ];
154 break;
155 case 'show_toolbar':
156 $metaData['show_admin_bar_front'] = $data_array[ $daKey ];
157 break;
158 case 'smack_uci_import':
159 $metaData['smack_uci_import'] = $data_array[ $daKey ];
160 }
161 }
162
163 if ( ! empty ( $metaData ) ) {
164 foreach ( $metaData as $meta_key => $meta_value ) {
165 update_user_meta( $retID, $meta_key, $meta_value );
166
167 //filter for modifying user metadata after import
168 apply_filters('smack_csv_modify_metadata_filter', $retID, $meta_key, $meta_value);
169 }
170 }
171 $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email'];
172 $core_instance->detailed_log[$line_number]['Role'] = $data_array['role'];
173 $ucisettings = get_option('sm_uci_pro_settings');
174 if(isset($ucisettings['send_user_password']) && $ucisettings['send_user_password'] == "true") {
175 $send_user_password = new SendPassword;
176 $send_user_password->send_login_credentials_to_users();
177 }
178 $returnArr['ID'] = $retID;
179 $returnArr['MODE'] = $mode_of_affect;
180 return $returnArr;
181 }
182
183 public function getRoles($capability = null) {
184 global $wp_roles;
185 $roles = array();
186 if($capability != null) {
187 foreach ( $wp_roles->roles as $rkey => $rval ) {
188 $roles[ $rkey ] = '';
189 for ( $cnt = 0; $cnt < count( $rval['capabilities'] ); $cnt ++ ) {
190 $findval = "level_" . $cnt;
191 if ( array_key_exists( $findval, $rval['capabilities'] ) ) {
192 $roles[ $rkey ] = $roles[ $rkey ] . $cnt . ',';
193 }
194 }
195 }
196 } else {
197 if ( ! isset( $wp_roles ) )
198 $wp_roles = new \WP_Roles();
199
200 $roles = $wp_roles->get_names();
201 }
202 return $roles;
203 }
204 }
205