PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / trunk
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on vtrunk
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 +215 -71 1.4.3trunk View file →
@@ -20,25 +20,31 @@
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) {
25 -
24 + public function users_import_function ($data_array, $mode, $hash_key, $line_number, $check = '', $update_based_on = 'normal', $duplicate_action = 'skip') {
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';
30 32
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 +
31 37 $updated_row_counts = $helpers_instance->update_count($hash_key);
32 38 $created_count = $updated_row_counts['created'];
33 39 $updated_count = $updated_row_counts['updated'];
34 40 $skipped_count = $updated_row_counts['skipped'];
35 41
36 - $data_array['role'] = trim(isset($data_array['role']));
42 + $data_array['role'] = trim(isset($data_array['role']) ? $data_array['role'] : '');
37 43 if ( isset( $data_array['role'] ) && $data_array['role'] != '') {
38 44 $user_capability = '';
39 - if ( !is_numeric( $data_array['role'] ) ) {
40 - $roles = $this->getRoles();
45 + if ( !is_numeric( $data_array['role'] ) ) {
46 + $roles = $this->getRoles();
41 47 if(array_key_exists($data_array['role'], $roles)) {
42 48 $user_capability = $data_array['role'];
43 49 }
44 50 } else {
@@ -52,90 +58,106 @@
52 58 $user_capability = $rkey;
53 59 }
54 60 }
55 61 } else {
56 - $user_capability = ''; #TODO: Add log message for assigning the default role
62 + $user_capability = '';
57 63 }
58 64 }
59 65 if($user_capability != '')
60 66 $data_array['role'] = $user_capability;
61 67 else
62 - $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
68 + $data_array['role'] = 'subscriber';
63 69 } else {
64 - $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
70 + $data_array['role'] = 'subscriber';
65 71 }
66 - $user_email = $data_array['user_email'];
67 72
68 - //filter to change user meta values before import
69 73 $data_array = apply_filters('smack_csv_modify_userdata_filter', $data_array);
70 74
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;
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);
94 126 }
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");
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);
99 133 }
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'");
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!.';
141 + $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 149 return array('MODE' => $mode);
106 150 }
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 -
151 + $retID = $result['ID'];
152 + $mode_of_affect = $result['MODE'];
110 153 } else {
111 - if ( $mode == 'Update') {
112 - $user_email = isset($data_array['user_email']) ? $data_array['user_email'] : '';
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 - }
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);
137 157 }
158 + $retID = $result['ID'];
159 + $mode_of_affect = $result['MODE'];
138 160 }
139 161 $metaData = array();
140 162 foreach ( $data_array as $daKey => $daVal ) {
141 163
@@ -167,8 +189,11 @@
167 189 //filter for modifying user metadata after import
168 190 apply_filters('smack_csv_modify_metadata_filter', $retID, $meta_key, $meta_value);
169 191 }
170 192 }
193 + $user_data = get_userdata($retID);
194 + $user_title = isset($user_data) ? $user_data->display_name : '';
195 + $core_instance->detailed_log[$line_number]['user_title'] = $user_title;
171 196 $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email'];
172 197 $core_instance->detailed_log[$line_number]['Role'] = $data_array['role'];
173 198 $ucisettings = get_option('sm_uci_pro_settings');
174 199 if(isset($ucisettings['send_user_password']) && $ucisettings['send_user_password'] == "true") {
@@ -177,8 +202,127 @@
177 202 }
178 203 $returnArr['ID'] = $retID;
179 204 $returnArr['MODE'] = $mode_of_affect;
180 205 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');
181 325 }
182 326
183 327 public function getRoles($capability = null) {
184 328 global $wp_roles;