PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / export / class-export-attachment.php

class-export-attachment.php in Code Snippets 3.6.6, at php/export/class-export-attachment.php

57 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 /**
6 * Handles exporting snippets from the site to a downloadable file over HTTP.
7 *
8 * @package Code_Snippets
9 */
10 class Export_Attachment extends Export {
11
12 /**
13 * Set up the current page to act like a downloadable file instead of being shown in the browser
14 *
15 * @param string $format File format. Used for file extension.
16 * @param string $mime_type File MIME type. Used for Content-Type header.
17 */
18 private function do_headers( string $format, string $mime_type = 'text/plain' ) {
19 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $this->build_filename( $format ) ) );
20 header( sprintf( 'Content-Type: %s; charset=%s', sanitize_mime_type( $mime_type ), get_bloginfo( 'charset' ) ) );
21 }
22
23 /**
24 * Export snippets in JSON format as a downloadable file.
25 */
26 public function download_snippets_json() {
27 $this->do_headers( 'json', 'application/json' );
28 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
29 echo wp_json_encode(
30 $this->create_export_object(),
31 apply_filters( 'code_snippets/export/json_encode_options', 0 )
32 );
33 exit;
34 }
35
36 /**
37 * Export snippets in their code file format.
38 */
39 public function download_snippets_code() {
40 $mime_types = [
41 'php' => 'text/php',
42 'css' => 'text/css',
43 'js' => 'text/javascript',
44 ];
45
46 $type = isset( $mime_types[ $this->snippets_list[0]->type ] ) ? $this->snippets_list[0]->type : 'php';
47 $this->do_headers( $type, $mime_types[ $type ] );
48
49 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
50 echo ( 'php' === $type || 'html' === $type ) ?
51 $this->export_snippets_php() :
52 $this->export_snippets_code( $type );
53
54 exit;
55 }
56 }
57