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
import-users / importExtensions / UsersImport.php

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

349 lines 12.0 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 public $plugin;
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, $check = '', $update_based_on = 'normal', $duplicate_action = 'skip') {
25
26 global $wpdb,$core_instance;
27 $helpers_instance = ImportHelpers::getInstance();
28 $returnArr = array();
29 $log_table_name = $wpdb->prefix ."import_detail_log";
30 $retID = 0;
31 $mode_of_affect = 'Inserted';
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
37 $updated_row_counts = $helpers_instance->update_count($hash_key);
38 $created_count = $updated_row_counts['created'];
39 $updated_count = $updated_row_counts['updated'];
40 $skipped_count = $updated_row_counts['skipped'];
41
42 $data_array['role'] = trim(isset($data_array['role']) ? $data_array['role'] : '');
43 if ( isset( $data_array['role'] ) && $data_array['role'] != '') {
44 $user_capability = '';
45 if ( !is_numeric( $data_array['role'] ) ) {
46 $roles = $this->getRoles();
47 if(array_key_exists($data_array['role'], $roles)) {
48 $user_capability = $data_array['role'];
49 }
50 } else {
51 for ( $i = 0; $i <= $data_array['role']; $i ++ ) {
52 $user_capability .= $i . ",";
53 }
54 $roles = $this->getRoles('cap');
55 if(in_array( $user_capability, $roles )) {
56 foreach ( $roles as $rkey => $rval ) {
57 if ( $rval == $user_capability ) {
58 $user_capability = $rkey;
59 }
60 }
61 } else {
62 $user_capability = '';
63 }
64 }
65 if($user_capability != '')
66 $data_array['role'] = $user_capability;
67 else
68 $data_array['role'] = 'subscriber';
69 } else {
70 $data_array['role'] = 'subscriber';
71 }
72
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);
126 }
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);
133 }
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'];
153 } 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);
157 }
158 $retID = $result['ID'];
159 $mode_of_affect = $result['MODE'];
160 }
161 $metaData = array();
162 foreach ( $data_array as $daKey => $daVal ) {
163
164 switch ( $daKey ) {
165 case 'biographical_info' :
166 $metaData['description'] = $data_array[ $daKey ];
167 break;
168 case 'disable_visual_editor' :
169 $metaData['rich_editing'] = $data_array[ $daKey ];
170 break;
171 case 'enable_keyboard_shortcuts':
172 $metaData['comment_shortcuts'] = $data_array[ $daKey ];
173 break;
174 case 'admin_color':
175 $metaData['admin_color'] = $data_array[ $daKey ];
176 break;
177 case 'show_toolbar':
178 $metaData['show_admin_bar_front'] = $data_array[ $daKey ];
179 break;
180 case 'smack_uci_import':
181 $metaData['smack_uci_import'] = $data_array[ $daKey ];
182 }
183 }
184
185 if ( ! empty ( $metaData ) ) {
186 foreach ( $metaData as $meta_key => $meta_value ) {
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);
191 }
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;
196 $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email'];
197 $core_instance->detailed_log[$line_number]['Role'] = $data_array['role'];
198 $ucisettings = get_option('sm_uci_pro_settings');
199 if(isset($ucisettings['send_user_password']) && $ucisettings['send_user_password'] == "true") {
200 $send_user_password = new SendPassword;
201 $send_user_password->send_login_credentials_to_users();
202 }
203 $returnArr['ID'] = $retID;
204 $returnArr['MODE'] = $mode_of_affect;
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');
325 }
326
327 public function getRoles($capability = null) {
328 global $wp_roles;
329 $roles = array();
330 if($capability != null) {
331 foreach ( $wp_roles->roles as $rkey => $rval ) {
332 $roles[ $rkey ] = '';
333 for ( $cnt = 0; $cnt < count( $rval['capabilities'] ); $cnt ++ ) {
334 $findval = "level_" . $cnt;
335 if ( array_key_exists( $findval, $rval['capabilities'] ) ) {
336 $roles[ $rkey ] = $roles[ $rkey ] . $cnt . ',';
337 }
338 }
339 }
340 } else {
341 if ( ! isset( $wp_roles ) )
342 $wp_roles = new \WP_Roles();
343
344 $roles = $wp_roles->get_names();
345 }
346 return $roles;
347 }
348 }
349