PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.7
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.7
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
← All changes | importExtensions/UsersImport.php +77 -211 trunk1.7 View file →
@@ -20,31 +20,25 @@
20 20 }
21 21 return UsersImport::$users_instance;
22 22 }
23 23
24 - public function users_import_function ($data_array, $mode, $hash_key, $line_number, $check = '', $update_based_on = 'normal', $duplicate_action = 'skip') {
25 -
24 + public function users_import_function ($data_array, $mode , $hash_key , $line_number) {
25 +
26 26 global $wpdb,$core_instance;
27 27 $helpers_instance = ImportHelpers::getInstance();
28 28 $returnArr = array();
29 29 $log_table_name = $wpdb->prefix ."import_detail_log";
30 - $retID = 0;
31 - $mode_of_affect = 'Inserted';
32 30
33 - $update_based_on = in_array($update_based_on, array('normal', 'skip'), true) ? $update_based_on : 'normal';
34 - $duplicate_action = in_array($duplicate_action, array('skip', 'update', 'create'), true) ? $duplicate_action : 'skip';
35 - $user_match_fields = array('ID', 'user_email');
36 -
37 31 $updated_row_counts = $helpers_instance->update_count($hash_key);
38 32 $created_count = $updated_row_counts['created'];
39 33 $updated_count = $updated_row_counts['updated'];
40 34 $skipped_count = $updated_row_counts['skipped'];
41 35
42 - $data_array['role'] = trim(isset($data_array['role']) ? $data_array['role'] : '');
36 + $data_array['role'] = trim(isset($data_array['role']));
43 37 if ( isset( $data_array['role'] ) && $data_array['role'] != '') {
44 38 $user_capability = '';
45 - if ( !is_numeric( $data_array['role'] ) ) {
46 - $roles = $this->getRoles();
39 + if ( !is_numeric( $data_array['role'] ) ) {
40 + $roles = $this->getRoles();
47 41 if(array_key_exists($data_array['role'], $roles)) {
48 42 $user_capability = $data_array['role'];
49 43 }
50 44 } else {
@@ -58,106 +52,97 @@
58 52 $user_capability = $rkey;
59 53 }
60 54 }
61 55 } else {
62 - $user_capability = '';
56 + $user_capability = ''; #TODO: Add log message for assigning the default role
63 57 }
64 58 }
65 59 if($user_capability != '')
66 60 $data_array['role'] = $user_capability;
67 61 else
68 - $data_array['role'] = 'subscriber';
62 + $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
69 63 } else {
70 - $data_array['role'] = 'subscriber';
64 + $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
71 65 }
66 + $user_email = $data_array['user_email'];
72 67
68 + //filter to change user meta values before import
73 69 $data_array = apply_filters('smack_csv_modify_userdata_filter', $data_array);
74 70
75 - $existing_id = $this->find_existing_user_id($data_array, $check);
76 - $has_match = $existing_id > 0;
77 - $duplicate_handling_active = (
78 - $update_based_on === 'normal'
79 - && !empty($check)
80 - && in_array($check, $user_match_fields, true)
81 - );
82 -
83 - // Content Update: do not create when no matching record (UpdateUsing = skip).
84 - if ($update_based_on === 'skip' && !empty($check) && in_array($check, $user_match_fields, true) && !$has_match) {
85 - $core_instance->detailed_log[$line_number]['Message'] = 'Skipped. No matching record found.';
86 - $core_instance->detailed_log[$line_number]['state'] = 'Skipped';
87 - $wpdb->update(
88 - $log_table_name,
89 - array( 'skipped' => (int) $skipped_count ),
90 - array( 'hash_key' => $hash_key ),
91 - array( '%d' ),
92 - array( '%s' )
93 - );
94 - return array('MODE' => $mode);
95 - }
96 -
97 - // Duplicate handling (Insert or Update mode): honor DuplicateAction from import configuration.
98 - if ($duplicate_handling_active && $has_match && $duplicate_action === 'skip') {
99 - $core_instance->detailed_log[$line_number]['Message'] = 'Skipped, Due to duplicate User found!.';
100 - $core_instance->detailed_log[$line_number]['state'] = 'Skipped';
101 - $core_instance->detailed_log[$line_number]['id'] = $existing_id;
102 - $wpdb->update(
103 - $log_table_name,
104 - array( 'skipped' => (int) $skipped_count ),
105 - array( 'hash_key' => $hash_key ),
106 - array( '%d' ),
107 - array( '%s' )
108 - );
109 - return array('MODE' => $mode, 'ID' => $existing_id);
110 - }
111 -
112 - if ($duplicate_handling_active && $has_match && $duplicate_action === 'update') {
113 - $data_array['ID'] = $existing_id;
114 - $result = $this->update_existing_user($data_array, $hash_key, $line_number, $log_table_name, $updated_count);
115 - if ($result === false) {
116 - $core_instance->detailed_log[$line_number]['Message'] = 'Skipped, Due to duplicate User update failed!.';
117 - $core_instance->detailed_log[$line_number]['state'] = 'Skipped';
118 - $wpdb->update(
119 - $log_table_name,
120 - array( 'skipped' => (int) $skipped_count ),
121 - array( 'hash_key' => $hash_key ),
122 - array( '%d' ),
123 - array( '%s' )
124 - );
125 - return array('MODE' => $mode);
71 + if ( $mode == 'Insert' ) {
72 + $send_password = isset($data_array['user_pass']) ? $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;
126 94 }
127 - $retID = $result['ID'];
128 - $mode_of_affect = $result['MODE'];
129 - } elseif ($duplicate_handling_active && $has_match && $duplicate_action === 'create') {
130 - $result = $this->insert_new_user($data_array, $hash_key, $line_number, $log_table_name, $created_count, $skipped_count);
131 - if ($result === false) {
132 - return array('MODE' => $mode);
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");
133 99 }
134 - $retID = $result['ID'];
135 - $mode_of_affect = $result['MODE'];
136 - } elseif ($mode === 'Update' && $has_match) {
137 - $data_array['ID'] = $existing_id;
138 - $result = $this->update_existing_user($data_array, $hash_key, $line_number, $log_table_name, $updated_count);
139 - if ($result === false) {
140 - $core_instance->detailed_log[$line_number]['Message'] = 'Skipped, Due to duplicate User update failed!.';
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!.";
141 104 $core_instance->detailed_log[$line_number]['state'] = 'Skipped';
142 - $wpdb->update(
143 - $log_table_name,
144 - array( 'skipped' => (int) $skipped_count ),
145 - array( 'hash_key' => $hash_key ),
146 - array( '%d' ),
147 - array( '%s' )
148 - );
105 + $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
149 106 return array('MODE' => $mode);
150 107 }
151 - $retID = $result['ID'];
152 - $mode_of_affect = $result['MODE'];
108 + $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
109 + $core_instance->detailed_log[$line_number]['state'] = 'Inserted';
110 + $core_instance->detailed_log[$line_number]['id'] = $retID;
111 + $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
112 +
153 113 } else {
154 - $result = $this->insert_new_user($data_array, $hash_key, $line_number, $log_table_name, $created_count, $skipped_count);
155 - if ($result === false) {
156 - return array('MODE' => $mode);
114 + if ( $mode == 'Update') {
115 + $user_email = isset($data_array['user_email']) ? $data_array['user_email'] : '';
116 + $update_query = $wpdb->prepare( "select ID from {$wpdb->prefix}users where user_email = %s order by ID DESC", $user_email );
117 + $ID_result = $wpdb->get_results( $update_query );
118 + if ( is_array( $ID_result ) && ! empty( $ID_result ) ) {
119 + $retID = $ID_result[0]->ID;
120 + $data_array['ID'] = $retID;
121 + wp_update_user( $data_array );
122 + $mode_of_affect = 'Updated';
123 +
124 + $core_instance->detailed_log[$line_number]['Message'] = 'Updated User ID: ' . $retID;
125 + $core_instance->detailed_log[$line_number]['state'] = 'Updated';
126 + $core_instance->detailed_log[$line_number]['id'] = $retID;
127 + $fields = $wpdb->get_results("UPDATE $log_table_name SET updated = $updated_count WHERE hash_key = '$hash_key'");
128 +
129 + }else{
130 + $retID = wp_insert_user($data_array);
131 + $mode_of_affect = 'Inserted';
132 +
133 + if ( is_wp_error( $retID ) ) {
134 +
135 + $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
136 + $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
137 + return array('MODE' => $mode);
138 + }
139 + $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
140 + $core_instance->detailed_log[$line_number]['state'] = 'Inserted';
141 + $core_instance->detailed_log[$line_number]['id'] = $retID;
142 + $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
143 + }
157 144 }
158 - $retID = $result['ID'];
159 - $mode_of_affect = $result['MODE'];
160 145 }
161 146 $metaData = array();
162 147 foreach ( $data_array as $daKey => $daVal ) {
163 148
@@ -202,127 +187,8 @@
202 187 }
203 188 $returnArr['ID'] = $retID;
204 189 $returnArr['MODE'] = $mode_of_affect;
205 190 return $returnArr;
206 - }
207 -
208 - private function find_existing_user_id($data_array, $check) {
209 - if (empty($check)) {
210 - return 0;
211 - }
212 - if ($check === 'ID') {
213 - $id = isset($data_array['ID']) ? trim((string) $data_array['ID']) : '';
214 - if ($id !== '' && is_numeric($id)) {
215 - $user_id = absint($id);
216 - if ($user_id > 0) {
217 - $user = get_user_by('id', $user_id);
218 - return $user ? (int) $user->ID : 0;
219 - }
220 - }
221 - return 0;
222 - }
223 - if ($check === 'user_email') {
224 - $email = isset($data_array['user_email']) ? trim((string) $data_array['user_email']) : '';
225 - if ($email !== '' && is_email($email)) {
226 - $user = get_user_by('email', $email);
227 - return $user ? (int) $user->ID : 0;
228 - }
229 - return 0;
230 - }
231 - return 0;
232 - }
233 -
234 - private function prepare_user_credentials(&$data_array) {
235 - $send_password = isset($data_array['user_pass']) ? $data_array['user_pass'] : '';
236 - if ( empty( $data_array['user_pass'] ) ) {
237 - $data_array['user_pass'] = wp_generate_password( 12, false );
238 - $additional_meta_info = array(
239 - 'user_login' => $data_array['user_login'],
240 - 'user_pass' => $data_array['user_pass'],
241 - 'user_email' => $data_array['user_email'],
242 - 'role' => $data_array['role'],
243 - );
244 - $data_array['smack_uci_import'] = $additional_meta_info;
245 - } else {
246 - if (strlen($data_array['user_pass']) !== 34 && $data_array['user_pass'][0] !== '$') {
247 - $data_array['user_pass'] = wp_hash_password($data_array['user_pass']);
248 - }
249 - $additional_meta_info = array(
250 - 'user_login' => $data_array['user_login'],
251 - 'user_pass' => $data_array['user_pass'],
252 - 'user_email' => $data_array['user_email'],
253 - 'role' => $data_array['role'],
254 - );
255 - $data_array['smack_uci_import'] = $additional_meta_info;
256 - }
257 - return $send_password;
258 - }
259 -
260 - private function insert_new_user($data_array, $hash_key, $line_number, $log_table_name, $created_count, $skipped_count) {
261 - global $wpdb, $core_instance;
262 - unset($data_array['ID']);
263 - $send_password = $this->prepare_user_credentials($data_array);
264 - $retID = wp_insert_user($data_array);
265 - if ( ! is_wp_error( $retID ) ) {
266 - update_user_meta($retID, 'sendPassword', $send_password);
267 - if ( ! empty( $data_array['user_pass'] ) ) {
268 - $wpdb->update(
269 - $wpdb->users,
270 - array( 'user_pass' => $data_array['user_pass'] ),
271 - array( 'ID' => (int) $retID ),
272 - array( '%s' ),
273 - array( '%d' )
274 - );
275 - }
276 - $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
277 - $core_instance->detailed_log[$line_number]['state'] = 'Inserted';
278 - $core_instance->detailed_log[$line_number]['id'] = $retID;
279 - $wpdb->update(
280 - $log_table_name,
281 - array( 'created' => (int) $created_count ),
282 - array( 'hash_key' => $hash_key ),
283 - array( '%d' ),
284 - array( '%s' )
285 - );
286 - return array('ID' => $retID, 'MODE' => 'Inserted');
287 - }
288 - $core_instance->detailed_log[$line_number]['Message'] = 'Skipped, Due to duplicate User found with same email!.';
289 - $core_instance->detailed_log[$line_number]['state'] = 'Skipped';
290 - $wpdb->update(
291 - $log_table_name,
292 - array( 'skipped' => (int) $skipped_count ),
293 - array( 'hash_key' => $hash_key ),
294 - array( '%d' ),
295 - array( '%s' )
296 - );
297 - return false;
298 - }
299 -
300 - private function update_existing_user($data_array, $hash_key, $line_number, $log_table_name, $updated_count) {
301 - global $wpdb, $core_instance;
302 - $update_data = $data_array;
303 - if (isset($update_data['user_pass']) && $update_data['user_pass'] !== '') {
304 - if (strlen($update_data['user_pass']) !== 34 || $update_data['user_pass'][0] !== '$') {
305 - $update_data['user_pass'] = wp_hash_password($update_data['user_pass']);
306 - }
307 - } else {
308 - unset($update_data['user_pass']);
309 - }
310 - $retID = wp_update_user($update_data);
311 - if ( is_wp_error( $retID ) ) {
312 - return false;
313 - }
314 - $core_instance->detailed_log[$line_number]['Message'] = 'Updated User ID: ' . $data_array['ID'];
315 - $core_instance->detailed_log[$line_number]['state'] = 'Updated';
316 - $core_instance->detailed_log[$line_number]['id'] = $data_array['ID'];
317 - $wpdb->update(
318 - $log_table_name,
319 - array( 'updated' => (int) $updated_count ),
320 - array( 'hash_key' => $hash_key ),
321 - array( '%d' ),
322 - array( '%s' )
323 - );
324 - return array('ID' => $data_array['ID'], 'MODE' => 'Updated');
325 191 }
326 192
327 193 public function getRoles($capability = null) {
328 194 global $wp_roles;