PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.14
Import WP – CSV & XML Import Export for WordPress v2.7.14
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.7.14, at class/Common/Exporter/Mapper/PostMapper.php

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