PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
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 / Exporter / Mapper / TaxMapper.php

TaxMapper.php in Import WP – CSV & XML Import Export for WordPress 2.14.23, at class/Common/Exporter/Mapper/TaxMapper.php

274 lines 9.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\Exporter\Mapper;
4
5 use ImportWP\Common\Exporter\ExporterRecord;
6 use ImportWP\Common\Exporter\MapperInterface;
7
8 class TaxMapper extends AbstractMapper implements MapperInterface
9 {
10
11 /**
12 * @var \WP_Term_Query
13 */
14 private $query;
15 private $taxonomy;
16
17 public function get_core_fields()
18 {
19 return array(
20 'term_id',
21 'name',
22 'slug',
23 'description',
24 );
25 }
26
27 public function __construct($taxonomy)
28 {
29 $this->taxonomy = $taxonomy;
30
31 add_filter('iwp/exporter_record/tax', [$this, 'get_record_data'], 10, 3);
32 }
33
34 public function get_fields()
35 {
36 /**
37 * @var \WPDB
38 */
39 global $wpdb;
40
41 $fields = [
42 'key' => 'main',
43 'label' => $this->taxonomy,
44 'loop' => true,
45 'fields' => [],
46 'children' => [
47 'parent' => [
48 'key' => 'parent',
49 'label' => __('Parent', 'jc-importer'),
50 'loop' => false,
51 'fields' => [],
52 'children' => []
53 ],
54 'anscestors' => [
55 'key' => 'anscestors',
56 'label' => __('Anscestors', 'jc-importer'),
57 'loop' => true,
58 'fields' => [],
59 'children' => []
60 ],
61 'custom_fields' => [
62 'key' => 'custom_fields',
63 'label' => __('Custom Fields', 'jc-importer'),
64 'loop' => true,
65 'loop_fields' => ['meta_key', 'meta_value'],
66 'fields' => [],
67 'children' => []
68 ]
69 ]
70
71 ];
72
73 $fields['fields'] = $this->get_core_fields();
74
75 // get taxonomy custom fields
76 global $wpdb;
77 $meta_fields = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_key FROM " . $wpdb->termmeta . " as tm INNER JOIN " . $wpdb->term_taxonomy . " as tt ON tm.term_id = tt.term_id WHERE tt.taxonomy = %s", [$this->taxonomy]));
78 $custom_file_fields = apply_filters('iwp/exporter/taxonomy/custom_file_id_fields', []);
79 foreach ($meta_fields as $field) {
80 $fields['children']['custom_fields']['fields'][] = $field;
81
82 if (in_array($field, $custom_file_fields)) {
83 $fields['children']['custom_fields']['fields'][] = $field . '::id';
84 $fields['children']['custom_fields']['fields'][] = $field . '::url';
85 $fields['children']['custom_fields']['fields'][] = $field . '::title';
86 $fields['children']['custom_fields']['fields'][] = $field . '::alt';
87 $fields['children']['custom_fields']['fields'][] = $field . '::caption';
88 $fields['children']['custom_fields']['fields'][] = $field . '::description';
89 }
90 }
91
92 $fields['children']['parent']['fields'] = [
93 'term_id',
94 'slug',
95 'name',
96 'parent'
97 ];
98
99 $fields['children']['anscestors']['fields'] = [
100 'term_id',
101 'slug',
102 'name',
103 'parent'
104 ];
105
106 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/taxonomy/custom_field_list', $fields['children']['custom_fields']['fields'], $this->taxonomy);
107 $fields = apply_filters('iwp/exporter/taxonomy/fields', $fields, $this->taxonomy);
108
109 return $this->parse_fields($fields);
110 }
111
112 public function have_records($exporter_id)
113 {
114 $query_args = [];
115 $query_args = apply_filters('iwp/exporter/tax_query', $query_args);
116 $query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query', $exporter_id), $query_args);
117 $query_args = apply_filters(sprintf('iwp/exporter/tax_query/%s', $this->taxonomy), $query_args);
118 $query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query/%s', $exporter_id, $this->taxonomy), $query_args);
119
120 $query_args = wp_parse_args($query_args, [
121 'taxonomy' => $this->taxonomy,
122 'hide_empty' => false,
123 'fields' => 'ids'
124 ]);
125
126 $this->query = new \WP_Term_Query($query_args);
127 $this->items = (array)$this->query->terms;
128
129 return $this->found_records() > 0;
130 }
131
132 public function found_records()
133 {
134 return count($this->items);
135 }
136
137 public function get_records()
138 {
139 return $this->items;
140 }
141
142 public function setup($i)
143 {
144 $term = get_term($this->items[$i], '', ARRAY_A);
145
146 // Remove WP_Term attribute name conflict
147 $term['_parent'] = $term['parent'];
148 unset($term['parent']);
149
150 $this->record = new ExporterRecord($term, 'tax');
151 $this->record = apply_filters('iwp/exporter/taxonomy/setup_data', $this->record, $this->taxonomy);
152 return true;
153 }
154
155 public function get_field($column, $record, $meta)
156 {
157 // Core fields
158 $core = $this->get_core_fields();
159
160 $output = '';
161
162 if (preg_match('/^ewp_cf_(.*?)$/', $column, $matches) == 1) {
163
164 $meta_key = $matches[1];
165 if (isset($meta[$meta_key])) {
166 $output = $meta[$meta_key];
167 }
168 } else {
169
170 if (in_array($column, $core, true)) {
171 $output = $record->{$column};
172 } else {
173 switch ($column) {
174 case 'parent_id':
175 /**
176 * @var \WP_Term $record
177 */
178 $parent = get_term($record->parent, $this->taxonomy);
179 if (!is_wp_error($parent)) {
180 $output = $parent->term_id;
181 }
182 break;
183 case 'parent_slug':
184 $parent = get_term($record->parent, $this->taxonomy);
185 if (!is_wp_error($parent)) {
186 $output = $parent->slug;
187 }
188 break;
189 case 'parent_name':
190 $parent = get_term($record->parent, $this->taxonomy);
191 if (!is_wp_error($parent)) {
192 $output = $parent->name;
193 }
194 break;
195 case 'parent_anscestors_id':
196 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
197 $output = implode(' > ', $parents);
198 break;
199 case 'parent_anscestors_slug':
200 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
201 $parents = array_filter(array_map(function ($item) {
202 $term = get_term($item, $this->taxonomy);
203 return !is_wp_error($term) ? $term->slug : '';
204 }, $parents));
205 $output = implode(' > ', $parents);
206 break;
207 case 'parent_anscestors_name':
208 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
209 $parents = array_filter(array_map(function ($item) {
210 $term = get_term($item, $this->taxonomy);
211 return !is_wp_error($term) ? $term->name : '';
212 }, $parents));
213
214 $output = implode(' > ', $parents);
215 break;
216 }
217 }
218 }
219
220 $output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta);
221 return $output;
222 }
223
224 public function get_record_data($value, $key, $record)
225 {
226 switch ($key) {
227 case 'custom_fields':
228 $value = get_term_meta($record['term_id']);
229 $value = $this->modify_custom_field_data($value, 'taxonomy');
230 break;
231 case 'parent':
232 $parent = get_term($record['_parent'], $this->taxonomy);
233 if (!is_wp_error($parent)) {
234 $value = [
235 'term_id' => $parent->term_id,
236 'slug' => $parent->slug,
237 'name' => $parent->name,
238 'parent' => $parent->parent
239 ];
240 } else {
241 $value = [
242 'term_id' => '',
243 'slug' => '',
244 'name' => '',
245 'parent' => ''
246 ];
247 }
248 break;
249 case 'anscestors':
250 $ancestor_ids = get_ancestors($record['term_id'], $this->taxonomy, 'taxonomy');
251 $ancestor_ids = array_reverse($ancestor_ids);
252
253 $value = [];
254
255 if ($ancestor_ids) {
256 foreach ($ancestor_ids as $ancestor_id) {
257 $ancestor = get_term($ancestor_id, $this->taxonomy);
258 if (!is_wp_error($ancestor)) {
259 $value[] = [
260 'term_id' => $ancestor->term_id,
261 'slug' => $ancestor->slug,
262 'name' => $ancestor->name,
263 'parent' => $ancestor->parent
264 ];
265 }
266 }
267 }
268 break;
269 }
270
271 return $value;
272 }
273 }
274