| @@ -38,30 +38,53 @@ | ||
| 38 | 38 | private static function read_file() { |
| 39 | 39 | $file = self::$file; |
| 40 | 40 | $csv_data = []; |
| 41 | 41 | |
| 42 | - $handle = fopen( $file, 'r' ); | |
| 43 | - $headers = fgetcsv( $handle ); | |
| 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 | + } | |
| 44 | 48 | |
| 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 ) ); | |
| 45 | 63 | if ( ! $headers ) { |
| 46 | 64 | return $csv_data; |
| 47 | 65 | } |
| 48 | 66 | |
| 49 | - if ( $handle !== false ) { | |
| 50 | - $header_count = count( $headers ); | |
| 67 | + $header_count = count( $headers ); | |
| 51 | 68 | |
| 52 | - while ( ( $data = fgetcsv( $handle ) ) !== false ) { | |
| 53 | - $row = []; | |
| 69 | + foreach ( $lines as $line ) { | |
| 70 | + // Skip empty lines | |
| 71 | + if ( empty( trim( $line ) ) ) { | |
| 72 | + continue; | |
| 73 | + } | |
| 54 | 74 | |
| 55 | - for ( $i = 0; $i < $header_count; $i++ ) { | |
| 56 | - $row[$headers[$i]] = $data[$i]; | |
| 57 | - } | |
| 75 | + $data = str_getcsv( $line ); | |
| 76 | + if ( false === $data ) { | |
| 77 | + continue; | |
| 78 | + } | |
| 58 | 79 | |
| 59 | - $csv_data[] = $row; | |
| 80 | + $row = []; | |
| 81 | + for ( $i = 0; $i < $header_count; $i++ ) { | |
| 82 | + $row[$headers[$i]] = isset( $data[$i] ) ? $data[$i] : ''; | |
| 60 | 83 | } |
| 84 | + | |
| 85 | + $csv_data[] = $row; | |
| 61 | 86 | } |
| 62 | - | |
| 63 | - fclose( $handle ); | |
| 64 | 87 | |
| 65 | 88 | return $csv_data; |
| 66 | 89 | } |
| 67 | 90 | } |