PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.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 / Migration / Export / Import.php

Import.php in Code Snippets 3.10.0, at php/Migration/Export/Import.php

213 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Migration\Export;
4
5 use Code_Snippets\Core\DB;
6 use Code_Snippets\Model\Snippet;
7 use DOMDocument;
8 use function Code_Snippets\get_snippets;
9 use function Code_Snippets\save_snippet;
10
11 /**
12 * Handles importing snippets from export files into the site
13 *
14 * @package Code_Snippets
15 * @since 3.0.0
16 *
17 * phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
18 * phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
19 */
20 class Import {
21
22 /**
23 * Path to file to import.
24 *
25 * @var string
26 */
27 private string $file;
28
29 /**
30 * Whether snippets should be imported into the network-wide or site-wide table.
31 *
32 * @var bool
33 */
34 private bool $multisite;
35
36 /**
37 * Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
38 *
39 * @var string
40 */
41 private string $dup_action;
42
43 /**
44 * Class constructor.
45 *
46 * @param string $file The path to the file to import.
47 * @param bool|null $network Import into network-wide table (true) or site-wide table (false).
48 * @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'.
49 */
50 public function __construct( string $file, ?bool $network = null, string $dup_action = 'ignore' ) {
51 $this->file = $file;
52 $this->multisite = DB::validate_network_param( $network );
53 $this->dup_action = $dup_action;
54 }
55
56 /**
57 * Imports snippets from a JSON file.
58 *
59 * @return array<int>|bool An array of imported snippet IDs on success, false on failure
60 */
61 public function import_json() {
62 if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
63 return false;
64 }
65
66 $raw_data = file_get_contents( $this->file );
67 $data = json_decode( $raw_data, true );
68 $snippets = array();
69
70 // Reformat the data into snippet objects.
71 foreach ( $data['snippets'] as $snippet_data ) {
72 $snippet = new Snippet();
73 $snippet->network = $this->multisite;
74
75 $import_fields = [
76 'name',
77 'desc',
78 'description',
79 'code',
80 'tags',
81 'scope',
82 'priority',
83 'shared_network',
84 'modified',
85 'cloud_id',
86 ];
87
88 foreach ( $import_fields as $field ) {
89 if ( isset( $snippet_data[ $field ] ) ) {
90 $snippet->set_field( $field, $snippet_data[ $field ] );
91 }
92 }
93
94 $snippets[] = $snippet;
95 }
96
97 $imported = $this->save_snippets( $snippets );
98
99 do_action( 'code_snippets/import/json', $this->file, $this->multisite );
100 return $imported;
101 }
102
103 /**
104 * Imports snippets from an XML file
105 *
106 * @return array<int>|bool An array of imported snippet IDs on success, false on failure
107 */
108 public function import_xml() {
109 if ( ! file_exists( $this->file ) || ! is_file( $this->file ) ) {
110 return false;
111 }
112
113 $dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) );
114 $dom->load( $this->file );
115
116 $snippets_xml = $dom->getElementsByTagName( 'snippet' );
117 $fields = array( 'name', 'description', 'desc', 'code', 'tags', 'scope' );
118
119 $snippets = array();
120
121 foreach ( $snippets_xml as $snippet_xml ) {
122 $snippet = new Snippet();
123 $snippet->network = $this->multisite;
124
125 // Build a snippet object by looping through the field names.
126 foreach ( $fields as $field_name ) {
127
128 // Fetch the field element from the document.
129 $field = $snippet_xml->getElementsByTagName( $field_name )->item( 0 );
130
131 // If the field element exists, add it to the snippet object.
132 if ( isset( $field->nodeValue ) ) {
133 $snippet->set_field( $field_name, $field->nodeValue );
134 }
135 }
136
137 // Get scope from attribute.
138 $scope = $snippet_xml->getAttribute( 'scope' );
139 if ( ! empty( $scope ) ) {
140 $snippet->scope = $scope;
141 }
142
143 $snippets[] = $snippet;
144 }
145
146 $imported = $this->save_snippets( $snippets );
147 do_action( 'code_snippets/import/xml', $this->file, $this->multisite );
148
149 return $imported;
150 }
151
152 /**
153 * Fetch a list of existing snippets for checking duplicates.
154 *
155 * @return array<string, int>
156 */
157 private function fetch_existing_snippets(): array {
158 $existing_snippets = array();
159
160 if ( 'replace' === $this->dup_action || 'skip' === $this->dup_action ) {
161 $all_snippets = get_snippets( array(), $this->multisite );
162
163 foreach ( $all_snippets as $snippet ) {
164 if ( $snippet->name ) {
165 $existing_snippets[ $snippet->name ] = $snippet->id;
166 }
167 }
168 }
169
170 return $existing_snippets;
171 }
172
173 /**
174 * Save imported snippets to the database
175 *
176 * @access private
177 *
178 * @param Snippet[] $snippets List of snippets to save.
179 *
180 * @return int[] IDs of imported snippets.
181 */
182 private function save_snippets( array $snippets ): array {
183 $existing_snippets = $this->fetch_existing_snippets();
184 $imported = array();
185
186 foreach ( $snippets as $snippet ) {
187
188 // Check if the snippet already exists.
189 if ( 'ignore' !== $this->dup_action && isset( $existing_snippets[ $snippet->name ] ) ) {
190
191 // If so, either overwrite the existing ID, or skip this import.
192 if ( 'replace' === $this->dup_action ) {
193 $snippet->id = $existing_snippets[ $snippet->name ];
194 } elseif ( 'skip' === $this->dup_action ) {
195 continue;
196 }
197 }
198
199 // Save the snippet and increase the counter if successful.
200 $saved_snippet = save_snippet( $snippet );
201
202 // Get ID of the saved snippet as save_snippet() returns complete snippet object.
203 $snippet_id = $saved_snippet->id;
204
205 if ( $snippet_id ) {
206 $imported[] = $snippet_id;
207 }
208 }
209
210 return $imported;
211 }
212 }
213