PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.14
Import WP – CSV & XML Import Export for WordPress v2.7.14
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.7.14, at class/Common/Importer/Template/TermTemplate.php

348 lines 11.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\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 ],
86 'type' => 'select',
87 'tooltip' => __('Select how the parent field should be handled', 'importwp')
88 ]),
89 $this->register_field('Parent Reference Column', '_parent_ref', [
90 'condition' => ['_parent_type', '==', 'column'],
91 'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp')
92 ])
93 ]),
94 $this->register_field('Alias of', 'alias_of', [
95 'tooltip' => __('Slug of the term to make this term an alias of', 'importwp')
96 ]),
97 ])
98 ];
99 }
100
101 public function register_settings()
102 {
103 }
104
105 /**
106 * Alter fields before they are parsed
107 *
108 * @param array $fields
109 * @return array
110 */
111 public function field_map($fields)
112 {
113 if ($this->importer->isEnabledField('term._parent') && $fields['term._parent._parent_type'] === 'column' && !empty($fields["term._parent._parent_ref"])) {
114 $fields['_iwp_ref_term_parent'] = $fields["term._parent._parent_ref"];
115 $this->virtual_fields[] = '_iwp_ref_term_parent';
116 }
117 return $fields;
118 }
119
120 /**
121 * Process data before record is importer.
122 *
123 * Alter data that is passed to the mapper.
124 *
125 * @param ParsedData $data
126 * @return ParsedData
127 */
128 public function pre_process(ParsedData $data)
129 {
130 $data = parent::pre_process($data);
131
132 $term_field_map = [
133 'term_id' => $data->getValue('term.term_id'),
134 'name' => $data->getValue('term.name'),
135 'alias_of' => $data->getValue('term.alias_of'),
136 'description' => $data->getValue('term.description'),
137 'parent' => $data->getValue('term._parent.parent'),
138 'slug' => $data->getValue('term.slug'),
139 ];
140
141 // remove fields that have not been set
142 foreach ($term_field_map as $field_key => $field_map) {
143
144 if ($field_map === false) {
145 unset($term_field_map[$field_key]);
146 continue;
147 }
148 }
149
150 $optional_fields = [
151 'term_id',
152 'alias_of',
153 'description',
154 'slug'
155 ];
156
157 foreach ($optional_fields as $optional_field) {
158 if (true !== $this->importer->isEnabledField('term.' . $optional_field)) {
159 unset($term_field_map[$optional_field]);
160 }
161 }
162
163 if (true !== $this->importer->isEnabledField('term._parent')) {
164 unset($term_field_map['parent']);
165 }
166
167 if (!empty($this->virtual_fields)) {
168 foreach ($this->virtual_fields as $virtual_field) {
169 $value = $data->getValue($virtual_field);
170 if (false !== $value) {
171 $term_field_map[$virtual_field] = $value;
172 }
173 }
174 }
175
176 if ($this->importer->isEnabledField('term._parent') && isset($term_field_map['parent'])) {
177
178 $parent_id = 0;
179 $parent_field_type = $data->getValue('term._parent._parent_type');
180 $taxonomy = $this->importer->getSetting('taxonomy');
181
182 if ($parent_field_type === 'name') {
183 // name
184 $term = get_term_by('name', $term_field_map['parent'], $taxonomy);
185 if ($term) {
186 $parent_id = intval($term->term_id);
187 }
188 } elseif ($parent_field_type === 'slug') {
189 // slug
190 $term = get_term_by('slug', $term_field_map['parent'], $taxonomy);
191 if ($term) {
192 $parent_id = intval($term->term_id);
193 }
194 } elseif ($parent_field_type === 'id') {
195 // ID
196 $parent_id = intval($term_field_map['parent']);
197 } elseif ($parent_field_type === 'column') {
198
199 // reference column
200 $temp_id = $this->get_term_by_cf('term_parent', $term_field_map['parent']);
201 if (intval($temp_id > 0)) {
202 $parent_id = intval($temp_id);
203 }
204 }
205
206 if ($parent_id > 0) {
207 $term_field_map['parent'] = $parent_id;
208 }
209 }
210
211 if ((!isset($term_field_map['slug']) || empty($term_field_map['slug'])) && isset($term_field_map['term']) && !empty($term_field_map['term'])) {
212 $term_field_map['slug'] = sanitize_title($term_field_map['term']);
213
214 // set flag to say slug has been generated
215 $data->add(['slug' => 'yes'], '_generated');
216 }
217
218 foreach ($term_field_map as $key => $value) {
219 $term_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
220 }
221
222 $data->replace($term_field_map, 'default');
223
224 return $data;
225 }
226
227 public function get_term_by_cf($field, $value)
228 {
229 $taxonomy = $this->importer->getSetting('taxonomy');
230 $terms = get_terms([
231 'taxonomy' => $taxonomy,
232 'fields' => 'ids',
233 'hide_empty' => false,
234 'meta_query' => [
235 [
236 'key' => '_iwp_ref_' . $field,
237 'value' => $value,
238 'compare' => '='
239 ]
240 ]
241 ]);
242
243 if (!is_wp_error($terms)) {
244 return $terms[0];
245 }
246
247 return false;
248 }
249
250 /**
251 * Get list of posts
252 *
253 * @return array|\WP_Error
254 */
255 public function get_term_parent_options($importer_data)
256 {
257 $taxonomy = $importer_data->getSetting('taxonomy');
258 $terms = get_terms([
259 'taxonomy' => $taxonomy,
260 'hide_empty' => false,
261 ]);
262
263 $result = [];
264 /**
265 * @var \WP_Term[] $terms
266 */
267 foreach ($terms as $term) {
268 $result[] = [
269 'value' => '' . $term->term_id,
270 'label' => $term->name
271 ];
272 }
273 return $result;
274 }
275
276 /**
277 * Convert fields/headings to data map
278 *
279 * @param mixed $fields
280 * @param ImporterModel $importer
281 * @return array
282 */
283 public function generate_field_map($fields, $importer)
284 {
285 $result = parent::generate_field_map($fields, $importer);
286 $map = $result['map'];
287 $enabled = $result['enabled'];
288
289 $parent = [];
290
291 foreach ($fields as $index => $field) {
292 if (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) {
293
294 // Capture parent
295 // parent.id,parent.name,parent.slug
296 if (!isset($parent['map'])) {
297 $parent['map'] = [];
298 }
299
300 $parent['map'][$matches[1]] = sprintf('{%d}', $index);
301 } elseif (isset($this->field_map[$field])) {
302
303 // Handle core fields
304 $field_key = $this->field_map[$field];
305 $map[$field_key] = sprintf('{%d}', $index);
306
307 if (in_array($field, $this->optional_fields)) {
308 $enabled[] = $field_key;
309 }
310
311 if (in_array($field, ['role'])) {
312 $map[$field_key . '._enable_text'] = 'yes';
313 }
314 }
315 }
316
317 if (!empty($parent)) {
318
319 // parent.id,parent.name,parent.slug
320 $enabled_key = 'term._parent';
321 if (isset($parent['map']['name'])) {
322
323 $enabled[] = $enabled_key;
324 $map['term._parent.parent'] = $parent['map']['name'];
325 $map['term._parent.parent._enable_text'] = 'yes';
326 $map['term._parent._parent_type'] = 'name';
327 } elseif (isset($parent['map']['slug'])) {
328
329 $enabled[] = $enabled_key;
330 $map['term._parent.parent'] = $parent['map']['slug'];
331 $map['term._parent.parent._enable_text'] = 'yes';
332 $map['term._parent._parent_type'] = 'slug';
333 } elseif (isset($parent['map']['id'])) {
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'] = 'id';
339 }
340 }
341
342 return [
343 'map' => $map,
344 'enabled' => $enabled
345 ];
346 }
347 }
348