File_Download::download_csv( bool $should_exit = true )

Outputs the CSV file for the current event report.


Parameters

$should_exit

(bool) (Optional) Whether the downloader should exit automatically or continue.

Default value: true


Top ↑

Return

(false|void)


Top ↑

Source

File: src/Events/Custom_Tables/V1/Migration/CSV_Report/File_Download.php

	public function download_csv( $should_exit = true ) {
		// Check if we are in WP-Admin
		if ( ! $this->should_download() ) {
			return false;
		}

		// Determine which reports we want. Different logic based on the current phase.
		$site_report = Site_Report::build();
		$state       = tribe( State::class );

		if ( State::PHASE_MIGRATION_FAILURE_COMPLETE === $state->get_phase() ) {
			$filter  = [ Event_Report::META_KEY_MIGRATION_PHASE => Event_Report::META_VALUE_MIGRATION_PHASE_MIGRATION_FAILURE ];
			$reports = $site_report->get_event_reports( - 1, 9999, $filter );
		} else {
			$reports = $site_report->get_event_reports();
		}

		$delimiter = ',';
		$output    = fopen( 'php://output', 'wb' );
		$charset   = get_option( 'blog_charset' );

		header( "Content-Type: text/csv; charset=$charset;" );
		header( 'Content-Disposition: attachment; filename="migration_event_report.csv"' );
		header( "Cache-Control: no-cache, must-revalidate" );
		header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );

		fputcsv( $output, self::CSV_COLUMNS, $delimiter );
		foreach ( $reports as $report ) {
			$has_error = (bool) $report->error;
			if ( $has_error ) {
				$message = str_replace( [ "\n", "\t" ], " ", strip_tags( $report->error ) );
			} else {
				$message = $report->get_migration_strategy_text();
			}

			$item = [
				$report->source_event_post->post_title,
				get_edit_post_link( $report->source_event_post->ID, 'url' ),
				$message,
				$has_error ? "Yes" : "No"
			];

			fputcsv( $output, $item, $delimiter );
		}
		fclose( $output );
		if ( $should_exit ) {
			exit;
		}
	}

Top ↑

Changelog

Changelog
Version Description
6.0.0 Introduced.