0 ) { ob_end_clean(); } } if ( ! headers_sent() ) { header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' ); header( 'X-Content-Type-Options: nosniff' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, private' ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); } $stream = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( false === $stream ) { if ( ! $testing ) { exit(); } return; } try { // UTF-8 BOM — makes Excel on Windows open the file with the correct // encoding without forcing the user through the import wizard. if ( false === fwrite( $stream, "\xEF\xBB\xBF" ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite throw new RuntimeException( 'Failed to write CSV BOM to output stream.' ); } if ( array() !== $headers && false === self::write_row( $stream, $headers ) ) { throw new RuntimeException( 'Failed to write CSV header row to output stream.' ); } foreach ( $rows as $row ) { if ( false === self::write_row( $stream, (array) $row ) ) { throw new RuntimeException( 'Failed to write CSV data row to output stream.' ); } } } finally { fclose( $stream ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose } if ( ! $testing ) { exit(); } } }