PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.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 / export / class-import.php

class-import.php in Code Snippets 3.2.1, at php/export/class-import.php

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