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
jc-importer / class / Common / Importer / Mapper / TermMapper.php

TermMapper.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/Mapper/TermMapper.php

326 lines 10.2 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\Util\Logger;
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 list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
32 $unique_field_found = false;
33
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 ];
41
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) {
88 return false;
89 }
90
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)));
93 }
94
95 if (count($query->terms) == 1) {
96 $this->ID = $query->terms[0];
97 return $this->ID;
98 }
99
100 return false;
101 }
102
103 public function insert(ParsedData $data)
104 {
105 $fields = $data->getData('default');
106
107 $this->ID = false;
108 $args = array();
109 $custom_fields = array();
110
111 // term_id Cant be inserted or updated, it is just used for reference
112 if (isset($fields['term_id'])) {
113 unset($fields['term_id']);
114 }
115
116 // escape if required fields are not entered
117 if (!isset($fields['name']) || empty($fields['name'])) {
118 throw new MapperException('Term name is missing.');
119 }
120
121 $term = !empty($fields['name']) ? $fields['name'] : false;
122 $taxonomy = $this->importer->getSetting('taxonomy');
123
124 if ($term && $taxonomy) {
125
126 unset($fields['name']);
127 unset($fields['taxonomy']);
128 foreach ($fields as $key => $value) {
129 //
130 if (empty($value)) {
131 continue;
132 }
133 if (in_array($key, $this->_term_fields)) {
134 $args[$key] = $value;
135 } else {
136 $custom_fields[$key] = $value;
137 }
138 }
139
140 Logger::debug('TermMapper::insert -term=' . $term . ' -tax=' . $taxonomy . ' -wp_insert_term=' . wp_json_encode($args));
141
142 $insert = wp_insert_term($term, $taxonomy, $args);
143 if (is_wp_error($insert)) {
144 throw new MapperException($insert->get_error_message());
145 }
146
147 $this->ID = $insert['term_id'];
148
149 // TODO: Do we merge in the custom fields, or do we process that in post_process
150 $this->template->process($this->ID, $data, $this->importer);
151
152 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
153 Logger::debug('TermMapper::insert -meta=' . wp_json_encode($custom_fields));
154
155 if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) {
156 foreach ($custom_fields as $key => $value) {
157 if (is_array($value)) {
158 foreach ($value as $v) {
159 $this->add_custom_field($this->ID, $key, $v);
160 }
161 } else {
162 $this->update_custom_field($this->ID, $key, $value);
163 }
164 }
165 }
166 }
167
168 $this->add_version_tag();
169 $this->add_reference_tag($data);
170 $this->template->post_process($this->ID, $data);
171
172 return $this->ID;
173 }
174
175 public function update(ParsedData $data)
176 {
177 $fields = $data->getData('default');
178 $args = array();
179 $custom_fields = array();
180
181 // term_id Cant be inserted or updated, it is just used for reference
182 if (isset($fields['term_id'])) {
183 unset($fields['term_id']);
184 }
185
186 $taxonomy = $this->importer->getSetting('taxonomy');
187 foreach ($fields as $key => $value) {
188 if (in_array($key, $this->_term_fields)) {
189 $args[$key] = $value;
190 } else {
191 $custom_fields[$key] = $value;
192 }
193 }
194
195 Logger::debug('TermMapper::update -tax=' . $taxonomy . ' -wp_update_term=' . wp_json_encode($args));
196
197 $result = wp_update_term($this->ID, $taxonomy, $args);
198 if (is_wp_error($result)) {
199 throw new MapperException($result->get_error_message());
200 }
201
202 // TODO: Do we merge in the custom fields, or do we process that in post_process
203 $this->template->process($this->ID, $data, $this->importer);
204
205 // merge meta group
206 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
207 Logger::debug('TermMapper::update -meta=' . wp_json_encode($custom_fields));
208
209 if (!empty($custom_fields)) {
210 foreach ($custom_fields as $key => $value) {
211 $this->clear_custom_field($this->ID, $key);
212 if (is_array($value)) {
213 foreach ($value as $v) {
214 $this->add_custom_field($this->ID, $key, $v);
215 }
216 } else {
217 $this->update_custom_field($this->ID, $key, $value);
218 }
219 }
220 }
221
222 $this->add_version_tag();
223 $this->add_reference_tag($data);
224 $this->template->post_process($this->ID, $data);
225
226 return $this->ID;
227 }
228
229 public function add_version_tag()
230 {
231 if ($this->is_session_tag_enabled()) {
232 $this->add_session_tag('t-' . $this->importer->getSetting('taxonomy'));
233 } else {
234 update_term_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
235 }
236 }
237
238 public function get_objects_for_removal()
239 {
240 if ($this->is_session_tag_enabled()) {
241 return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy'));
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
262 $taxonomy = $this->importer->getSetting('taxonomy');
263 $terms = get_terms([
264 'taxonomy' => $taxonomy,
265 'fields' => 'ids',
266 'hide_empty' => false,
267 'meta_query' => $meta_query
268 ]);
269
270 if (!is_wp_error($terms)) {
271 return $terms;
272 }
273 }
274
275 return false;
276 }
277
278 public function delete($id)
279 {
280 wp_delete_term($id, $this->importer->getSetting('taxonomy'));
281
282 $this->remove_session_tag($id, 't-' . $this->importer->getSetting('taxonomy'));
283 }
284
285 /**
286 * Clear all post meta before adding custom field
287 */
288 public function clear_custom_field($term_id, $key)
289 {
290 delete_term_meta($term_id, $key);
291 }
292
293 /**
294 * Add custom field, allow for multiple records using the same key
295 *
296 * @param int $term_id
297 * @param string $key
298 * @param string $value
299 * @return void
300 */
301 public function add_custom_field($term_id, $key, $value)
302 {
303 // Stop double serialization
304 if (is_serialized($value)) {
305 $value = unserialize($value);
306 }
307
308 add_term_meta($term_id, $key, $value);
309 }
310
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)
317 {
318 // Stop double serialization
319 if (is_serialized($value)) {
320 $value = unserialize($value);
321 }
322
323 update_term_meta($id, $key, $value);
324 }
325 }
326