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

260 lines 8.5 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\EventHandler;
8
9 class TermTemplate extends Template implements TemplateInterface
10 {
11 protected $name = 'Term';
12 protected $mapper = 'term';
13
14 public function __construct(EventHandler $event_handler)
15 {
16 parent::__construct($event_handler);
17
18 $this->groups[] = 'term';
19 $this->field_options = array_merge($this->field_options, [
20 'term._parent.parent' => [$this, 'get_term_parent_options'],
21 ]);
22 }
23
24 public function register_options()
25 {
26 $options = [
27 ['value' => '', 'label' => 'Choose a taxonomy']
28 ];
29 foreach (get_taxonomies(array(), 'names') as $key => $tax) {
30 $options[] = ['value' => $key, 'label' => $tax];
31 }
32
33 return [
34 $this->register_field('Taxonomy', 'taxonomy', [
35 'options' => $options
36 ])
37 ];
38 }
39
40 public function register()
41 {
42 return [
43 $this->register_group('Term', 'term', [
44 $this->register_field('ID', 'term_id', [
45 'tooltip' => __('ID is only used to reference existing records', 'importwp')
46 ]),
47 $this->register_core_field('Name', 'name', [
48 'tooltip' => __('The term name', 'importwp')
49 ]),
50 $this->register_field('Description', 'description', [
51 'tooltip' => __('The term description', 'importwp')
52 ]),
53 $this->register_field('Slug', 'slug', [
54 'tooltip' => __('The term slug to use', 'importwp')
55 ]),
56 $this->register_group('Parent Settings', '_parent', [
57 $this->register_field('Parent', 'parent', [
58 'default' => '',
59 'options' => 'callback',
60 'tooltip' => __('The id of the parent term', 'importwp')
61 ]),
62 $this->register_field('Parent Field Type', '_parent_type', [
63 'default' => 'id',
64 'options' => [
65 ['value' => 'id', 'label' => 'ID'],
66 ['value' => 'slug', 'label' => 'Slug'],
67 ['value' => 'name', 'label' => 'Name'],
68 ['value' => 'column', 'label' => 'Reference Column'],
69 ],
70 'type' => 'select',
71 'tooltip' => __('Select how the parent field should be handled', 'importwp')
72 ]),
73 $this->register_field('Parent Reference Column', '_parent_ref', [
74 'condition' => ['_parent_type', '==', 'column'],
75 'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp')
76 ])
77 ]),
78 $this->register_field('Alias of', 'alias_of', [
79 'tooltip' => __('Slug of the term to make this term an alias of', 'importwp')
80 ]),
81 ])
82 ];
83 }
84
85 public function register_settings()
86 {
87 }
88
89 /**
90 * Alter fields before they are parsed
91 *
92 * @param array $fields
93 * @return array
94 */
95 public function field_map($fields)
96 {
97 if ($this->importer->isEnabledField('term._parent') && $fields['term._parent._parent_type'] === 'column' && !empty($fields["term._parent._parent_ref"])) {
98 $fields['_iwp_ref_term_parent'] = $fields["term._parent._parent_ref"];
99 $this->virtual_fields[] = '_iwp_ref_term_parent';
100 }
101 return $fields;
102 }
103
104 /**
105 * Process data before record is importer.
106 *
107 * Alter data that is passed to the mapper.
108 *
109 * @param ParsedData $data
110 * @return ParsedData
111 */
112 public function pre_process(ParsedData $data)
113 {
114 $data = parent::pre_process($data);
115
116 $term_field_map = [
117 'term_id' => $data->getValue('term.term_id'),
118 'name' => $data->getValue('term.name'),
119 'alias_of' => $data->getValue('term.alias_of'),
120 'description' => $data->getValue('term.description'),
121 'parent' => $data->getValue('term._parent.parent'),
122 'slug' => $data->getValue('term.slug'),
123 ];
124
125 // remove fields that have not been set
126 foreach ($term_field_map as $field_key => $field_map) {
127
128 if ($field_map === false) {
129 unset($term_field_map[$field_key]);
130 continue;
131 }
132 }
133
134 $optional_fields = [
135 'term_id',
136 'alias_of',
137 'description',
138 'slug'
139 ];
140
141 foreach ($optional_fields as $optional_field) {
142 if (true !== $this->importer->isEnabledField('term.' . $optional_field)) {
143 unset($term_field_map[$optional_field]);
144 }
145 }
146
147 if (true !== $this->importer->isEnabledField('term._parent')) {
148 unset($term_field_map['parent']);
149 }
150
151 if (!empty($this->virtual_fields)) {
152 foreach ($this->virtual_fields as $virtual_field) {
153 $value = $data->getValue($virtual_field);
154 if (false !== $value) {
155 $term_field_map[$virtual_field] = $value;
156 }
157 }
158 }
159
160 if ($this->importer->isEnabledField('term._parent') && isset($term_field_map['parent'])) {
161
162 $parent_id = 0;
163 $parent_field_type = $data->getValue('term._parent._parent_type');
164 $taxonomy = $this->importer->getSetting('taxonomy');
165
166 if ($parent_field_type === 'name') {
167 // name
168 $term = get_term_by('name', $term_field_map['parent'], $taxonomy);
169 if ($term) {
170 $parent_id = intval($term->term_id);
171 }
172 } elseif ($parent_field_type === 'slug') {
173 // slug
174 $term = get_term_by('slug', $term_field_map['parent'], $taxonomy);
175 if ($term) {
176 $parent_id = intval($term->term_id);
177 }
178 } elseif ($parent_field_type === 'id') {
179 // ID
180 $parent_id = intval($term_field_map['parent']);
181 } elseif ($parent_field_type === 'column') {
182
183 // reference column
184 $temp_id = $this->get_term_by_cf('term_parent', $term_field_map['parent']);
185 if (intval($temp_id > 0)) {
186 $parent_id = intval($temp_id);
187 }
188 }
189
190 if ($parent_id > 0) {
191 $term_field_map['parent'] = $parent_id;
192 }
193 }
194
195 if ((!isset($term_field_map['slug']) || empty($term_field_map['slug'])) && isset($term_field_map['term']) && !empty($term_field_map['term'])) {
196 $term_field_map['slug'] = sanitize_title($term_field_map['term']);
197
198 // set flag to say slug has been generated
199 $data->add(['slug' => 'yes'], '_generated');
200 }
201
202 foreach ($term_field_map as $key => $value) {
203 $term_field_map[$key] = apply_filters('iwp/template/process_field', $value, $key, $this->importer);
204 }
205
206 $data->replace($term_field_map, 'default');
207
208 return $data;
209 }
210
211 public function get_term_by_cf($field, $value)
212 {
213 $taxonomy = $this->importer->getSetting('taxonomy');
214 $terms = get_terms([
215 'taxonomy' => $taxonomy,
216 'fields' => 'ids',
217 'hide_empty' => false,
218 'meta_query' => [
219 [
220 'key' => '_iwp_ref_' . $field,
221 'value' => $value,
222 'compare' => '='
223 ]
224 ]
225 ]);
226
227 if (!is_wp_error($terms)) {
228 return $terms[0];
229 }
230
231 return false;
232 }
233
234 /**
235 * Get list of posts
236 *
237 * @return array|\WP_Error
238 */
239 public function get_term_parent_options($importer_data)
240 {
241 $taxonomy = $importer_data->getSetting('taxonomy');
242 $terms = get_terms([
243 'taxonomy' => $taxonomy,
244 'hide_empty' => false,
245 ]);
246
247 $result = [];
248 /**
249 * @var \WP_Term[] $terms
250 */
251 foreach ($terms as $term) {
252 $result[] = [
253 'value' => '' . $term->term_id,
254 'label' => $term->name
255 ];
256 }
257 return $result;
258 }
259 }
260