PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / ReportExporter.php
ReportExporter.php
215 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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