PluginProbe
Code Snippets / 2.13.3
Code Snippets v2.13.3
4.0.0-beta.2 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 All 65 releases
code-snippets / php / import-export.php

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

276 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 * @access private
9 *
10 * @param $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 foreach ( $snippets as $snippet ) {
35
36 /* Check if the snippet already exists */
37 if ( 'ignore' !== $dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
38
39 /* If so, either overwrite the existing ID, or skip this import */
40 if ( 'replace' === $dup_action ) {
41 $snippet->id = $existing_snippets[ $snippet->name ];
42 } elseif ( 'skip' === $dup_action ) {
43 continue;
44 }
45 }
46
47 /* Save the snippet and increase the counter if successful */
48 if ( $snippet_id = save_snippet( $snippet ) ) {
49 $imported[] = $snippet_id;
50 }
51 }
52
53 return $imported;
54 }
55
56 /**
57 * Imports snippets from a JSON file
58 *
59 * @since 2.9.7
60 *
61 * @uses save_snippet() to add the snippets to the database
62 *
63 * @param string $file The path to the file to import
64 * @param bool|null $multisite Import into network-wide table or site-wide table?
65 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
66 *
67 * @return array|bool An array of imported snippet IDs on success, false on failure
68 */
69 function import_snippets_json( $file, $multisite = null, $dup_action = 'ignore' ) {
70
71 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
72 return false;
73 }
74
75 $raw_data = file_get_contents( $file );
76 $data = json_decode( $raw_data, true );
77 $snippets = array();
78
79 /* Reformat the data into snippet objects */
80 foreach ( $data['snippets'] as $snippet ) {
81 $snippet = new Code_Snippet( $snippet );
82 $snippet->network = $multisite;
83 $snippets[] = $snippet;
84 }
85
86 $imported = _code_snippets_save_imported_snippets( $snippets, $multisite, $dup_action );
87 do_action( 'code_snippets/import/json', $file, $multisite );
88
89 return $imported;
90 }
91
92 /**
93 * Imports snippets from an XML file
94 *
95 * @since 2.0
96 *
97 * @uses save_snippet() to add the snippets to the database
98 *
99 * @param string $file The path to the file to import
100 * @param bool|null $multisite Import into network-wide table or site-wide table?
101 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
102 *
103 * @return array|bool An array of imported snippet IDs on success, false on failure
104 */
105 function import_snippets_xml( $file, $multisite = null, $dup_action = 'ignore' ) {
106
107 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
108 return false;
109 }
110
111 $dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
112 $dom->load( $file );
113
114 $snippets_xml = $dom->getElementsByTagName( 'snippet' );
115 $fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
116
117 $snippets = array();
118
119 /* Loop through all snippets */
120
121 /** @var DOMElement $snippet_xml */
122 foreach ( $snippets_xml as $snippet_xml ) {
123 $snippet = new Code_Snippet();
124 $snippet->network = $multisite;
125
126 /* Build a snippet object by looping through the field names */
127 foreach ( $fields as $field_name ) {
128
129 /* Fetch the field element from the document */
130 $field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
131
132 /* If the field element exists, add it to the snippet object */
133 if ( isset( $field->nodeValue ) ) {
134 $snippet->set_field( $field_name, $field->nodeValue );
135 }
136 }
137
138 /* Get scope from attribute */
139 $scope = $snippet_xml->getAttribute( 'scope' );
140 if ( ! empty( $scope ) ) {
141 $snippet->scope = $scope;
142 }
143
144 $snippets[] = $snippet;
145 }
146
147 $imported = _code_snippets_save_imported_snippets( $snippets, $dup_action, $multisite );
148 do_action( 'code_snippets/import/xml', $file, $multisite );
149
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
206 return $snippets;
207 }
208
209 /**
210 * Export snippets to a downloadable PHP file
211 *
212 * @param $ids
213 * @param $table_name
214 */
215 function download_snippets( $ids, $table_name = '' ) {
216 $snippets = code_snippets_prepare_export( 'php', $ids, $table_name );
217
218 echo "<?php\n";
219
220 /* Loop through the snippets */
221 foreach ( $snippets as $snippet ) {
222 $snippet = new Code_Snippet( $snippet );
223
224 echo "\n/**\n * {$snippet->name}\n";
225
226 if ( ! empty( $snippet->desc ) ) {
227
228 /* Convert description to PhpDoc */
229 $desc = strip_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
230
231 echo " *\n * $desc\n";
232 }
233
234 echo " */\n{$snippet->code}\n";
235 }
236
237 exit;
238 }
239
240 /**
241 * Export snippets in JSON format
242 *
243 * @param array $ids list of snippet IDs to export
244 * @param string $table_name name of the database table to fetch snippets from
245 */
246 function export_snippets( $ids, $table_name = '' ) {
247 $raw_snippets = code_snippets_prepare_export( 'json', $ids, $table_name, 'application/json' );
248 $final_snippets = array();
249
250 foreach ( $raw_snippets as $snippet ) {
251 $snippet = new Code_Snippet( $snippet );
252
253 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
254 $final_snippet = array();
255
256 foreach ( $fields as $field ) {
257 if ( ! empty( $snippet->$field ) ) {
258 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
259 }
260 }
261
262 if ( $final_snippet ) {
263 $final_snippets[] = $final_snippet;
264 }
265 }
266
267 $data = array(
268 'generator' => 'Code Snippets v' . code_snippets()->version,
269 'date_created' => date( 'Y-m-d H:i' ),
270 'snippets' => $final_snippets,
271 );
272
273 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
274 exit;
275 }
276