| 1 |
<?php |
| 2 |
/** |
| 3 |
* Summary of Import_User_Service |
| 4 |
* This class handles the import of users from CSV files into External Lists. |
| 5 |
* |
| 6 |
*/ |
| 7 |
class Import_User_Service |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Summary of upload_file |
| 11 |
* Upload the CSV file into the uploads directory. |
| 12 |
* @param mixed $request_file |
| 13 |
* @return array |
| 14 |
*/ |
| 15 |
public static function upload_file($request_file) |
| 16 |
{ |
| 17 |
return Import_CSV::upload_file($request_file); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Summary of handle_import_request |
| 22 |
* Process the import request and insert data into the database. |
| 23 |
* @param mixed $request_data |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public static function handle_import_request($request_data) |
| 27 |
{ |
| 28 |
$file_path = sanitize_text_field($request_data['file_path'] ?? ''); |
| 29 |
$list_id = uniqid(); |
| 30 |
$list_name = sanitize_text_field($request_data['list_name'] ?? ''); |
| 31 |
|
| 32 |
$inserted_count = 0; |
| 33 |
$duplicate_rows = 0; |
| 34 |
$skipped_rows = 0; |
| 35 |
|
| 36 |
$get_data = Import_CSV::get_import_data($file_path); |
| 37 |
|
| 38 |
if ( isset($get_data['errors']) && count($get_data['errors']) > 0 ) { |
| 39 |
return [ |
| 40 |
'import' => [ |
| 41 |
'errors' => $get_data['errors'], |
| 42 |
], |
| 43 |
'request_data' => $request_data, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
if (isset($get_data['data']) |
| 48 |
&& ! empty($get_data['data']) |
| 49 |
&& is_array($get_data['data']) |
| 50 |
&& count($get_data['data']) > 0 |
| 51 |
) { |
| 52 |
foreach ($get_data['data'] as $row) { |
| 53 |
$email = sanitize_email($row[0]); |
| 54 |
$first_name = sanitize_text_field($row[1] ?? ''); |
| 55 |
$last_name = sanitize_text_field($row[2] ?? ''); |
| 56 |
$title = sanitize_text_field($row[3] ?? ''); |
| 57 |
$salutation = sanitize_text_field($row[4] ?? ''); |
| 58 |
$field_01 = sanitize_text_field($row[5] ?? ''); |
| 59 |
$field_02 = sanitize_text_field($row[6] ?? ''); |
| 60 |
$field_03 = sanitize_text_field($row[7] ?? ''); |
| 61 |
$field_04 = sanitize_text_field($row[8] ?? ''); |
| 62 |
$field_05 = sanitize_text_field($row[9] ?? ''); |
| 63 |
$subscribed = sanitize_text_field($row[10] ?? 1); |
| 64 |
$user_id = get_current_user_id(); |
| 65 |
|
| 66 |
$has_duplicate = false; |
| 67 |
// Check for duplicate email |
| 68 |
if (External_List_Model::email_exists($email)) { |
| 69 |
$duplicate_rows++; |
| 70 |
$has_duplicate = true; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! $has_duplicate) { |
| 74 |
// Insert into database |
| 75 |
$data = [ |
| 76 |
'list_id' => $list_id, |
| 77 |
'user_id' => $user_id, |
| 78 |
'email' => $email, |
| 79 |
'first_name' => $first_name, |
| 80 |
'last_name' => $last_name, |
| 81 |
'title' => $title, |
| 82 |
'salutation' => $salutation, |
| 83 |
'field_01' => $field_01, |
| 84 |
'field_02' => $field_02, |
| 85 |
'field_03' => $field_03, |
| 86 |
'field_04' => $field_04, |
| 87 |
'field_05' => $field_05, |
| 88 |
'subscribed' => $subscribed, |
| 89 |
]; |
| 90 |
|
| 91 |
$result = External_List_Model::insert_data($data); |
| 92 |
if ($result === false) { |
| 93 |
$get_data['errors'][] = "Failed to insert record for email: $email"; |
| 94 |
} else { |
| 95 |
$inserted_count++; |
| 96 |
|
| 97 |
$get_data['inserted_count'] = $inserted_count; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$get_data['duplicate_rows'] = $duplicate_rows; |
| 102 |
$get_data['skipped_rows'] = $skipped_rows; |
| 103 |
} |
| 104 |
|
| 105 |
if ($list_name && $inserted_count > 0) { |
| 106 |
// Insert metadata |
| 107 |
$meta_data = [ |
| 108 |
'user_id' => get_current_user_id(), |
| 109 |
'list_id' => $list_id, |
| 110 |
'list_name' => $list_name, |
| 111 |
'count' => $inserted_count, |
| 112 |
]; |
| 113 |
External_List_Model::insert_metadata($meta_data); |
| 114 |
} |
| 115 |
} |
| 116 |
return [ |
| 117 |
'list_id' => $list_id, |
| 118 |
'list_name' => $list_name, |
| 119 |
'inserted_count' => $inserted_count, |
| 120 |
'duplicate_rows' => $duplicate_rows, |
| 121 |
'skipped_rows' => $skipped_rows, |
| 122 |
'errors' => $get_data['errors'] ?? [], |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Summary of ajax_mock_chunk_process |
| 128 |
* Mock the processing of a CSV chunk for testing purposes. |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public static function ajax_mock_chunk_process() |
| 132 |
{ |
| 133 |
if ( !isset($_POST['nonce']) && !wp_verify_nonce( $_POST['nonce'], 'sue_external_lists_import' ) ) { |
| 134 |
wp_die(__('Security check failed.', 'external-lists')); |
| 135 |
} |
| 136 |
|
| 137 |
$chunk_index = intval($_POST['chunk_index']); |
| 138 |
$total_chunks = intval($_POST['total_chunks']); |
| 139 |
|
| 140 |
// Define the minimum and maximum sleep duration in microseconds |
| 141 |
$minMicroseconds = 100000; // 0.1 seconds |
| 142 |
$maxMicroseconds = 500000; // 0.5 seconds |
| 143 |
|
| 144 |
// Generate a random number of microseconds within the defined range |
| 145 |
$randomMicroseconds = mt_rand($minMicroseconds, $maxMicroseconds); |
| 146 |
|
| 147 |
// Simulate processing time |
| 148 |
usleep($randomMicroseconds); // Random delay between 0.1 and 0.5 seconds |
| 149 |
|
| 150 |
// Simulate inserted rows per chunk |
| 151 |
$inserted = rand(10, 50); |
| 152 |
|
| 153 |
wp_send_json_success([ |
| 154 |
'inserted' => $inserted, |
| 155 |
'chunk_index' => $chunk_index, |
| 156 |
'total_chunks' => $total_chunks |
| 157 |
]); |
| 158 |
} |
| 159 |
} |