WC_Product_CSV_Importer::read_file()
Read file.
Description Description
Source Source
File: includes/import/class-wc-product-csv-importer.php
protected function read_file() {
if ( ! WC_Product_CSV_Importer_Controller::is_file_valid_csv( $this->file ) ) {
wp_die( esc_html__( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) );
}
$handle = fopen( $this->file, 'r' ); // @codingStandardsIgnoreLine.
if ( false !== $handle ) {
$this->raw_keys = version_compare( PHP_VERSION, '5.3', '>=' ) ? array_map( 'trim', fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ) ) : array_map( 'trim', fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] ) ); // @codingStandardsIgnoreLine
// Remove BOM signature from the first item.
if ( isset( $this->raw_keys[0] ) ) {
$this->raw_keys[0] = $this->remove_utf8_bom( $this->raw_keys[0] );
}
if ( 0 !== $this->params['start_pos'] ) {
fseek( $handle, (int) $this->params['start_pos'] );
}
while ( 1 ) {
$row = version_compare( PHP_VERSION, '5.3', '>=' ) ? fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ) : fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'] ); // @codingStandardsIgnoreLine
if ( false !== $row ) {
$this->raw_data[] = $row;
$this->file_positions[ count( $this->raw_data ) ] = ftell( $handle );
if ( ( $this->params['end_pos'] > 0 && ftell( $handle ) >= $this->params['end_pos'] ) || 0 === --$this->params['lines'] ) {
break;
}
} else {
break;
}
}
$this->file_position = ftell( $handle );
}
if ( ! empty( $this->params['mapping'] ) ) {
$this->set_mapped_keys();
}
if ( $this->params['parse'] ) {
$this->set_parsed_data();
}
}