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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 2 years ago BlockTemplates 2 years ago Composer 2 years ago DateTimeProvider 4 years ago Features 2 years ago Marketing 2 years ago Notes 2 years ago Overrides 3 years ago PluginsInstallLoggers 2 years ago PluginsProvider 4 years ago RemoteInboxNotifications 2 years ago Schedulers 4 years ago DataSourcePoller.php 3 years ago DeprecatedClassFacade.php 2 years ago FeaturePlugin.php 4 years ago Loader.php 4 years ago PageController.php 2 years ago PluginsHelper.php 2 years ago PluginsInstaller.php 4 years ago ReportCSVEmail.php 2 years ago ReportCSVExporter.php 3 years ago ReportExporter.php 3 years ago ReportsSync.php 3 years ago WCAdminHelper.php 4 years ago
ReportCSVExporter.php
333 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 // @todo - Add filter to this list.
162 $controller_map = array(
163 'products' => 'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
164 'variations' => 'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
165 'orders' => 'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
166 'categories' => 'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
167 'taxes' => 'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
168 'coupons' => 'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
169 'stock' => 'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
170 'downloads' => 'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
171 'customers' => 'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
172 'revenue' => 'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
173 );
174
175 if ( isset( $controller_map[ $this->report_type ] ) ) {
176 // Load the controllers if accessing outside the REST API.
177 return new $controller_map[ $this->report_type ]();
178 }
179
180 // Should this do something else?
181 return false;
182 }
183
184 /**
185 * Get the report columns from the controller.
186 *
187 * @return array Array of report column names.
188 */
189 protected function get_report_columns() {
190 // Default to the report's defined export columns.
191 if ( $this->controller instanceof ExportableInterface ) {
192 return $this->controller->get_export_columns();
193 }
194
195 // Fallback to generating columns from the report schema.
196 $report_columns = array();
197 $report_schema = $this->controller->get_item_schema();
198
199 if ( isset( $report_schema['properties'] ) ) {
200 foreach ( $report_schema['properties'] as $column_name => $column_info ) {
201 // Expand extended info columns into export.
202 if ( 'extended_info' === $column_name ) {
203 // Remove columns with questionable CSV values, like markup.
204 $extended_info = array_diff( array_keys( $column_info ), array( 'image' ) );
205 $report_columns = array_merge( $report_columns, $extended_info );
206 } else {
207 $report_columns[] = $column_name;
208 }
209 }
210 }
211
212 return $report_columns;
213 }
214
215 /**
216 * Get total % complete.
217 *
218 * Forces an int from parent::get_percent_complete(), which can return a float.
219 *
220 * @return int Percent complete.
221 */
222 public function get_percent_complete() {
223 return intval( parent::get_percent_complete() );
224 }
225
226 /**
227 * Get total number of rows in export.
228 *
229 * @return int Number of rows to export.
230 */
231 public function get_total_rows() {
232 return $this->total_rows;
233 }
234
235 /**
236 * Prepare data for export.
237 */
238 public function prepare_data_to_export() {
239 $request = new \WP_REST_Request( 'GET', "/wc-analytics/reports/{$this->report_type}" );
240 $params = $this->controller->get_collection_params();
241 $defaults = array();
242
243 foreach ( $params as $arg => $options ) {
244 if ( isset( $options['default'] ) ) {
245 $defaults[ $arg ] = $options['default'];
246 }
247 }
248
249 $request->set_attributes( array( 'args' => $params ) );
250 $request->set_default_params( $defaults );
251 $request->set_query_params( $this->report_args );
252 $request->sanitize_params();
253
254 // Does the controller have an export-specific item retrieval method?
255 // @todo - Potentially revisit. This is only for /revenue/stats/.
256 if ( is_callable( array( $this->controller, 'get_export_items' ) ) ) {
257 $response = $this->controller->get_export_items( $request );
258 } else {
259 $response = $this->controller->get_items( $request );
260 }
261
262 // Use WP_REST_Server::response_to_data() to embed links in data.
263 add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
264 $rest_server = rest_get_server();
265 $report_data = $rest_server->response_to_data( $response, true );
266 remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
267
268 $report_meta = $response->get_headers();
269 $this->total_rows = $report_meta['X-WP-Total'];
270 $this->row_data = array_map( array( $this, 'generate_row_data' ), $report_data );
271 }
272
273 /**
274 * Generate row data from a raw report item.
275 *
276 * @param object $item Report item data.
277 * @return array CSV row data.
278 */
279 protected function get_raw_row_data( $item ) {
280 $columns = $this->get_column_names();
281 $row = array();
282
283 // Expand extended info.
284 if ( isset( $item['extended_info'] ) ) {
285 // Pull extended info property from report item object.
286 $extended_info = (array) $item['extended_info'];
287 unset( $item['extended_info'] );
288
289 // Merge extended info columns into report item object.
290 $item = array_merge( $item, $extended_info );
291 }
292
293 foreach ( $columns as $column_id => $column_name ) {
294 $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
295
296 if ( has_filter( "woocommerce_export_{$this->export_type}_column_{$column_name}" ) ) {
297 // Filter for 3rd parties.
298 $value = apply_filters( "woocommerce_export_{$this->export_type}_column_{$column_name}", '', $item );
299
300 } elseif ( is_callable( array( $this, "get_column_value_{$column_name}" ) ) ) {
301 // Handle special columns which don't map 1:1 to item data.
302 $value = $this->{"get_column_value_{$column_name}"}( $item, $this->export_type );
303
304 } elseif ( ! is_scalar( $value ) ) {
305 // Ensure that the value is somewhat readable in CSV.
306 $value = wp_json_encode( $value );
307 }
308
309 $row[ $column_id ] = $value;
310 }
311
312 return $row;
313 }
314
315 /**
316 * Get the export row for a given report item.
317 *
318 * @param object $item Report item data.
319 * @return array CSV row data.
320 */
321 protected function generate_row_data( $item ) {
322 // Default to the report's export method.
323 if ( $this->controller instanceof ExportableInterface ) {
324 $row = $this->controller->prepare_item_for_export( $item );
325 } else {
326 // Fallback to raw report data.
327 $row = $this->get_raw_row_data( $item );
328 }
329
330 return apply_filters( "woocommerce_export_{$this->export_type}_row_data", $row, $item );
331 }
332 }
333