diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php index e0d263dc544..5d9e76fb764 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-export-controller.php @@ -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? 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. diff --git a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php b/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php index c18821c6c45..887952edcb9 100644 --- a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php +++ b/plugins/woocommerce-admin/includes/export/class-wc-admin-report-csv-exporter.php @@ -47,12 +47,17 @@ class WC_Admin_Report_CSV_Exporter extends WC_CSV_Batch_Exporter { * @param string $type Report type. E.g. 'customers'. * @param array $args Report parameters. */ - public function __construct( $type, $args ) { + public function __construct( $type = false, $args = array() ) { parent::__construct(); - $this->set_report_type( $type ); - $this->set_report_args( $args ); - $this->set_column_names( $this->get_report_columns() ); + if ( ! empty( $type ) ) { + $this->set_report_type( $type ); + $this->set_column_names( $this->get_report_columns() ); + } + + if ( ! empty( $args ) ) { + $this->set_report_args( $args ); + } } /** diff --git a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php b/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php index fb424e88a38..2b4a99a273d 100644 --- a/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php +++ b/plugins/woocommerce-admin/includes/export/class-wc-admin-report-exporter.php @@ -28,6 +28,11 @@ class WC_Admin_Report_Exporter { */ const EXPORT_STATUS_OPTION = 'wc_admin_report_export_status'; + /** + * Export file download action. + */ + const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv'; + /** * Queue instance. * @@ -63,6 +68,8 @@ class WC_Admin_Report_Exporter { public static function init() { // Initialize scheduled action handlers. 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; } + + /** + * 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();