PluginProbe
Code Snippets / 3.1.0
Code Snippets v3.1.0
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 / class-export.php

class-export.php in Code Snippets 3.1.0, at php/class-export.php

157 lines 4.1 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 in various downloadable formats
7 *
8 * @package Code_Snippets
9 * @since 3.0.0
10 */
11 class Export {
12
13 /**
14 * Array of snippet data fetched from the database
15 *
16 * @var array
17 */
18 private $snippets_list;
19
20 /**
21 * Class constructor
22 *
23 * @param array|int $ids List of snippet IDs to export.
24 * @param string $table_name Name of the database table to fetch snippets from.
25 */
26 public function __construct( $ids, $table_name = '' ) {
27 $this->fetch_snippets( $ids, $table_name );
28 }
29
30 /**
31 * Fetch the selected snippets from the database
32 *
33 * @param array|int $ids List of snippet IDs to export.
34 * @param string $table_name Name of database table to fetch snippets from.
35 */
36 private function fetch_snippets( $ids, $table_name ) {
37
38 if ( '' === $table_name ) {
39 $table_name = code_snippets()->db->get_table_name();
40 }
41
42 if ( ! is_array( $ids ) ) {
43 $ids = array( $ids );
44 }
45
46 $this->snippets_list = count( $ids ) ? get_snippets( $ids, $table_name ) : array();
47 }
48
49 /**
50 * Set up the current page to act like a downloadable file instead of being shown in the browser
51 *
52 * @param string $format File format. Used for file extension.
53 * @param string $mime_type File MIME type. Used for Content-Type header.
54 */
55 private function do_headers( $format, $mime_type = 'text/plain' ) {
56
57 /* Build the export filename */
58 if ( 1 === count( $this->snippets_list ) ) {
59 /* If there is only snippet to export, use its name instead of the site name */
60 $first_snippet = new Snippet( $this->snippets_list[0] );
61 $title = strtolower( $first_snippet->name );
62 } else {
63 /* Otherwise, use the site name as set in Settings > General */
64 $title = strtolower( get_bloginfo( 'name' ) );
65 }
66
67 $filename = "$title.code-snippets.$format";
68 $filename = apply_filters( 'code_snippets/export/filename', $filename, $title );
69
70 /* Set HTTP headers */
71 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) );
72 header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) );
73 }
74
75 /**
76 * Export snippets in JSON format
77 */
78 public function export_snippets() {
79 $snippets = array();
80
81 foreach ( $this->snippets_list as $snippet ) {
82 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
83 $final_snippet = array();
84
85 foreach ( $fields as $field ) {
86 if ( ! empty( $snippet->$field ) ) {
87 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
88 }
89 }
90
91 if ( $final_snippet ) {
92 $snippets[] = $final_snippet;
93 }
94 }
95
96 $this->do_headers( 'json', 'application/json' );
97
98 $data = array(
99 'generator' => 'Code Snippets v' . code_snippets()->version,
100 'date_created' => gmdate( 'Y-m-d H:i' ),
101 'snippets' => $snippets,
102 );
103
104 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
105 exit;
106 }
107
108 /**
109 * Export snippets to a downloadable file.
110 *
111 * The Pro version of this function can download snippets to .css and .js files.
112 */
113 public function download_snippets() {
114 $this->download_php_snippets();
115 }
116
117 /**
118 * Generate a downloadable PHP file from a list of snippets
119 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
120 */
121 public function download_php_snippets() {
122 $this->do_headers( 'php', 'text/php' );
123 $last_type = '';
124
125 /* Loop through the snippets */
126 foreach ( $this->snippets_list as $snippet ) {
127 $snippet = new Snippet( $snippet );
128
129 if ( 'php' !== $snippet->type && 'html' !== $snippet->type ) {
130 continue;
131 }
132
133 if ( 'html' === $last_type ) {
134 echo "\n?>\n";
135 }
136
137 if ( ! $last_type || 'html' === $last_type ) {
138 echo "<?php\n";
139 }
140
141 echo "\n/**\n * $snippet->display_name\n";
142
143 if ( ! empty( $snippet->desc ) ) {
144 /* Convert description to PhpDoc */
145 $desc = str_replace( "\n", "\n * ", $snippet->desc );
146 echo " *\n * ", wp_strip_all_tags( $desc ), "\n";
147 }
148
149 printf( " */\n\n%s\n%s\n", 'html' === $snippet->type ? '?>' : '', trim( $snippet->code ) );
150
151 $last_type = $snippet->type;
152 }
153
154 exit;
155 }
156 }
157