Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion classes/helpers/FrmCSVExportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,34 @@ class FrmCSVExportHelper {
*/
protected static $meta = array();

/**
* Whether to include the BOM (Byte Order Mark) in the CSV file.
* Only applicable for UTF-8 exports.
*
* @since x.x
*
* @var bool
*/
private static $include_bom = false;

/**
* Get all options for the CSV export format dropdown.
*
* @since x.x The UTF-8 with BOM option was added.
*
* @return array
*/
public static function csv_format_options() {
$formats = array( 'UTF-8', 'ISO-8859-1', 'windows-1256', 'windows-1251', 'macintosh' );

// Do not add the UTF-8 with BOM option if this function is called on Global Settings.
// This is to improve compatibility with the Export View as CSV add-on (v1.10).
// Otherwise, the option will appear twice since it is added in the add-on as well.
$on_global_settings_page = 'formidable-settings' === FrmAppHelper::get_param( 'page' );
if ( ! $on_global_settings_page ) {
array_splice( $formats, 1, 0, 'UTF-8 with BOM' );
}

$formats = apply_filters( 'frm_csv_format_options', $formats );

return $formats;
Expand Down Expand Up @@ -241,6 +267,10 @@ private static function print_file_headers( $filename ) {
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Pragma: no-cache' );

if ( self::$include_bom ) {
echo esc_html( chr( 239 ) . chr( 187 ) . chr( 191 ) );
}

do_action(
'frm_csv_headers',
array(
Expand All @@ -250,8 +280,19 @@ private static function print_file_headers( $filename ) {
);
}

/**
* Check the POST request data for the CSV format to use.
*
* @return void
*/
public static function get_csv_format() {
$csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' );
$csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' );

if ( 'UTF-8 with BOM' === $csv_format ) {
self::$include_bom = true;
$csv_format = 'UTF-8';
}

$csv_format = apply_filters( 'frm_csv_format', $csv_format, self::get_standard_filter_args() );
self::$to_encoding = $csv_format;
}
Expand Down