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

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