2019-06-25 19:05:25 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles reports CSV export.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Export
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Admin_Report_Exporter Class.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Report_Exporter {
|
|
|
|
/**
|
|
|
|
* Action hook for generating a report export.
|
|
|
|
*/
|
|
|
|
const REPORT_EXPORT_ACTION = 'wc-admin_report_export';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action scheduler group.
|
|
|
|
*/
|
|
|
|
const QUEUE_GROUP = 'wc-admin-data';
|
|
|
|
|
2019-06-26 00:40:45 +00:00
|
|
|
/**
|
|
|
|
* Export status option name.
|
|
|
|
*/
|
|
|
|
const EXPORT_STATUS_OPTION = 'wc_admin_report_export_status';
|
|
|
|
|
2019-06-25 19:05:25 +00:00
|
|
|
/**
|
|
|
|
* Queue instance.
|
|
|
|
*
|
|
|
|
* @var WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
protected static $queue = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get queue instance.
|
|
|
|
*
|
|
|
|
* @return WC_Queue_Interface
|
|
|
|
*/
|
|
|
|
public static function queue() {
|
|
|
|
if ( is_null( self::$queue ) ) {
|
|
|
|
self::$queue = WC()->queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set queue instance.
|
|
|
|
*
|
|
|
|
* @param WC_Queue_Interface $queue Queue instance.
|
|
|
|
*/
|
|
|
|
public static function set_queue( $queue ) {
|
|
|
|
self::$queue = $queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook in action methods.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
// Initialize scheduled action handlers.
|
2019-06-26 19:50:55 +00:00
|
|
|
add_action( self::REPORT_EXPORT_ACTION, array( __CLASS__, 'report_export_action' ), 10, 5 );
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue up actions for a full report export.
|
|
|
|
*
|
|
|
|
* @param int $user_id User requesting export.
|
|
|
|
* @param string $export_id Unique ID for report (timestamp expected).
|
|
|
|
* @param string $report_type Report type. E.g. 'customers'.
|
|
|
|
* @param array $report_args Report parameters, passed to data query.
|
2019-06-26 16:25:12 +00:00
|
|
|
* @return int Number of items to export.
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
|
|
|
public static function queue_report_export( $user_id, $export_id, $report_type, $report_args = array() ) {
|
|
|
|
$exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args );
|
|
|
|
$exporter->prepare_data_to_export();
|
|
|
|
|
|
|
|
$total_rows = $exporter->get_total_rows();
|
|
|
|
$batch_size = $exporter->get_limit();
|
|
|
|
$num_batches = (int) ceil( $total_rows / $batch_size );
|
|
|
|
$start_time = time() + 5;
|
|
|
|
|
2019-06-26 19:50:55 +00:00
|
|
|
// Create batches, like initial import.
|
|
|
|
$report_batch_args = array( $user_id, $export_id, $report_type, $report_args );
|
|
|
|
|
|
|
|
if ( 0 < $num_batches ) {
|
|
|
|
WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, self::REPORT_EXPORT_ACTION, $report_batch_args );
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
2019-06-26 16:25:12 +00:00
|
|
|
|
|
|
|
return $total_rows;
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a report export action.
|
|
|
|
*
|
2019-06-26 19:50:55 +00:00
|
|
|
* @param int $page_number Page number for this action.
|
2019-06-25 19:05:25 +00:00
|
|
|
* @param int $user_id User requesting export.
|
|
|
|
* @param string $export_id Unique ID for report (timestamp expected).
|
|
|
|
* @param string $report_type Report type. E.g. 'customers'.
|
|
|
|
* @param array $report_args Report parameters, passed to data query.
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-06-26 19:50:55 +00:00
|
|
|
public static function report_export_action( $page_number, $user_id, $export_id, $report_type, $report_args ) {
|
|
|
|
$report_args['page'] = $page_number;
|
|
|
|
|
2019-06-25 19:05:25 +00:00
|
|
|
$exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args );
|
|
|
|
$exporter->set_filename( "wc-{$report_type}-report-export-{$user_id}-{$export_id}" );
|
|
|
|
$exporter->generate_file();
|
2019-06-26 00:40:45 +00:00
|
|
|
|
2019-06-26 16:24:06 +00:00
|
|
|
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 ) );
|
2019-06-26 00:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the completion percentage of a report export.
|
|
|
|
*
|
2019-06-26 16:24:06 +00:00
|
|
|
* @param int $user_id User ID who requested the export.
|
2019-06-26 00:40:45 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2019-06-26 16:24:06 +00:00
|
|
|
public static function update_export_percentage_complete( $user_id, $report_type, $export_id, $percentage ) {
|
2019-06-26 00:40:45 +00:00
|
|
|
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
|
2019-06-26 16:24:06 +00:00
|
|
|
$status_key = self::get_status_key( $user_id, $report_type, $export_id );
|
2019-06-26 00:40:45 +00:00
|
|
|
|
2019-06-26 16:24:06 +00:00
|
|
|
$exports_status[ $status_key ] = $percentage;
|
2019-06-26 00:40:45 +00:00
|
|
|
|
|
|
|
update_option( self::EXPORT_STATUS_OPTION, $exports_status );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the completion percentage of a report export.
|
|
|
|
*
|
2019-06-26 16:24:06 +00:00
|
|
|
* @param int $user_id User ID who requested the export.
|
2019-06-26 00:40:45 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2019-06-26 16:24:06 +00:00
|
|
|
public static function get_export_percentage_complete( $user_id, $report_type, $export_id ) {
|
2019-06-26 00:40:45 +00:00
|
|
|
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
|
2019-06-26 16:24:06 +00:00
|
|
|
$status_key = self::get_status_key( $user_id, $report_type, $export_id );
|
2019-06-26 00:40:45 +00:00
|
|
|
|
2019-06-26 16:24:06 +00:00
|
|
|
if ( isset( $exports_status[ $status_key ] ) ) {
|
|
|
|
return $exports_status[ $status_key ];
|
2019-06-26 00:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Admin_Report_Exporter::init();
|