Attendees::maybe_generate_csv()

Checks if the user requested a CSV export from the attendees list.

Contents

If so, generates the download and finishes the execution.


Source

File: src/Tickets/Commerce/Reports/Attendees.php

	public function maybe_generate_csv() {
		if ( empty( $_GET['attendees_csv'] ) || empty( $_GET['attendees_csv_nonce'] ) || empty( $_GET['event_id'] ) ) {
			return;
		}

		$event_id = absint( $_GET['event_id'] );

		// Verify event ID is a valid integer and the nonce is accepted.
		if ( empty( $event_id ) || ! wp_verify_nonce( $_GET['attendees_csv_nonce'], 'attendees_csv_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			return;
		}

		$event = get_post( $event_id );

		// Verify event exists and current user has access to it.
		if (
			! $event instanceof \WP_Post
			|| ! current_user_can( 'edit_posts', $event_id )
		) {
			return;
		}

		// Generate filtered list of attendees.
		$items = $this->generate_filtered_list( $event_id );

		// Sanitize items for CSV usage.
		$items = $this->sanitize_csv_rows( $items );

		/**
		 * Allow for filtering and modifying the list of attendees that will be exported via CSV for a given event.
		 *
		 * @since 5.2.0
		 *
		 * @param array $items    The array of attendees that will be exported in this CSV file.
		 * @param int   $event_id The ID of the event these attendees are associated with.
		 */
		$items = apply_filters( 'tribe_events_tickets_attendees_csv_items', $items, $event_id );

		if ( ! empty( $items ) ) {
			$charset  = get_option( 'blog_charset' );
			$filename = sanitize_file_name( $event->post_title . '-' . __( 'attendees', 'event-tickets' ) );

			// Output headers so that the file is downloaded rather than displayed.
			header( "Content-Type: text/csv; charset=$charset" );
			header( "Content-Disposition: attachment; filename=$filename.csv" );

			// Create the file pointer connected to the output stream.
			$output = fopen( 'php://output', 'w' );

			/**
			 * Allow filtering the field delimiter used in the CSV export file.
			 *
			 * @since 5.1.3
			 *
			 * @param string $delimiter The field delimiter used in the CSV export file.
			 */
			$delimiter = apply_filters( 'tribe_tickets_attendees_csv_export_delimiter', ',' );

			// Output the lines into the file.
			foreach ( $items as $item ) {
				fputcsv( $output, $item, $delimiter ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fputcsv
			}

			fclose( $output );
			exit;
		}
	}

Top ↑

Changelog

Changelog
Version Description
5.2.0 Introduced.