PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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.6, at php/import-export.php

279 lines 7.9 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 * @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 * @since 2.9.7
70 *
71 * @uses save_snippet() to add the snippets to the database
72 *
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 /** @phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents */
81 $raw_data = file_get_contents( $file );
82 $data = json_decode( $raw_data, true );
83 $snippets = array();
84
85 /* Reformat the data into snippet objects */
86 foreach ( $data['snippets'] as $snippet ) {
87 $snippet = new Code_Snippet( $snippet );
88 $snippet->network = $multisite;
89 $snippets[] = $snippet;
90 }
91
92 $imported = _code_snippets_save_imported_snippets( $snippets, $multisite, $dup_action );
93 do_action( 'code_snippets/import/json', $file, $multisite );
94
95 return $imported;
96 }
97
98 /**
99 * Imports snippets from an XML file
100 *
101 * @param string $file The path to the file to import
102 * @param bool|null $multisite Import into network-wide table or site-wide table?
103 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
104 *
105 * @return array|bool An array of imported snippet IDs on success, false on failure
106 * @since 2.0
107 *
108 * @uses save_snippet() to add the snippets to the database
109 *
110 */
111 function import_snippets_xml( $file, $multisite = null, $dup_action = 'ignore' ) {
112
113 if ( ! file_exists( $file ) || ! is_file( $file ) ) {
114 return false;
115 }
116
117 /** @phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
118 $dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
119 $dom->load( $file );
120
121 $snippets_xml = $dom->getElementsByTagName( 'snippet' );
122 $fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
123
124 $snippets = array();
125
126 /* Loop through all snippets */
127
128 /** @var DOMElement $snippet_xml */
129 foreach ( $snippets_xml as $snippet_xml ) {
130 $snippet = new Code_Snippet();
131 $snippet->network = $multisite;
132
133 /* Build a snippet object by looping through the field names */
134 foreach ( $fields as $field_name ) {
135
136 /* Fetch the field element from the document */
137 $field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
138
139 /* If the field element exists, add it to the snippet object */
140 if ( isset( $field->nodeValue ) ) {
141 $snippet->set_field( $field_name, $field->nodeValue );
142 }
143 }
144
145 /* Get scope from attribute */
146 $scope = $snippet_xml->getAttribute( 'scope' );
147 if ( ! empty( $scope ) ) {
148 $snippet->scope = $scope;
149 }
150
151 $snippets[] = $snippet;
152 }
153
154 $imported = _code_snippets_save_imported_snippets( $snippets, $dup_action, $multisite );
155 do_action( 'code_snippets/import/xml', $file, $multisite );
156
157 /** @phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
158
159 return $imported;
160 }
161
162 /**
163 * Set up the current page to act like a downloadable file instead of being shown in the browser
164 *
165 * @param string $format
166 * @param array $ids
167 * @param string $table_name
168 * @param string $mime_type
169 *
170 * @return array
171 */
172 function code_snippets_prepare_export( $format, $ids, $table_name = '', $mime_type = '' ) {
173 global $wpdb;
174
175 /* Fetch the snippets from the database */
176 if ( count( $ids ) ) {
177 $table_name = '' === $table_name ? code_snippets()->db->get_table_name() : $table_name;
178
179 $sql_in_format = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
180 $snippets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE id IN ($sql_in_format)", $ids ), ARRAY_A );
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 * @phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
217 */
218 function download_snippets( $ids, $table_name = '' ) {
219 $snippets = code_snippets_prepare_export( 'php', $ids, $table_name );
220
221 echo "<?php\n";
222
223 /* Loop through the snippets */
224 foreach ( $snippets as $snippet ) {
225 $snippet = new Code_Snippet( $snippet );
226
227 echo "\n/**\n * {$snippet->name}\n";
228
229 if ( ! empty( $snippet->desc ) ) {
230
231 /* Convert description to PhpDoc */
232 $desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) );
233
234 echo " *\n * $desc\n";
235 }
236
237 echo " */\n{$snippet->code}\n";
238 }
239
240 exit;
241 }
242
243 /**
244 * Export snippets in JSON format
245 *
246 * @param array $ids list of snippet IDs to export
247 * @param string $table_name name of the database table to fetch snippets from
248 */
249 function export_snippets( $ids, $table_name = '' ) {
250 $raw_snippets = code_snippets_prepare_export( 'json', $ids, $table_name, 'application/json' );
251 $final_snippets = array();
252
253 foreach ( $raw_snippets as $snippet ) {
254 $snippet = new Code_Snippet( $snippet );
255
256 $fields = array( 'name', 'desc', 'tags', 'scope', 'code', 'priority' );
257 $final_snippet = array();
258
259 foreach ( $fields as $field ) {
260 if ( ! empty( $snippet->$field ) ) {
261 $final_snippet[ $field ] = str_replace( "\r\n", "\n", $snippet->$field );
262 }
263 }
264
265 if ( $final_snippet ) {
266 $final_snippets[] = $final_snippet;
267 }
268 }
269
270 $data = array(
271 'generator' => 'Code Snippets v' . code_snippets()->version,
272 'date_created' => gmdate( 'Y-m-d H:i' ),
273 'snippets' => $final_snippets,
274 );
275
276 echo wp_json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) );
277 exit;
278 }
279