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

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

270 lines 9.3 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',
50 'loop' => false,
51 'fields' => [],
52 'children' => []
53 ],
54 'anscestors' => [
55 'key' => 'anscestors',
56 'label' => 'Anscestors',
57 'loop' => true,
58 'fields' => [],
59 'children' => []
60 ],
61 'custom_fields' => [
62 'key' => 'custom_fields',
63 'label' => 'Custom Fields',
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 $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 $this->record = new ExporterRecord($term, 'tax');
147 $this->record = apply_filters('iwp/exporter/taxonomy/setup_data', $this->record, $this->taxonomy);
148 return true;
149 }
150
151 public function get_field($column, $record, $meta)
152 {
153 // Core fields
154 $core = $this->get_core_fields();
155
156 $output = '';
157
158 if (preg_match('/^ewp_cf_(.*?)$/', $column, $matches) == 1) {
159
160 $meta_key = $matches[1];
161 if (isset($meta[$meta_key])) {
162 $output = $meta[$meta_key];
163 }
164 } else {
165
166 if (in_array($column, $core, true)) {
167 $output = $record->{$column};
168 } else {
169 switch ($column) {
170 case 'parent_id':
171 /**
172 * @var \WP_Term $record
173 */
174 $parent = get_term($record->parent, $this->taxonomy);
175 if (!is_wp_error($parent)) {
176 $output = $parent->term_id;
177 }
178 break;
179 case 'parent_slug':
180 $parent = get_term($record->parent, $this->taxonomy);
181 if (!is_wp_error($parent)) {
182 $output = $parent->slug;
183 }
184 break;
185 case 'parent_name':
186 $parent = get_term($record->parent, $this->taxonomy);
187 if (!is_wp_error($parent)) {
188 $output = $parent->name;
189 }
190 break;
191 case 'parent_anscestors_id':
192 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
193 $output = implode(' > ', $parents);
194 break;
195 case 'parent_anscestors_slug':
196 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
197 $parents = array_filter(array_map(function ($item) {
198 $term = get_term($item, $this->taxonomy);
199 return !is_wp_error($term) ? $term->slug : '';
200 }, $parents));
201 $output = implode(' > ', $parents);
202 break;
203 case 'parent_anscestors_name':
204 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
205 $parents = array_filter(array_map(function ($item) {
206 $term = get_term($item, $this->taxonomy);
207 return !is_wp_error($term) ? $term->name : '';
208 }, $parents));
209
210 $output = implode(' > ', $parents);
211 break;
212 }
213 }
214 }
215
216 $output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta);
217 return $output;
218 }
219
220 public function get_record_data($value, $key, $record)
221 {
222 switch ($key) {
223 case 'custom_fields':
224 $value = get_term_meta($record['term_id']);
225 $value = $this->modify_custom_field_data($value, 'taxonomy');
226 break;
227 case 'parent':
228 $parent = get_term($record['parent'], $this->taxonomy);
229 if (!is_wp_error($parent)) {
230 $value = [
231 'term_id' => $parent->term_id,
232 'slug' => $parent->slug,
233 'name' => $parent->name,
234 'parent' => $parent->parent
235 ];
236 } else {
237 $value = [
238 'term_id' => '',
239 'slug' => '',
240 'name' => '',
241 'parent' => ''
242 ];
243 }
244 break;
245 case 'anscestors':
246 $ancestor_ids = get_ancestors($record['term_id'], $this->taxonomy, 'taxonomy');
247 $ancestor_ids = array_reverse($ancestor_ids);
248
249 $value = [];
250
251 if ($ancestor_ids) {
252 foreach ($ancestor_ids as $ancestor_id) {
253 $ancestor = get_term($ancestor_id, $this->taxonomy);
254 if (!is_wp_error($ancestor)) {
255 $value[] = [
256 'term_id' => $ancestor->term_id,
257 'slug' => $ancestor->slug,
258 'name' => $ancestor->name,
259 'parent' => $ancestor->parent
260 ];
261 }
262 }
263 }
264 break;
265 }
266
267 return $value;
268 }
269 }
270