PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.9.0
Import WP – CSV & XML Import Export for WordPress v2.9.0
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.9.0, at class/Common/Importer/Mapper/TermMapper.php

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