PluginProbe
Code Snippets / 2.14.0
Code Snippets v2.14.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 / import-export.php

import-export.php in Code Snippets 2.14.0, at php/import-export.php

284 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file includes functions for importing and exporting snippets
5 */
6
7 /**
8 * @access private
9 *
10 * @param array $snippets
11 * @param null $multisite
12 * @param string $dup_action
13 *
14 * @return array
15 */
16 function _code_snippets_save_imported_snippets( $snippets, $multisite = null, $dup_action = 'ignore' ) {
17
18 /* Get a list of existing snippet names keyed to their IDs */
19 $existing_snippets = array();
20 if ( 'replace' === $dup_action || 'skip' === $dup_action ) {
21 $all_snippets = get_snippets( array(), $multisite );
22
23 foreach ( $all_snippets as $snippet ) {
24 if ( $snippet->name ) {
25 $existing_snippets[ $snippet->name ] = $snippet->id;
26 }
27 }
28 }
29
30 /* Save a record of the snippets which were imported */
31 $imported = array();
32
33 /* Loop through the provided snippets */
34 /** @var Code_Snippet $snippet */
35 foreach ( $snippets as $snippet ) {
36
37 /* Check if the snippet already exists */
38 if ( 'ignore' !== $dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
39
40 /* If so, either overwrite the existing ID, or skip this import */
41 if ( 'replace' === $dup_action ) {
42 $snippet->id = $existing_snippets[ $snippet->name ];
43 } elseif ( 'skip' === $dup_action ) {
44 continue;
45 }
46 }
47
48 /* Ensure that imported snippets are inactive */
49 $snippet->active = 0;
50
51 /* Save the snippet and increase the counter if successful */
52 $snippet_id = save_snippet( $snippet );
53 if ( $snippet_id ) {
54 $imported[] = $snippet_id;
55 }
56 }
57
58 return $imported;
59 }
60
61 /**f
62 * Imports snippets from a JSON file
63 *
64 * @since 2.9.7
65 *
66 * @uses save_snippet() to add the snippets to the database
67 *
68 * @param string $file The path to the file to import
69 * @param bool|null $multisite Import into network-wide table or site-wide table?
70 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
71 *
72 * @return array|bool An array of imported snippet IDs on success, false on failure
73 */
74 function import_snippets_json( $file, $multisite = null, $dup_action = 'ignore' ) {
75
76 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
77 return false;
78 }
79
80 $raw_data = file_get_contents( $file );
81 $data = json_decode( $raw_data, true );
82 $snippets = array();
83
84 /* Reformat the data into snippet objects */
85 foreach ( $data['snippets'] as $snippet ) {
86 $snippet = new Code_Snippet( $snippet );
87 $snippet->network = $multisite;
88 $snippets[] = $snippet;
89 }
90
91 $imported = _code_snippets_save_imported_snippets( $snippets, $multisite, $dup_action );
92 do_action( 'code_snippets/import/json', $file, $multisite );
93
94 return $imported;
95 }
96
97 /**
98 * Imports snippets from an XML file
99 *
100 * @since 2.0
101 *
102 * @uses save_snippet() to add the snippets to the database
103 *
104 * @param string $file The path to the file to import
105 * @param bool|null $multisite Import into network-wide table or site-wide table?
106 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
107 *
108 * @return array|bool An array of imported snippet IDs on success, false on failure
109 */
110 function import_snippets_xml( $file, $multisite = null, $dup_action = 'ignore' ) {
111
112 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
113 return false;
114 }
115
116 /** @phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
117 $dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
118 $dom->load( $file );
119
120 $snippets_xml = $dom->getElementsByTagName( 'snippet' );
121 $fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
122
123 $snippets = array();
124
125 /* Loop through all snippets */
126
127 /** @var DOMElement $snippet_xml */
128 foreach ( $snippets_xml as $snippet_xml ) {
129 $snippet = new Code_Snippet();
130 $snippet->network = $multisite;
131
132 /* Build a snippet object by looping through the field names */
133 foreach ( $fields as $field_name ) {
134
135 /* Fetch the field element from the document */
136 $field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
137
138 /* If the field element exists, add it to the snippet object */
139 if ( isset( $field->nodeValue ) ) {
140 $snippet->set_field( $field_name, $field->nodeValue );
141 }
142 }
143
144 /* Get scope from attribute */
145 $scope = $snippet_xml->getAttribute( 'scope' );
146 if ( ! empty( $scope ) ) {
147 $snippet->scope = $scope;
148 }
149
150 $snippets[] = $snippet;
151 }
152
153 $imported = _code_snippets_save_imported_snippets( $snippets, $dup_action, $multisite );
154 do_action( 'code_snippets/import/xml', $file, $multisite );
155
156 /** @phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
157
158 return $imported;
159 }
160
161 /**
162 * Set up the current page to act like a downloadable file instead of being shown in the browser
163 *
164 * @param string $format
165 * @param array $ids
166 * @param string $table_name
167 * @param string $mime_type
168 *
169 * @return array
170 */
171 function code_snippets_prepare_export( $format, $ids, $table_name = '', $mime_type = '' ) {
172 global $wpdb;
173
174 /* Fetch the snippets from the database */
175 if ( '' === $table_name ) {
176 $table_name = code_snippets()->db->get_table_name();
177 }
178
179 if ( count( $ids ) ) {
180
181 $sql = sprintf(
182 'SELECT * FROM %s WHERE id IN (%s)', $table_name,
183 implode( ',', array_fill( 0, count( $ids ), '%d' ) )
184 );
185
186 $snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
187
188 } else {
189 $snippets = array();
190 }
191
192 /* Build the export filename */
193 if ( 1 === count( $ids ) ) {
194 /* If there is only snippet to export, use its name instead of the site name */
195 $first_snippet = new Code_Snippet( $snippets[0] );
196 $title = strtolower( $first_snippet->name );
197 } else {
198 /* Otherwise, use the site name as set in Settings > General */
199 $title = strtolower( get_bloginfo( 'name' ) );
200 }
201
202 $filename = "{$title}.code-snippets.{$format}";
203 $filename = apply_filters( 'code_snippets/export/filename', $filename, $title );
204
205 /* Set HTTP headers */
206 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) );
207
208 if ( '' !== $mime_type ) {
209 header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) );
210 }
211
212 /* Return the retrieved snippets to build the rest of the export file */
213
214 return $snippets;
215 }
216
217 /**
218 * Export snippets to a downloadable PHP file
219 *
220 * @param $ids
221 * @param $table_name
222 */
223 function download_snippets( $ids, $table_name = '' ) {
224 $snippets = code_snippets_prepare_export( 'php', $ids, $table_name );
225
226 echo "<?php\n";
227
228 /* Loop through the snippets */
229 foreach ( $snippets as $snippet ) {
230 $snippet = new Code_Snippet( $snippet );
231
232 echo "\n/**\n * {$snippet->name}\n";
233
234 if ( ! empty( $snippet->desc ) ) {
235
236 /* Convert description to PhpDoc */
237 $desc = strip_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
238
239 echo " *\n * $desc\n";
240 }
241
242 echo " */\n{$snippet->code}\n";
243 }
244
245 exit;
246 }
247
248 /**
249 * Export snippets in JSON format
250 *
251 * @param array $ids list of snippet IDs to export
252 * @param string $table_name name of the database table to fetch snippets from
253 */
254 function export_snippets( $ids, $table_name = '' ) {
255 $raw_snippets = code_snippets_prepare_export( 'json', $ids, $table_name, 'application/json' );
256 $final_snippets = array();
257
258 foreach ( $raw_snippets as $snippet ) {
259 $snippet = new Code_Snippet( $snippet );
260
261 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
262 $final_snippet = array();
263
264 foreach ( $fields as $field ) {
265 if ( ! empty( $snippet->$field ) ) {
266 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
267 }
268 }
269
270 if ( $final_snippet ) {
271 $final_snippets[] = $final_snippet;
272 }
273 }
274
275 $data = array(
276 'generator' => 'Code Snippets v' . code_snippets()->version,
277 'date_created' => gmdate( 'Y-m-d H:i' ),
278 'snippets' => $final_snippets,
279 );
280
281 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
282 exit;
283 }
284