update_export_percentage_complete( $report_type, $export_id, $percentage ) { $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() ); $status_key = self::get_status_key( $report_type, $export_id ); $exports_status[ $status_key ] = $percentage; update_option( self::EXPORT_STATUS_OPTION, $exports_status ); } /** * Get the completion percentage of a report export. * * @param string $report_type Report type. E.g. 'customers'. * @param string $export_id Unique ID for report (timestamp expected). * @return bool|int Completion percentage, or false if export not found. */ public static function get_export_percentage_complete( $report_type, $export_id ) { $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() ); $status_key = self::get_status_key( $report_type, $export_id ); if ( isset( $exports_status[ $status_key ] ) ) { return $exports_status[ $status_key ]; } 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. current_user_can( 'view_woocommerce_reports' ) ) { $exporter = new ReportCSVExporter(); $exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok. $exporter->export(); } } /** * Process a report export email action. * * @param int $user_id User ID that requested the email. * @param string $export_id Unique ID for report (timestamp expected). * @param string $report_type Report type. E.g. 'customers'. * @return void */ public static function email_report_download_link( $user_id, $export_id, $report_type ) { $percent_complete = self::get_export_percentage_complete( $report_type, $export_id ); if ( 100 === $percent_complete ) { $query_args = array( 'action' => self::DOWNLOAD_EXPORT_ACTION, 'filename' => "wc-{$report_type}-report-export-{$export_id}", ); $download_url = add_query_arg( $query_args, admin_url() ); \WC_Emails::instance(); $email = new ReportCSVEmail(); $email->trigger( $user_id, $report_type, $download_url ); } } }