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

235 lines 7.6 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 private 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 ]);
110
111 $this->query = new \WP_Term_Query($query_args);
112
113 return $this->found_records() > 0;
114 }
115
116 public function found_records()
117 {
118 return count($this->query->terms);
119 }
120
121 public function setup($i)
122 {
123 $this->record = (array)$this->query->terms[$i];
124 $this->record['custom_fields'] = get_term_meta($this->record['term_id']);
125
126 $parent = get_term($this->record['parent'], $this->taxonomy);
127 if (!is_wp_error($parent)) {
128 $this->record['parent'] = [
129 'term_id' => $parent->term_id,
130 'slug' => $parent->slug,
131 'name' => $parent->name,
132 'parent' => $parent->parent
133 ];
134 } else {
135 $this->record['parent'] = [
136 'term_id' => '',
137 'slug' => '',
138 'name' => '',
139 'parent' => ''
140 ];
141 }
142
143 $ancestor_ids = get_ancestors($this->record['term_id'], $this->taxonomy, 'taxonomy');
144 $ancestor_ids = array_reverse($ancestor_ids);
145
146 $this->record['anscestors'] = [];
147
148 if ($ancestor_ids) {
149 foreach ($ancestor_ids as $ancestor_id) {
150
151 $ancestor = get_term($ancestor_id, $this->taxonomy);
152 if (!is_wp_error($ancestor)) {
153 $this->record['anscestors'][] = [
154 'term_id' => $ancestor->term_id,
155 'slug' => $ancestor->slug,
156 'name' => $ancestor->name,
157 'parent' => $ancestor->parent
158 ];
159 }
160 }
161 }
162
163 return true;
164 }
165
166 public function get_field($column, $record, $meta)
167 {
168 // Core fields
169 $core = $this->get_core_fields();
170
171 $output = '';
172
173 if (preg_match('/^ewp_cf_(.*?)$/', $column, $matches) == 1) {
174
175 $meta_key = $matches[1];
176 if (isset($meta[$meta_key])) {
177 $output = $meta[$meta_key];
178 }
179 } else {
180
181 if (in_array($column, $core, true)) {
182 $output = $record->{$column};
183 } else {
184 switch ($column) {
185 case 'parent_id':
186 /**
187 * @var \WP_Term $record
188 */
189 $parent = get_term($record->parent, $this->taxonomy);
190 if (!is_wp_error($parent)) {
191 $output = $parent->term_id;
192 }
193 break;
194 case 'parent_slug':
195 $parent = get_term($record->parent, $this->taxonomy);
196 if (!is_wp_error($parent)) {
197 $output = $parent->slug;
198 }
199 break;
200 case 'parent_name':
201 $parent = get_term($record->parent, $this->taxonomy);
202 if (!is_wp_error($parent)) {
203 $output = $parent->name;
204 }
205 break;
206 case 'parent_anscestors_id':
207 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
208 $output = implode(' > ', $parents);
209 break;
210 case 'parent_anscestors_slug':
211 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
212 $parents = array_filter(array_map(function ($item) {
213 $term = get_term($item, $this->taxonomy);
214 return !is_wp_error($term) ? $term->slug : '';
215 }, $parents));
216 $output = implode(' > ', $parents);
217 break;
218 case 'parent_anscestors_name':
219 $parents = get_ancestors($record->term_id, $this->taxonomy, 'taxonomy');
220 $parents = array_filter(array_map(function ($item) {
221 $term = get_term($item, $this->taxonomy);
222 return !is_wp_error($term) ? $term->name : '';
223 }, $parents));
224
225 $output = implode(' > ', $parents);
226 break;
227 }
228 }
229 }
230
231 $output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta);
232 return $output;
233 }
234 }
235