| @@ -38,53 +38,30 @@ | ||
| 38 | 38 | private static function read_file() { |
| 39 | 39 | $file = self::$file; |
| 40 | 40 | $csv_data = []; |
| 41 | 41 | |
| 42 | - // Initialize WP_Filesystem | |
| 43 | - global $wp_filesystem; | |
| 44 | - if ( empty( $wp_filesystem ) ) { | |
| 45 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 46 | - WP_Filesystem(); | |
| 47 | - } | |
| 42 | + $handle = fopen( $file, 'r' ); | |
| 43 | + $headers = fgetcsv( $handle ); | |
| 48 | 44 | |
| 49 | - // Read file contents using WP_Filesystem | |
| 50 | - $contents = $wp_filesystem->get_contents( $file ); | |
| 51 | - if ( false === $contents ) { | |
| 52 | - return $csv_data; | |
| 53 | - } | |
| 54 | - | |
| 55 | - // Split content into lines | |
| 56 | - $lines = explode( "\n", $contents ); | |
| 57 | - if ( empty( $lines ) ) { | |
| 58 | - return $csv_data; | |
| 59 | - } | |
| 60 | - | |
| 61 | - // Parse header row | |
| 62 | - $headers = str_getcsv( array_shift( $lines ) ); | |
| 63 | 45 | if ( ! $headers ) { |
| 64 | 46 | return $csv_data; |
| 65 | 47 | } |
| 66 | 48 | |
| 67 | - $header_count = count( $headers ); | |
| 49 | + if ( $handle !== false ) { | |
| 50 | + $header_count = count( $headers ); | |
| 68 | 51 | |
| 69 | - foreach ( $lines as $line ) { | |
| 70 | - // Skip empty lines | |
| 71 | - if ( empty( trim( $line ) ) ) { | |
| 72 | - continue; | |
| 73 | - } | |
| 52 | + while ( ( $data = fgetcsv( $handle ) ) !== false ) { | |
| 53 | + $row = []; | |
| 74 | 54 | |
| 75 | - $data = str_getcsv( $line ); | |
| 76 | - if ( false === $data ) { | |
| 77 | - continue; | |
| 78 | - } | |
| 55 | + for ( $i = 0; $i < $header_count; $i++ ) { | |
| 56 | + $row[$headers[$i]] = $data[$i]; | |
| 57 | + } | |
| 79 | 58 | |
| 80 | - $row = []; | |
| 81 | - for ( $i = 0; $i < $header_count; $i++ ) { | |
| 82 | - $row[$headers[$i]] = isset( $data[$i] ) ? $data[$i] : ''; | |
| 59 | + $csv_data[] = $row; | |
| 83 | 60 | } |
| 61 | + } | |
| 84 | 62 | |
| 85 | - $csv_data[] = $row; | |
| 86 | - } | |
| 63 | + fclose( $handle ); | |
| 87 | 64 | |
| 88 | 65 | return $csv_data; |
| 89 | 66 | } |
| 90 | 67 | } |