Automattic\WooCommerce\Admin

ReportCSVExporter::prepare_data_to_exportpublicWC 1.0

Prepare data for export.

Метод класса: ReportCSVExporter{}

Хуки из метода

Возвращает

null. Ничего (null).

Использование

$ReportCSVExporter = new ReportCSVExporter();
$ReportCSVExporter->prepare_data_to_export();

Код ReportCSVExporter::prepare_data_to_export() WC 11.0.1

public function prepare_data_to_export() {
	/**
	 * Used to add/overwrite report data endpoint.
	 *
	 * @since x.x.x
	 *
	 * @param string $endpoint The report's data endpoint.
	 * @param string $type     The report's type.
	 *
	 * @returns string The report's endpoint.
	 */
	$report_endpoint = apply_filters(
		'woocommerce_export_report_data_endpoint',
		"/wc-analytics/reports/{$this->report_type}",
		$this->report_type
	);

	$request  = new \WP_REST_Request( 'GET', $report_endpoint );
	$params   = $this->controller->get_collection_params();
	$defaults = array();

	foreach ( $params as $arg => $options ) {
		if ( isset( $options['default'] ) ) {
			$defaults[ $arg ] = $options['default'];
		}
	}

	$request->set_attributes( array( 'args' => $params ) );
	$request->set_default_params( $defaults );
	$request->set_query_params( $this->report_args );
	$request->sanitize_params();

	// Enforce the report's own schema on every export path, including direct
	// ReportExporter::queue_report_export() callers that bypass the REST
	// route's validation. Never pass unvalidated args (e.g. orderby) to the
	// query.
	$validity = $request->has_valid_params();
	if ( is_wp_error( $validity ) ) {
		wc_get_logger()->warning(
			sprintf( 'Skipping %s report export: %s', $this->report_type, $validity->get_error_message() ),
			array( 'source' => 'report-csv-exporter' )
		);
		$this->total_rows = 0;
		$this->row_data   = array();
		return;
	}

	// Apply our internal export args after validation. The batch size and
	// extended info are ours, not user input, so they must not be validated
	// against the user-facing schema (e.g. per_page's maximum). The batch
	// size in particular is filterable via
	// woocommerce_{$export_type}_export_batch_limit and can legitimately
	// exceed the schema's per_page maximum.
	$request->set_param( 'per_page', $this->get_limit() );
	$request->set_param( 'extended_info', true );

	// Does the controller have an export-specific item retrieval method?
	// @todo - Potentially revisit. This is only for /revenue/stats/.
	if ( is_callable( array( $this->controller, 'get_export_items' ) ) ) {
		$response = $this->controller->get_export_items( $request );
	} else {
		$response = $this->controller->get_items( $request );
	}

	// Use WP_REST_Server::response_to_data() to embed links in data.
	add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
	$rest_server = rest_get_server();
	$report_data = $rest_server->response_to_data( $response, true );
	remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );

	$report_meta      = $response->get_headers();
	$this->total_rows = $report_meta['X-WP-Total'];
	$this->row_data   = array_map( array( $this, 'generate_row_data' ), $report_data );
}