PluginProbe
Code Snippets / 2.12.1
Code Snippets v2.12.1
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.12.1, at php/import-export.php

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