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