| 1 |
<?php |
| 2 |
/** |
| 3 |
* Summary of Import_CSV |
| 4 |
* Handles the import of CSV files for External Lists. |
| 5 |
*/ |
| 6 |
class Import_CSV |
| 7 |
{ |
| 8 |
/** |
| 9 |
* Summary of upload_file |
| 10 |
* Upload the CSV file into the uploads directory. |
| 11 |
* @param mixed $request_file |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public static function upload_file($request_file) |
| 15 |
{ |
| 16 |
$upload_dir = wp_upload_dir(); |
| 17 |
|
| 18 |
$file = $request_file; |
| 19 |
|
| 20 |
$wp_error = new \WP_Error(); |
| 21 |
|
| 22 |
$ret = [ |
| 23 |
'errors' => [], |
| 24 |
'file_path' => '', |
| 25 |
]; |
| 26 |
|
| 27 |
if ($file['error'] !== UPLOAD_ERR_OK) { |
| 28 |
$wp_error->add('upload_error', __('File upload error.', 'send-users-email')); |
| 29 |
} |
| 30 |
|
| 31 |
// Validate file type |
| 32 |
$filetype = wp_check_filetype($file['name']); |
| 33 |
if ($filetype['ext'] !== 'csv') { |
| 34 |
$wp_error->add('invalid_file_type', __('Invalid file type. Please upload a CSV file.', 'send-users-email')); |
| 35 |
} |
| 36 |
|
| 37 |
// Move file to uploads directory |
| 38 |
$target_path = $upload_dir['basedir'] . '/' . basename($file['name']); |
| 39 |
|
| 40 |
// delete the file if it already exists |
| 41 |
if (file_exists($target_path)) { |
| 42 |
unlink($target_path); |
| 43 |
} |
| 44 |
|
| 45 |
move_uploaded_file($file['tmp_name'], $target_path); |
| 46 |
|
| 47 |
$handle = fopen($target_path, 'r'); |
| 48 |
if (!$handle) { |
| 49 |
$wp_error->add('file_open_error', __('Could not open the uploaded file.', 'send-users-email')); |
| 50 |
} |
| 51 |
|
| 52 |
if ($wp_error->has_errors()) { |
| 53 |
$ret['errors'] = $wp_error->get_error_messages(); |
| 54 |
} else { |
| 55 |
$ret['file_path'] = $target_path; |
| 56 |
} |
| 57 |
|
| 58 |
return $ret; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Summary of get_import_data |
| 63 |
* Read the CSV file and return the data for import. |
| 64 |
* @param mixed $file_path |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public static function get_import_data($file_path) |
| 68 |
{ |
| 69 |
$error = new \WP_Error(); |
| 70 |
|
| 71 |
$data = [ |
| 72 |
'skipped_rows' => 0, |
| 73 |
'row_count' => 0, |
| 74 |
'errors' => [], |
| 75 |
'data' => [], |
| 76 |
'file_path' => $file_path, |
| 77 |
]; |
| 78 |
|
| 79 |
if (!file_exists($file_path)) { |
| 80 |
$error->add('file_not_found', __('File not found.', 'send-users-email')); |
| 81 |
} |
| 82 |
|
| 83 |
$handle = fopen($file_path, 'r'); |
| 84 |
if (!$handle) { |
| 85 |
$error->add('file_open_error', __('Could not open the file.', 'send-users-email')); |
| 86 |
} |
| 87 |
|
| 88 |
// Read first line to detect delimiter |
| 89 |
$first_line = fgets($handle); |
| 90 |
$delimiter = self::_detect_delimiter($first_line); |
| 91 |
|
| 92 |
// Rewind and skip header using detected delimiter |
| 93 |
rewind($handle); |
| 94 |
$header = fgetcsv($handle, 0, $delimiter); |
| 95 |
|
| 96 |
$skipped_rows = 0; |
| 97 |
$row_count = 0; |
| 98 |
|
| 99 |
while (($row = fgetcsv($handle, 0, $delimiter)) !== false) { |
| 100 |
$email = sanitize_email($row[0]); |
| 101 |
|
| 102 |
if (empty($email) || !is_email($email)) { |
| 103 |
$skipped_rows++; |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$row_count++; |
| 108 |
|
| 109 |
$data['data'][] = $row; |
| 110 |
} |
| 111 |
|
| 112 |
fclose($handle); |
| 113 |
|
| 114 |
$data['skipped_rows'] = $skipped_rows; |
| 115 |
$data['row_count'] = $row_count; |
| 116 |
|
| 117 |
return $data; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Summary of handle_csv_import |
| 122 |
* Upload the CSV file into the uploads directory and process it to avoid timeouts. |
| 123 |
* @param mixed $request_files |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public static function handle_csv_import($request_files) |
| 127 |
{ |
| 128 |
$ret = [ |
| 129 |
'skipped_rows' => 0, |
| 130 |
'errors' => [], |
| 131 |
'data' => [], |
| 132 |
]; |
| 133 |
|
| 134 |
$upload = self::upload_file($request_files); |
| 135 |
|
| 136 |
if ( isset($upload['errors']) && count($upload['errors']) > 0 ) { |
| 137 |
$ret['errors'] = $upload['errors']; |
| 138 |
return $ret; |
| 139 |
} |
| 140 |
|
| 141 |
$get_data = self::get_import_data($upload['file_path']); |
| 142 |
|
| 143 |
if ( isset($get_data['errors']) && count($get_data['errors']) > 0 ) { |
| 144 |
$ret['errors'] = $get_data['errors']; |
| 145 |
return $ret; |
| 146 |
} |
| 147 |
|
| 148 |
return $get_data; |
| 149 |
} |
| 150 |
|
| 151 |
private static function _detect_delimiter($csv_line) |
| 152 |
{ |
| 153 |
$delimiters = [',', ';', "\t", '|']; |
| 154 |
$results = []; |
| 155 |
|
| 156 |
foreach ($delimiters as $delimiter) { |
| 157 |
$fields = str_getcsv(trim($csv_line), $delimiter); |
| 158 |
$results[$delimiter] = count($fields); |
| 159 |
} |
| 160 |
|
| 161 |
// Return the delimiter with the highest field count |
| 162 |
return array_search(max($results), $results); |
| 163 |
} |
| 164 |
|
| 165 |
} |