| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if (!defined('WPINC')) { |
|---|
| 4 | exit; |
|---|
| 5 | } |
|---|
| 6 | |
|---|
| 7 | use Automattic\WooCommerce\Admin\ReportsSync; |
|---|
| 8 | |
|---|
| 9 | if(!class_exists('Wt_Import_Export_For_Woo_basic_User_Import')){ |
|---|
| 10 | class Wt_Import_Export_For_Woo_basic_User_Import { |
|---|
| 11 | |
|---|
| 12 | public $parent_module = null; |
|---|
| 13 | public $parsed_data = array(); |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | var $merge; |
|---|
| 17 | |
|---|
| 18 | public $merge_with = 'id'; |
|---|
| 19 | public $found_action = 'skip'; |
|---|
| 20 | public $id_conflict = 'skip'; |
|---|
| 21 | |
|---|
| 22 | public $is_user_exist = false; |
|---|
| 23 | |
|---|
| 24 | // Results |
|---|
| 25 | var $import_results = array(); |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | var $row; |
|---|
| 29 | var $post_defaults; // Default post data |
|---|
| 30 | var $postmeta_defaults; // default post meta |
|---|
| 31 | var $postmeta_allowed; // post meta validation |
|---|
| 32 | |
|---|
| 33 | public function __construct($parent_object) { |
|---|
| 34 | |
|---|
| 35 | $this->parent_module = $parent_object; |
|---|
| 36 | $this->user_all_fields = include plugin_dir_path( __FILE__).'../data/data-user-columns.php'; |
|---|
| 37 | $this->user_base_fields = array_slice($this->user_all_fields, 0, 13); |
|---|
| 38 | $this->user_meta_fields = array_slice($this->user_all_fields, 13); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public function hf_log_data_change($content = 'review-csv-import', $data = '') { |
|---|
| 44 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', $data); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public function prepare_data_to_import($import_data,$form_data, $batch_offset, $is_last_batch){ |
|---|
| 48 | |
|---|
| 49 | $this->merge_with = !empty($form_data['advanced_form_data']['wt_iew_merge_with']) ? $form_data['advanced_form_data']['wt_iew_merge_with'] : 'email'; |
|---|
| 50 | $this->found_action = !empty($form_data['advanced_form_data']['wt_iew_found_action']) ? $form_data['advanced_form_data']['wt_iew_found_action'] : 'skip'; |
|---|
| 51 | $this->use_same_password = isset($form_data['advanced_form_data']['wt_iew_use_same_password']) ? $form_data['advanced_form_data']['wt_iew_use_same_password'] : 1; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | wp_defer_term_counting(true); |
|---|
| 55 | wp_defer_comment_counting(true); |
|---|
| 56 | wp_suspend_cache_invalidation(true); |
|---|
| 57 | |
|---|
| 58 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Preparing for import."); |
|---|
| 59 | |
|---|
| 60 | $success = 0; |
|---|
| 61 | $failed = 0; |
|---|
| 62 | $msg = 'User imported successfully.'; |
|---|
| 63 | foreach ($import_data as $key => $data) { |
|---|
| 64 | $row = $batch_offset+$key+1; |
|---|
| 65 | |
|---|
| 66 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Row :$row - Parsing item."); |
|---|
| 67 | $parsed_data = $this->parse_users($data); |
|---|
| 68 | |
|---|
| 69 | if (!is_wp_error($parsed_data)){ |
|---|
| 70 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Row :$row - Processing item."); |
|---|
| 71 | |
|---|
| 72 | $result = $this->process_users($parsed_data,$data); |
|---|
| 73 | |
|---|
| 74 | if(!is_wp_error($result)){ |
|---|
| 75 | if($this->is_user_exist){ |
|---|
| 76 | $msg = 'User updated successfully.'; |
|---|
| 77 | } |
|---|
| 78 | $this->import_results[$row] = array('row'=>$row, 'message'=>$msg, 'status'=>true, 'status_msg' => __( 'Success' ), 'post_id'=>$result['id'], 'post_link' => Wt_Import_Export_For_Woo_basic_User::get_item_link_by_id($result['id'])); |
|---|
| 79 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Row :$row - ".$msg); |
|---|
| 80 | $success++; |
|---|
| 81 | }else{ |
|---|
| 82 | $this->import_results[$row] = array('row'=>$row, 'message'=>$result->get_error_message(), 'status'=>false, 'status_msg' => __( 'Failed/Skipped' ), 'post_id'=>'', 'post_link' => array( 'title' => __( 'Untitled' ), 'edit_url' => false ) ); |
|---|
| 83 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Row :$row - Processing failed. Reason: ".$result->get_error_message()); |
|---|
| 84 | $failed++; |
|---|
| 85 | } |
|---|
| 86 | }else{ |
|---|
| 87 | $this->import_results[$row] = array('row'=>$row, 'message'=>$parsed_data->get_error_message(), 'status'=>false, 'status_msg' => __( 'Failed/Skipped' ), 'post_id'=>'', 'post_link' => array( 'title' => __( 'Untitled' ), 'edit_url' => false ) ); |
|---|
| 88 | Wt_Import_Export_For_Woo_Basic_Logwriter::write_log($this->parent_module->module_base, 'import', "Row :$row - Parsing failed. Reason: ".$parsed_data->get_error_message()); |
|---|
| 89 | $failed++; |
|---|
| 90 | } |
|---|
| 91 | unset($data, $parsed_data); |
|---|
| 92 | } |
|---|
| 93 | wp_suspend_cache_invalidation(false); |
|---|
| 94 | wp_defer_term_counting(false); |
|---|
| 95 | wp_defer_comment_counting(false); |
|---|
| 96 | |
|---|
| 97 | $import_response=array( |
|---|
| 98 | 'total_success'=>$success, |
|---|
| 99 | 'total_failed'=>$failed, |
|---|
| 100 | 'log_data'=>$this->import_results, |
|---|
| 101 | ); |
|---|
| 102 | |
|---|
| 103 | if($is_last_batch){ |
|---|
| 104 | /** |
|---|
| 105 | * Regenerate data for reports section. |
|---|
| 106 | * |
|---|
| 107 | * @param int|bool $days Number of days to import. |
|---|
| 108 | * @param bool $skip_existing Skip existing records. |
|---|
| 109 | * @return string |
|---|
| 110 | */ |
|---|
| 111 | if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) { |
|---|
| 112 | $days = false; // Initiate now. |
|---|
| 113 | $skip_existing = 1; |
|---|
| 114 | ReportsSync::regenerate_report_data( $days, $skip_existing ); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | return $import_response; |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Parse users |
|---|
| 125 | * @param array $item |
|---|
| 126 | * @return array |
|---|
| 127 | */ |
|---|
| 128 | public function parse_users( $data ) { |
|---|
| 129 | |
|---|
| 130 | try{ |
|---|
| 131 | $data = apply_filters('wt_user_importer_pre_parse_data', $data); |
|---|
| 132 | $item = $data['mapping_fields']; |
|---|
| 133 | foreach ($data['meta_mapping_fields'] as $value) { |
|---|
| 134 | $item = array_merge($item,$value); |
|---|
| 135 | } |
|---|
| 136 | global $wpdb; |
|---|
| 137 | |
|---|
| 138 | $this->is_user_exist = false; |
|---|
| 139 | $this->merge = false; |
|---|
| 140 | |
|---|
| 141 | $id = $user_id = isset($item['ID']) && !empty($item['ID']) ? absint($item['ID']) : 0; |
|---|
| 142 | $id_found_with_id = ''; |
|---|
| 143 | if($id && 'id' == $this->merge_with){ |
|---|
| 144 | $id_found_with_id = get_userdata($id); |
|---|
| 145 | if ($id_found_with_id) { |
|---|
| 146 | $this->is_user_exist = true; |
|---|
| 147 | $user_id = $id; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | $email = isset($item['user_email']) && '' != $item['user_email'] ? trim($item['user_email']) : ''; |
|---|
| 152 | $id_found_with_email = ''; |
|---|
| 153 | if(!empty($email) && 'email' == $this->merge_with){ |
|---|
| 154 | |
|---|
| 155 | if (is_email($email) && false !== email_exists($email)) { |
|---|
| 156 | $id_found_with_email = email_exists($email); |
|---|
| 157 | $this->is_user_exist = true; |
|---|
| 158 | $user_id = $id_found_with_email; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | $username = isset($item['user_login']) && '' != $item['user_login'] ? trim($item['user_login']) : ''; |
|---|
| 164 | $id_found_with_username = ''; |
|---|
| 165 | if(!empty($username) && 'username' == $this->merge_with){ |
|---|
| 166 | |
|---|
| 167 | if (false !== username_exists($username)) { |
|---|
| 168 | $id_found_with_username = username_exists($username); |
|---|
| 169 | $this->is_user_exist = true; |
|---|
| 170 | $user_id = $id_found_with_username; |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | if($this->is_user_exist){ |
|---|
| 178 | if('skip' == $this->found_action){ |
|---|
| 179 | if($id && $id_found_with_id ){ |
|---|
| 180 | throw new Exception(sprintf('User with same ID already exists. ID: %d',$id )); |
|---|
| 181 | }elseif($email && $id_found_with_email ){ |
|---|
| 182 | throw new Exception(sprintf('User with same Email already exists. Email: %s',$email )); |
|---|
| 183 | }elseif($username && $id_found_with_username ){ |
|---|
| 184 | throw new Exception(sprintf('User with same Username already exists. Username: %s',$username )); |
|---|
| 185 | }else{ |
|---|
| 186 | throw new Exception('User already exists.'); |
|---|
| 187 | } |
|---|
| 188 | }elseif('update' == $this->found_action){ |
|---|
| 189 | $this->merge = true; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | // if(!$this->is_user_exist && $this->skip_new){ |
|---|
| 194 | // throw new Exception('Skipping new item' ); |
|---|
| 195 | // } |
|---|
| 196 | |
|---|
| 197 | if (!$this->is_user_exist) { |
|---|
| 198 | $create_user_without_email = apply_filters('wt_create_user_without_email', FALSE); // create user without email address |
|---|
| 199 | if (empty($email) && $create_user_without_email === FALSE) { |
|---|
| 200 | $this->hf_log_data_change('user-csv-import', __('> skipped: cannot insert user without email.')); |
|---|
| 201 | unset($item); |
|---|
| 202 | return new WP_Error('parse-error', __('> skipped: cannot insert user without email.')); |
|---|
| 203 | } elseif (!is_email($email) && $create_user_without_email === FALSE) { |
|---|
| 204 | $this->hf_log_data_change('user-csv-import', sprintf(__('> skipped: Email is not valid. %s'), $item['user_email'])); |
|---|
| 205 | unset($item); |
|---|
| 206 | return new WP_Error('parse-error', __('> skipped: Email is not valid.')); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | $user_meta = $user_details = array(); |
|---|
| 211 | |
|---|
| 212 | $user_details['ID'] = $user_id; |
|---|
| 213 | |
|---|
| 214 | foreach ($this->user_base_fields as $key => $value) { |
|---|
| 215 | if(in_array($key, array('ID'))){ // ID alreay set |
|---|
| 216 | continue; |
|---|
| 217 | } |
|---|
| 218 | if(isset($item[$value])) |
|---|
| 219 | $user_details[$key] = trim($item[$value]); |
|---|
| 220 | //$user_details[$key] = isset( $item[$value] ) ? $item[$value] : "" ; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | foreach ($this->user_meta_fields as $key => $value){ |
|---|
| 224 | $user_meta[] = array( 'key' => $key, 'value' => isset( $item[$key] ) ? trim($item[$key]) : "" ); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | // the $user_details array will now contain the necessary name-value pairs for the wp_users table, and also any meta data in the 'usermeta' array |
|---|
| 228 | $parsed_details = array(); |
|---|
| 229 | |
|---|
| 230 | $parsed_details['user_details'] = $user_details; |
|---|
| 231 | $parsed_details['user_meta'] = $user_meta; |
|---|
| 232 | |
|---|
| 233 | return $parsed_details; |
|---|
| 234 | } catch (Exception $e) { |
|---|
| 235 | return new WP_Error('woocommerce_product_importer_error', $e->getMessage(), array('status' => $e->getCode())); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | /** |
|---|
| 241 | * Create new posts based on import information |
|---|
| 242 | */ |
|---|
| 243 | private function process_users($post, $parsed_item) { |
|---|
| 244 | try{ |
|---|
| 245 | global $wpdb; |
|---|
| 246 | |
|---|
| 247 | $this->hf_log_data_change('user-csv-import', __('Processing users.')); |
|---|
| 248 | |
|---|
| 249 | $user_id = !empty($post['user_details']['ID']) && $post['user_details']['ID'] ? $post['user_details']['ID'] : 0; |
|---|
| 250 | |
|---|
| 251 | // user exists when importing & merge not ticked |
|---|
| 252 | $new_added = false; |
|---|
| 253 | |
|---|
| 254 | if ($user_id && $this->merge) { |
|---|
| 255 | $current_user = get_current_user_id(); |
|---|
| 256 | if ($current_user == $user_id) { |
|---|
| 257 | $usr_msg = 'This user is currently logged in hence we cannot update.'; |
|---|
| 258 | $this->hf_log_data_change('user-csv-import', sprintf(__('> “%s”' . $usr_msg), $user_id), true); |
|---|
| 259 | unset($post); |
|---|
| 260 | return new WP_Error( 'parse-error',sprintf(__('> “%s”' . $usr_msg), $user_id)); |
|---|
| 261 | } |
|---|
| 262 | $user_id = $this->hf_update_customer($user_id, $post); |
|---|
| 263 | } else { |
|---|
| 264 | |
|---|
| 265 | $user_id = $this->hf_create_customer($post); |
|---|
| 266 | $new_added = true; |
|---|
| 267 | if (is_wp_error($user_id)) { |
|---|
| 268 | |
|---|
| 269 | $this->hf_log_data_change('user-csv-import', sprintf(__('> Error inserting %s: %s'), 1, $user_id->get_error_message()), true); |
|---|
| 270 | //$skipped++; |
|---|
| 271 | unset($post); |
|---|
| 272 | return new WP_Error( 'parse-error',sprintf(__('> Error inserting %s: %s'), 1, $user_id->get_error_message())); |
|---|
| 273 | } elseif (empty($user_id)) { |
|---|
| 274 | |
|---|
| 275 | //$skipped++; |
|---|
| 276 | unset($post); |
|---|
| 277 | return new WP_Error( 'parse-error',__('An error occurred with the customer information provided.')); |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | if ($this->merge && !$new_added) |
|---|
| 282 | $out_msg = "User updated successfully. ID:$user_id"; |
|---|
| 283 | else |
|---|
| 284 | $out_msg = "User imported successfully. ID:$user_id"; |
|---|
| 285 | |
|---|
| 286 | $this->hf_log_data_change('user-csv-import', sprintf(__('> “%s”' . $out_msg), $user_id), true); |
|---|
| 287 | |
|---|
| 288 | $this->hf_log_data_change('user-csv-import', sprintf(__('> Finished importing user %s'), $user_id )); |
|---|
| 289 | |
|---|
| 290 | do_action('wt_customer_csv_import_data', $parsed_item, $user_id); |
|---|
| 291 | unset($post); |
|---|
| 292 | return $result = array( |
|---|
| 293 | 'id' => $user_id, |
|---|
| 294 | 'updated' => $this->merge, |
|---|
| 295 | ); |
|---|
| 296 | } catch (Exception $e) { |
|---|
| 297 | return new WP_Error('woocommerce_product_importer_error', $e->getMessage(), array('status' => $e->getCode())); |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | public function hf_create_customer($data) { |
|---|
| 302 | $customer_email = (!empty($data['user_details']['user_email']) ) ? $data['user_details']['user_email'] : ''; |
|---|
| 303 | $username = (!empty($data['user_details']['user_login']) ) ? $data['user_details']['user_login'] : ''; |
|---|
| 304 | $customer_id = (!empty($data['user_details']['customer_id']) ) ? $data['user_details']['customer_id'] : ''; |
|---|
| 305 | $insertion_id = (!empty($data['user_details']['ID']) ) ? $data['user_details']['ID'] : ''; |
|---|
| 306 | $merge_empty_cells = apply_filters('wt_user_import_empty_csv_column', FALSE); |
|---|
| 307 | |
|---|
| 308 | if('email' == $this->merge_with){ |
|---|
| 309 | $insertion_id = ''; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | if (!empty($data['user_details']['user_pass'])) { |
|---|
| 313 | $password = ($this->use_same_password) ? $data['user_details']['user_pass'] : wp_hash_password($data['user_details']['user_pass']); |
|---|
| 314 | $password_generated = false; |
|---|
| 315 | } else { |
|---|
| 316 | $password = wp_generate_password(12, true); |
|---|
| 317 | $password_generated = true; |
|---|
| 318 | } |
|---|
| 319 | $found_customer = false; |
|---|
| 320 | $create_user_without_email = apply_filters('wt_create_user_without_email', FALSE); // create user without email address |
|---|
| 321 | if (is_email($customer_email) || $create_user_without_email === TRUE) { |
|---|
| 322 | $maybe_username = $username; |
|---|
| 323 | // Not in test mode, create a user account for this email |
|---|
| 324 | if (empty($username)) { |
|---|
| 325 | $maybe_username = explode('@', $customer_email); |
|---|
| 326 | $maybe_username = sanitize_user($maybe_username[0]); |
|---|
| 327 | } |
|---|
| 328 | $counter = 1; |
|---|
| 329 | $username = $maybe_username; |
|---|
| 330 | while (username_exists($username)) { |
|---|
| 331 | $username = $maybe_username . $counter; |
|---|
| 332 | $counter++; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | if (!$this->is_user_exist && $insertion_id) { |
|---|
| 336 | global $wpdb; |
|---|
| 337 | $insertion_id = (int) $insertion_id; |
|---|
| 338 | $username = sanitize_user($username); |
|---|
| 339 | $customer_email = sanitize_email($customer_email); |
|---|
| 340 | if($password_generated && !$this->use_same_password){ |
|---|
| 341 | $password = wp_hash_password($password); |
|---|
| 342 | } |
|---|
| 343 | $result = $wpdb->insert($wpdb->users, array('ID' => $insertion_id, 'user_login' => $username, 'user_email' => $customer_email, 'user_pass' => $password)); |
|---|
| 344 | if ($result) { |
|---|
| 345 | $found_customer = $insertion_id; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | } |
|---|
| 349 | if(!$found_customer) { |
|---|
| 350 | $found_customer = wp_create_user($username, $password, $customer_email); |
|---|
| 351 | } |
|---|
| 352 | $user_data = array('ID' => $found_customer, 'user_login' => $username, 'user_email' => $customer_email); |
|---|
| 353 | if (!$password_generated) { |
|---|
| 354 | $user_data['user_pass'] = $password; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | if (!is_wp_error($found_customer)) { |
|---|
| 358 | wp_insert_user($user_data); |
|---|
| 359 | $wp_user_object = new WP_User($found_customer); |
|---|
| 360 | if ( ! function_exists( 'get_editable_roles' ) ) { |
|---|
| 361 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
|---|
| 362 | } |
|---|
| 363 | $roles = get_editable_roles(); |
|---|
| 364 | $new_roles_str = str_replace(' ', '', strtolower(isset($data['user_details']['roles']) ? $data['user_details']['roles'] : '')); |
|---|
| 365 | |
|---|
| 366 | if(empty($new_roles_str)){ |
|---|
| 367 | $new_roles_str = get_option('default_role'); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | $new_roles = explode(',', $new_roles_str); |
|---|
| 371 | $new_roles = array_intersect($new_roles, array_keys($roles)); |
|---|
| 372 | $roles_to_remove = array(); |
|---|
| 373 | $user_roles = array_intersect(array_values($wp_user_object->roles), array_keys($roles)); |
|---|
| 374 | if (!$new_roles) { |
|---|
| 375 | // If there are no roles, delete all of the user's roles |
|---|
| 376 | $roles_to_remove = $user_roles; |
|---|
| 377 | } else { |
|---|
| 378 | $roles_to_remove = array_diff($user_roles, $new_roles); |
|---|
| 379 | } |
|---|
| 380 | array_push($roles_to_remove, 'subscriber'); |
|---|
| 381 | if (!empty($new_roles)) { |
|---|
| 382 | foreach ($roles_to_remove as $_role) { |
|---|
| 383 | $wp_user_object->remove_role($_role); //remove the default role before adding new roles |
|---|
| 384 | } |
|---|
| 385 | // Make sure that we don't call $wp_user_object->add_role() any more than it's necessary |
|---|
| 386 | $_new_roles = array_diff($new_roles, array_intersect(array_values($wp_user_object->roles), array_keys($roles))); |
|---|
| 387 | foreach ($_new_roles as $_role) { |
|---|
| 388 | $wp_user_object->add_role($_role); |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | $meta_array = array(); |
|---|
| 392 | foreach ($data['user_meta'] as $meta) { |
|---|
| 393 | $meta_array[$meta['key']] = $meta['value']; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | $user_nicename = (!empty($data['user_details']['user_nicename'])) ? $data['user_details']['user_nicename'] : ''; |
|---|
| 397 | $website = (!empty($data['user_details']['user_url'])) ? $data['user_details']['user_url'] : ''; |
|---|
| 398 | $user_registered = (!empty($data['user_details']['user_registered'])) ? $data['user_details']['user_registered'] : ''; |
|---|
| 399 | $display_name = (!empty($data['user_details']['display_name'])) ? $data['user_details']['display_name'] : ''; |
|---|
| 400 | $first_name = (!empty($data['user_details']['first_name'])) ? $data['user_details']['first_name'] : ''; |
|---|
| 401 | $last_name = (!empty($data['user_details']['last_name'])) ? $data['user_details']['last_name'] : ''; |
|---|
| 402 | $user_status = (!empty($data['user_details']['user_status'])) ? $data['user_details']['user_status'] : ''; |
|---|
| 403 | wp_update_user(array( |
|---|
| 404 | 'ID' => $found_customer, |
|---|
| 405 | 'user_nicename' => $user_nicename, |
|---|
| 406 | 'user_url' => $website, |
|---|
| 407 | 'user_registered' => $user_registered, |
|---|
| 408 | 'display_name' => $display_name, |
|---|
| 409 | 'first_name' => $first_name, |
|---|
| 410 | 'last_name' => $last_name, |
|---|
| 411 | 'user_status' => $user_status, |
|---|
| 412 | ) |
|---|
| 413 | ); |
|---|
| 414 | |
|---|
| 415 | //unset($this->user_meta_fields['role']); |
|---|
| 416 | // update user meta data |
|---|
| 417 | global $wpdb; |
|---|
| 418 | foreach ($this->user_meta_fields as $key => $meta) { |
|---|
| 419 | if (($key == $wpdb->prefix.'capabilities') || (in_array($key, $this->user_base_fields))) { continue; |
|---|
| 420 | } |
|---|
| 421 | $key = trim(str_replace('meta:', '', $key)); |
|---|
| 422 | // $meta = trim(str_replace('meta:', '', $meta)); |
|---|
| 423 | $meta_value = (!empty($meta_array[$key]) ) ? maybe_unserialize($meta_array[$key]) : ''; |
|---|
| 424 | if ($key == $wpdb->prefix.'user_level' && $meta_value == ''){ |
|---|
| 425 | $meta_value = 0; |
|---|
| 426 | } |
|---|
| 427 | if ('session_tokens' == $key) { |
|---|
| 428 | if (!empty($meta_array[$key]) ) { |
|---|
| 429 | $session_json = base64_decode($meta_array[$key]); |
|---|
| 430 | $meta_value = json_decode($session_json, true); |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | if (empty($meta_value) && !$merge_empty_cells) { |
|---|
| 434 | continue; |
|---|
| 435 | } |
|---|
| 436 | update_user_meta($found_customer, $key, $meta_value); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | } |
|---|
| 440 | } else { |
|---|
| 441 | $found_customer = new WP_Error('hf_invalid_customer', sprintf(__('User could not be created without Email.'), $customer_id)); |
|---|
| 442 | } |
|---|
| 443 | return apply_filters('xa_user_impexp_alter_user_meta', $found_customer, $this->user_meta_fields, $meta_array); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | public function hf_update_customer($found_customer, $data) { |
|---|
| 447 | $meta_array = array(); |
|---|
| 448 | if ($found_customer) { |
|---|
| 449 | $wp_user_object = new WP_User($found_customer); |
|---|
| 450 | $roles = get_editable_roles(); |
|---|
| 451 | $new_rolse_input = isset($data['user_details']['roles']) ? strtolower($data['user_details']['roles']) : ''; |
|---|
| 452 | $new_roles_str = str_replace(' ', '', $new_rolse_input); |
|---|
| 453 | $new_roles = explode(',', $new_roles_str); |
|---|
| 454 | $new_roles = array_intersect($new_roles, array_keys($roles)); |
|---|
| 455 | $roles_to_remove = array(); |
|---|
| 456 | $user_roles = array_intersect(array_values($wp_user_object->roles), array_keys($roles)); |
|---|
| 457 | $merge_empty_cells = apply_filters('wt_user_import_empty_csv_column', FALSE); |
|---|
| 458 | if (!$new_roles) { |
|---|
| 459 | // If there are no roles, delete all of the user's roles |
|---|
| 460 | $roles_to_remove = $user_roles; |
|---|
| 461 | } else { |
|---|
| 462 | $roles_to_remove = array_diff($user_roles, $new_roles); |
|---|
| 463 | } |
|---|
| 464 | if (!empty($new_roles)) { |
|---|
| 465 | foreach ($roles_to_remove as $_role) { |
|---|
| 466 | $wp_user_object->remove_role($_role); //remove the default role before adding new roles |
|---|
| 467 | } |
|---|
| 468 | // Make sure that we don't call $wp_user_object->add_role() any more than it's necessary |
|---|
| 469 | $_new_roles = array_diff($new_roles, array_intersect(array_values($wp_user_object->roles), array_keys($roles))); |
|---|
| 470 | foreach ($_new_roles as $_role) { |
|---|
| 471 | $wp_user_object->add_role($_role); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | foreach ($data['user_meta'] as $meta) { |
|---|
| 476 | $meta_array[$meta['key']] = $meta['value']; |
|---|
| 477 | } |
|---|
| 478 | // update user meta data |
|---|
| 479 | global $wpdb; |
|---|
| 480 | foreach ($this->user_meta_fields as $key => $meta) { |
|---|
| 481 | if (($key == $wpdb->prefix.'capabilities') || (in_array($key, $this->user_base_fields))) { continue; |
|---|
| 482 | } |
|---|
| 483 | $key = trim(str_replace('meta:', '', $key)); |
|---|
| 484 | // $meta = trim(str_replace('meta:', '', $meta)); |
|---|
| 485 | $meta_value = (!empty($meta_array[$key]) ) ? maybe_unserialize($meta_array[$key]) : ''; |
|---|
| 486 | if ($key == $wpdb->prefix.'user_level' && $meta_value == ''){ |
|---|
| 487 | $meta_value = 0; |
|---|
| 488 | } |
|---|
| 489 | if (empty($meta_value) && !$merge_empty_cells) { |
|---|
| 490 | continue; |
|---|
| 491 | } |
|---|
| 492 | update_user_meta($found_customer, $key, $meta_value); |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | $user_data = array( |
|---|
| 496 | 'ID' => $found_customer |
|---|
| 497 | ); |
|---|
| 498 | if (isset($data['user_details']['user_nicename'])) { |
|---|
| 499 | $user_data['user_nicename'] = $data['user_details']['user_nicename']; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | $email_updated = false; |
|---|
| 503 | //added when implement merge with user id use for update email |
|---|
| 504 | if (isset($data['user_details']['user_email']) && !empty($data['user_details']['user_email'])) { |
|---|
| 505 | if ($wp_user_object->user_email != $data['user_details']['user_email']) { |
|---|
| 506 | $email_updated = true; |
|---|
| 507 | $customer_email = $data['user_details']['user_email']; |
|---|
| 508 | } |
|---|
| 509 | $user_data['user_email'] = $data['user_details']['user_email']; |
|---|
| 510 | } |
|---|
| 511 | if (isset($data['user_details']['user_url'])) { |
|---|
| 512 | $user_data['user_url'] = $data['user_details']['user_url']; |
|---|
| 513 | } |
|---|
| 514 | if (isset($data['user_details']['user_registered'])) { |
|---|
| 515 | $user_data['user_registered'] = $data['user_details']['user_registered']; |
|---|
| 516 | } |
|---|
| 517 | if (isset($data['user_details']['user_pass'])) { |
|---|
| 518 | $user_data['user_pass'] = $data['user_details']['user_pass']; |
|---|
| 519 | } |
|---|
| 520 | if (isset($data['user_details']['display_name'])) { |
|---|
| 521 | $user_data['display_name'] = $data['user_details']['display_name']; |
|---|
| 522 | } |
|---|
| 523 | if (isset($data['user_details']['first_name'])) { |
|---|
| 524 | $user_data['first_name'] = $data['user_details']['first_name']; |
|---|
| 525 | } |
|---|
| 526 | if (isset($data['user_details']['last_name'])) { |
|---|
| 527 | $user_data['last_name'] = $data['user_details']['last_name']; |
|---|
| 528 | } |
|---|
| 529 | if (isset($data['user_details']['user_status'])) { |
|---|
| 530 | $user_data['user_status'] = $data['user_details']['user_status']; |
|---|
| 531 | } |
|---|
| 532 | add_filter('send_password_change_email', '__return_false'); // for preventing sending password change notification mail on by wp_update_user. |
|---|
| 533 | if (sizeof($user_data) > 1) { |
|---|
| 534 | wp_update_user($user_data); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | if ($this->use_same_password && isset($data['user_details']['user_pass']) && !empty($data['user_details']['user_pass'])) { |
|---|
| 538 | $password = $data['user_details']['user_pass']; |
|---|
| 539 | $user_data = array_merge($user_data, array('user_login' => $wp_user_object->user_login, 'user_email' => ( $email_updated ) ? $customer_email : $wp_user_object->user_email, 'user_url' =>$wp_user_object->user_url)); |
|---|
| 540 | $user_data['user_pass'] = $password; |
|---|
| 541 | $user_default_meta = array('nickname','first_name','last_name','display_name','description','rich_editing','syntax_highlighting','comment_shortcuts','admin_color','use_ssl','show_admin_bar_front','locale'); |
|---|
| 542 | foreach ($user_default_meta as $meta_key => $meta) { |
|---|
| 543 | $user_data[$meta] = $wp_user_object->$meta; |
|---|
| 544 | } |
|---|
| 545 | wp_insert_user($user_data); |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | } else { |
|---|
| 549 | $found_customer = new WP_Error('hf_invalid_customer', sprintf(__('User could not be found with given Email or username.'), $customer_email)); |
|---|
| 550 | } |
|---|
| 551 | return apply_filters('xa_user_impexp_alter_user_meta', $found_customer, $this->user_meta_fields, $meta_array); |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | } |
|---|