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

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