PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.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
← All changes | class/Common/Importer/Mapper/TermMapper.php +58 -69 2.9.0 → 2.15.0 View file →
@@ -4,9 +4,8 @@
4 4
5 5 use ImportWP\Common\Importer\Exception\MapperException;
6 6 use ImportWP\Common\Importer\MapperInterface;
7 7 use ImportWP\Common\Importer\ParsedData;
8 -use ImportWP\Common\Importer\Template\TemplateManager;
9 8 use ImportWP\Common\Util\Logger;
10 9
11 10 class TermMapper extends AbstractMapper implements MapperInterface
12 11 {
@@ -28,23 +27,12 @@
28 27 }
29 28
30 29 public function exists(ParsedData $data)
31 30 {
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 -
31 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
43 32 $unique_field_found = false;
44 33
45 34 $taxonomy = $this->importer->getSetting('taxonomy');
46 -
47 35 $query_args = [
48 36 'fields' => 'ids',
49 37 'hide_empty' => false,
50 38 'update_term_meta_cache' => false,
@@ -50,60 +38,44 @@
50 38 'update_term_meta_cache' => false,
51 39 'taxonomy' => $taxonomy
52 40 ];
53 41
54 - $has_unique_field = false;
42 + if (!$has_unique_field) {
43 + foreach ($unique_fields as $field) {
55 44
56 - foreach ($unique_fields as $field) {
45 + // check all groups for a unique value
46 + $unique_value = $this->find_unique_field_in_data($data, $field);
57 47
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 - }
48 + if (!$this->has_identifier_value($unique_value)) {
49 + continue;
75 50 }
76 - }
77 51
78 - if (empty($unique_value)) {
79 - continue;
80 - }
52 + $has_unique_field = true;
81 53
82 - $has_unique_field = true;
54 + if (in_array($field, $this->_term_fields, true)) {
83 55
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;
56 + switch ($field) {
57 + case 'term_id':
58 + $query_args['include'] = [intval($unique_value)];
59 + break;
60 + default:
61 + $query_args[$field] = $unique_value;
62 + break;
63 + }
64 + } else {
65 + $meta_args[] = array(
66 + 'key' => $field,
67 + 'value' => $unique_value
68 + );
93 69 }
94 - } else {
95 - $meta_args[] = array(
96 - 'key' => $field,
97 - 'value' => $unique_value
98 - );
70 + $unique_field_found = $field;
71 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
72 + break;
99 73 }
100 - $unique_field_found = $field;
101 - break;
102 74 }
103 75
104 76 if (!$has_unique_field) {
105 - throw new MapperException("No Unique fields present.");
77 + throw new MapperException(__('No unique identifier value present. Check that Permissions has a unique identifier set and this row has a non-empty value. File column references are stored as _iwp_ref_uid.', 'jc-importer'));
106 78 }
107 79
108 80 if (!empty($meta_args)) {
109 81 $query_args['meta_query'] = $meta_args;
@@ -108,8 +80,10 @@
108 80 if (!empty($meta_args)) {
109 81 $query_args['meta_query'] = $meta_args;
110 82 }
111 83
84 + $query_args = apply_filters('iwp/importer/mapper/term_exists_query', $query_args);
85 + Logger::debug("TermMapper::exists -query=" . wp_json_encode($query_args));
112 86 $query = new \WP_Term_Query($query_args);
113 87 if (!$query->terms) {
114 88 return false;
115 89 }
@@ -114,9 +88,9 @@
114 88 return false;
115 89 }
116 90
117 91 if (count($query->terms) > 1) {
118 - throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->terms) . ").");
92 + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->terms)));
119 93 }
120 94
121 95 if (count($query->terms) == 1) {
122 96 $this->ID = $query->terms[0];
@@ -180,9 +154,8 @@
180 154
181 155 if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) {
182 156 foreach ($custom_fields as $key => $value) {
183 157 if (is_array($value)) {
184 - $this->clear_custom_field($this->ID, $key);
185 158 foreach ($value as $v) {
186 159 $this->add_custom_field($this->ID, $key, $v);
187 160 }
188 161 } else {
@@ -192,8 +165,9 @@
192 165 }
193 166 }
194 167
195 168 $this->add_version_tag();
169 + $this->add_reference_tag($data);
196 170 $this->template->post_process($this->ID, $data);
197 171
198 172 return $this->ID;
199 173 }
@@ -210,12 +184,8 @@
210 184 }
211 185
212 186 $taxonomy = $this->importer->getSetting('taxonomy');
213 187 foreach ($fields as $key => $value) {
214 - //
215 - if (empty($value)) {
216 - continue;
217 - }
218 188 if (in_array($key, $this->_term_fields)) {
219 189 $args[$key] = $value;
220 190 } else {
221 191 $custom_fields[$key] = $value;
@@ -237,10 +207,10 @@
237 207 Logger::debug('TermMapper::update -meta=' . wp_json_encode($custom_fields));
238 208
239 209 if (!empty($custom_fields)) {
240 210 foreach ($custom_fields as $key => $value) {
211 + $this->clear_custom_field($this->ID, $key);
241 212 if (is_array($value)) {
242 - $this->clear_custom_field($this->ID, $key);
243 213 foreach ($value as $v) {
244 214 $this->add_custom_field($this->ID, $key, $v);
245 215 }
246 216 } else {
@@ -249,8 +219,9 @@
249 219 }
250 220 }
251 221
252 222 $this->add_version_tag();
223 + $this->add_reference_tag($data);
253 224 $this->template->post_process($this->ID, $data);
254 225
255 226 return $this->ID;
256 227 }
@@ -268,20 +239,33 @@
268 239 {
269 240 if ($this->is_session_tag_enabled()) {
270 241 return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy'));
271 242 } else {
243 +
244 + $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
245 + if (empty($import_ids)) {
246 + return false;
247 + }
248 +
249 + $meta_query = [];
250 + foreach ($import_ids as $import_id) {
251 + $meta_query[] = [
252 + 'key' => '_iwp_session_' . $import_id,
253 + 'value' => $this->importer->getStatusId(),
254 + 'compare' => '!='
255 + ];
256 + }
257 +
258 + if (count($meta_query) > 1) {
259 + $meta_query['relation'] = 'AND';
260 + }
261 +
272 262 $taxonomy = $this->importer->getSetting('taxonomy');
273 263 $terms = get_terms([
274 264 'taxonomy' => $taxonomy,
275 265 'fields' => 'ids',
276 266 'hide_empty' => false,
277 - 'meta_query' => [
278 - [
279 - 'key' => '_iwp_session_' . $this->importer->getId(),
280 - 'value' => $this->importer->getStatusId(),
281 - 'compare' => '!='
282 - ]
283 - ]
267 + 'meta_query' => $meta_query
284 268 ]);
285 269
286 270 if (!is_wp_error($terms)) {
287 271 return $terms;
@@ -323,9 +307,14 @@
323 307
324 308 add_term_meta($term_id, $key, $value);
325 309 }
326 310
327 - public function update_custom_field($id, $key, $value)
311 + public function get_custom_field($id, $key = '', $single = false)
312 + {
313 + return get_term_meta($id, $key, $single);
314 + }
315 +
316 + public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
328 317 {
329 318 // Stop double serialization
330 319 if (is_serialized($value)) {
331 320 $value = unserialize($value);