2019-06-25 19:05:25 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles reports CSV export.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Export
|
|
|
|
*/
|
|
|
|
|
2019-08-01 16:49:55 +00:00
|
|
|
namespace Automattic\WooCommerce\Admin;
|
|
|
|
|
2019-06-25 19:05:25 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2020-01-02 16:00:37 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
|
|
|
|
|
2019-06-25 19:05:25 +00:00
|
|
|
/**
|
2019-08-12 21:52:09 +00:00
|
|
|
* ReportExporter Class.
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2019-08-12 21:52:09 +00:00
|
|
|
class ReportExporter {
|
2019-06-25 19:05:25 +00:00
|
|
|
/**
|
2020-01-02 16:00:37 +00:00
|
|
|
* Slug to identify the scheduler.
|
|
|
|
*
|
|
|
|
* @var string
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public static $name = 'report_exporter';
|
2019-06-25 19:05:25 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-02 16:00:37 +00:00
|
|
|
* Scheduler traits.
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
use SchedulerTraits {
|
|
|
|
init as scheduler_init;
|
|
|
|
}
|
2019-06-25 19:05:25 +00:00
|
|
|
|
2019-06-26 00:40:45 +00:00
|
|
|
/**
|
|
|
|
* Export status option name.
|
|
|
|
*/
|
|
|
|
const EXPORT_STATUS_OPTION = 'wc_admin_report_export_status';
|
|
|
|
|
2019-06-27 02:06:11 +00:00
|
|
|
/**
|
|
|
|
* Export file download action.
|
|
|
|
*/
|
|
|
|
const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv';
|
|
|
|
|
2019-09-26 20:38:32 +00:00
|
|
|
/**
|
2020-01-02 16:00:37 +00:00
|
|
|
* Get all available scheduling actions.
|
|
|
|
* Used to determine action hook names and clear events.
|
2019-06-25 19:05:25 +00:00
|
|
|
*
|
2020-01-02 16:00:37 +00:00
|
|
|
* @return array
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public static function get_scheduler_actions() {
|
|
|
|
return array(
|
|
|
|
'export_report' => 'woocommerce_admin_report_export',
|
|
|
|
'email_report_download_link' => 'woocommerce_admin_email_report_download_link',
|
|
|
|
);
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-02 16:00:37 +00:00
|
|
|
* Add action dependencies.
|
2019-06-25 19:05:25 +00:00
|
|
|
*
|
2020-01-02 16:00:37 +00:00
|
|
|
* @return array
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public static function get_dependencies() {
|
|
|
|
return array(
|
|
|
|
'email_report_download_link' => self::get_action( 'export_report' ),
|
|
|
|
);
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook in action methods.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
// Initialize scheduled action handlers.
|
2020-01-02 16:00:37 +00:00
|
|
|
self::scheduler_init();
|
2019-10-08 22:02:26 +00:00
|
|
|
|
2019-06-27 02:06:11 +00:00
|
|
|
// Report download handler.
|
|
|
|
add_action( 'admin_init', array( __CLASS__, 'download_export_file' ) );
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue up actions for a full report 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-09-26 20:38:32 +00:00
|
|
|
* @param bool $send_email Optional. Send an email when the export is complete.
|
2019-06-26 16:25:12 +00:00
|
|
|
* @return int Number of items to export.
|
2019-06-25 19:05:25 +00:00
|
|
|
*/
|
2019-09-26 20:38:32 +00:00
|
|
|
public static function queue_report_export( $export_id, $report_type, $report_args = array(), $send_email = false ) {
|
2019-08-12 21:52:09 +00:00
|
|
|
$exporter = new ReportCSVExporter( $report_type, $report_args );
|
2019-06-25 19:05:25 +00:00
|
|
|
$exporter->prepare_data_to_export();
|
|
|
|
|
|
|
|
$total_rows = $exporter->get_total_rows();
|
|
|
|
$batch_size = $exporter->get_limit();
|
|
|
|
$num_batches = (int) ceil( $total_rows / $batch_size );
|
|
|
|
|
2019-06-26 19:50:55 +00:00
|
|
|
// Create batches, like initial import.
|
2019-06-27 01:36:20 +00:00
|
|
|
$report_batch_args = array( $export_id, $report_type, $report_args );
|
2019-06-26 19:50:55 +00:00
|
|
|
|
|
|
|
if ( 0 < $num_batches ) {
|
2020-01-02 16:00:37 +00:00
|
|
|
self::queue_batches( 1, $num_batches, 'export_report', $report_batch_args );
|
2019-09-26 20:38:32 +00:00
|
|
|
|
|
|
|
if ( $send_email ) {
|
|
|
|
$email_action_args = array( get_current_user_id(), $export_id, $report_type );
|
2020-01-02 16:00:37 +00:00
|
|
|
self::schedule_action( 'email_report_download_link', $email_action_args );
|
2019-09-26 20:38:32 +00:00
|
|
|
}
|
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 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
|
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public static function export_report( $page_number, $export_id, $report_type, $report_args ) {
|
2019-06-26 19:50:55 +00:00
|
|
|
$report_args['page'] = $page_number;
|
|
|
|
|
2019-08-12 21:52:09 +00:00
|
|
|
$exporter = new ReportCSVExporter( $report_type, $report_args );
|
2019-06-27 01:36:20 +00:00
|
|
|
$exporter->set_filename( "wc-{$report_type}-report-export-{$export_id}" );
|
2019-06-25 19:05:25 +00:00
|
|
|
$exporter->generate_file();
|
2019-06-26 00:40:45 +00:00
|
|
|
|
2019-06-27 01:36:20 +00:00
|
|
|
self::update_export_percentage_complete( $report_type, $export_id, $exporter->get_percent_complete() );
|
2019-06-26 16:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a key to reference an export status.
|
|
|
|
*
|
|
|
|
* @param string $report_type Report type. E.g. 'customers'.
|
|
|
|
* @param string $export_id Unique ID for report (timestamp expected).
|
|
|
|
* @return string Status key.
|
|
|
|
*/
|
2019-06-27 01:36:20 +00:00
|
|
|
protected static function get_status_key( $report_type, $export_id ) {
|
|
|
|
return $report_type . ':' . $export_id;
|
2019-06-26 00:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update 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).
|
|
|
|
* @param int $percentage Completion percentage.
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-06-27 01:36:20 +00:00
|
|
|
public static function update_export_percentage_complete( $report_type, $export_id, $percentage ) {
|
2019-06-26 00:40:45 +00:00
|
|
|
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
|
2019-06-27 01:36:20 +00:00
|
|
|
$status_key = self::get_status_key( $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.
|
|
|
|
*
|
|
|
|
* @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-27 01:36:20 +00:00
|
|
|
public static function get_export_percentage_complete( $report_type, $export_id ) {
|
2019-06-26 00:40:45 +00:00
|
|
|
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
|
2019-06-27 01:36:20 +00:00
|
|
|
$status_key = self::get_status_key( $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
|
|
|
}
|
2019-06-27 02:06:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'] ) &&
|
2019-07-01 18:06:25 +00:00
|
|
|
self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) && // WPCS: input var ok, sanitization ok.
|
|
|
|
current_user_can( 'view_woocommerce_reports' )
|
2019-06-27 02:06:11 +00:00
|
|
|
) {
|
2019-08-12 21:52:09 +00:00
|
|
|
$exporter = new ReportCSVExporter();
|
2019-06-27 02:06:11 +00:00
|
|
|
$exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
|
|
|
|
$exporter->export();
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 20:38:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-01-02 16:00:37 +00:00
|
|
|
public static function email_report_download_link( $user_id, $export_id, $report_type ) {
|
2019-09-26 20:38:32 +00:00
|
|
|
$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() );
|
|
|
|
|
2019-10-08 22:02:26 +00:00
|
|
|
\WC_Emails::instance();
|
2019-09-26 20:38:32 +00:00
|
|
|
$email = new ReportCSVEmail();
|
|
|
|
$email->trigger( $user_id, $report_type, $download_url );
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 19:05:25 +00:00
|
|
|
}
|