FacetWP template for generating csv of result set

<?php
/*
/*
Plugin Name: Download member report as csv
Plugin URI:  http://
Description: This plugin is called when the 'download csv' button is clicked on the Member Reports page containing FacetWP search
Version:     1.0
Author:      clare@greenbee-web.com
Author URI:  http://www.greenbee-web.com/
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages

Called from form submit in FacetWP template

All it does it write a csv directly to the browser for download. 
The csv data is passed from the FacetWP template "Member reporting", containing member-reporting-loop.php

*/
if($_POST){
	if(isset($_POST['member_csv_data'])){

	header('Content-Type: application/excel');
	header('Content-Disposition: attachment; filename="upmasanational-member-data.csv"');

	$data = unserialize($_POST['member_csv_data']); 
	write_csv($data); 

	}else{
		return; 
	}

}else{

	return; 
}

function write_csv($data){

	$out = fopen('php://output', 'w');
	foreach ( $data as $line ) {
    	fputcsv($out, $line);
	}

	fclose($out);

}

?>