WC_Product_CSV_Importer::read_file()protectedWC 1.0

Read file.

Method of the class: WC_Product_CSV_Importer{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->read_file();

WC_Product_CSV_Importer::read_file() code WC 8.7.0

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 = array_map( 'trim', fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ) ); // @codingStandardsIgnoreLine

		if ( ArrayUtil::is_truthy( $this->params, 'character_encoding' ) ) {
			$this->raw_keys = array_map( array( $this, 'adjust_character_encoding' ), $this->raw_keys );
		}

		// Remove line breaks in keys, to avoid mismatch mapping of keys.
		$this->raw_keys = wc_clean( wp_unslash( $this->raw_keys ) );

		// 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 = fgetcsv( $handle, 0, $this->params['delimiter'], $this->params['enclosure'], $this->params['escape'] ); // @codingStandardsIgnoreLine

			if ( false !== $row ) {
				if ( ArrayUtil::is_truthy( $this->params, 'character_encoding' ) ) {
					$row = array_map( array( $this, 'adjust_character_encoding' ), $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();
	}
}