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

307 lines 9.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\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
179 return $this->found_records() > 0;
180 }
181
182 public function found_records()
183 {
184 return $this->query->post_count;
185 }
186
187
188 public function setup($i)
189 {
190 $post = get_post($this->query->posts[$i], ARRAY_A);
191 if (!$post) {
192 return false;
193 }
194
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']);
198
199 $user = get_userdata($post['post_author']);
200 $this->record['author'] = (array)$user->data;
201
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 ];
225 }
226
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 }
241
242 // taxonomies
243 $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects');
244 foreach (array_keys($taxonomies) as $taxonomy) {
245 $tmp = [];
246
247 $terms = wp_get_object_terms($post['ID'], $taxonomy);
248 $tmp = [
249 'id' => [],
250 'slug' => [],
251 'name' => [],
252 'hierarchy::id' => [],
253 'hierarchy::name' => [],
254 'hierarchy::slug' => [],
255 ];
256
257 if (!empty($terms)) {
258 foreach ($terms as $term) {
259
260 /**
261 * @var \WP_Term $term
262 */
263 $tmp['id'][] = $term->term_id;
264 $tmp['slug'][] = $term->slug;
265 $tmp['name'][] = $term->name;
266
267 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
268 // $ancestor_ids = array_reverse($ancestor_ids);
269
270 $ancestor_tmp = [
271 'id' => [$term->term_id],
272 'name' => [$term->name],
273 'slug' => [$term->slug]
274 ];
275
276 foreach ($ancestor_ids as $ancestor_id) {
277
278 $ancestor = get_term($ancestor_id, $taxonomy);
279 if (is_wp_error($ancestor)) {
280 continue;
281 }
282
283 $ancestor_tmp['id'][] = $ancestor->term_id;
284 $ancestor_tmp['name'][] = $ancestor->name;
285 $ancestor_tmp['slug'][] = $ancestor->slug;
286 }
287
288 $ancestor_tmp['id'] = array_reverse($ancestor_tmp['id']);
289 $tmp['hierarchy::id'][] = implode(' > ', $ancestor_tmp['id']);
290
291 $ancestor_tmp['name'] = array_reverse($ancestor_tmp['name']);
292 $tmp['hierarchy::name'][] = implode(' > ', $ancestor_tmp['name']);
293
294 $ancestor_tmp['slug'] = array_reverse($ancestor_tmp['slug']);
295 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
296 }
297 }
298
299 $this->record['tax_' . $taxonomy] = $tmp;
300 }
301
302 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
303
304 return true;
305 }
306 }
307