Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin
File: ReportExporter.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Handles reports CSV export.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Admin;
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit;
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
use Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* ReportExporter Class.
[14] Fix | Delete
*/
[15] Fix | Delete
class ReportExporter {
[16] Fix | Delete
/**
[17] Fix | Delete
* Slug to identify the scheduler.
[18] Fix | Delete
*
[19] Fix | Delete
* @var string
[20] Fix | Delete
*/
[21] Fix | Delete
public static $name = 'report_exporter';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Scheduler traits.
[25] Fix | Delete
*/
[26] Fix | Delete
use SchedulerTraits {
[27] Fix | Delete
init as scheduler_init;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Export status option name.
[32] Fix | Delete
*/
[33] Fix | Delete
const EXPORT_STATUS_OPTION = 'woocommerce_admin_report_export_status';
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Export file download action.
[37] Fix | Delete
*/
[38] Fix | Delete
const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv';
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Get all available scheduling actions.
[42] Fix | Delete
* Used to determine action hook names and clear events.
[43] Fix | Delete
*
[44] Fix | Delete
* @return array
[45] Fix | Delete
*/
[46] Fix | Delete
public static function get_scheduler_actions() {
[47] Fix | Delete
return array(
[48] Fix | Delete
'export_report' => 'woocommerce_admin_report_export',
[49] Fix | Delete
'email_report_download_link' => 'woocommerce_admin_email_report_download_link',
[50] Fix | Delete
);
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Add action dependencies.
[55] Fix | Delete
*
[56] Fix | Delete
* @return array
[57] Fix | Delete
*/
[58] Fix | Delete
public static function get_dependencies() {
[59] Fix | Delete
return array(
[60] Fix | Delete
'email_report_download_link' => self::get_action( 'export_report' ),
[61] Fix | Delete
);
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Hook in action methods.
[66] Fix | Delete
*/
[67] Fix | Delete
public static function init() {
[68] Fix | Delete
// Initialize scheduled action handlers.
[69] Fix | Delete
self::scheduler_init();
[70] Fix | Delete
[71] Fix | Delete
// Report download handler.
[72] Fix | Delete
add_action( 'admin_init', array( __CLASS__, 'download_export_file' ) );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Queue up actions for a full report export.
[77] Fix | Delete
*
[78] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[79] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[80] Fix | Delete
* @param array $report_args Report parameters, passed to data query.
[81] Fix | Delete
* @param bool $send_email Optional. Send an email when the export is complete.
[82] Fix | Delete
* @return int Number of items to export.
[83] Fix | Delete
*/
[84] Fix | Delete
public static function queue_report_export( $export_id, $report_type, $report_args = array(), $send_email = false ) {
[85] Fix | Delete
$exporter = new ReportCSVExporter( $report_type, $report_args );
[86] Fix | Delete
$exporter->prepare_data_to_export();
[87] Fix | Delete
[88] Fix | Delete
$total_rows = $exporter->get_total_rows();
[89] Fix | Delete
$batch_size = $exporter->get_limit();
[90] Fix | Delete
$num_batches = (int) ceil( $total_rows / $batch_size );
[91] Fix | Delete
[92] Fix | Delete
// Create batches, like initial import.
[93] Fix | Delete
$report_batch_args = array( $export_id, $report_type, $report_args );
[94] Fix | Delete
[95] Fix | Delete
if ( 0 < $num_batches ) {
[96] Fix | Delete
self::queue_batches( 1, $num_batches, 'export_report', $report_batch_args );
[97] Fix | Delete
[98] Fix | Delete
if ( $send_email ) {
[99] Fix | Delete
$email_action_args = array( get_current_user_id(), $export_id, $report_type );
[100] Fix | Delete
self::schedule_action( 'email_report_download_link', $email_action_args );
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
return $total_rows;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Process a report export action.
[109] Fix | Delete
*
[110] Fix | Delete
* @param int $page_number Page number for this action.
[111] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[112] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[113] Fix | Delete
* @param array $report_args Report parameters, passed to data query.
[114] Fix | Delete
* @return void
[115] Fix | Delete
*/
[116] Fix | Delete
public static function export_report( $page_number, $export_id, $report_type, $report_args ) {
[117] Fix | Delete
$report_args['page'] = $page_number;
[118] Fix | Delete
[119] Fix | Delete
$exporter = new ReportCSVExporter( $report_type, $report_args );
[120] Fix | Delete
$exporter->set_filename( "wc-{$report_type}-report-export-{$export_id}" );
[121] Fix | Delete
$exporter->generate_file();
[122] Fix | Delete
[123] Fix | Delete
self::update_export_percentage_complete( $report_type, $export_id, $exporter->get_percent_complete() );
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Generate a key to reference an export status.
[128] Fix | Delete
*
[129] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[130] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[131] Fix | Delete
* @return string Status key.
[132] Fix | Delete
*/
[133] Fix | Delete
protected static function get_status_key( $report_type, $export_id ) {
[134] Fix | Delete
return $report_type . ':' . $export_id;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Update the completion percentage of a report export.
[139] Fix | Delete
*
[140] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[141] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[142] Fix | Delete
* @param int $percentage Completion percentage.
[143] Fix | Delete
* @return void
[144] Fix | Delete
*/
[145] Fix | Delete
public static function update_export_percentage_complete( $report_type, $export_id, $percentage ) {
[146] Fix | Delete
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
[147] Fix | Delete
$status_key = self::get_status_key( $report_type, $export_id );
[148] Fix | Delete
[149] Fix | Delete
$exports_status[ $status_key ] = $percentage;
[150] Fix | Delete
[151] Fix | Delete
update_option( self::EXPORT_STATUS_OPTION, $exports_status );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Get the completion percentage of a report export.
[156] Fix | Delete
*
[157] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[158] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[159] Fix | Delete
* @return bool|int Completion percentage, or false if export not found.
[160] Fix | Delete
*/
[161] Fix | Delete
public static function get_export_percentage_complete( $report_type, $export_id ) {
[162] Fix | Delete
$exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
[163] Fix | Delete
$status_key = self::get_status_key( $report_type, $export_id );
[164] Fix | Delete
[165] Fix | Delete
if ( isset( $exports_status[ $status_key ] ) ) {
[166] Fix | Delete
return $exports_status[ $status_key ];
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return false;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Serve the export file.
[174] Fix | Delete
*/
[175] Fix | Delete
public static function download_export_file() {
[176] Fix | Delete
// @todo - add nonce? (nonces are good for 24 hours)
[177] Fix | Delete
if (
[178] Fix | Delete
isset( $_GET['action'] ) &&
[179] Fix | Delete
! empty( $_GET['filename'] ) &&
[180] Fix | Delete
self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) && // WPCS: input var ok, sanitization ok.
[181] Fix | Delete
current_user_can( 'view_woocommerce_reports' )
[182] Fix | Delete
) {
[183] Fix | Delete
$exporter = new ReportCSVExporter();
[184] Fix | Delete
$exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
[185] Fix | Delete
$exporter->export();
[186] Fix | Delete
}
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Process a report export email action.
[191] Fix | Delete
*
[192] Fix | Delete
* @param int $user_id User ID that requested the email.
[193] Fix | Delete
* @param string $export_id Unique ID for report (timestamp expected).
[194] Fix | Delete
* @param string $report_type Report type. E.g. 'customers'.
[195] Fix | Delete
* @return void
[196] Fix | Delete
*/
[197] Fix | Delete
public static function email_report_download_link( $user_id, $export_id, $report_type ) {
[198] Fix | Delete
$percent_complete = self::get_export_percentage_complete( $report_type, $export_id );
[199] Fix | Delete
[200] Fix | Delete
if ( 100 === $percent_complete ) {
[201] Fix | Delete
$query_args = array(
[202] Fix | Delete
'action' => self::DOWNLOAD_EXPORT_ACTION,
[203] Fix | Delete
'filename' => "wc-{$report_type}-report-export-{$export_id}",
[204] Fix | Delete
);
[205] Fix | Delete
$download_url = add_query_arg( $query_args, admin_url() );
[206] Fix | Delete
[207] Fix | Delete
\WC_Emails::instance();
[208] Fix | Delete
$email = new ReportCSVEmail();
[209] Fix | Delete
$email->trigger( $user_id, $report_type, $download_url );
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function