| 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 |
*******************************************************************************************/ |
| 8 |
|
| 9 |
namespace Smackcoders\SMUSERS; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) |
| 12 |
exit; // Exit if accessed directly |
| 13 |
|
| 14 |
class UsersImport{ |
| 15 |
private static $users_instance = null,$send_user_password; |
| 16 |
|
| 17 |
public static function getInstance() { |
| 18 |
if (UsersImport::$users_instance == null) { |
| 19 |
UsersImport::$users_instance = new UsersImport; |
| 20 |
return UsersImport::$users_instance; |
| 21 |
} |
| 22 |
return UsersImport::$users_instance; |
| 23 |
} |
| 24 |
|
| 25 |
public function users_import_function ($data_array, $mode , $hash_key , $line_number) { |
| 26 |
|
| 27 |
global $wpdb,$core_instance; |
| 28 |
$helpers_instance = ImportHelpers::getInstance(); |
| 29 |
$returnArr = array(); |
| 30 |
$log_table_name = $wpdb->prefix ."import_detail_log"; |
| 31 |
|
| 32 |
$updated_row_counts = $helpers_instance->update_count($hash_key); |
| 33 |
$created_count = $updated_row_counts['created']; |
| 34 |
$updated_count = $updated_row_counts['updated']; |
| 35 |
$skipped_count = $updated_row_counts['skipped']; |
| 36 |
|
| 37 |
$data_array['role'] = trim($data_array['role']); |
| 38 |
if ( isset( $data_array['role'] ) && $data_array['role'] != '') { |
| 39 |
$user_capability = ''; |
| 40 |
if ( !is_numeric( $data_array['role'] ) ) { |
| 41 |
$roles = $this->getRoles(); |
| 42 |
if(array_key_exists($data_array['role'], $roles)) { |
| 43 |
$user_capability = $data_array['role']; |
| 44 |
} |
| 45 |
} else { |
| 46 |
for ( $i = 0; $i <= $data_array['role']; $i ++ ) { |
| 47 |
$user_capability .= $i . ","; |
| 48 |
} |
| 49 |
$roles = $this->getRoles('cap'); |
| 50 |
if(in_array( $user_capability, $roles )) { |
| 51 |
foreach ( $roles as $rkey => $rval ) { |
| 52 |
if ( $rval == $user_capability ) { |
| 53 |
$user_capability = $rkey; |
| 54 |
} |
| 55 |
} |
| 56 |
} else { |
| 57 |
$user_capability = ''; #TODO: Add log message for assigning the default role |
| 58 |
} |
| 59 |
} |
| 60 |
if($user_capability != '') |
| 61 |
$data_array['role'] = $user_capability; |
| 62 |
else |
| 63 |
$data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role |
| 64 |
} else { |
| 65 |
$data_array['role'] = 'subscriber'; #TODO: Add log message for assigning the default role |
| 66 |
} |
| 67 |
$user_email = $data_array['user_email']; |
| 68 |
|
| 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'; |
| 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'"); |
| 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'"); |
| 99 |
|
| 100 |
} 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 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
$metaData = array(); |
| 130 |
foreach ( $data_array as $daKey => $daVal ) { |
| 131 |
|
| 132 |
switch ( $daKey ) { |
| 133 |
case 'biographical_info' : |
| 134 |
$metaData['description'] = $data_array[ $daKey ]; |
| 135 |
break; |
| 136 |
case 'disable_visual_editor' : |
| 137 |
$metaData['rich_editing'] = $data_array[ $daKey ]; |
| 138 |
break; |
| 139 |
case 'enable_keyboard_shortcuts': |
| 140 |
$metaData['comment_shortcuts'] = $data_array[ $daKey ]; |
| 141 |
break; |
| 142 |
case 'admin_color': |
| 143 |
$metaData['admin_color'] = $data_array[ $daKey ]; |
| 144 |
break; |
| 145 |
case 'show_toolbar': |
| 146 |
$metaData['show_admin_bar_front'] = $data_array[ $daKey ]; |
| 147 |
break; |
| 148 |
case 'smack_uci_import': |
| 149 |
$metaData['smack_uci_import'] = $data_array[ $daKey ]; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! empty ( $metaData ) ) { |
| 154 |
foreach ( $metaData as $meta_key => $meta_value ) { |
| 155 |
update_user_meta( $retID, $meta_key, $meta_value ); |
| 156 |
} |
| 157 |
} |
| 158 |
$core_instance->detailed_log[$line_number]['Email'] = $data_array['user_email']; |
| 159 |
$core_instance->detailed_log[$line_number]['Role'] = $data_array['role']; |
| 160 |
$ucisettings = get_option('sm_uci_pro_settings'); |
| 161 |
if($ucisettings['send_user_password'] == "true") { |
| 162 |
$send_user_password = new SendPassword; |
| 163 |
$send_user_password->send_login_credentials_to_users(); |
| 164 |
} |
| 165 |
$returnArr['ID'] = $retID; |
| 166 |
$returnArr['MODE'] = $mode_of_affect; |
| 167 |
return $returnArr; |
| 168 |
} |
| 169 |
|
| 170 |
public function getRoles($capability = null) { |
| 171 |
global $wp_roles; |
| 172 |
$roles = array(); |
| 173 |
if($capability != null) { |
| 174 |
foreach ( $wp_roles->roles as $rkey => $rval ) { |
| 175 |
$roles[ $rkey ] = ''; |
| 176 |
for ( $cnt = 0; $cnt < count( $rval['capabilities'] ); $cnt ++ ) { |
| 177 |
$findval = "level_" . $cnt; |
| 178 |
if ( array_key_exists( $findval, $rval['capabilities'] ) ) { |
| 179 |
$roles[ $rkey ] = $roles[ $rkey ] . $cnt . ','; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} else { |
| 184 |
if ( ! isset( $wp_roles ) ) |
| 185 |
$wp_roles = new \WP_Roles(); |
| 186 |
|
| 187 |
$roles = $wp_roles->get_names(); |
| 188 |
} |
| 189 |
return $roles; |
| 190 |
} |
| 191 |
} |
| 192 |
|