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 +138 -68 2.7.3 → 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 {
@@ -46,8 +49,15 @@
46 49 'comment_count',
47 50 );
48 51 }
49 52
53 + public function get_post_types_array()
54 + {
55 + $post_types = is_string($this->post_type) ? explode(',', $this->post_type) : (array)$this->post_type;
56 + $post_types = array_values(array_filter(array_map('trim', $post_types)));
57 + return $post_types;
58 + }
59 +
50 60 public function get_fields()
51 61 {
52 62 /**
53 63 * @var \WPDB
@@ -55,15 +65,15 @@
55 65 global $wpdb;
56 66
57 67 $fields = [
58 68 'key' => 'main',
59 - 'label' => 'Post',
69 + 'label' => __('Post', 'jc-importer'),
60 70 'loop' => true,
61 71 'fields' => [],
62 72 'children' => [
63 73 'author' => [
64 74 'key' => 'author',
65 - 'label' => 'Author',
75 + 'label' => __('Author', 'jc-importer'),
66 76 'loop' => false,
67 77 'fields' => [],
68 78 'children' => []
69 79 ],
@@ -68,9 +78,9 @@
68 78 'children' => []
69 79 ],
70 80 'image' => [
71 81 'key' => 'image',
72 - 'label' => 'Featured Image',
82 + 'label' => __('Featured Image', 'jc-importer'),
73 83 'loop' => false,
74 84 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'],
75 85 'children' => []
76 86 ],
@@ -75,9 +85,9 @@
75 85 'children' => []
76 86 ],
77 87 'parent' => [
78 88 'key' => 'parent',
79 - 'label' => 'Parent',
89 + 'label' => __('Parent', 'jc-importer'),
80 90 'loop' => false,
81 91 'fields' => ['id', 'name', 'slug'],
82 92 'children' => []
83 93 ],
@@ -82,9 +92,9 @@
82 92 'children' => []
83 93 ],
84 94 'custom_fields' => [
85 95 'key' => 'custom_fields',
86 - 'label' => 'Custom Fields',
96 + 'label' => __('Custom Fields', 'jc-importer'),
87 97 'loop' => true,
88 98 'loop_fields' => ['meta_key', 'meta_value'],
89 99 'fields' => [],
90 100 'children' => []
@@ -94,10 +104,9 @@
94 104 ];
95 105
96 106 $fields['fields'] = $this->get_core_fields();
97 107
98 - $post_types = is_string($this->post_type) ? explode(',', $this->post_type) : (array)$this->post_type;
99 - $post_types = array_values(array_filter(array_map('trim', $post_types)));
108 + $post_types = $this->get_post_types_array();
100 109
101 110 // add post_thumbnail
102 111 if (post_type_supports($post_types[0], 'thumbnail')) {
103 112 $fields['fields'][] = 'post_thumbnail';
@@ -112,10 +121,21 @@
112 121 $fields['children']['author']['fields'][] = 'display_name';
113 122
114 123 // post_meta
115 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', []);
116 126 foreach ($meta_fields as $field) {
127 +
117 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 + }
118 138 }
119 139
120 140 // taxonomies
121 141 $taxonomies = get_object_taxonomies($post_types[0], 'objects');
@@ -136,18 +156,33 @@
136 156 ];
137 157 }
138 158
139 159 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/post_type/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type);
140 - $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());
141 161
142 - return $fields;
162 + if (in_array('attachment', $post_types)) {
163 + $fields['fields'][] = 'url';
164 + }
165 +
166 + return $this->parse_fields($fields);
143 167 }
144 168
145 169
146 - public function have_records()
170 + public function have_records($exporter_id)
147 171 {
148 - $this->query = new \WP_Query(array(
149 - 'post_type' => $this->post_type,
172 + $post_types = $this->get_post_types_array();
173 +
174 + $query_args = [];
175 + $query_args = apply_filters('iwp/exporter/post_query', $query_args);
176 + $query_args = apply_filters(sprintf('iwp/exporter/%d/post_query', $exporter_id), $query_args);
177 +
178 + foreach ($post_types as $post_type) {
179 + $query_args = apply_filters(sprintf('iwp/exporter/post_query/%s', $post_type), $query_args);
180 + $query_args = apply_filters(sprintf('iwp/exporter/%d/post_query/%s', $exporter_id, $post_type), $query_args);
181 + }
182 +
183 + $query_args = wp_parse_args($query_args, [
184 + 'post_type' => $post_types,
150 185 'posts_per_page' => -1,
151 186 'post_status' => 'any, trash, future',
152 187 'fields' => 'ids',
153 188 'cache_results' => false,
@@ -154,79 +189,101 @@
154 189 'update_post_meta_cache' => false,
155 190 'update_post_term_cache' => false,
156 191 'no_found_rows' => true,
157 192 'order' => 'ASC'
158 - ));
193 + ]);
159 194
195 + $this->query = new \WP_Query($query_args);
196 + $this->items = $this->query->posts;
197 +
160 198 return $this->found_records() > 0;
161 199 }
162 200
163 201 public function found_records()
164 202 {
165 - return $this->query->post_count;
203 + return count($this->items);
166 204 }
167 205
206 + public function get_records()
207 + {
208 + return $this->items;
209 + }
168 210
169 - public function setup($i)
211 + public function get_record_data($value, $key, $record)
170 212 {
171 - $post = get_post($this->query->posts[$i], ARRAY_A);
172 - if (!$post) {
173 - return false;
174 - }
213 + switch ($key) {
214 + case 'post_thumbnail':
215 + $value = wp_get_attachment_url(get_post_thumbnail_id($record['ID']));
216 + break;
217 + case 'image':
175 218
176 - $this->record = $post;
177 - $this->record['post_thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($this->record['ID']));
178 - $this->record['custom_fields'] = get_post_meta($post['ID']);
219 + $value = [
220 + 'id' => '',
221 + 'url' => '',
222 + 'title' => '',
223 + 'alt' => '',
224 + 'caption' => '',
225 + 'description' => ''
226 + ];
179 227
180 - $user = get_userdata($post['post_author']);
181 - $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) {
182 232
183 - $thumbnail_id = intval(get_post_thumbnail_id($this->record['ID']));
184 - if ($thumbnail_id > 0) {
185 -
186 - $attachment = get_post($thumbnail_id, ARRAY_A);
187 - $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
188 -
189 - $this->record['image'] = [
190 - 'id' => $thumbnail_id,
191 - 'url' => wp_get_attachment_url($thumbnail_id),
192 - 'title' => $attachment['post_title'],
193 - 'alt' => $alt,
194 - 'caption' => $attachment['post_excerpt'],
195 - 'description' => $attachment['post_content']
196 - ];
197 - } else {
198 - $this->record['image'] = [
199 - 'id' => '',
200 - 'url' => '',
201 - 'title' => '',
202 - 'alt' => '',
203 - 'caption' => '',
204 - 'description' => ''
205 - ];
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;
206 276 }
207 277
208 - $parent = get_post_parent($this->record['ID']);
209 - if ($parent) {
210 - $this->record['parent'] = [
211 - 'id' => $parent->ID,
212 - 'name' => $parent->post_title,
213 - 'slug' => $parent->post_name
214 - ];
215 - } else {
216 - $this->record['parent'] = [
217 - 'id' => '',
218 - 'name' => '',
219 - 'slug' => ''
220 - ];
221 - }
278 + if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) {
279 + $taxonomy = $tax_match[1];
222 280
223 - // taxonomies
224 - $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects');
225 - foreach (array_keys($taxonomies) as $taxonomy) {
226 - $tmp = [];
281 + $taxonomies = get_object_taxonomies($record['post_type'], 'objects');
282 + if (!in_array($taxonomy, array_keys($taxonomies))) {
283 + return $value;
284 + }
227 285
228 - $terms = wp_get_object_terms($post['ID'], $taxonomy);
229 286 $tmp = [
230 287 'id' => [],
231 288 'slug' => [],
232 289 'name' => [],
@@ -233,8 +290,9 @@
233 290 'hierarchy::id' => [],
234 291 'hierarchy::name' => [],
235 292 'hierarchy::slug' => [],
236 293 ];
294 + $terms = wp_get_object_terms($record['ID'], $taxonomy);
237 295
238 296 if (!empty($terms)) {
239 297 foreach ($terms as $term) {
240 298
@@ -245,9 +303,8 @@
245 303 $tmp['slug'][] = $term->slug;
246 304 $tmp['name'][] = $term->name;
247 305
248 306 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
249 - // $ancestor_ids = array_reverse($ancestor_ids);
250 307
251 308 $ancestor_tmp = [
252 309 'id' => [$term->term_id],
253 310 'name' => [$term->name],
@@ -276,11 +333,24 @@
276 333 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
277 334 }
278 335 }
279 336
280 - $this->record['tax_' . $taxonomy] = $tmp;
337 + $value = $tmp;
281 338 }
282 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');
283 353 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
284 354
285 355 return true;
286 356 }