PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.2.3
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.2.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.2.3, at importExtensions/UsersImport.php

191 lines 6.4 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($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 if ( $mode == 'Insert' ) {
69 if ( empty( $data_array['user_pass'] ) ) {
70 $data_array['user_pass'] = wp_generate_password( 12, false );
71 $additional_meta_info = array(
72 'user_login' => $data_array['user_login'],
73 'user_pass' => $data_array['user_pass'],
74 'user_email' => $data_array['user_email'],
75 'role' => $data_array['role']
76 );
77 $data_array['smack_uci_import'] = $additional_meta_info;
78 }
79 else{
80 $additional_meta_info = array(
81 'user_login' => $data_array['user_login'],
82 'user_pass' => $data_array['user_pass'],
83 'user_email' => $data_array['user_email'],
84 'role' => $data_array['role']
85 );
86 $data_array['smack_uci_import'] = $additional_meta_info;
87 }
88 $retID = wp_insert_user($data_array);
89 $mode_of_affect = 'Inserted';
90
91 if ( is_wp_error( $retID ) ) {
92 $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
93 $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
94 return array('MODE' => $mode);
95 }
96 $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
97 $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
98
99 } else {
100 if ( $mode == 'Update') {
101
102 $update_query = $wpdb->prepare( "select ID from {$wpdb->prefix}users where user_email = %s order by ID DESC", $user_email );
103 $ID_result = $wpdb->get_results( $update_query );
104 if ( is_array( $ID_result ) && ! empty( $ID_result ) ) {
105 $retID = $ID_result[0]->ID;
106 $data_array['ID'] = $retID;
107 wp_update_user( $data_array );
108 $mode_of_affect = 'Updated';
109
110 $core_instance->detailed_log[$line_number]['Message'] = 'Updated User ID: ' . $retID;
111 $fields = $wpdb->get_results("UPDATE $log_table_name SET updated = $updated_count WHERE hash_key = '$hash_key'");
112
113 }else{
114 $retID = wp_insert_user($data_array);
115 $mode_of_affect = 'Inserted';
116
117 if ( is_wp_error( $retID ) ) {
118
119 $core_instance->detailed_log[$line_number]['Message'] = "Skipped, Due to duplicate User found with same email!.";
120 $fields = $wpdb->get_results("UPDATE $log_table_name SET skipped = $skipped_count WHERE hash_key = '$hash_key'");
121 return array('MODE' => $mode);
122 }
123 $core_instance->detailed_log[$line_number]['Message'] = 'Inserted User ID: ' . $retID;
124 $fields = $wpdb->get_results("UPDATE $log_table_name SET created = $created_count WHERE hash_key = '$hash_key'");
125 }
126 }
127 }
128 $metaData = array();
129 foreach ( $data_array as $daKey => $daVal ) {
130
131 switch ( $daKey ) {
132 case 'biographical_info' :
133 $metaData['description'] = $data_array[ $daKey ];
134 break;
135 case 'disable_visual_editor' :
136 $metaData['rich_editing'] = $data_array[ $daKey ];
137 break;
138 case 'enable_keyboard_shortcuts':
139 $metaData['comment_shortcuts'] = $data_array[ $daKey ];
140 break;
141 case 'admin_color':
142 $metaData['admin_color'] = $data_array[ $daKey ];
143 break;
144 case 'show_toolbar':
145 $metaData['show_admin_bar_front'] = $data_array[ $daKey ];
146 break;
147 case 'smack_uci_import':
148 $metaData['smack_uci_import'] = $data_array[ $daKey ];
149 }
150 }
151
152 if ( ! empty ( $metaData ) ) {
153 foreach ( $metaData as $meta_key => $meta_value ) {
154 update_user_meta( $retID, $meta_key, $meta_value );
155 }
156 }
157 $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email'];
158 $core_instance->detailed_log[$line_number]['Role'] = $data_array['role'];
159 $ucisettings = get_option('sm_uci_pro_settings');
160 if($ucisettings['send_user_password'] == "true") {
161 $send_user_password = new SendPassword;
162 $send_user_password->send_login_credentials_to_users();
163 }
164 $returnArr['ID'] = $retID;
165 $returnArr['MODE'] = $mode_of_affect;
166 return $returnArr;
167 }
168
169 public function getRoles($capability = null) {
170 global $wp_roles;
171 $roles = array();
172 if($capability != null) {
173 foreach ( $wp_roles->roles as $rkey => $rval ) {
174 $roles[ $rkey ] = '';
175 for ( $cnt = 0; $cnt < count( $rval['capabilities'] ); $cnt ++ ) {
176 $findval = "level_" . $cnt;
177 if ( array_key_exists( $findval, $rval['capabilities'] ) ) {
178 $roles[ $rkey ] = $roles[ $rkey ] . $cnt . ',';
179 }
180 }
181 }
182 } else {
183 if ( ! isset( $wp_roles ) )
184 $wp_roles = new \WP_Roles();
185
186 $roles = $wp_roles->get_names();
187 }
188 return $roles;
189 }
190 }
191