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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 / ReportCSVExporter.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
ReportCSVExporter.php
360 lines
1 <?php
2 /**
3 * Handles reports CSV export batches.
4 */
5
6 namespace Automattic\WooCommerce\Admin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
13
14 /**
15 * Include dependencies.
16 */
17 if ( ! class_exists( 'WC_CSV_Batch_Exporter', false ) ) {
18 include_once WC_ABSPATH . 'includes/export/abstract-wc-csv-batch-exporter.php';
19 }
20
21 /**
22 * ReportCSVExporter Class.
23 */
24 class ReportCSVExporter extends \WC_CSV_Batch_Exporter {
25 /**
26 * Type of report being exported.
27 *
28 * @var string
29 */
30 protected $report_type;
31
32 /**
33 * Parameters for the report query.
34 *
35 * @var array
36 */
37 protected $report_args;
38
39 /**
40 * REST controller for the report.
41 *
42 * @var WC_REST_Reports_Controller
43 */
44 protected $controller;
45
46 /**
47 * Constructor.
48 *
49 * @param string $type Report type. E.g. 'customers'.
50 * @param array $args Report parameters.
51 */
52 public function __construct( $type = false, $args = array() ) {
53 parent::__construct();
54
55 self::maybe_create_directory();
56
57 if ( ! empty( $type ) ) {
58 $this->set_report_type( $type );
59 $this->set_column_names( $this->get_report_columns() );
60 }
61
62 if ( ! empty( $args ) ) {
63 $this->set_report_args( $args );
64 }
65 }
66
67 /**
68 * Create the directory for reports if it does not yet exist.
69 */
70 public static function maybe_create_directory() {
71 $reports_dir = self::get_reports_directory();
72
73 $files = array(
74 array(
75 'base' => $reports_dir,
76 'file' => '.htaccess',
77 'content' => 'DirectoryIndex index.php index.html' . PHP_EOL . 'deny from all',
78 ),
79 array(
80 'base' => $reports_dir,
81 'file' => 'index.html',
82 'content' => '',
83 ),
84 );
85
86 foreach ( $files as $file ) {
87 if ( ! file_exists( trailingslashit( $file['base'] ) ) ) {
88 wp_mkdir_p( $file['base'] );
89 }
90 if ( ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
91 $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
92 if ( $file_handle ) {
93 fwrite( $file_handle, $file['content'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
94 fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
95 }
96 }
97 }
98 }
99
100 /**
101 * Get report uploads directory.
102 *
103 * @return string
104 */
105 public static function get_reports_directory() {
106 $upload_dir = wp_upload_dir();
107 return trailingslashit( $upload_dir['basedir'] ) . 'woocommerce_uploads/reports/';
108 }
109
110 /**
111 * Get file path to export to.
112 *
113 * @return string
114 */
115 protected function get_file_path() {
116 return self::get_reports_directory() . $this->get_filename();
117 }
118
119
120 /**
121 * Setter for report type.
122 *
123 * @param string $type The report type. E.g. customers.
124 */
125 public function set_report_type( $type ) {
126 $this->report_type = $type;
127 $this->export_type = "admin_{$type}_report";
128 $this->filename = "wc-{$type}-report-export";
129 $this->controller = $this->map_report_controller();
130 }
131
132 /**
133 * Setter for report args.
134 *
135 * @param array $args The report args.
136 */
137 public function set_report_args( $args ) {
138 // Use our own internal limit and include all extended info.
139 $report_args = array_merge(
140 $args,
141 array(
142 'per_page' => $this->get_limit(),
143 'extended_info' => true,
144 )
145 );
146
147 // Should this happen externally?
148 if ( isset( $report_args['page'] ) ) {
149 $this->set_page( $report_args['page'] );
150 }
151
152 $this->report_args = $report_args;
153 }
154
155 /**
156 * Get a REST controller instance for the report type.
157 *
158 * @return bool|WC_REST_Reports_Controller Report controller instance or boolean false on error.
159 */
160 protected function map_report_controller() {
161 /**
162 * Used to add custom report controllers.
163 *
164 * @since x.x.x
165 *
166 * @params array $controller_map A report type to report controller class map.
167 *
168 * @returns array Report type to report controller class map.
169 */
170 $controller_map = apply_filters(
171 'woocommerce_export_report_controller_map',
172 array(
173 'products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
174 'variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
175 'orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
176 'categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
177 'taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
178 'coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
179 'stock' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
180 'downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
181 'customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
182 'revenue' => 'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
183 )
184 );
185
186 if ( isset( $controller_map[ $this->report_type ] ) ) {
187 // Load the controllers if accessing outside the REST API.
188 return new $controller_map[ $this->report_type ]();
189 }
190
191 // Should this do something else?
192 return false;
193 }
194
195 /**
196 * Get the report columns from the controller.
197 *
198 * @return array Array of report column names.
199 */
200 protected function get_report_columns() {
201 // Default to the report's defined export columns.
202 if ( $this->controller instanceof ExportableInterface ) {
203 return $this->controller->get_export_columns();
204 }
205
206 // Fallback to generating columns from the report schema.
207 $report_columns = array();
208 $report_schema = $this->controller->get_item_schema();
209
210 if ( isset( $report_schema['properties'] ) ) {
211 foreach ( $report_schema['properties'] as $column_name => $column_info ) {
212 // Expand extended info columns into export.
213 if ( 'extended_info' === $column_name ) {
214 // Remove columns with questionable CSV values, like markup.
215 $extended_info = array_diff( array_keys( $column_info ), array( 'image' ) );
216 $report_columns = array_merge( $report_columns, $extended_info );
217 } else {
218 $report_columns[] = $column_name;
219 }
220 }
221 }
222
223 return $report_columns;
224 }
225
226 /**
227 * Get total % complete.
228 *
229 * Forces an int from parent::get_percent_complete(), which can return a float.
230 *
231 * @return int Percent complete.
232 */
233 public function get_percent_complete() {
234 return intval( parent::get_percent_complete() );
235 }
236
237 /**
238 * Get total number of rows in export.
239 *
240 * @return int Number of rows to export.
241 */
242 public function get_total_rows() {
243 return $this->total_rows;
244 }
245
246 /**
247 * Prepare data for export.
248 */
249 public function prepare_data_to_export() {
250 /**
251 * Used to add/overwrite report data endpoint.
252 *
253 * @since x.x.x
254 *
255 * @param string $endpoint The report's data endpoint.
256 * @param string $type The report's type.
257 *
258 * @returns string The report's endpoint.
259 */
260 $report_endpoint = apply_filters(
261 'woocommerce_export_report_data_endpoint',
262 "/wc-analytics/reports/{$this->report_type}",
263 $this->report_type
264 );
265
266 $request = new \WP_REST_Request( 'GET', $report_endpoint );
267 $params = $this->controller->get_collection_params();
268 $defaults = array();
269
270 foreach ( $params as $arg => $options ) {
271 if ( isset( $options['default'] ) ) {
272 $defaults[ $arg ] = $options['default'];
273 }
274 }
275
276 $request->set_attributes( array( 'args' => $params ) );
277 $request->set_default_params( $defaults );
278 $request->set_query_params( $this->report_args );
279 $request->sanitize_params();
280
281 // Does the controller have an export-specific item retrieval method?
282 // @todo - Potentially revisit. This is only for /revenue/stats/.
283 if ( is_callable( array( $this->controller, 'get_export_items' ) ) ) {
284 $response = $this->controller->get_export_items( $request );
285 } else {
286 $response = $this->controller->get_items( $request );
287 }
288
289 // Use WP_REST_Server::response_to_data() to embed links in data.
290 add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
291 $rest_server = rest_get_server();
292 $report_data = $rest_server->response_to_data( $response, true );
293 remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
294
295 $report_meta = $response->get_headers();
296 $this->total_rows = $report_meta['X-WP-Total'];
297 $this->row_data = array_map( array( $this, 'generate_row_data' ), $report_data );
298 }
299
300 /**
301 * Generate row data from a raw report item.
302 *
303 * @param object $item Report item data.
304 * @return array CSV row data.
305 */
306 protected function get_raw_row_data( $item ) {
307 $columns = $this->get_column_names();
308 $row = array();
309
310 // Expand extended info.
311 if ( isset( $item['extended_info'] ) ) {
312 // Pull extended info property from report item object.
313 $extended_info = (array) $item['extended_info'];
314 unset( $item['extended_info'] );
315
316 // Merge extended info columns into report item object.
317 $item = array_merge( $item, $extended_info );
318 }
319
320 foreach ( $columns as $column_id => $column_name ) {
321 $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
322
323 if ( has_filter( "woocommerce_export_{$this->export_type}_column_{$column_name}" ) ) {
324 // Filter for 3rd parties.
325 $value = apply_filters( "woocommerce_export_{$this->export_type}_column_{$column_name}", '', $item );
326
327 } elseif ( is_callable( array( $this, "get_column_value_{$column_name}" ) ) ) {
328 // Handle special columns which don't map 1:1 to item data.
329 $value = $this->{"get_column_value_{$column_name}"}( $item, $this->export_type );
330
331 } elseif ( ! is_scalar( $value ) ) {
332 // Ensure that the value is somewhat readable in CSV.
333 $value = wp_json_encode( $value );
334 }
335
336 $row[ $column_id ] = $value;
337 }
338
339 return $row;
340 }
341
342 /**
343 * Get the export row for a given report item.
344 *
345 * @param object $item Report item data.
346 * @return array CSV row data.
347 */
348 protected function generate_row_data( $item ) {
349 // Default to the report's export method.
350 if ( $this->controller instanceof ExportableInterface ) {
351 $row = $this->controller->prepare_item_for_export( $item );
352 } else {
353 // Fallback to raw report data.
354 $row = $this->get_raw_row_data( $item );
355 }
356
357 return apply_filters( "woocommerce_export_{$this->export_type}_row_data", $row, $item );
358 }
359 }
360