PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.12
Import WP – CSV & XML Import Export for WordPress v2.7.12
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.7.12, at class/Common/Exporter/Mapper/TaxMapper.php

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