PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.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
← All changes | class/Common/Exporter/Mapper/TaxMapper.php +84 -45 2.7.7 → 2.15.0 View file →
@@ -1,8 +1,9 @@
1 1 <?php
2 2
3 3 namespace ImportWP\Common\Exporter\Mapper;
4 4
5 +use ImportWP\Common\Exporter\ExporterRecord;
5 6 use ImportWP\Common\Exporter\MapperInterface;
6 7
7 8 class TaxMapper extends AbstractMapper implements MapperInterface
8 9 {
@@ -12,9 +13,9 @@
12 13 */
13 14 private $query;
14 15 private $taxonomy;
15 16
16 - private function get_core_fields()
17 + public function get_core_fields()
17 18 {
18 19 return array(
19 20 'term_id',
20 21 'name',
@@ -25,8 +26,10 @@
25 26
26 27 public function __construct($taxonomy)
27 28 {
28 29 $this->taxonomy = $taxonomy;
30 +
31 + add_filter('iwp/exporter_record/tax', [$this, 'get_record_data'], 10, 3);
29 32 }
30 33
31 34 public function get_fields()
32 35 {
@@ -42,9 +45,9 @@
42 45 'fields' => [],
43 46 'children' => [
44 47 'parent' => [
45 48 'key' => 'parent',
46 - 'label' => 'Parent',
49 + 'label' => __('Parent', 'jc-importer'),
47 50 'loop' => false,
48 51 'fields' => [],
49 52 'children' => []
50 53 ],
@@ -49,9 +52,9 @@
49 52 'children' => []
50 53 ],
51 54 'anscestors' => [
52 55 'key' => 'anscestors',
53 - 'label' => 'Anscestors',
56 + 'label' => __('Anscestors', 'jc-importer'),
54 57 'loop' => true,
55 58 'fields' => [],
56 59 'children' => []
57 60 ],
@@ -56,9 +59,9 @@
56 59 'children' => []
57 60 ],
58 61 'custom_fields' => [
59 62 'key' => 'custom_fields',
60 - 'label' => 'Custom Fields',
63 + 'label' => __('Custom Fields', 'jc-importer'),
61 64 'loop' => true,
62 65 'loop_fields' => ['meta_key', 'meta_value'],
63 66 'fields' => [],
64 67 'children' => []
@@ -71,10 +74,20 @@
71 74
72 75 // get taxonomy custom fields
73 76 global $wpdb;
74 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', []);
75 79 foreach ($meta_fields as $field) {
76 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 + }
77 90 }
78 91
79 92 $fields['children']['parent']['fields'] = [
80 93 'term_id',
@@ -90,10 +103,11 @@
90 103 'parent'
91 104 ];
92 105
93 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);
94 108
95 - return $fields;
109 + return $this->parse_fields($fields);
96 110 }
97 111
98 112 public function have_records($exporter_id)
99 113 {
@@ -104,12 +118,14 @@
104 118 $query_args = apply_filters(sprintf('iwp/exporter/%d/tax_query/%s', $exporter_id, $this->taxonomy), $query_args);
105 119
106 120 $query_args = wp_parse_args($query_args, [
107 121 'taxonomy' => $this->taxonomy,
108 - 'hide_empty' => false
122 + 'hide_empty' => false,
123 + 'fields' => 'ids'
109 124 ]);
110 125
111 126 $this->query = new \WP_Term_Query($query_args);
127 + $this->items = (array)$this->query->terms;
112 128
113 129 return $this->found_records() > 0;
114 130 }
115 131
@@ -114,53 +130,26 @@
114 130 }
115 131
116 132 public function found_records()
117 133 {
118 - return count($this->query->terms);
134 + return count($this->items);
119 135 }
120 136
137 + public function get_records()
138 + {
139 + return $this->items;
140 + }
141 +
121 142 public function setup($i)
122 143 {
123 - $this->record = (array)$this->query->terms[$i];
124 - $this->record['custom_fields'] = get_term_meta($this->record['term_id']);
144 + $term = get_term($this->items[$i], '', ARRAY_A);
125 145
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 - }
146 + // Remove WP_Term attribute name conflict
147 + $term['_parent'] = $term['parent'];
148 + unset($term['parent']);
142 149
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 -
150 + $this->record = new ExporterRecord($term, 'tax');
151 + $this->record = apply_filters('iwp/exporter/taxonomy/setup_data', $this->record, $this->taxonomy);
163 152 return true;
164 153 }
165 154
166 155 public function get_field($column, $record, $meta)
@@ -229,6 +218,56 @@
229 218 }
230 219
231 220 $output = apply_filters('iwp/exporter/taxonomy/value', $output, $column, $record, $meta);
232 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;
233 272 }
234 273 }