From 1650794f2b66dfb6ada1f96ef944700282fd39fd Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Wed, 26 Jun 2019 10:24:06 -0600 Subject: [PATCH] Include user ID in report export status key. --- ...c-admin-rest-reports-export-controller.php | 5 +-- .../export/class-wc-admin-report-exporter.php | 35 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) 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 c0eb875db95..bbc715cc8de 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 @@ -164,7 +164,7 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont $export_id = str_replace( '.', '', microtime( true ) ); WC_Admin_Report_Exporter::queue_report_export( $user_id, $export_id, $report_type, $report_args ); - WC_Admin_Report_Exporter::update_export_percentage_complete( $report_type, $export_id, 0 ); + WC_Admin_Report_Exporter::update_export_percentage_complete( $user_id, $report_type, $export_id, 0 ); // @todo: handle error case? $result = array( @@ -197,7 +197,8 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont public function export_status( $request ) { $report_type = $request['type']; $export_id = $request['export_id']; - $percentage = WC_Admin_Report_Exporter::get_export_percentage_complete( $report_type, $export_id ); + $user_id = get_current_user_id(); + $percentage = WC_Admin_Report_Exporter::get_export_percentage_complete( $user_id, $report_type, $export_id ); if ( false === $percentage ) { return new WP_Error( 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 42e2392073d..0cf958baab4 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 @@ -115,25 +115,35 @@ class WC_Admin_Report_Exporter { $exporter->set_filename( "wc-{$report_type}-report-export-{$user_id}-{$export_id}" ); $exporter->generate_file(); - self::update_export_percentage_complete( $report_type, $export_id, $exporter->get_percent_complete() ); + self::update_export_percentage_complete( $user_id, $report_type, $export_id, $exporter->get_percent_complete() ); + } + + /** + * Generate a key to reference an export status. + * + * @param int $user_id User ID who requested the export. + * @param string $report_type Report type. E.g. 'customers'. + * @param string $export_id Unique ID for report (timestamp expected). + * @return string Status key. + */ + protected static function get_status_key( $user_id, $report_type, $export_id ) { + return implode( ':', array( $user_id, $report_type, $export_id ) ); } /** * Update the completion percentage of a report export. * + * @param int $user_id User ID who requested the export. * @param string $report_type Report type. E.g. 'customers'. * @param string $export_id Unique ID for report (timestamp expected). * @param int $percentage Completion percentage. * @return void */ - public static function update_export_percentage_complete( $report_type, $export_id, $percentage ) { + public static function update_export_percentage_complete( $user_id, $report_type, $export_id, $percentage ) { $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() ); + $status_key = self::get_status_key( $user_id, $report_type, $export_id ); - if ( ! isset( $exports_status[ $report_type ] ) ) { - $exports_status[ $report_type ] = array(); - } - - $exports_status[ $report_type ][ $export_id ] = $percentage; + $exports_status[ $status_key ] = $percentage; update_option( self::EXPORT_STATUS_OPTION, $exports_status ); } @@ -141,18 +151,17 @@ class WC_Admin_Report_Exporter { /** * Get the completion percentage of a report export. * + * @param int $user_id User ID who requested the 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 ) { + public static function get_export_percentage_complete( $user_id, $report_type, $export_id ) { $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() ); + $status_key = self::get_status_key( $user_id, $report_type, $export_id ); - if ( - isset( $exports_status[ $report_type ] ) && - isset( $exports_status[ $report_type ][ $export_id ] ) - ) { - return $exports_status[ $report_type ][ $export_id ]; + if ( isset( $exports_status[ $status_key ] ) ) { + return $exports_status[ $status_key ]; } return false;