Add report CSV downloading.

This commit is contained in:
Jeff Stieler 2019-06-26 20:06:11 -06:00
parent c68010f184
commit 74ab15aebf
3 changed files with 38 additions and 5 deletions

View File

@ -222,7 +222,12 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
// @todo - add thing in the links below instead? // @todo - add thing in the links below instead?
if ( 100 === $percentage ) { if ( 100 === $percentage ) {
$result['download_url'] = rest_url( sprintf( '%s/reports/%s/export/%s/download', $this->namespace, $report_type, $export_id ) ); $query_args = array(
'action' => WC_Admin_Report_Exporter::DOWNLOAD_EXPORT_ACTION,
'filename' => "wc-{$report_type}-report-export-{$export_id}",
);
$result['download_url'] = add_query_arg( $query_args, admin_url() );
} }
// Wrap the data in a response object. // Wrap the data in a response object.

View File

@ -47,12 +47,17 @@ class WC_Admin_Report_CSV_Exporter extends WC_CSV_Batch_Exporter {
* @param string $type Report type. E.g. 'customers'. * @param string $type Report type. E.g. 'customers'.
* @param array $args Report parameters. * @param array $args Report parameters.
*/ */
public function __construct( $type, $args ) { public function __construct( $type = false, $args = array() ) {
parent::__construct(); parent::__construct();
$this->set_report_type( $type ); if ( ! empty( $type ) ) {
$this->set_report_args( $args ); $this->set_report_type( $type );
$this->set_column_names( $this->get_report_columns() ); $this->set_column_names( $this->get_report_columns() );
}
if ( ! empty( $args ) ) {
$this->set_report_args( $args );
}
} }
/** /**

View File

@ -28,6 +28,11 @@ class WC_Admin_Report_Exporter {
*/ */
const EXPORT_STATUS_OPTION = 'wc_admin_report_export_status'; const EXPORT_STATUS_OPTION = 'wc_admin_report_export_status';
/**
* Export file download action.
*/
const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv';
/** /**
* Queue instance. * Queue instance.
* *
@ -63,6 +68,8 @@ class WC_Admin_Report_Exporter {
public static function init() { public static function init() {
// Initialize scheduled action handlers. // Initialize scheduled action handlers.
add_action( self::REPORT_EXPORT_ACTION, array( __CLASS__, 'report_export_action' ), 10, 4 ); add_action( self::REPORT_EXPORT_ACTION, array( __CLASS__, 'report_export_action' ), 10, 4 );
// Report download handler.
add_action( 'admin_init', array( __CLASS__, 'download_export_file' ) );
} }
/** /**
@ -156,6 +163,22 @@ class WC_Admin_Report_Exporter {
return false; return false;
} }
/**
* Serve the export file.
*/
public static function download_export_file() {
// @todo - add nonce? (nonces are good for 24 hours)
if (
isset( $_GET['action'] ) &&
! empty( $_GET['filename'] ) &&
self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) // WPCS: input var ok, sanitization ok.
) {
$exporter = new WC_Admin_Report_CSV_Exporter();
$exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
$exporter->export();
}
}
} }
WC_Admin_Report_Exporter::init(); WC_Admin_Report_Exporter::init();