PluginProbe
Code Snippets / 3.0.0
Code Snippets v3.0.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.0.0, at php/class-export.php

174 lines 4.4 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 * @phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
37 */
38 private function fetch_snippets( $ids, $table_name ) {
39 global $wpdb;
40
41 /* Fetch the snippets from the database */
42 if ( '' === $table_name ) {
43 $table_name = code_snippets()->db->get_table_name();
44 }
45
46 if ( ! is_array( $ids ) ) {
47 $ids = array( $ids );
48 }
49
50 if ( count( $ids ) ) {
51 $sql = sprintf(
52 'SELECT * FROM %s WHERE id IN (%s)',
53 $table_name,
54 implode( ',', array_fill( 0, count( $ids ), '%d' ) )
55 );
56
57 $this->snippets_list = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
58
59 } else {
60 $this->snippets_list = array();
61 }
62 }
63
64 /**
65 * Set up the current page to act like a downloadable file instead of being shown in the browser
66 *
67 * @param string $format File format. Used for file extension.
68 * @param string $mime_type File MIME type. Used for Content-Type header.
69 */
70 private function do_headers( $format, $mime_type = 'text/plain' ) {
71
72 /* Build the export filename */
73 if ( 1 === count( $this->snippets_list ) ) {
74 /* If there is only snippet to export, use its name instead of the site name */
75 $first_snippet = new Snippet( $this->snippets_list[0] );
76 $title = strtolower( $first_snippet->name );
77 } else {
78 /* Otherwise, use the site name as set in Settings > General */
79 $title = strtolower( get_bloginfo( 'name' ) );
80 }
81
82 $filename = "$title.code-snippets.$format";
83 $filename = apply_filters( 'code_snippets/export/filename', $filename, $title );
84
85 /* Set HTTP headers */
86 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) );
87 header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) );
88 }
89
90 /**
91 * Export snippets in JSON format
92 */
93 public function export_snippets() {
94 $snippets = array();
95
96 foreach ( $this->snippets_list as $snippet ) {
97 $snippet = new Snippet( $snippet );
98
99 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
100 $final_snippet = array();
101
102 foreach ( $fields as $field ) {
103 if ( ! empty( $snippet->$field ) ) {
104 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
105 }
106 }
107
108 if ( $final_snippet ) {
109 $snippets[] = $final_snippet;
110 }
111 }
112
113 $this->do_headers( 'json', 'application/json' );
114
115 $data = array(
116 'generator' => 'Code Snippets v' . code_snippets()->version,
117 'date_created' => gmdate( 'Y-m-d H:i' ),
118 'snippets' => $snippets,
119 );
120
121 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
122 exit;
123 }
124
125 /**
126 * Export snippets to a downloadable file.
127 *
128 * The Pro version of this function can download snippets to .css and .js files.
129 */
130 public function download_snippets() {
131 $this->download_php_snippets();
132 }
133
134 /**
135 * Generate a downloadable PHP file from a list of snippets
136 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
137 */
138 public function download_php_snippets() {
139 $this->do_headers( 'php', 'text/php' );
140 $last_type = '';
141
142 /* Loop through the snippets */
143 foreach ( $this->snippets_list as $snippet ) {
144 $snippet = new Snippet( $snippet );
145
146 if ( 'php' !== $snippet->type && 'html' !== $snippet->type ) {
147 continue;
148 }
149
150 if ( 'html' === $last_type ) {
151 echo "\n?>\n";
152 }
153
154 if ( ! $last_type || 'html' === $last_type ) {
155 echo "<?php\n";
156 }
157
158 echo "\n/**\n * $snippet->display_name\n";
159
160 if ( ! empty( $snippet->desc ) ) {
161 /* Convert description to PhpDoc */
162 $desc = str_replace( "\n", "\n * ", $snippet->desc );
163 echo " *\n * ", wp_strip_all_tags( $desc ), "\n";
164 }
165
166 printf( " */\n\n%s\n%s\n", 'html' === $snippet->type ? '?>' : '', trim( $snippet->code ) );
167
168 $last_type = $snippet->type;
169 }
170
171 exit;
172 }
173 }
174