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 |