activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
9 months ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
9 months ago
carts_helper.php
1 year ago
connector_helper.php
9 months ago
csv_helper.php
9 months ago
customer_helper.php
9 months ago
customer_import_helper.php
4 months ago
database_helper.php
9 months ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
9 months ago
encrypt_helper.php
1 year ago
events_helper.php
9 months ago
form_helper.php
9 months ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
9 months ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
9 months ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
9 months ago
order_intent_helper.php
9 months ago
orders_helper.php
1 year ago
otp_helper.php
9 months ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
7 months ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
4 months ago
short_links_systems_helper.php
9 months ago
shortcodes_helper.php
9 months ago
sms_helper.php
1 year ago
steps_helper.php
5 months ago
stripe_connect_helper.php
9 months ago
styles_helper.php
9 months ago
support_topics_helper.php
1 year ago
time_helper.php
9 months ago
timeline_helper.php
9 months ago
transaction_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
7 months ago
version_specific_updates_helper.php
9 months ago
whatsapp_helper.php
1 year ago
work_periods_helper.php
5 months ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
csv_helper.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | class OsCSVHelper { |
| 4 | public static function array_to_csv( $data ) { |
| 5 | $output = fopen( "php://output", "wb" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen |
| 6 | foreach ( $data as $row ) { |
| 7 | fputcsv( $output, $row ); |
| 8 | } |
| 9 | fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 10 | } |
| 11 | |
| 12 | |
| 13 | public static function get_import_dir( bool $create = true ): string { |
| 14 | $wp_upload_dir = wp_upload_dir( null, $create ); |
| 15 | if ( $wp_upload_dir['error'] ) { |
| 16 | throw new \Exception( esc_html( $wp_upload_dir['error'] ) ); |
| 17 | } |
| 18 | |
| 19 | $upload_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'latepoint'; |
| 20 | if ( $create ) { |
| 21 | if ( ! file_exists( $upload_dir ) ) { |
| 22 | wp_mkdir_p( $upload_dir ); |
| 23 | } |
| 24 | } |
| 25 | return $upload_dir; |
| 26 | } |
| 27 | |
| 28 | public static function upload_csv_file($files, $file_name ) { |
| 29 | if(empty($files[$file_name])){ |
| 30 | throw new \Exception('File not selected'); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | $file = $files[$file_name]; |
| 35 | |
| 36 | $upload_dir = OsCsvHelper::get_import_dir(); |
| 37 | $tmp_name = uniqid('latepoint_customers_csv_') . '.csv'; |
| 38 | $filepath = $upload_dir . '/' . $tmp_name; |
| 39 | |
| 40 | if (!move_uploaded_file($file['tmp_name'][0], $filepath)) { |
| 41 | throw new \Exception('Error uploading file'); |
| 42 | } |
| 43 | set_transient('csv_import_file_' . OsWpUserHelper::get_current_user_id(), $filepath, 3600); |
| 44 | return $filepath; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | public static function is_valid_csv( $file_path ): bool { |
| 49 | $valid_filetypes = [ |
| 50 | 'csv' => 'text/csv', |
| 51 | 'txt' => 'text/plain', |
| 52 | ]; |
| 53 | |
| 54 | $filetype = wp_check_filetype( $file_path, $valid_filetypes ); |
| 55 | |
| 56 | if ( in_array( $filetype['type'], $valid_filetypes, true ) ) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | public static function get_csv_data( $file_path, $limit = false ) { |
| 64 | if (!file_exists($file_path)) { |
| 65 | throw new \Exception('File does not exist'); |
| 66 | } |
| 67 | |
| 68 | if (!OsCSVHelper::is_valid_csv($file_path)) { |
| 69 | throw new \Exception('Invalid file format'); |
| 70 | } |
| 71 | |
| 72 | $data = []; |
| 73 | $i = 0; |
| 74 | if (($handle = fopen($file_path, 'r')) !== false) { |
| 75 | while (($row = fgetcsv($handle)) !== false) { |
| 76 | $data[] = $row; |
| 77 | $i++; |
| 78 | if ($limit && $i >= $limit) { |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | fclose($handle); |
| 83 | } else { |
| 84 | throw new \Exception('Error reading file'); |
| 85 | } |
| 86 | return $data; |
| 87 | } |
| 88 | |
| 89 | } |