PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.3
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 / Template / TermTemplate.php

TermTemplate.php in Import WP – CSV & XML Import Export for WordPress 2.8.3, at class/Common/Importer/Template/TermTemplate.php

360 lines 11.9 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\Template;
4
5 use ImportWP\Common\Importer\ParsedData;
6 use ImportWP\Common\Importer\TemplateInterface;
7 use ImportWP\Common\Model\ImporterModel;
8 use ImportWP\EventHandler;
9
10 class TermTemplate extends Template implements TemplateInterface
11 {
12 protected $name = 'Term';
13 protected $mapper = 'term';
14
15 protected $field_map = [
16 'term_id' => 'term.term_id',
17 'name' => 'term.name',
18 'description' => 'term.description',
19 'slug' => 'term.slug',
20 'alias_of' => 'term.alias_of'
21 ];
22
23 protected $optional_fields = [
24 'term_id',
25 'description',
26 'slug',
27 'alias_of',
28 ];
29
30 public function __construct(EventHandler $event_handler)
31 {
32 parent::__construct($event_handler);
33
34 $this->groups[] = 'term';
35 $this->field_options = array_merge($this->field_options, [
36 'term._parent.parent' => [$this, 'get_term_parent_options'],
37 ]);
38 }
39
40 public function register_options()
41 {
42 $options = [
43 ['value' => '', 'label' => 'Choose a taxonomy']
44 ];
45 foreach (get_taxonomies(array(), 'names') as $key => $tax) {
46 $options[] = ['value' => $key, 'label' => $tax];
47 }
48
49 return [
50 $this->register_field('Taxonomy', 'taxonomy', [
51 'options' => $options
52 ])
53 ];
54 }
55
56 public function register()
57 {
58 return [
59 $this->register_group('Term', 'term', [
60 $this->register_field('ID', 'term_id', [
61 'tooltip' => __('ID is only used to reference existing records', 'importwp')
62 ]),
63 $this->register_core_field('Name', 'name', [
64 'tooltip' => __('The term name', 'importwp')
65 ]),
66 $this->register_field('Description', 'description', [
67 'tooltip' => __('The term description', 'importwp')
68 ]),
69 $this->register_field('Slug', 'slug', [
70 'tooltip' => __('The term slug to use', 'importwp')
71 ]),
72 $this->register_group('Parent Settings', '_parent', [
73 $this->register_field('Parent', 'parent', [
74 'default' => '',
75 'options' => 'callback',
76 'tooltip' => __('The id of the parent term', 'importwp')
77 ]),
78 $this->register_field('Parent Field Type', '_parent_type', [
79 'default' => 'id',
80 'options' => [
81 ['value' => 'id', 'label' => 'ID'],
82 ['value' => 'slug', 'label' => 'Slug'],
83 ['value' => 'name', 'label' => 'Name'],
84 ['value' => 'column', 'label' => 'Reference Column'],
85 ['value' => 'custom_field', 'label' => 'Custom Field'],
86 ],
87 'type' => 'select',
88 'tooltip' => __('Select how the parent field should be handled', 'importwp')
89 ]),
90 $this->register_field('Parent Reference Column', '_parent_ref', [
91 'condition' => ['_parent_type', '==', 'column'],
92 'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp')
93 ]),
94 $this->register_field('Custom Field Key', '_custom_field', [
95 'condition' => ['_parent_type', '==', 'custom_field'],
96 'tooltip' => __('Enter the name of the custom field.', 'importwp')
97 ])
98 ]),
99 $this->register_field('Alias of', 'alias_of', [
100 'tooltip' => __('Slug of the term to make this term an alias of', 'importwp')
101 ]),
102 ])
103 ];
104 }
105
106 public function register_settings()
107 {
108 }
109
110 /**
111 * Alter fields before they are parsed
112 *
113 * @param array $fields
114 * @return array
115 */
116 public function field_map($fields)
117 {
118 if ($this->importer->isEnabledField('term._parent') && $fields['term._parent._parent_type'] === 'column' && !empty($fields["term._parent._parent_ref"])) {
119 $fields['_iwp_ref_term_parent'] = $fields["term._parent._parent_ref"];
120 $this->virtual_fields[] = '_iwp_ref_term_parent';
121 }
122 return $fields;
123 }
124
125 /**
126 * Process data before record is importer.
127 *
128 * Alter data that is passed to the mapper.
129 *
130 * @param ParsedData $data
131 * @return ParsedData
132 */
133 public function pre_process(ParsedData $data)
134 {
135 $data = parent::pre_process($data);
136
137 $term_field_map = [
138 'term_id' => $data->getValue('term.term_id'),
139 'name' => $data->getValue('term.name'),
140 'alias_of' => $data->getValue('term.alias_of'),
141 'description' => $data->getValue('term.description'),
142 'parent' => $data->getValue('term._parent.parent'),
143 'slug' => $data->getValue('term.slug'),
144 ];
145
146 // remove fields that have not been set
147 foreach ($term_field_map as $field_key => $field_map) {
148
149 if ($field_map === false) {
150 unset($term_field_map[$field_key]);
151 continue;
152 }
153 }
154
155 $optional_fields = [
156 'term_id',
157 'alias_of',
158 'description',
159 'slug'
160 ];
161
162 foreach ($optional_fields as $optional_field) {
163 if (true !== $this->importer->isEnabledField('term.' . $optional_field)) {
164 unset($term_field_map[$optional_field]);
165 }
166 }
167
168 if (true !== $this->importer->isEnabledField('term._parent')) {
169 unset($term_field_map['parent']);
170 }
171
172 if (!empty($this->virtual_fields)) {
173 foreach ($this->virtual_fields as $virtual_field) {
174 $value = $data->getValue($virtual_field);
175 if (false !== $value) {
176 $term_field_map[$virtual_field] = $value;
177 }
178 }
179 }
180
181 if ($this->importer->isEnabledField('term._parent') && isset($term_field_map['parent'])) {
182
183 $parent_id = 0;
184 $parent_field_type = $data->getValue('term._parent._parent_type');
185 $taxonomy = $this->importer->getSetting('taxonomy');
186
187 if ($parent_field_type === 'name') {
188 // name
189 $term = get_term_by('name', $term_field_map['parent'], $taxonomy);
190 if ($term) {
191 $parent_id = intval($term->term_id);
192 }
193 } elseif ($parent_field_type === 'slug') {
194 // slug
195 $term = get_term_by('slug', $term_field_map['parent'], $taxonomy);
196 if ($term) {
197 $parent_id = intval($term->term_id);
198 }
199 } elseif ($parent_field_type === 'id') {
200 // ID
201 $parent_id = intval($term_field_map['parent']);
202 } elseif ($parent_field_type === 'column') {
203
204 // reference column
205 $temp_id = $this->get_term_by_cf('_iwp_ref_term_parent', $term_field_map['parent']);
206 if (intval($temp_id > 0)) {
207 $parent_id = intval($temp_id);
208 }
209 } elseif ($parent_field_type === 'custom_field') {
210
211 $parent_custom_field = $data->getValue('term._parent._custom_field');
212 $temp_id = $this->get_term_by_cf($parent_custom_field, $term_field_map['parent']);
213 if (intval($temp_id > 0)) {
214 $parent_id = intval($temp_id);
215 }
216 }
217
218 if ($parent_id > 0) {
219 $term_field_map['parent'] = $parent_id;
220 }
221 }
222
223 if ((!isset($term_field_map['slug']) || empty($term_field_map['slug'])) && isset($term_field_map['term']) && !empty($term_field_map['term'])) {
224 $term_field_map['slug'] = sanitize_title($term_field_map['term']);
225
226 // set flag to say slug has been generated
227 $data->add(['slug' => 'yes'], '_generated');
228 }
229
230 foreach ($term_field_map as $key => $value) {
231 $term_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
232 }
233
234 $data->replace($term_field_map, 'default');
235
236 return $data;
237 }
238
239 public function get_term_by_cf($field, $value)
240 {
241 $taxonomy = $this->importer->getSetting('taxonomy');
242 $terms = get_terms([
243 'taxonomy' => $taxonomy,
244 'fields' => 'ids',
245 'hide_empty' => false,
246 'meta_query' => [
247 [
248 'key' => $field,
249 'value' => $value,
250 'compare' => '='
251 ]
252 ]
253 ]);
254
255 if (!is_wp_error($terms)) {
256 return $terms[0];
257 }
258
259 return false;
260 }
261
262 /**
263 * Get list of posts
264 *
265 * @return array|\WP_Error
266 */
267 public function get_term_parent_options($importer_data)
268 {
269 $taxonomy = $importer_data->getSetting('taxonomy');
270 $terms = get_terms([
271 'taxonomy' => $taxonomy,
272 'hide_empty' => false,
273 ]);
274
275 $result = [];
276 /**
277 * @var \WP_Term[] $terms
278 */
279 foreach ($terms as $term) {
280 $result[] = [
281 'value' => '' . $term->term_id,
282 'label' => $term->name
283 ];
284 }
285 return $result;
286 }
287
288 /**
289 * Convert fields/headings to data map
290 *
291 * @param mixed $fields
292 * @param ImporterModel $importer
293 * @return array
294 */
295 public function generate_field_map($fields, $importer)
296 {
297 $result = parent::generate_field_map($fields, $importer);
298 $map = $result['map'];
299 $enabled = $result['enabled'];
300
301 $parent = [];
302
303 foreach ($fields as $index => $field) {
304 if (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) {
305
306 // Capture parent
307 // parent.id,parent.name,parent.slug
308 if (!isset($parent['map'])) {
309 $parent['map'] = [];
310 }
311
312 $parent['map'][$matches[1]] = sprintf('{%d}', $index);
313 } elseif (isset($this->field_map[$field])) {
314
315 // Handle core fields
316 $field_key = $this->field_map[$field];
317 $map[$field_key] = sprintf('{%d}', $index);
318
319 if (in_array($field, $this->optional_fields)) {
320 $enabled[] = $field_key;
321 }
322
323 if (in_array($field, ['role'])) {
324 $map[$field_key . '._enable_text'] = 'yes';
325 }
326 }
327 }
328
329 if (!empty($parent)) {
330
331 // parent.id,parent.name,parent.slug
332 $enabled_key = 'term._parent';
333 if (isset($parent['map']['name'])) {
334
335 $enabled[] = $enabled_key;
336 $map['term._parent.parent'] = $parent['map']['name'];
337 $map['term._parent.parent._enable_text'] = 'yes';
338 $map['term._parent._parent_type'] = 'name';
339 } elseif (isset($parent['map']['slug'])) {
340
341 $enabled[] = $enabled_key;
342 $map['term._parent.parent'] = $parent['map']['slug'];
343 $map['term._parent.parent._enable_text'] = 'yes';
344 $map['term._parent._parent_type'] = 'slug';
345 } elseif (isset($parent['map']['id'])) {
346
347 $enabled[] = $enabled_key;
348 $map['term._parent.parent'] = $parent['map']['name'];
349 $map['term._parent.parent._enable_text'] = 'yes';
350 $map['term._parent._parent_type'] = 'id';
351 }
352 }
353
354 return [
355 'map' => $map,
356 'enabled' => $enabled
357 ];
358 }
359 }
360