<?php
/**
 * CSV exporter.
 *
 * @package SubmissionInbox
 */

namespace SUBIN\Export;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Streams admin CSV exports.
 */
class CsvExporter {
	/**
	 * Export leads.
	 *
	 * @param array<int,array<int,string>> $rows CSV rows.
	 * @param string                       $filename File name.
	 * @return void
	 */
	public function export_leads( array $rows, $filename ) {
		$this->export( $rows, $filename );
	}

	/**
	 * Export email events.
	 *
	 * @param array<int,array<int,string>> $rows CSV rows.
	 * @param string                       $filename File name.
	 * @return void
	 */
	public function export_email_events( array $rows, $filename ) {
		$this->export( $rows, $filename );
	}

	/**
	 * Stream CSV response.
	 *
	 * @param array<int,array<int,string>> $rows CSV rows.
	 * @param string                       $filename File name.
	 * @return void
	 */
	private function export( array $rows, $filename ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to perform this action.', 'submission-inbox' ) );
		}

		if ( ob_get_length() ) {
			ob_clean();
		}

		nocache_headers();
		header( 'Content-Type: text/csv; charset=utf-8' );
		header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' );
		header( 'Pragma: no-cache' );
		header( 'Expires: 0' );

		$handle = fopen( 'php://output', 'w' );
		if ( false === $handle ) {
			exit;
		}

		echo "\xEF\xBB\xBF";
		foreach ( $rows as $row ) {
			fputcsv( $handle, array_map( array( $this, 'string_value' ), $row ) );
		}

		exit;
	}

	/**
	 * Normalize CSV cell.
	 *
	 * @param mixed $value Value.
	 * @return string
	 */
	private function string_value( $value ) {
		return sanitize_textarea_field( (string) $value );
	}
}
