PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.9.0
Import WP – CSV & XML Import Export for WordPress v2.9.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
jc-importer / class / Common / Exporter / Mapper / PostMapper.php

PostMapper.php in Import WP – CSV & XML Import Export for WordPress 2.9.0, at class/Common/Exporter/Mapper/PostMapper.php

356 lines 11.9 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\ExporterRecord;
6 use ImportWP\Common\Exporter\MapperInterface;
7
8 class PostMapper extends AbstractMapper implements MapperInterface
9 {
10
11 /**
12 * @var WP_Query
13 */
14 protected $query;
15 protected $post_type;
16
17 public function __construct($post_type = 'post')
18 {
19 $this->post_type = $post_type;
20
21 add_filter('iwp/exporter_record/post', [$this, 'get_record_data'], 10, 3);
22 }
23
24 public function get_core_fields()
25 {
26 return array(
27 'ID',
28 'post_author',
29 'post_date',
30 'post_date_gmt',
31 'post_content',
32 'post_title',
33 'post_excerpt',
34 'post_status',
35 'comment_status',
36 'ping_status',
37 'post_password',
38 'post_name',
39 'to_ping',
40 'pinged',
41 'post_modified',
42 'post_modified_gmt',
43 'post_content_filtered',
44 'post_parent',
45 'guid',
46 'menu_order',
47 'post_type',
48 'post_mime_type',
49 'comment_count',
50 );
51 }
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
60 public function get_fields()
61 {
62 /**
63 * @var \WPDB
64 */
65 global $wpdb;
66
67 $fields = [
68 'key' => 'main',
69 'label' => 'Post',
70 'loop' => true,
71 'fields' => [],
72 'children' => [
73 'author' => [
74 'key' => 'author',
75 'label' => 'Author',
76 'loop' => false,
77 'fields' => [],
78 'children' => []
79 ],
80 'image' => [
81 'key' => 'image',
82 'label' => 'Featured Image',
83 'loop' => false,
84 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'],
85 'children' => []
86 ],
87 'parent' => [
88 'key' => 'parent',
89 'label' => 'Parent',
90 'loop' => false,
91 'fields' => ['id', 'name', 'slug'],
92 'children' => []
93 ],
94 'custom_fields' => [
95 'key' => 'custom_fields',
96 'label' => 'Custom Fields',
97 'loop' => true,
98 'loop_fields' => ['meta_key', 'meta_value'],
99 'fields' => [],
100 'children' => []
101 ]
102 ]
103
104 ];
105
106 $fields['fields'] = $this->get_core_fields();
107
108 $post_types = $this->get_post_types_array();
109
110 // add post_thumbnail
111 if (post_type_supports($post_types[0], 'thumbnail')) {
112 $fields['fields'][] = 'post_thumbnail';
113 }
114
115 // post author
116 $fields['children']['author']['fields'][] = 'ID';
117 $fields['children']['author']['fields'][] = 'user_login';
118 $fields['children']['author']['fields'][] = 'user_nicename';
119 $fields['children']['author']['fields'][] = 'user_email';
120 $fields['children']['author']['fields'][] = 'user_url';
121 $fields['children']['author']['fields'][] = 'display_name';
122
123 // post_meta
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', []);
126 foreach ($meta_fields as $field) {
127
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 }
138 }
139
140 // taxonomies
141 $taxonomies = get_object_taxonomies($post_types[0], 'objects');
142 foreach ($taxonomies as $tax) {
143 $fields['children']['tax_' . $tax->name] = [
144 'key' => 'tax_' . $tax->name,
145 'label' => $tax->label,
146 'loop' => true,
147 'loop_fields' => ['id', 'name', 'slug'],
148 'fields' => [
149 'name',
150 'slug',
151 'id',
152 'hierarchy::id',
153 'hierarchy::name',
154 'hierarchy::slug',
155 ]
156 ];
157 }
158
159 $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/post_type/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type);
160 $fields = apply_filters('iwp/exporter/post_type/fields', $fields, $this->post_type);
161
162 if (in_array('attachment', $post_types)) {
163 $fields['fields'][] = 'url';
164 }
165
166 return $fields;
167 }
168
169
170 public function have_records($exporter_id)
171 {
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,
185 'posts_per_page' => -1,
186 'post_status' => 'any, trash, future',
187 'fields' => 'ids',
188 'cache_results' => false,
189 'update_post_meta_cache' => false,
190 'update_post_term_cache' => false,
191 'no_found_rows' => true,
192 'order' => 'ASC'
193 ]);
194
195 $this->query = new \WP_Query($query_args);
196 $this->items = $this->query->posts;
197
198 return $this->found_records() > 0;
199 }
200
201 public function found_records()
202 {
203 return count($this->items);
204 }
205
206 public function get_records()
207 {
208 return $this->items;
209 }
210
211 public function get_record_data($value, $key, $record)
212 {
213 switch ($key) {
214 case 'post_thumbnail':
215 $value = wp_get_attachment_url(get_post_thumbnail_id($record['ID']));
216 break;
217 case 'image':
218 $thumbnail_id = intval(get_post_thumbnail_id($record['ID']));
219 if ($thumbnail_id > 0) {
220
221 $attachment = get_post($thumbnail_id, ARRAY_A);
222 $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
223
224 $value = [
225 'id' => $thumbnail_id,
226 'url' => wp_get_attachment_url($thumbnail_id),
227 'title' => $attachment['post_title'],
228 'alt' => $alt,
229 'caption' => $attachment['post_excerpt'],
230 'description' => $attachment['post_content']
231 ];
232 } else {
233 $value = [
234 'id' => '',
235 'url' => '',
236 'title' => '',
237 'alt' => '',
238 'caption' => '',
239 'description' => ''
240 ];
241 }
242 break;
243 case 'custom_fields':
244 $value = get_post_meta($record['ID']);
245 $value = $this->modify_custom_field_data($value, 'post_type');
246 break;
247 case 'author':
248 $user = get_userdata($record['post_author']);
249 if ($user) {
250 $value = (array)$user->data;
251 }
252 break;
253 case 'parent':
254 $parent = get_post_parent($record['ID']);
255 if ($parent) {
256 $value = [
257 'id' => $parent->ID,
258 'name' => $parent->post_title,
259 'slug' => $parent->post_name
260 ];
261 } else {
262 $value = [
263 'id' => '',
264 'name' => '',
265 'slug' => ''
266 ];
267 }
268 break;
269 case 'url':
270 if ($record['post_type'] === 'attachment') {
271 $value = wp_get_attachment_url($record['ID']);
272 }
273 break;
274 }
275
276 if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) {
277 $taxonomy = $tax_match[1];
278
279 $taxonomies = get_object_taxonomies($record['post_type'], 'objects');
280 if (!in_array($taxonomy, array_keys($taxonomies))) {
281 return $value;
282 }
283
284 $tmp = [
285 'id' => [],
286 'slug' => [],
287 'name' => [],
288 'hierarchy::id' => [],
289 'hierarchy::name' => [],
290 'hierarchy::slug' => [],
291 ];
292 $terms = wp_get_object_terms($record['ID'], $taxonomy);
293
294 if (!empty($terms)) {
295 foreach ($terms as $term) {
296
297 /**
298 * @var \WP_Term $term
299 */
300 $tmp['id'][] = $term->term_id;
301 $tmp['slug'][] = $term->slug;
302 $tmp['name'][] = $term->name;
303
304 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
305
306 $ancestor_tmp = [
307 'id' => [$term->term_id],
308 'name' => [$term->name],
309 'slug' => [$term->slug]
310 ];
311
312 foreach ($ancestor_ids as $ancestor_id) {
313
314 $ancestor = get_term($ancestor_id, $taxonomy);
315 if (is_wp_error($ancestor)) {
316 continue;
317 }
318
319 $ancestor_tmp['id'][] = $ancestor->term_id;
320 $ancestor_tmp['name'][] = $ancestor->name;
321 $ancestor_tmp['slug'][] = $ancestor->slug;
322 }
323
324 $ancestor_tmp['id'] = array_reverse($ancestor_tmp['id']);
325 $tmp['hierarchy::id'][] = implode(' > ', $ancestor_tmp['id']);
326
327 $ancestor_tmp['name'] = array_reverse($ancestor_tmp['name']);
328 $tmp['hierarchy::name'][] = implode(' > ', $ancestor_tmp['name']);
329
330 $ancestor_tmp['slug'] = array_reverse($ancestor_tmp['slug']);
331 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
332 }
333 }
334
335 $value = $tmp;
336 }
337
338 return $value;
339 }
340
341
342 public function setup($i)
343 {
344 $post = get_post($this->items[$i], ARRAY_A);
345 if (!$post) {
346 return false;
347 }
348
349 // Add the base post item
350 $this->record = new ExporterRecord($post, 'post');
351 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
352
353 return true;
354 }
355 }
356