set_limit( 10 ); $this->set_report_type( $type ); $this->set_report_args( $args ); $this->set_column_names( $this->get_report_columns() ); } /** * Setter for report type. * * @param string $type The report type. E.g. customers. */ public function set_report_type( $type ) { $this->report_type = $type; $this->export_type = "admin_{$type}_report"; $this->filename = "wc-{$type}-report-export.csv"; $this->controller = $this->map_report_controller(); } /** * Setter for report args. * * @param array $args The report args. */ public function set_report_args( $args ) { // Use our own internal limit and include all extended info. $report_args = array_merge( $args, array( 'per_page' => $this->get_limit(), 'extended_info' => true, ) ); // Should this happen externally? if ( isset( $report_args['page'] ) ) { $this->set_page( $report_args['page'] ); } $this->report_args = $report_args; } /** * Get a REST controller instance for the report type. * * @return bool|WC_REST_Reports_Controller Report controller instance or boolean false on error. */ protected function map_report_controller() { $controller_map = array( 'products' => 'WC_Admin_REST_Reports_Products_Controller', 'variations' => 'WC_Admin_REST_Reports_Variations_Controller', 'orders' => 'WC_Admin_REST_Reports_Orders_Controller', 'categories' => 'WC_Admin_REST_Reports_Categories_Controller', 'taxes' => 'WC_Admin_REST_Reports_Taxes_Controller', 'coupons' => 'WC_Admin_REST_Reports_Coupons_Controller', 'stock' => 'WC_Admin_REST_Reports_Stock_Controller', 'downloads' => 'WC_Admin_REST_Reports_Downloads_Controller', 'customers' => 'WC_Admin_REST_Reports_Customers_Controller', ); if ( isset( $controller_map[ $this->report_type ] ) ) { return new $controller_map[ $this->report_type ](); } // Should this do something else? return false; } /** * Get the report columns from the schema. * * @return array Array of report column names. */ protected function get_report_columns() { $report_columns = array(); $report_schema = $this->controller->get_item_schema(); if ( isset( $report_schema['properties'] ) ) { foreach ( $report_schema['properties'] as $column_name => $column_info ) { // Expand extended info columns into export. if ( 'extended_info' === $column_name ) { $report_columns = array_merge( $report_columns, array_keys( $column_info ) ); } else { $report_columns[] = $column_name; } } } return $report_columns; } /** * Get total % complete. * * Forces an int from parent::get_percent_complete(), which can return a float. * * @return int Percent complete. */ public function get_percent_complete() { return intval( parent::get_percent_complete() ); } /** * Prepare data for export. */ public function prepare_data_to_export() { $request = new WP_REST_Request( 'GET', "/wc/v4/reports/{$this->report_type}" ); $request->set_query_params( $this->report_args ); $response = $this->controller->get_items( $request ); $report_meta = $response->get_headers(); $report_data = $response->get_data(); $this->total_rows = $report_meta['X-WP-Total']; $this->row_data = array_map( array( $this, 'generate_row_data' ), $report_data ); } /** * Take a report item and generate row data from it for export. * * @param object $item Report item data. * @return array CSV row data. */ protected function generate_row_data( $item ) { $columns = $this->get_column_names(); $row = array(); // Expand extended info. if ( isset( $item['extended_info'] ) ) { $extended_info = $item['extended_info']; unset( $item['extended_info'] ); $item = array_merge( $item, $extended_info ); } foreach ( $columns as $column_id => $column_name ) { $value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null; if ( has_filter( "woocommerce_export_{$this->export_type}_column_{$column_name}" ) ) { // Filter for 3rd parties. $value = apply_filters( "woocommerce_export_{$this->export_type}_column_{$column_name}", '', $item, $column_name ); } elseif ( is_callable( array( $this, "get_column_value_{$column_name}" ) ) ) { // Handle special columns which don't map 1:1 to item data. $value = $this->{"get_column_value_{$column_name}"}( $item ); } $row[ $column_id ] = $value; } return apply_filters( "woocommerce_export_{$this->export_type}_row_data", $row, $item ); } }