# code-snippets/3.2.1/php/export/class-export-attachment.php

Code Snippets, version 3.2.1. 54 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/3.2.1/code/php/export/class-export-attachment.php
- Raw: https://pluginprobe.com/plugins/code-snippets/3.2.1/raw/php/export/class-export-attachment.php
- Modified: 2022-10-05T07:00:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/3.2.1/code/php/export/class-export-attachment.php#L10-L20`.

```php
<?php

namespace Code_Snippets;

/**
 * Handles exporting snippets from the site to a downloadable file over HTTP.
 *
 * @package Code_Snippets
 */
class Export_Attachment extends Export {

	/**
	 * Set up the current page to act like a downloadable file instead of being shown in the browser
	 *
	 * @param string $format    File format. Used for file extension.
	 * @param string $mime_type File MIME type. Used for Content-Type header.
	 */
	private function do_headers( $format, $mime_type = 'text/plain' ) {
		header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $this->build_filename( $format ) ) );
		header( sprintf( 'Content-Type: %s; charset=%s', sanitize_mime_type( $mime_type ), get_bloginfo( 'charset' ) ) );
	}

	/**
	 * Export snippets in JSON format as a downloadable file.
	 */
	public function download_snippets_json() {
		$this->do_headers( 'json', 'application/json' );
		// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
		echo $this->export_snippets_json();
		exit;
	}

	/**
	 * Export snippets in their code file format.
	 */
	public function download_snippets_code() {
		$mime_types = [
			'php' => 'text/php',
			'css' => 'text/css',
			'js'  => 'text/javascript',
		];

		$type = isset( $mime_types[ $this->snippets_list[0]->type ] ) ? $this->snippets_list[0]->type : 'php';
		$this->do_headers( $type, $mime_types[ $type ] );

		// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
		echo ( 'php' === $type || 'html' === $type ) ?
			$this->export_snippets_php() :
			$this->export_snippets_code( $type );

		exit;
	}
}

```
