PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.1
Import WP – CSV & XML Import Export for WordPress v2.8.1
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Mapper / TermMapper.php

TermMapper.php in Import WP – CSV & XML Import Export for WordPress 2.8.1, at class/Common/Importer/Mapper/TermMapper.php

331 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\Mapper;
4
5 use ImportWP\Common\Importer\Exception\MapperException;
6 use ImportWP\Common\Importer\MapperInterface;
7 use ImportWP\Common\Importer\ParsedData;
8
9 class TermMapper extends AbstractMapper implements MapperInterface
10 {
11 protected $_term_fields = array(
12 'term_id',
13 'alias_of',
14 'description',
15 'parent',
16 'slug',
17 'name'
18 );
19
20 public function setup()
21 {
22 }
23
24 public function teardown()
25 {
26 }
27
28 public function exists(ParsedData $data)
29 {
30 $unique_fields = ['term_id', 'slug', 'name'];
31
32 // allow user to set unique field name, get from importer setting
33 $unique_field = $this->importer->getSetting('unique_field');
34 if ($unique_field !== null) {
35 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
36 }
37
38 $unique_fields = $this->getUniqueIdentifiers($unique_fields);
39 $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
40
41 $unique_field_found = false;
42
43 $taxonomy = $this->importer->getSetting('taxonomy');
44
45 $query_args = [
46 'fields' => 'ids',
47 'hide_empty' => false,
48 'update_term_meta_cache' => false,
49 'taxonomy' => $taxonomy
50 ];
51
52 $has_unique_field = false;
53
54 foreach ($unique_fields as $field) {
55
56 // check all groups for a unique value
57 $unique_value = $data->getValue($field, '*');
58 if (empty($unique_value)) {
59 $cf = $data->getData('custom_fields');
60 if (!empty($cf)) {
61 $cf_index = intval($cf['custom_fields._index']);
62 if ($cf_index > 0) {
63 for ($i = 0; $i < $cf_index; $i++) {
64 $row = 'custom_fields.' . $i . '.';
65 $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
66 if ($custom_field_key !== $field) {
67 continue;
68 }
69 $unique_value = $cf[$row . 'value'];
70 break;
71 }
72 }
73 }
74 }
75
76 if (empty($unique_value)) {
77 continue;
78 }
79
80 $has_unique_field = true;
81
82 if (in_array($field, $this->_term_fields, true)) {
83
84 switch ($field) {
85 case 'term_id':
86 $query_args['include'] = [intval($unique_value)];
87 break;
88 default:
89 $query_args[$field] = $unique_value;
90 break;
91 }
92 } else {
93 $meta_args[] = array(
94 'key' => $field,
95 'value' => $unique_value
96 );
97 }
98 $unique_field_found = $field;
99 break;
100 }
101
102 if (!$has_unique_field) {
103 throw new MapperException("No Unique fields present.");
104 }
105
106 if (!empty($meta_args)) {
107 $query_args['meta_query'] = $meta_args;
108 }
109
110 $query = new \WP_Term_Query($query_args);
111 if (!$query->terms) {
112 return false;
113 }
114
115 if (count($query->terms) > 1) {
116 throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->terms) . ").");
117 }
118
119 if (count($query->terms) == 1) {
120 $this->ID = $query->terms[0];
121 return $this->ID;
122 }
123
124 return false;
125 }
126
127 public function insert(ParsedData $data)
128 {
129 $fields = $data->getData('default');
130
131 $this->ID = false;
132 $args = array();
133 $custom_fields = array();
134
135 // term_id Cant be inserted or updated, it is just used for reference
136 if (isset($fields['term_id'])) {
137 unset($fields['term_id']);
138 }
139
140 // escape if required fields are not entered
141 if (!isset($fields['name']) || empty($fields['name'])) {
142 throw new MapperException('Term name is missing.');
143 }
144
145 $term = !empty($fields['name']) ? $fields['name'] : false;
146 $taxonomy = $this->importer->getSetting('taxonomy');
147
148 if ($term && $taxonomy) {
149
150 unset($fields['name']);
151 unset($fields['taxonomy']);
152 foreach ($fields as $key => $value) {
153 //
154 if (empty($value)) {
155 continue;
156 }
157 if (in_array($key, $this->_term_fields)) {
158 $args[$key] = $value;
159 } else {
160 $custom_fields[$key] = $value;
161 }
162 }
163
164 // returns array('term_id' => #, 'taxonomy_id' => #)
165 $insert = wp_insert_term($term, $taxonomy, $args);
166 if (is_wp_error($insert)) {
167 throw new MapperException($insert->get_error_message());
168 }
169
170 $this->ID = $insert['term_id'];
171
172 // TODO: Do we merge in the custom fields, or do we process that in post_process
173 $this->template->process($this->ID, $data, $this->importer);
174
175 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
176
177 if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) {
178 foreach ($custom_fields as $key => $value) {
179 if (is_array($value)) {
180 $this->clear_custom_field($this->ID, $key);
181 foreach ($value as $v) {
182 $this->add_custom_field($this->ID, $key, $v);
183 }
184 } else {
185 $this->update_custom_field($this->ID, $key, $value);
186 }
187 }
188 }
189 }
190
191 $this->add_version_tag();
192 $this->template->post_process($this->ID, $data);
193
194 return $this->ID;
195 }
196
197 public function update(ParsedData $data)
198 {
199 $fields = $data->getData('default');
200 $args = array();
201 $custom_fields = array();
202
203 // term_id Cant be inserted or updated, it is just used for reference
204 if (isset($fields['term_id'])) {
205 unset($fields['term_id']);
206 }
207
208 $taxonomy = $this->importer->getSetting('taxonomy');
209 foreach ($fields as $key => $value) {
210 //
211 if (empty($value)) {
212 continue;
213 }
214 if (in_array($key, $this->_term_fields)) {
215 $args[$key] = $value;
216 } else {
217 $custom_fields[$key] = $value;
218 }
219 }
220
221 // returns array('term_id' => #, 'taxonomy_id' => #)
222 $result = wp_update_term($this->ID, $taxonomy, $args);
223 if (is_wp_error($result)) {
224 throw new MapperException($result->get_error_message());
225 }
226
227 // TODO: Do we merge in the custom fields, or do we process that in post_process
228 $this->template->process($this->ID, $data, $this->importer);
229
230 // merge meta group
231 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
232
233 if (!empty($custom_fields)) {
234 foreach ($custom_fields as $key => $value) {
235 if (is_array($value)) {
236 $this->clear_custom_field($this->ID, $key);
237 foreach ($value as $v) {
238 $this->add_custom_field($this->ID, $key, $v);
239 }
240 } else {
241 $this->update_custom_field($this->ID, $key, $value);
242 }
243 }
244 }
245
246 $this->add_version_tag();
247 $this->template->post_process($this->ID, $data);
248
249 return $this->ID;
250 }
251
252 public function add_version_tag()
253 {
254 if ($this->is_session_tag_enabled()) {
255 $this->add_session_tag('t-' . $this->importer->getSetting('taxonomy'));
256 } else {
257 update_term_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
258 }
259 }
260
261 public function get_objects_for_removal()
262 {
263 if ($this->is_session_tag_enabled()) {
264 return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy'));
265 } else {
266 $taxonomy = $this->importer->getSetting('taxonomy');
267 $terms = get_terms([
268 'taxonomy' => $taxonomy,
269 'fields' => 'ids',
270 'hide_empty' => false,
271 'meta_query' => [
272 [
273 'key' => '_iwp_session_' . $this->importer->getId(),
274 'value' => $this->importer->getStatusId(),
275 'compare' => '!='
276 ]
277 ]
278 ]);
279
280 if (!is_wp_error($terms)) {
281 return $terms;
282 }
283 }
284
285 return false;
286 }
287
288 public function delete($id)
289 {
290 wp_delete_term($id, $this->importer->getSetting('taxonomy'));
291
292 $this->remove_session_tag($id, 't-' . $this->importer->getSetting('taxonomy'));
293 }
294
295 /**
296 * Clear all post meta before adding custom field
297 */
298 public function clear_custom_field($term_id, $key)
299 {
300 delete_term_meta($term_id, $key);
301 }
302
303 /**
304 * Add custom field, allow for multiple records using the same key
305 *
306 * @param int $term_id
307 * @param string $key
308 * @param string $value
309 * @return void
310 */
311 public function add_custom_field($term_id, $key, $value)
312 {
313 // Stop double serialization
314 if (is_serialized($value)) {
315 $value = unserialize($value);
316 }
317
318 add_term_meta($term_id, $key, $value);
319 }
320
321 public function update_custom_field($id, $key, $value)
322 {
323 // Stop double serialization
324 if (is_serialized($value)) {
325 $value = unserialize($value);
326 }
327
328 update_term_meta($id, $key, $value);
329 }
330 }
331