PluginProbe
Code Snippets / 2.12.0
Code Snippets v2.12.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.12.0, at php/import-export.php

275 lines 7.4 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 *
10 * @access private
11 *
12 * @param $snippets
13 * @param null $multisite
14 * @param string $dup_action
15 *
16 * @return array
17 */
18 function _code_snippets_save_imported_snippets( $snippets, $multisite = null, $dup_action = 'ignore' ) {
19
20 /* Get a list of existing snippet names keyed to their IDs */
21 $existing_snippets = array();
22 if ( 'replace' == $dup_action || 'skip' === $dup_action ) {
23 $all_snippets = get_snippets( array(), $multisite );
24
25 foreach ( $all_snippets as $snippet ) {
26 if ( $snippet->name ) {
27 $existing_snippets[ $snippet->name ] = $snippet->id;
28 }
29 }
30 }
31
32 /* Save a record of the snippets which were imported */
33 $imported = array();
34
35 /* Loop through the provided snippets */
36 foreach ( $snippets as $snippet ) {
37
38 /* Check if the snippet already exists */
39 if ( 'ignore' !== $dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
40
41 /* If so, either overwrite the existing ID, or skip this import */
42 if ( 'replace' === $dup_action ) {
43 $snippet->id = $existing_snippets[ $snippet->name ];
44 } elseif ( 'skip' === $dup_action ) {
45 continue;
46 }
47 }
48
49 /* Save the snippet and increase the counter if successful */
50 if ( $snippet_id = save_snippet( $snippet ) ) {
51 $imported[] = $snippet_id;
52 }
53 }
54
55 return $imported;
56 }
57
58 /**
59 * Imports snippets from a JSON file
60 *
61 * @since 2.9.7
62 *
63 * @uses save_snippet() to add the snippets to the database
64 *
65 * @param string $file The path to the file to import
66 * @param bool|null $multisite Import into network-wide table or site-wide table?
67 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
68 *
69 * @return array|bool An array of imported snippet IDs on success, false on failure
70 */
71 function import_snippets_json( $file, $multisite = null, $dup_action = 'ignore' ) {
72
73 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
74 return false;
75 }
76
77 $raw_data = file_get_contents( $file );
78 $data = json_decode( $raw_data, true );
79 $snippets = array();
80
81 /* Reformat the data into snippet objects */
82 foreach ( $data['snippets'] as $snippet ) {
83 $snippet = new Code_Snippet( $snippet );
84 $snippet->network = $multisite;
85 $snippets[] = $snippet;
86 }
87
88 $imported = _code_snippets_save_imported_snippets( $snippets, $multisite, $dup_action );
89 do_action( 'code_snippets/import/json', $file, $multisite );
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 return $imported;
151 }
152
153 /**
154 * Set up the current page to act like a downloadable file instead of being shown in the browser
155 *
156 * @param string $format
157 * @param array $ids
158 * @param string $table_name
159 * @param string $mime_type
160 *
161 * @return array
162 */
163 function code_snippets_prepare_export( $format, $ids, $table_name = '', $mime_type = '' ) {
164 global $wpdb;
165
166 /* Fetch the snippets from the database */
167 if ( '' === $table_name ) {
168 $table_name = code_snippets()->db->get_table_name();
169 }
170
171 if ( count( $ids ) ) {
172
173 $sql = sprintf(
174 'SELECT * FROM %s WHERE id IN (%s)', $table_name,
175 implode( ',', array_fill( 0, count( $ids ), '%d' ) )
176 );
177
178 $snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
179
180 } else {
181 $snippets = array();
182 }
183
184 /* Build the export filename */
185 if ( 1 == count( $ids ) ) {
186 /* If there is only snippet to export, use its name instead of the site name */
187 $first_snippet = new Code_Snippet( $snippets[0] );
188 $title = strtolower( $first_snippet->name );
189 } else {
190 /* Otherwise, use the site name as set in Settings > General */
191 $title = strtolower( get_bloginfo( 'name' ) );
192 }
193
194 $filename = "{$title}.code-snippets.{$format}";
195 $filename = apply_filters( 'code_snippets/export/filename', $filename, $title );
196
197 /* Set HTTP headers */
198 header( 'Content-Disposition: attachment; filename=' . sanitize_file_name( $filename ) );
199
200 if ( '' !== $mime_type ) {
201 header( "Content-Type: $mime_type; charset=" . get_bloginfo( 'charset' ) );
202 }
203
204 /* Return the retrieved snippets to build the rest of the export file */
205 return $snippets;
206 }
207
208 /**
209 * Export snippets to a downloadable PHP file
210 *
211 * @param $ids
212 * @param $table_name
213 */
214 function download_snippets( $ids, $table_name = '' ) {
215 $snippets = code_snippets_prepare_export( 'php', $ids, $table_name );
216
217 echo "<?php\n";
218
219 /* Loop through the snippets */
220 foreach ( $snippets as $snippet ) {
221 $snippet = new Code_Snippet( $snippet );
222
223 echo "\n/**\n * {$snippet->name}\n";
224
225 if ( ! empty( $snippet->desc ) ) {
226
227 /* Convert description to PhpDoc */
228 $desc = strip_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
229
230 echo " *\n * $desc\n";
231 }
232
233 echo " */\n{$snippet->code}\n";
234 }
235
236 exit;
237 }
238
239 /**
240 * Export snippets in JSON format
241 *
242 * @param array $ids list of snippet IDs to export
243 * @param string $table_name name of the database table to fetch snippets from
244 */
245 function export_snippets( $ids, $table_name = '' ) {
246 $raw_snippets = code_snippets_prepare_export( 'json', $ids, $table_name, 'application/json' );
247 $final_snippets = array();
248
249 foreach ( $raw_snippets as $snippet ) {
250 $snippet = new Code_Snippet( $snippet );
251
252 $fields = array( 'name', 'desc', 'tags', 'scope', 'code' );
253 $final_snippet = array();
254
255 foreach ( $fields as $field ) {
256 if ( ! empty( $snippet->$field ) ) {
257 $final_snippet[ $field ] = $snippet->$field;
258 }
259 }
260
261 if ( $final_snippet ) {
262 $final_snippets[] = $final_snippet;
263 }
264 }
265
266 $data = array(
267 'generator' => 'Code Snippets v' . CODE_SNIPPETS_VERSION,
268 'date_created' => date( 'Y-m-d H:i' ),
269 'snippets' => $final_snippets,
270 );
271
272 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
273 exit;
274 }
275