| @@ -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,31 +27,77 @@ | ||
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | public function exists(ParsedData $data) |
| 29 | 30 | { |
| 30 | - $fields = $data->getData('default'); | |
| 31 | - $term = !empty($fields['name']) ? $fields['name'] : false; | |
| 32 | - $slug = !empty($fields['slug']) ? $fields['slug'] : false; | |
| 33 | - $term_id = !empty($fields['term_id']) ? $fields['term_id'] : false; | |
| 31 | + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data); | |
| 32 | + $unique_field_found = false; | |
| 33 | + | |
| 34 | 34 | $taxonomy = $this->importer->getSetting('taxonomy'); |
| 35 | + $query_args = [ | |
| 36 | + 'fields' => 'ids', | |
| 37 | + 'hide_empty' => false, | |
| 38 | + 'update_term_meta_cache' => false, | |
| 39 | + 'taxonomy' => $taxonomy | |
| 40 | + ]; | |
| 35 | 41 | |
| 36 | - // escape if required fields are not entered | |
| 37 | - if (false === $taxonomy || (false === $term && false === $slug && false === $term_id)) { | |
| 42 | + if (!$has_unique_field) { | |
| 43 | + foreach ($unique_fields as $field) { | |
| 44 | + | |
| 45 | + // check all groups for a unique value | |
| 46 | + $unique_value = $this->find_unique_field_in_data($data, $field); | |
| 47 | + | |
| 48 | + if (!$this->has_identifier_value($unique_value)) { | |
| 49 | + continue; | |
| 50 | + } | |
| 51 | + | |
| 52 | + $has_unique_field = true; | |
| 53 | + | |
| 54 | + if (in_array($field, $this->_term_fields, true)) { | |
| 55 | + | |
| 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 | + ); | |
| 69 | + } | |
| 70 | + $unique_field_found = $field; | |
| 71 | + $this->set_unique_identifier_settings($unique_field_found, $unique_value); | |
| 72 | + break; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + if (!$has_unique_field) { | |
| 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')); | |
| 78 | + } | |
| 79 | + | |
| 80 | + if (!empty($meta_args)) { | |
| 81 | + $query_args['meta_query'] = $meta_args; | |
| 82 | + } | |
| 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)); | |
| 86 | + $query = new \WP_Term_Query($query_args); | |
| 87 | + if (!$query->terms) { | |
| 38 | 88 | return false; |
| 39 | 89 | } |
| 40 | 90 | |
| 41 | - if (!empty($term_id)) { | |
| 42 | - $term = get_term_by('id', $term_id, $taxonomy); | |
| 43 | - } else if (!empty($slug) && 'yes' !== $data->getValue('slug', '_generated')) { | |
| 44 | - // use slug if its not been generated from name | |
| 45 | - $term = get_term_by('slug', $slug, $taxonomy); | |
| 46 | - } else if (!empty($term)) { | |
| 47 | - $term = get_term_by('name', $term, $taxonomy); | |
| 91 | + if (count($query->terms) > 1) { | |
| 92 | + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->terms))); | |
| 48 | 93 | } |
| 49 | - if ($term && isset($term->term_id)) { | |
| 50 | - $this->ID = $term->term_id; | |
| 51 | - return true; | |
| 94 | + | |
| 95 | + if (count($query->terms) == 1) { | |
| 96 | + $this->ID = $query->terms[0]; | |
| 97 | + return $this->ID; | |
| 52 | 98 | } |
| 99 | + | |
| 53 | 100 | return false; |
| 54 | 101 | } |
| 55 | 102 | |
| 56 | 103 | public function insert(ParsedData $data) |
| @@ -89,9 +136,10 @@ | ||
| 89 | 136 | $custom_fields[$key] = $value; |
| 90 | 137 | } |
| 91 | 138 | } |
| 92 | 139 | |
| 93 | - // returns array('term_id' => #, 'taxonomy_id' => #) | |
| 140 | + Logger::debug('TermMapper::insert -term=' . $term . ' -tax=' . $taxonomy . ' -wp_insert_term=' . wp_json_encode($args)); | |
| 141 | + | |
| 94 | 142 | $insert = wp_insert_term($term, $taxonomy, $args); |
| 95 | 143 | if (is_wp_error($insert)) { |
| 96 | 144 | throw new MapperException($insert->get_error_message()); |
| 97 | 145 | } |
| @@ -101,13 +149,13 @@ | ||
| 101 | 149 | // TODO: Do we merge in the custom fields, or do we process that in post_process |
| 102 | 150 | $this->template->process($this->ID, $data, $this->importer); |
| 103 | 151 | |
| 104 | 152 | $custom_fields = array_merge($custom_fields, $data->getData('custom_fields')); |
| 153 | + Logger::debug('TermMapper::insert -meta=' . wp_json_encode($custom_fields)); | |
| 105 | 154 | |
| 106 | 155 | if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) { |
| 107 | 156 | foreach ($custom_fields as $key => $value) { |
| 108 | 157 | if (is_array($value)) { |
| 109 | - $this->clear_custom_field($this->ID, $key); | |
| 110 | 158 | foreach ($value as $v) { |
| 111 | 159 | $this->add_custom_field($this->ID, $key, $v); |
| 112 | 160 | } |
| 113 | 161 | } else { |
| @@ -117,8 +165,9 @@ | ||
| 117 | 165 | } |
| 118 | 166 | } |
| 119 | 167 | |
| 120 | 168 | $this->add_version_tag(); |
| 169 | + $this->add_reference_tag($data); | |
| 121 | 170 | $this->template->post_process($this->ID, $data); |
| 122 | 171 | |
| 123 | 172 | return $this->ID; |
| 124 | 173 | } |
| @@ -135,12 +184,8 @@ | ||
| 135 | 184 | } |
| 136 | 185 | |
| 137 | 186 | $taxonomy = $this->importer->getSetting('taxonomy'); |
| 138 | 187 | foreach ($fields as $key => $value) { |
| 139 | - // | |
| 140 | - if (empty($value)) { | |
| 141 | - continue; | |
| 142 | - } | |
| 143 | 188 | if (in_array($key, $this->_term_fields)) { |
| 144 | 189 | $args[$key] = $value; |
| 145 | 190 | } else { |
| 146 | 191 | $custom_fields[$key] = $value; |
| @@ -146,9 +191,10 @@ | ||
| 146 | 191 | $custom_fields[$key] = $value; |
| 147 | 192 | } |
| 148 | 193 | } |
| 149 | 194 | |
| 150 | - // returns array('term_id' => #, 'taxonomy_id' => #) | |
| 195 | + Logger::debug('TermMapper::update -tax=' . $taxonomy . ' -wp_update_term=' . wp_json_encode($args)); | |
| 196 | + | |
| 151 | 197 | $result = wp_update_term($this->ID, $taxonomy, $args); |
| 152 | 198 | if (is_wp_error($result)) { |
| 153 | 199 | throw new MapperException($result->get_error_message()); |
| 154 | 200 | } |
| @@ -157,13 +203,14 @@ | ||
| 157 | 203 | $this->template->process($this->ID, $data, $this->importer); |
| 158 | 204 | |
| 159 | 205 | // merge meta group |
| 160 | 206 | $custom_fields = array_merge($custom_fields, $data->getData('custom_fields')); |
| 207 | + Logger::debug('TermMapper::update -meta=' . wp_json_encode($custom_fields)); | |
| 161 | 208 | |
| 162 | 209 | if (!empty($custom_fields)) { |
| 163 | 210 | foreach ($custom_fields as $key => $value) { |
| 211 | + $this->clear_custom_field($this->ID, $key); | |
| 164 | 212 | if (is_array($value)) { |
| 165 | - $this->clear_custom_field($this->ID, $key); | |
| 166 | 213 | foreach ($value as $v) { |
| 167 | 214 | $this->add_custom_field($this->ID, $key, $v); |
| 168 | 215 | } |
| 169 | 216 | } else { |
| @@ -172,8 +219,9 @@ | ||
| 172 | 219 | } |
| 173 | 220 | } |
| 174 | 221 | |
| 175 | 222 | $this->add_version_tag(); |
| 223 | + $this->add_reference_tag($data); | |
| 176 | 224 | $this->template->post_process($this->ID, $data); |
| 177 | 225 | |
| 178 | 226 | return $this->ID; |
| 179 | 227 | } |
| @@ -191,20 +239,33 @@ | ||
| 191 | 239 | { |
| 192 | 240 | if ($this->is_session_tag_enabled()) { |
| 193 | 241 | return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy')); |
| 194 | 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 | + | |
| 195 | 262 | $taxonomy = $this->importer->getSetting('taxonomy'); |
| 196 | 263 | $terms = get_terms([ |
| 197 | 264 | 'taxonomy' => $taxonomy, |
| 198 | 265 | 'fields' => 'ids', |
| 199 | 266 | 'hide_empty' => false, |
| 200 | - 'meta_query' => [ | |
| 201 | - [ | |
| 202 | - 'key' => '_iwp_session_' . $this->importer->getId(), | |
| 203 | - 'value' => $this->importer->getStatusId(), | |
| 204 | - 'compare' => '!=' | |
| 205 | - ] | |
| 206 | - ] | |
| 267 | + 'meta_query' => $meta_query | |
| 207 | 268 | ]); |
| 208 | 269 | |
| 209 | 270 | if (!is_wp_error($terms)) { |
| 210 | 271 | return $terms; |
| @@ -246,9 +307,14 @@ | ||
| 246 | 307 | |
| 247 | 308 | add_term_meta($term_id, $key, $value); |
| 248 | 309 | } |
| 249 | 310 | |
| 250 | - 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) | |
| 251 | 317 | { |
| 252 | 318 | // Stop double serialization |
| 253 | 319 | if (is_serialized($value)) { |
| 254 | 320 | $value = unserialize($value); |