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 +65 -70 2.8.1 → 2.15.0 View file →
@@ -4,8 +4,9 @@
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\Util\Logger;
8 9
9 10 class TermMapper extends AbstractMapper implements MapperInterface
10 11 {
11 12 protected $_term_fields = array(
@@ -26,23 +27,12 @@
26 27 }
27 28
28 29 public function exists(ParsedData $data)
29 30 {
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 -
31 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
41 32 $unique_field_found = false;
42 33
43 34 $taxonomy = $this->importer->getSetting('taxonomy');
44 -
45 35 $query_args = [
46 36 'fields' => 'ids',
47 37 'hide_empty' => false,
48 38 'update_term_meta_cache' => false,
@@ -48,60 +38,44 @@
48 38 'update_term_meta_cache' => false,
49 39 'taxonomy' => $taxonomy
50 40 ];
51 41
52 - $has_unique_field = false;
42 + if (!$has_unique_field) {
43 + foreach ($unique_fields as $field) {
53 44
54 - foreach ($unique_fields as $field) {
45 + // check all groups for a unique value
46 + $unique_value = $this->find_unique_field_in_data($data, $field);
55 47
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 - }
48 + if (!$this->has_identifier_value($unique_value)) {
49 + continue;
73 50 }
74 - }
75 51
76 - if (empty($unique_value)) {
77 - continue;
78 - }
52 + $has_unique_field = true;
79 53
80 - $has_unique_field = true;
54 + if (in_array($field, $this->_term_fields, true)) {
81 55
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;
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 + );
91 69 }
92 - } else {
93 - $meta_args[] = array(
94 - 'key' => $field,
95 - 'value' => $unique_value
96 - );
70 + $unique_field_found = $field;
71 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
72 + break;
97 73 }
98 - $unique_field_found = $field;
99 - break;
100 74 }
101 75
102 76 if (!$has_unique_field) {
103 - 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'));
104 78 }
105 79
106 80 if (!empty($meta_args)) {
107 81 $query_args['meta_query'] = $meta_args;
@@ -106,8 +80,10 @@
106 80 if (!empty($meta_args)) {
107 81 $query_args['meta_query'] = $meta_args;
108 82 }
109 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));
110 86 $query = new \WP_Term_Query($query_args);
111 87 if (!$query->terms) {
112 88 return false;
113 89 }
@@ -112,9 +88,9 @@
112 88 return false;
113 89 }
114 90
115 91 if (count($query->terms) > 1) {
116 - 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)));
117 93 }
118 94
119 95 if (count($query->terms) == 1) {
120 96 $this->ID = $query->terms[0];
@@ -160,9 +136,10 @@
160 136 $custom_fields[$key] = $value;
161 137 }
162 138 }
163 139
164 - // returns array('term_id' => #, 'taxonomy_id' => #)
140 + Logger::debug('TermMapper::insert -term=' . $term . ' -tax=' . $taxonomy . ' -wp_insert_term=' . wp_json_encode($args));
141 +
165 142 $insert = wp_insert_term($term, $taxonomy, $args);
166 143 if (is_wp_error($insert)) {
167 144 throw new MapperException($insert->get_error_message());
168 145 }
@@ -172,13 +149,13 @@
172 149 // TODO: Do we merge in the custom fields, or do we process that in post_process
173 150 $this->template->process($this->ID, $data, $this->importer);
174 151
175 152 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
153 + Logger::debug('TermMapper::insert -meta=' . wp_json_encode($custom_fields));
176 154
177 155 if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) {
178 156 foreach ($custom_fields as $key => $value) {
179 157 if (is_array($value)) {
180 - $this->clear_custom_field($this->ID, $key);
181 158 foreach ($value as $v) {
182 159 $this->add_custom_field($this->ID, $key, $v);
183 160 }
184 161 } else {
@@ -188,8 +165,9 @@
188 165 }
189 166 }
190 167
191 168 $this->add_version_tag();
169 + $this->add_reference_tag($data);
192 170 $this->template->post_process($this->ID, $data);
193 171
194 172 return $this->ID;
195 173 }
@@ -206,12 +184,8 @@
206 184 }
207 185
208 186 $taxonomy = $this->importer->getSetting('taxonomy');
209 187 foreach ($fields as $key => $value) {
210 - //
211 - if (empty($value)) {
212 - continue;
213 - }
214 188 if (in_array($key, $this->_term_fields)) {
215 189 $args[$key] = $value;
216 190 } else {
217 191 $custom_fields[$key] = $value;
@@ -217,9 +191,10 @@
217 191 $custom_fields[$key] = $value;
218 192 }
219 193 }
220 194
221 - // returns array('term_id' => #, 'taxonomy_id' => #)
195 + Logger::debug('TermMapper::update -tax=' . $taxonomy . ' -wp_update_term=' . wp_json_encode($args));
196 +
222 197 $result = wp_update_term($this->ID, $taxonomy, $args);
223 198 if (is_wp_error($result)) {
224 199 throw new MapperException($result->get_error_message());
225 200 }
@@ -228,13 +203,14 @@
228 203 $this->template->process($this->ID, $data, $this->importer);
229 204
230 205 // merge meta group
231 206 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
207 + Logger::debug('TermMapper::update -meta=' . wp_json_encode($custom_fields));
232 208
233 209 if (!empty($custom_fields)) {
234 210 foreach ($custom_fields as $key => $value) {
211 + $this->clear_custom_field($this->ID, $key);
235 212 if (is_array($value)) {
236 - $this->clear_custom_field($this->ID, $key);
237 213 foreach ($value as $v) {
238 214 $this->add_custom_field($this->ID, $key, $v);
239 215 }
240 216 } else {
@@ -243,8 +219,9 @@
243 219 }
244 220 }
245 221
246 222 $this->add_version_tag();
223 + $this->add_reference_tag($data);
247 224 $this->template->post_process($this->ID, $data);
248 225
249 226 return $this->ID;
250 227 }
@@ -262,20 +239,33 @@
262 239 {
263 240 if ($this->is_session_tag_enabled()) {
264 241 return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy'));
265 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 +
266 262 $taxonomy = $this->importer->getSetting('taxonomy');
267 263 $terms = get_terms([
268 264 'taxonomy' => $taxonomy,
269 265 'fields' => 'ids',
270 266 'hide_empty' => false,
271 - 'meta_query' => [
272 - [
273 - 'key' => '_iwp_session_' . $this->importer->getId(),
274 - 'value' => $this->importer->getStatusId(),
275 - 'compare' => '!='
276 - ]
277 - ]
267 + 'meta_query' => $meta_query
278 268 ]);
279 269
280 270 if (!is_wp_error($terms)) {
281 271 return $terms;
@@ -317,9 +307,14 @@
317 307
318 308 add_term_meta($term_id, $key, $value);
319 309 }
320 310
321 - 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)
322 317 {
323 318 // Stop double serialization
324 319 if (is_serialized($value)) {
325 320 $value = unserialize($value);