WC_CSV_Batch_Exporter::write_csv_data()protectedWC 3.1.0

Write data to the file.

Method of the class: WC_CSV_Batch_Exporter{}

Hooks from the method

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->write_csv_data( $data );
$data(string) (required)
Data.

Changelog

Since 3.1.0 Introduced.

WC_CSV_Batch_Exporter::write_csv_data() code WC 9.4.2

protected function write_csv_data( $data ) {

	if ( ! file_exists( $this->get_file_path() ) || ! is_writeable( $this->get_file_path() ) ) {
		wc_get_logger()->error(
			sprintf(
				/* translators: %s is file path. */
				__( 'Unable to create or write to %s during CSV export. Please check file permissions.', 'woocommerce' ),
				esc_html( $this->get_file_path() )
			)
		);
		return false;
	}

	/**
	 * Filters the mode parameter which specifies the type of access you require to the stream (used during file
	 * writing for CSV exports). Defaults to 'a+' (which supports both reading and writing, and places the file
	 * pointer at the end of the file).
	 *
	 * @see   https://www.php.net/manual/en/function.fopen.php
	 * @since 6.8.0
	 *
	 * @param string $fopen_mode, either (r, r+, w, w+, a, a+, x, x+, c, c+, e)
	 */
	$fopen_mode = apply_filters( 'woocommerce_csv_exporter_fopen_mode', 'a+' );
	$fp         = fopen( $this->get_file_path(), $fopen_mode );

	if ( $fp ) {
		fwrite( $fp, $data );
		fclose( $fp );
	}

	// Add all columns when finished.
	if ( 100 === $this->get_percent_complete() ) {
		$header = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers();

		// We need to use a temporary file to store headers, this will make our life so much easier.
		@file_put_contents( $this->get_headers_row_file_path(), $header ); //phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_file_put_contents, Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
	}

}