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/PostMapper.php +113 -62 2.7.5 → 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 PostMapper extends AbstractMapper implements MapperInterface
8 9 {
@@ -15,8 +16,10 @@
15 16
16 17 public function __construct($post_type = 'post')
17 18 {
18 19 $this->post_type = $post_type;
20 +
21 + add_filter('iwp/exporter_record/post', [$this, 'get_record_data'], 10, 3);
19 22 }
20 23
21 24 public function get_core_fields()
22 25 {
@@ -62,15 +65,15 @@
62 65 global $wpdb;
63 66
64 67 $fields = [
65 68 'key' => 'main',
66 - 'label' => 'Post',
69 + 'label' => __('Post', 'jc-importer'),
67 70 'loop' => true,
68 71 'fields' => [],
69 72 'children' => [
70 73 'author' => [
71 74 'key' => 'author',
72 - 'label' => 'Author',
75 + 'label' => __('Author', 'jc-importer'),
73 76 'loop' => false,
74 77 'fields' => [],
75 78 'children' => []
76 79 ],
@@ -75,9 +78,9 @@
75 78 'children' => []
76 79 ],
77 80 'image' => [
78 81 'key' => 'image',
79 - 'label' => 'Featured Image',
82 + 'label' => __('Featured Image', 'jc-importer'),
80 83 'loop' => false,
81 84 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'],
82 85 'children' => []
83 86 ],
@@ -82,9 +85,9 @@
82 85 'children' => []
83 86 ],
84 87 'parent' => [
85 88 'key' => 'parent',
86 - 'label' => 'Parent',
89 + 'label' => __('Parent', 'jc-importer'),
87 90 'loop' => false,
88 91 'fields' => ['id', 'name', 'slug'],
89 92 'children' => []
90 93 ],
@@ -89,9 +92,9 @@
89 92 'children' => []
90 93 ],
91 94 'custom_fields' => [
92 95 'key' => 'custom_fields',
93 - 'label' => 'Custom Fields',
96 + 'label' => __('Custom Fields', 'jc-importer'),
94 97 'loop' => true,
95 98 'loop_fields' => ['meta_key', 'meta_value'],
96 99 'fields' => [],
97 100 'children' => []
@@ -118,10 +121,21 @@
118 121 $fields['children']['author']['fields'][] = 'display_name';
119 122
120 123 // post_meta
121 124 $meta_fields = $wpdb->get_col(sprintf("SELECT DISTINCT meta_key FROM " . $wpdb->postmeta . " WHERE post_id IN (SELECT DISTINCT ID FROM " . $wpdb->posts . " WHERE post_type IN ('%s'))", implode("','", $post_types)));
125 + $custom_file_fields = apply_filters('iwp/exporter/post_type/custom_file_id_fields', []);
122 126 foreach ($meta_fields as $field) {
127 +
123 128 $fields['children']['custom_fields']['fields'][] = $field;
129 +
130 + if (in_array($field, $custom_file_fields)) {
131 + $fields['children']['custom_fields']['fields'][] = $field . '::id';
132 + $fields['children']['custom_fields']['fields'][] = $field . '::url';
133 + $fields['children']['custom_fields']['fields'][] = $field . '::title';
134 + $fields['children']['custom_fields']['fields'][] = $field . '::alt';
135 + $fields['children']['custom_fields']['fields'][] = $field . '::caption';
136 + $fields['children']['custom_fields']['fields'][] = $field . '::description';
137 + }
124 138 }
125 139
126 140 // taxonomies
127 141 $taxonomies = get_object_taxonomies($post_types[0], 'objects');
@@ -142,11 +156,15 @@
142 156 ];
143 157 }
144 158
145 159 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/post_type/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type);
146 - $fields = apply_filters('iwp/exporter/post_type/fields', $fields, $this->post_type);
160 + $fields = apply_filters('iwp/exporter/post_type/fields', $fields, $this->get_post_types_array());
147 161
148 - return $fields;
162 + if (in_array('attachment', $post_types)) {
163 + $fields['fields'][] = 'url';
164 + }
165 +
166 + return $this->parse_fields($fields);
149 167 }
150 168
151 169
152 170 public function have_records($exporter_id)
@@ -174,8 +192,9 @@
174 192 'order' => 'ASC'
175 193 ]);
176 194
177 195 $this->query = new \WP_Query($query_args);
196 + $this->items = $this->query->posts;
178 197
179 198 return $this->found_records() > 0;
180 199 }
181 200
@@ -180,72 +199,91 @@
180 199 }
181 200
182 201 public function found_records()
183 202 {
184 - return $this->query->post_count;
203 + return count($this->items);
185 204 }
186 205
206 + public function get_records()
207 + {
208 + return $this->items;
209 + }
187 210
188 - public function setup($i)
211 + public function get_record_data($value, $key, $record)
189 212 {
190 - $post = get_post($this->query->posts[$i], ARRAY_A);
191 - if (!$post) {
192 - return false;
193 - }
213 + switch ($key) {
214 + case 'post_thumbnail':
215 + $value = wp_get_attachment_url(get_post_thumbnail_id($record['ID']));
216 + break;
217 + case 'image':
194 218
195 - $this->record = $post;
196 - $this->record['post_thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($this->record['ID']));
197 - $this->record['custom_fields'] = get_post_meta($post['ID']);
219 + $value = [
220 + 'id' => '',
221 + 'url' => '',
222 + 'title' => '',
223 + 'alt' => '',
224 + 'caption' => '',
225 + 'description' => ''
226 + ];
198 227
199 - $user = get_userdata($post['post_author']);
200 - $this->record['author'] = (array)$user->data;
228 + $thumbnail_id = intval(get_post_thumbnail_id($record['ID']));
229 + if ($thumbnail_id > 0) {
230 + $attachment = get_post($thumbnail_id, ARRAY_A);
231 + if ($attachment) {
201 232
202 - $thumbnail_id = intval(get_post_thumbnail_id($this->record['ID']));
203 - if ($thumbnail_id > 0) {
204 -
205 - $attachment = get_post($thumbnail_id, ARRAY_A);
206 - $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
207 -
208 - $this->record['image'] = [
209 - 'id' => $thumbnail_id,
210 - 'url' => wp_get_attachment_url($thumbnail_id),
211 - 'title' => $attachment['post_title'],
212 - 'alt' => $alt,
213 - 'caption' => $attachment['post_excerpt'],
214 - 'description' => $attachment['post_content']
215 - ];
216 - } else {
217 - $this->record['image'] = [
218 - 'id' => '',
219 - 'url' => '',
220 - 'title' => '',
221 - 'alt' => '',
222 - 'caption' => '',
223 - 'description' => ''
224 - ];
233 + $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
234 + $value = [
235 + 'id' => $thumbnail_id,
236 + 'url' => wp_get_attachment_url($thumbnail_id),
237 + 'title' => $attachment['post_title'],
238 + 'alt' => $alt,
239 + 'caption' => $attachment['post_excerpt'],
240 + 'description' => $attachment['post_content']
241 + ];
242 + }
243 + }
244 + break;
245 + case 'custom_fields':
246 + $value = get_post_meta($record['ID']);
247 + $value = $this->modify_custom_field_data($value, 'post_type');
248 + break;
249 + case 'author':
250 + $user = get_userdata($record['post_author']);
251 + if ($user) {
252 + $value = (array)$user->data;
253 + }
254 + break;
255 + case 'parent':
256 + $parent = get_post_parent($record['ID']);
257 + if ($parent) {
258 + $value = [
259 + 'id' => $parent->ID,
260 + 'name' => $parent->post_title,
261 + 'slug' => $parent->post_name
262 + ];
263 + } else {
264 + $value = [
265 + 'id' => '',
266 + 'name' => '',
267 + 'slug' => ''
268 + ];
269 + }
270 + break;
271 + case 'url':
272 + if ($record['post_type'] === 'attachment') {
273 + $value = wp_get_attachment_url($record['ID']);
274 + }
275 + break;
225 276 }
226 277
227 - $parent = get_post_parent($this->record['ID']);
228 - if ($parent) {
229 - $this->record['parent'] = [
230 - 'id' => $parent->ID,
231 - 'name' => $parent->post_title,
232 - 'slug' => $parent->post_name
233 - ];
234 - } else {
235 - $this->record['parent'] = [
236 - 'id' => '',
237 - 'name' => '',
238 - 'slug' => ''
239 - ];
240 - }
278 + if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) {
279 + $taxonomy = $tax_match[1];
241 280
242 - // taxonomies
243 - $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects');
244 - foreach (array_keys($taxonomies) as $taxonomy) {
245 - $tmp = [];
281 + $taxonomies = get_object_taxonomies($record['post_type'], 'objects');
282 + if (!in_array($taxonomy, array_keys($taxonomies))) {
283 + return $value;
284 + }
246 285
247 - $terms = wp_get_object_terms($post['ID'], $taxonomy);
248 286 $tmp = [
249 287 'id' => [],
250 288 'slug' => [],
251 289 'name' => [],
@@ -252,8 +290,9 @@
252 290 'hierarchy::id' => [],
253 291 'hierarchy::name' => [],
254 292 'hierarchy::slug' => [],
255 293 ];
294 + $terms = wp_get_object_terms($record['ID'], $taxonomy);
256 295
257 296 if (!empty($terms)) {
258 297 foreach ($terms as $term) {
259 298
@@ -264,9 +303,8 @@
264 303 $tmp['slug'][] = $term->slug;
265 304 $tmp['name'][] = $term->name;
266 305
267 306 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
268 - // $ancestor_ids = array_reverse($ancestor_ids);
269 307
270 308 $ancestor_tmp = [
271 309 'id' => [$term->term_id],
272 310 'name' => [$term->name],
@@ -295,11 +333,24 @@
295 333 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
296 334 }
297 335 }
298 336
299 - $this->record['tax_' . $taxonomy] = $tmp;
337 + $value = $tmp;
300 338 }
301 339
340 + return $value;
341 + }
342 +
343 +
344 + public function setup($i)
345 + {
346 + $post = get_post($this->items[$i], ARRAY_A);
347 + if (!$post) {
348 + return false;
349 + }
350 +
351 + // Add the base post item
352 + $this->record = new ExporterRecord($post, 'post');
302 353 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
303 354
304 355 return true;
305 356 }