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
jc-importer / class / Common / Exporter / Mapper / PostMapper.php

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

358 lines 12.0 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', 'jc-importer'),
70 'loop' => true,
71 'fields' => [],
72 'children' => [
73 'author' => [
74 'key' => 'author',
75 'label' => __('Author', 'jc-importer'),
76 'loop' => false,
77 'fields' => [],
78 'children' => []
79 ],
80 'image' => [
81 'key' => 'image',
82 'label' => __('Featured Image', 'jc-importer'),
83 'loop' => false,
84 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'],
85 'children' => []
86 ],
87 'parent' => [
88 'key' => 'parent',
89 'label' => __('Parent', 'jc-importer'),
90 'loop' => false,
91 'fields' => ['id', 'name', 'slug'],
92 'children' => []
93 ],
94 'custom_fields' => [
95 'key' => 'custom_fields',
96 'label' => __('Custom Fields', 'jc-importer'),
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->get_post_types_array());
161
162 if (in_array('attachment', $post_types)) {
163 $fields['fields'][] = 'url';
164 }
165
166 return $this->parse_fields($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
219 $value = [
220 'id' => '',
221 'url' => '',
222 'title' => '',
223 'alt' => '',
224 'caption' => '',
225 'description' => ''
226 ];
227
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) {
232
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;
276 }
277
278 if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) {
279 $taxonomy = $tax_match[1];
280
281 $taxonomies = get_object_taxonomies($record['post_type'], 'objects');
282 if (!in_array($taxonomy, array_keys($taxonomies))) {
283 return $value;
284 }
285
286 $tmp = [
287 'id' => [],
288 'slug' => [],
289 'name' => [],
290 'hierarchy::id' => [],
291 'hierarchy::name' => [],
292 'hierarchy::slug' => [],
293 ];
294 $terms = wp_get_object_terms($record['ID'], $taxonomy);
295
296 if (!empty($terms)) {
297 foreach ($terms as $term) {
298
299 /**
300 * @var \WP_Term $term
301 */
302 $tmp['id'][] = $term->term_id;
303 $tmp['slug'][] = $term->slug;
304 $tmp['name'][] = $term->name;
305
306 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
307
308 $ancestor_tmp = [
309 'id' => [$term->term_id],
310 'name' => [$term->name],
311 'slug' => [$term->slug]
312 ];
313
314 foreach ($ancestor_ids as $ancestor_id) {
315
316 $ancestor = get_term($ancestor_id, $taxonomy);
317 if (is_wp_error($ancestor)) {
318 continue;
319 }
320
321 $ancestor_tmp['id'][] = $ancestor->term_id;
322 $ancestor_tmp['name'][] = $ancestor->name;
323 $ancestor_tmp['slug'][] = $ancestor->slug;
324 }
325
326 $ancestor_tmp['id'] = array_reverse($ancestor_tmp['id']);
327 $tmp['hierarchy::id'][] = implode(' > ', $ancestor_tmp['id']);
328
329 $ancestor_tmp['name'] = array_reverse($ancestor_tmp['name']);
330 $tmp['hierarchy::name'][] = implode(' > ', $ancestor_tmp['name']);
331
332 $ancestor_tmp['slug'] = array_reverse($ancestor_tmp['slug']);
333 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
334 }
335 }
336
337 $value = $tmp;
338 }
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');
353 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
354
355 return true;
356 }
357 }
358