Orders::maybe_generate_csv()
Maybe generate a CSV file for orders.
This method checks if the necessary GET parameters are set to trigger the CSV generation. If conditions are met, it generates a CSV file with order data and outputs it for download.
Return
(void)
Source
File: src/Tickets/Commerce/Admin_Tables/Orders.php
public function maybe_generate_csv(): void {
// Early bail: Check if the necessary GET parameters are not set.
if ( empty( $_GET['orders_csv'] ) || empty( $_GET['orders_csv_nonce'] ) || empty( $_GET['post_id'] ) ) {
return;
}
$event_id = absint( $_GET['post_id'] );
/**
* Filters the event ID before using it to fetch orders.
*
* @since 5.8.1
*
* @param int $event_id The event ID.
*/
$event_id = apply_filters( 'tec_tickets_filter_event_id', $event_id );
// Early bail: Verify the event ID and the nonce.
if ( empty( $event_id ) || ! wp_verify_nonce( $_GET['orders_csv_nonce'], 'orders_csv_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
return;
}
$post_id = tribe_get_request_var(
'event_id',
tribe_get_request_var(
'post_id',
0
)
);
$product_ids = explode(
',',
tribe_get_request_var(
'product_ids',
''
)
);
// Initialize arguments for fetching orders.
$arguments = [
'status' => 'any',
'events' => $post_id,
'tickets' => ! empty( $product_ids ) ? $product_ids : null,
'orderby' => tribe_get_request_var( 'orderby', '' ),
'order' => tribe_get_request_var( 'order', '' ),
];
/**
* Filters the arguments for the order report export.
*
* @since 5.8.1
*
* @param array $arguments The arguments for order retrieval.
*/
$arguments = apply_filters( 'tec_tc_order_report_export_args', $arguments );
// Fetch orders using the repository.
$orders_repository = tec_tc_orders()->by_args( $arguments );
$items = $orders_repository->all();
// Format the orders data for CSV.
$formatted_data = $this->format_for_csv( $items );
// Get the event post for filename.
$event = get_post( $post_id );
$filename = sanitize_title( $event->post_title ) . '-' . __(
'orders',
'event-tickets'
) . '.csv';
// Generate and output the CSV file.
$this->generate_csv_file( $formatted_data, $filename );
}
Changelog
| Version | Description |
|---|---|
| 5.8.1 | Introduced. |