| @@ -1,10 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Import Users plugin file. | |
| 4 | - * | |
| 5 | - * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com | |
| 6 | - */ | |
| 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 | + *******************************************************************************************/ | |
| 7 | 8 | |
| 8 | 9 | namespace Smackcoders\SMUSERS; |
| 9 | 10 | |
| 10 | 11 | if ( ! defined( 'ABSPATH' ) ) |
| @@ -11,9 +12,9 @@ | ||
| 11 | 12 | exit; // Exit if accessed directly |
| 12 | 13 | |
| 13 | 14 | class UsersImport{ |
| 14 | 15 | private static $users_instance = null,$send_user_password; |
| 15 | - public $plugin; | |
| 16 | + | |
| 16 | 17 | public static function getInstance() { |
| 17 | 18 | if (UsersImport::$users_instance == null) { |
| 18 | 19 | UsersImport::$users_instance = new UsersImport; |
| 19 | 20 | return UsersImport::$users_instance; |
| @@ -20,31 +21,25 @@ | ||
| 20 | 21 | } |
| 21 | 22 | return UsersImport::$users_instance; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | - public function users_import_function ($data_array, $mode, $hash_key, $line_number, $check = '', $update_based_on = 'normal', $duplicate_action = 'skip') { | |
| 25 | - | |
| 25 | + public function users_import_function ($data_array, $mode , $hash_key , $line_number) { | |
| 26 | + | |
| 26 | 27 | global $wpdb,$core_instance; |
| 27 | 28 | $helpers_instance = ImportHelpers::getInstance(); |
| 28 | 29 | $returnArr = array(); |
| 29 | 30 | $log_table_name = $wpdb->prefix ."import_detail_log"; |
| 30 | - $retID = 0; | |
| 31 | - $mode_of_affect = 'Inserted'; | |
| 32 | 31 | |
| 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 | 32 | $updated_row_counts = $helpers_instance->update_count($hash_key); |
| 38 | 33 | $created_count = $updated_row_counts['created']; |
| 39 | 34 | $updated_count = $updated_row_counts['updated']; |
| 40 | 35 | $skipped_count = $updated_row_counts['skipped']; |
| 41 | 36 | |
| 42 | - $data_array['role'] = trim(isset($data_array['role']) ? $data_array['role'] : ''); | |
| 37 | + $data_array['role'] = trim($data_array['role']); | |
| 43 | 38 | if ( isset( $data_array['role'] ) && $data_array['role'] != '') { |
| 44 | 39 | $user_capability = ''; |
| 45 | - if ( !is_numeric( $data_array['role'] ) ) { | |
| 46 | - $roles = $this->getRoles(); | |
| 40 | + if ( !is_numeric( $data_array['role'] ) ) { | |
| 41 | + $roles = $this->getRoles(); | |
| 47 | 42 | if(array_key_exists($data_array['role'], $roles)) { |
| 48 | 43 | $user_capability = $data_array['role']; |
| 49 | 44 | } |
| 50 | 45 | } else { |
| @@ -58,106 +53,79 @@ | ||
| 58 | 53 | $user_capability = $rkey; |
| 59 | 54 | } |
| 60 | 55 | } |
| 61 | 56 | } else { |
| 62 | - $user_capability = ''; | |
| 57 | + $user_capability = ''; #TODO: Add log message for assigning the default role | |
| 63 | 58 | } |
| 64 | 59 | } |
| 65 | 60 | if($user_capability != '') |
| 66 | 61 | $data_array['role'] = $user_capability; |
| 67 | 62 | else |
| 68 | - $data_array['role'] = 'subscriber'; | |
| 63 | + $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role | |
| 69 | 64 | } else { |
| 70 | - $data_array['role'] = 'subscriber'; | |
| 65 | + $data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role | |
| 71 | 66 | } |
| 67 | + $user_email = $data_array['user_email']; | |
| 72 | 68 | |
| 73 | - $data_array = apply_filters('smack_csv_modify_userdata_filter', $data_array); | |
| 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; | |
| 88 | + } | |
| 89 | + $retID = wp_insert_user($data_array); | |
| 90 | + $mode_of_affect = 'Inserted'; | |
| 74 | 91 | |
| 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 | - ); | |
| 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'"); | |
| 95 | + return array('MODE' => $mode); | |
| 96 | + } | |
| 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'"); | |
| 82 | 99 | |
| 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 | - } | |
| 100 | + } else { | |
| 101 | + if ( $mode == 'Update') { | |
| 96 | 102 | |
| 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 | - } | |
| 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'; | |
| 111 | 110 | |
| 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); | |
| 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 | + } | |
| 126 | 127 | } |
| 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 | 128 | } |
| 161 | 129 | $metaData = array(); |
| 162 | 130 | foreach ( $data_array as $daKey => $daVal ) { |
| 163 | 131 | |
| @@ -184,20 +152,14 @@ | ||
| 184 | 152 | |
| 185 | 153 | if ( ! empty ( $metaData ) ) { |
| 186 | 154 | foreach ( $metaData as $meta_key => $meta_value ) { |
| 187 | 155 | 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 | 156 | } |
| 192 | 157 | } |
| 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 | 158 | $core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email']; |
| 197 | 159 | $core_instance->detailed_log[$line_number]['Role'] = $data_array['role']; |
| 198 | 160 | $ucisettings = get_option('sm_uci_pro_settings'); |
| 199 | - if(isset($ucisettings['send_user_password']) && $ucisettings['send_user_password'] == "true") { | |
| 161 | + if($ucisettings['send_user_password'] == "true") { | |
| 200 | 162 | $send_user_password = new SendPassword; |
| 201 | 163 | $send_user_password->send_login_credentials_to_users(); |
| 202 | 164 | } |
| 203 | 165 | $returnArr['ID'] = $retID; |
| @@ -202,127 +164,8 @@ | ||
| 202 | 164 | } |
| 203 | 165 | $returnArr['ID'] = $retID; |
| 204 | 166 | $returnArr['MODE'] = $mode_of_affect; |
| 205 | 167 | 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 | 168 | } |
| 326 | 169 | |
| 327 | 170 | public function getRoles($capability = null) { |
| 328 | 171 | global $wp_roles; |