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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / ReportExporter.php
woocommerce / src / Admin Last commit date
API 3 weeks ago BlockTemplates 2 years ago Composer 2 years ago DateTimeProvider 4 years ago Features 1 week ago Marketing 1 year ago Notes 4 months ago Overrides 4 weeks ago PluginsInstallLoggers 1 year ago PluginsProvider 4 years ago RemoteInboxNotifications 4 weeks ago RemoteSpecs 4 weeks ago Schedulers 1 year ago DataSourcePoller.php 2 years ago DeprecatedClassFacade.php 1 year ago FeaturePlugin.php 4 years ago Loader.php 2 years ago PageController.php 3 months ago PluginsHelper.php 2 months ago PluginsInstaller.php 4 years ago ReportCSVEmail.php 1 year ago ReportCSVExporter.php 1 year ago ReportExporter.php 3 years ago ReportsSync.php 3 months ago WCAdminHelper.php 1 year ago
ReportExporter.php
215 lines
1 <?php
2 /**
3 * Handles reports CSV export.
4 */
5
6 namespace Automattic\WooCommerce\Admin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 use Automattic\WooCommerce\Admin\Schedulers\SchedulerTraits;
13
14 /**
15 * ReportExporter Class.
16 */
17 class ReportExporter {
18 /**
19 * Slug to identify the scheduler.
20 *
21 * @var string
22 */
23 public static $name = 'report_exporter';
24
25 /**
26 * Scheduler traits.
27 */
28 use SchedulerTraits {
29 init as scheduler_init;
30 }
31
32 /**
33 * Export status option name.
34 */
35 const EXPORT_STATUS_OPTION = 'woocommerce_admin_report_export_status';
36
37 /**
38 * Export file download action.
39 */
40 const DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv';
41
42 /**
43 * Get all available scheduling actions.
44 * Used to determine action hook names and clear events.
45 *
46 * @return array
47 */
48 public static function get_scheduler_actions() {
49 return array(
50 'export_report' => 'woocommerce_admin_report_export',
51 'email_report_download_link' => 'woocommerce_admin_email_report_download_link',
52 );
53 }
54
55 /**
56 * Add action dependencies.
57 *
58 * @return array
59 */
60 public static function get_dependencies() {
61 return array(
62 'email_report_download_link' => self::get_action( 'export_report' ),
63 );
64 }
65
66 /**
67 * Hook in action methods.
68 */
69 public static function init() {
70 // Initialize scheduled action handlers.
71 self::scheduler_init();
72
73 // Report download handler.
74 add_action( 'admin_init', array( __CLASS__, 'download_export_file' ) );
75 }
76
77 /**
78 * Queue up actions for a full report export.
79 *
80 * @param string $export_id Unique ID for report (timestamp expected).
81 * @param string $report_type Report type. E.g. 'customers'.
82 * @param array $report_args Report parameters, passed to data query.
83 * @param bool $send_email Optional. Send an email when the export is complete.
84 * @return int Number of items to export.
85 */
86 public static function queue_report_export( $export_id, $report_type, $report_args = array(), $send_email = false ) {
87 $exporter = new ReportCSVExporter( $report_type, $report_args );
88 $exporter->prepare_data_to_export();
89
90 $total_rows = $exporter->get_total_rows();
91 $batch_size = $exporter->get_limit();
92 $num_batches = (int) ceil( $total_rows / $batch_size );
93
94 // Create batches, like initial import.
95 $report_batch_args = array( $export_id, $report_type, $report_args );
96
97 if ( 0 < $num_batches ) {
98 self::queue_batches( 1, $num_batches, 'export_report', $report_batch_args );
99
100 if ( $send_email ) {
101 $email_action_args = array( get_current_user_id(), $export_id, $report_type );
102 self::schedule_action( 'email_report_download_link', $email_action_args );
103 }
104 }
105
106 return $total_rows;
107 }
108
109 /**
110 * Process a report export action.
111 *
112 * @param int $page_number Page number for this action.
113 * @param string $export_id Unique ID for report (timestamp expected).
114 * @param string $report_type Report type. E.g. 'customers'.
115 * @param array $report_args Report parameters, passed to data query.
116 * @return void
117 */
118 public static function export_report( $page_number, $export_id, $report_type, $report_args ) {
119 $report_args['page'] = $page_number;
120
121 $exporter = new ReportCSVExporter( $report_type, $report_args );
122 $exporter->set_filename( "wc-{$report_type}-report-export-{$export_id}" );
123 $exporter->generate_file();
124
125 self::update_export_percentage_complete( $report_type, $export_id, $exporter->get_percent_complete() );
126 }
127
128 /**
129 * Generate a key to reference an export status.
130 *
131 * @param string $report_type Report type. E.g. 'customers'.
132 * @param string $export_id Unique ID for report (timestamp expected).
133 * @return string Status key.
134 */
135 protected static function get_status_key( $report_type, $export_id ) {
136 return $report_type . ':' . $export_id;
137 }
138
139 /**
140 * Update the completion percentage of a report export.
141 *
142 * @param string $report_type Report type. E.g. 'customers'.
143 * @param string $export_id Unique ID for report (timestamp expected).
144 * @param int $percentage Completion percentage.
145 * @return void
146 */
147 public static function update_export_percentage_complete( $report_type, $export_id, $percentage ) {
148 $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
149 $status_key = self::get_status_key( $report_type, $export_id );
150
151 $exports_status[ $status_key ] = $percentage;
152
153 update_option( self::EXPORT_STATUS_OPTION, $exports_status );
154 }
155
156 /**
157 * Get the completion percentage of a report export.
158 *
159 * @param string $report_type Report type. E.g. 'customers'.
160 * @param string $export_id Unique ID for report (timestamp expected).
161 * @return bool|int Completion percentage, or false if export not found.
162 */
163 public static function get_export_percentage_complete( $report_type, $export_id ) {
164 $exports_status = get_option( self::EXPORT_STATUS_OPTION, array() );
165 $status_key = self::get_status_key( $report_type, $export_id );
166
167 if ( isset( $exports_status[ $status_key ] ) ) {
168 return $exports_status[ $status_key ];
169 }
170
171 return false;
172 }
173
174 /**
175 * Serve the export file.
176 */
177 public static function download_export_file() {
178 // @todo - add nonce? (nonces are good for 24 hours)
179 if (
180 isset( $_GET['action'] ) &&
181 ! empty( $_GET['filename'] ) &&
182 self::DOWNLOAD_EXPORT_ACTION === wp_unslash( $_GET['action'] ) && // WPCS: input var ok, sanitization ok.
183 current_user_can( 'view_woocommerce_reports' )
184 ) {
185 $exporter = new ReportCSVExporter();
186 $exporter->set_filename( wp_unslash( $_GET['filename'] ) ); // WPCS: input var ok, sanitization ok.
187 $exporter->export();
188 }
189 }
190
191 /**
192 * Process a report export email action.
193 *
194 * @param int $user_id User ID that requested the email.
195 * @param string $export_id Unique ID for report (timestamp expected).
196 * @param string $report_type Report type. E.g. 'customers'.
197 * @return void
198 */
199 public static function email_report_download_link( $user_id, $export_id, $report_type ) {
200 $percent_complete = self::get_export_percentage_complete( $report_type, $export_id );
201
202 if ( 100 === $percent_complete ) {
203 $query_args = array(
204 'action' => self::DOWNLOAD_EXPORT_ACTION,
205 'filename' => "wc-{$report_type}-report-export-{$export_id}",
206 );
207 $download_url = add_query_arg( $query_args, admin_url() );
208
209 \WC_Emails::instance();
210 $email = new ReportCSVEmail();
211 $email->trigger( $user_id, $report_type, $download_url );
212 }
213 }
214 }
215