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

288 lines 9.2 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_fields()
51 {
52 /**
53 * @var \WPDB
54 */
55 global $wpdb;
56
57 $fields = [
58 'key' => 'main',
59 'label' => 'Post',
60 'loop' => true,
61 'fields' => [],
62 'children' => [
63 'author' => [
64 'key' => 'author',
65 'label' => 'Author',
66 'loop' => false,
67 'fields' => [],
68 'children' => []
69 ],
70 'image' => [
71 'key' => 'image',
72 'label' => 'Featured Image',
73 'loop' => false,
74 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'],
75 'children' => []
76 ],
77 'parent' => [
78 'key' => 'parent',
79 'label' => 'Parent',
80 'loop' => false,
81 'fields' => ['id', 'name', 'slug'],
82 'children' => []
83 ],
84 'custom_fields' => [
85 'key' => 'custom_fields',
86 'label' => 'Custom Fields',
87 'loop' => true,
88 'loop_fields' => ['meta_key', 'meta_value'],
89 'fields' => [],
90 'children' => []
91 ]
92 ]
93
94 ];
95
96 $fields['fields'] = $this->get_core_fields();
97
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)));
100
101 // add post_thumbnail
102 if (post_type_supports($post_types[0], 'thumbnail')) {
103 $fields['fields'][] = 'post_thumbnail';
104 }
105
106 // post author
107 $fields['children']['author']['fields'][] = 'ID';
108 $fields['children']['author']['fields'][] = 'user_login';
109 $fields['children']['author']['fields'][] = 'user_nicename';
110 $fields['children']['author']['fields'][] = 'user_email';
111 $fields['children']['author']['fields'][] = 'user_url';
112 $fields['children']['author']['fields'][] = 'display_name';
113
114 // post_meta
115 $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)));
116 foreach ($meta_fields as $field) {
117 $fields['children']['custom_fields']['fields'][] = $field;
118 }
119
120 // taxonomies
121 $taxonomies = get_object_taxonomies($post_types[0], 'objects');
122 foreach ($taxonomies as $tax) {
123 $fields['children']['tax_' . $tax->name] = [
124 'key' => 'tax_' . $tax->name,
125 'label' => $tax->label,
126 'loop' => true,
127 'loop_fields' => ['id', 'name', 'slug'],
128 'fields' => [
129 'name',
130 'slug',
131 'id',
132 'hierarchy::id',
133 'hierarchy::name',
134 'hierarchy::slug',
135 ]
136 ];
137 }
138
139 $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);
141
142 return $fields;
143 }
144
145
146 public function have_records()
147 {
148 $this->query = new \WP_Query(array(
149 'post_type' => $this->post_type,
150 'posts_per_page' => -1,
151 'post_status' => 'any, trash, future',
152 'fields' => 'ids',
153 'cache_results' => false,
154 'update_post_meta_cache' => false,
155 'update_post_term_cache' => false,
156 'no_found_rows' => true,
157 'order' => 'ASC'
158 ));
159
160 return $this->found_records() > 0;
161 }
162
163 public function found_records()
164 {
165 return $this->query->post_count;
166 }
167
168
169 public function setup($i)
170 {
171 $post = get_post($this->query->posts[$i], ARRAY_A);
172 if (!$post) {
173 return false;
174 }
175
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']);
179
180 $user = get_userdata($post['post_author']);
181 $this->record['author'] = (array)$user->data;
182
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 ];
206 }
207
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 }
222
223 // taxonomies
224 $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects');
225 foreach (array_keys($taxonomies) as $taxonomy) {
226 $tmp = [];
227
228 $terms = wp_get_object_terms($post['ID'], $taxonomy);
229 $tmp = [
230 'id' => [],
231 'slug' => [],
232 'name' => [],
233 'hierarchy::id' => [],
234 'hierarchy::name' => [],
235 'hierarchy::slug' => [],
236 ];
237
238 if (!empty($terms)) {
239 foreach ($terms as $term) {
240
241 /**
242 * @var \WP_Term $term
243 */
244 $tmp['id'][] = $term->term_id;
245 $tmp['slug'][] = $term->slug;
246 $tmp['name'][] = $term->name;
247
248 $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy');
249 // $ancestor_ids = array_reverse($ancestor_ids);
250
251 $ancestor_tmp = [
252 'id' => [$term->term_id],
253 'name' => [$term->name],
254 'slug' => [$term->slug]
255 ];
256
257 foreach ($ancestor_ids as $ancestor_id) {
258
259 $ancestor = get_term($ancestor_id, $taxonomy);
260 if (is_wp_error($ancestor)) {
261 continue;
262 }
263
264 $ancestor_tmp['id'][] = $ancestor->term_id;
265 $ancestor_tmp['name'][] = $ancestor->name;
266 $ancestor_tmp['slug'][] = $ancestor->slug;
267 }
268
269 $ancestor_tmp['id'] = array_reverse($ancestor_tmp['id']);
270 $tmp['hierarchy::id'][] = implode(' > ', $ancestor_tmp['id']);
271
272 $ancestor_tmp['name'] = array_reverse($ancestor_tmp['name']);
273 $tmp['hierarchy::name'][] = implode(' > ', $ancestor_tmp['name']);
274
275 $ancestor_tmp['slug'] = array_reverse($ancestor_tmp['slug']);
276 $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']);
277 }
278 }
279
280 $this->record['tax_' . $taxonomy] = $tmp;
281 }
282
283 $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type);
284
285 return true;
286 }
287 }
288