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 +228 -71 1.2.1trunk View file →
@@ -1,11 +1,10 @@
1 1 <?php
2 -/******************************************************************************************
3 - * Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License
4 - * Unauthorized copying of this file, via any medium is strictly prohibited
5 - * Proprietary and confidential
6 - * You can contact Smackcoders at email address info@smackcoders.com.
7 - *******************************************************************************************/
2 +/**
3 + * Import Users plugin file.
4 + *
5 + * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com
6 + */
8 7
9 8 namespace Smackcoders\SMUSERS;
10 9
11 10 if ( ! defined( 'ABSPATH' ) )
@@ -12,9 +11,9 @@
12 11 exit; // Exit if accessed directly
13 12
14 13 class UsersImport{
15 14 private static $users_instance = null,$send_user_password;
16 -
15 + public $plugin;
17 16 public static function getInstance() {
18 17 if (UsersImport::$users_instance == null) {
19 18 UsersImport::$users_instance = new UsersImport;
20 19 return UsersImport::$users_instance;
@@ -21,25 +20,31 @@
21 20 }
22 21 return UsersImport::$users_instance;
23 22 }
24 23
25 - public function users_import_function ($data_array, $mode , $hash_key , $line_number) {
26 -
24 + public function users_import_function ($data_array, $mode, $hash_key, $line_number, $check = '', $update_based_on = 'normal', $duplicate_action = 'skip') {
25 +
27 26 global $wpdb,$core_instance;
28 27 $helpers_instance = ImportHelpers::getInstance();
29 28 $returnArr = array();
30 29 $log_table_name = $wpdb->prefix ."import_detail_log";
30 + $retID = 0;
31 + $mode_of_affect = 'Inserted';
31 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 +
32 37 $updated_row_counts = $helpers_instance->update_count($hash_key);
33 38 $created_count = $updated_row_counts['created'];
34 39 $updated_count = $updated_row_counts['updated'];
35 40 $skipped_count = $updated_row_counts['skipped'];
36 41
37 - $data_array['role'] = trim($data_array['role']);
42 + $data_array['role'] = trim(isset($data_array['role']) ? $data_array['role'] : '');
38 43 if ( isset( $data_array['role'] ) && $data_array['role'] != '') {
39 44 $user_capability = '';
40 - if ( !is_numeric( $data_array['role'] ) ) {
41 - $roles = $this->getRoles();
45 + if ( !is_numeric( $data_array['role'] ) ) {
46 + $roles = $this->getRoles();
42 47 if(array_key_exists($data_array['role'], $roles)) {
43 48 $user_capability = $data_array['role'];
44 49 }
45 50 } else {
@@ -53,79 +58,106 @@
53 58 $user_capability = $rkey;
54 59 }
55 60 }
56 61 } else {
57 - $user_capability = ''; #TODO: Add log message for assigning the default role
62 + $user_capability = '';
58 63 }
59 64 }
60 65 if($user_capability != '')
61 66 $data_array['role'] = $user_capability;
62 67 else
63 - $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
68 + $data_array['role'] = 'subscriber';
64 69 } else {
65 - $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role
70 + $data_array['role'] = 'subscriber';
66 71 }
67 - $user_email = $data_array['user_email'];
68 72
69 - if ( $mode == 'Insert' ) {
70 - if ( empty( $data_array['user_pass'] ) ) {
71 - $data_array['user_pass'] = wp_generate_password( 12, false );
72 - $additional_meta_info = array(
73 - 'user_login' => $data_array['user_login'],
74 - 'user_pass' => $data_array['user_pass'],
75 - 'user_email' => $data_array['user_email'],
76 - 'role' => $data_array['role']
77 - );
78 - $data_array['smack_uci_import'] = $additional_meta_info;
79 - }
80 - else{
81 - $additional_meta_info = array(
82 - 'user_login' => $data_array['user_login'],
83 - 'user_pass' => $data_array['user_pass'],
84 - 'user_email' => $data_array['user_email'],
85 - 'role' => $data_array['role']
86 - );
87 - $data_array['smack_uci_import'] = $additional_meta_info;
73 + $data_array = apply_filters('smack_csv_modify_userdata_filter', $data_array);
74 +
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);
88 126 }
89 - $retID = wp_insert_user($data_array);
90 - $mode_of_affect = 'Inserted';
91 -
92 - if ( is_wp_error( $retID ) ) {
93 - $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
94 - $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
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) {
95 132 return array('MODE' => $mode);
96 133 }
97 - $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
98 - $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
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!.';
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 + );
149 + return array('MODE' => $mode);
150 + }
151 + $retID = $result['ID'];
152 + $mode_of_affect = $result['MODE'];
100 153 } else {
101 - if ( $mode == 'Update') {
102 -
103 - $update_query = $wpdb->prepare( "select ID from {$wpdb->prefix}users where user_email = %s order by ID DESC", $user_email );
104 - $ID_result = $wpdb->get_results( $update_query );
105 - if ( is_array( $ID_result ) && ! empty( $ID_result ) ) {
106 - $retID = $ID_result[0]->ID;
107 - $data_array['ID'] = $retID;
108 - wp_update_user( $data_array );
109 - $mode_of_affect = 'Updated';
110 -
111 - $core_instance->detailed_log[$line_number]['Message'] = 'Updated User ID: ' . $retID;
112 - $fields = $wpdb->get_results("UPDATE $log_table_name SET updated = $updated_count WHERE hash_key = '$hash_key'");
113 -
114 - }else{
115 - $retID = wp_insert_user($data_array);
116 - $mode_of_affect = 'Inserted';
117 -
118 - if ( is_wp_error( $retID ) ) {
119 -
120 - $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
121 - $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
122 - return array('MODE' => $mode);
123 - }
124 - $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
125 - $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
126 - }
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);
127 157 }
158 + $retID = $result['ID'];
159 + $mode_of_affect = $result['MODE'];
128 160 }
129 161 $metaData = array();
130 162 foreach ( $data_array as $daKey => $daVal ) {
131 163
@@ -152,14 +184,20 @@
152 184
153 185 if ( ! empty ( $metaData ) ) {
154 186 foreach ( $metaData as $meta_key => $meta_value ) {
155 187 update_user_meta( $retID, $meta_key, $meta_value );
188 +
189 + //filter for modifying user metadata after import
190 + apply_filters('smack_csv_modify_metadata_filter', $retID, $meta_key, $meta_value);
156 191 }
157 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;
158 196 $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email'];
159 197 $core_instance->detailed_log[$line_number]['Role'] = $data_array['role'];
160 198 $ucisettings = get_option('sm_uci_pro_settings');
161 - if($ucisettings['send_user_password'] == "true") {
199 + if(isset($ucisettings['send_user_password']) && $ucisettings['send_user_password'] == "true") {
162 200 $send_user_password = new SendPassword;
163 201 $send_user_password->send_login_credentials_to_users();
164 202 }
165 203 $returnArr['ID'] = $retID;
@@ -164,8 +202,127 @@
164 202 }
165 203 $returnArr['ID'] = $retID;
166 204 $returnArr['MODE'] = $mode_of_affect;
167 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');
168 325 }
169 326
170 327 public function getRoles($capability = null) {
171 328 global $wp_roles;