PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.3
Import WP – CSV & XML Import Export for WordPress v2.8.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 / Importer / Mapper / PostMapper.php

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

454 lines 13.8 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\Importer\Mapper;
4
5 use ImportWP\Common\Importer\Exception\MapperException;
6 use ImportWP\Common\Importer\MapperInterface;
7 use ImportWP\Common\Importer\ParsedData;
8 use ImportWP\Common\Importer\Template\TemplateManager;
9
10 class PostMapper extends AbstractMapper implements MapperInterface
11 {
12 /**
13 * Reserved Field Names for post table
14 * @var array
15 */
16 protected $_post_fields = array(
17 'ID',
18 'menu_order',
19 'comment_status',
20 'ping_status',
21 'pinged',
22 'post_author',
23 'post_category',
24 'post_content',
25 'post_date',
26 'post_date_gmt',
27 'post_excerpt',
28 'post_name',
29 'post_parent',
30 'post_password',
31 'post_status',
32 'post_title',
33 'post_type',
34 'tags_input',
35 'to_ping',
36 'tax_input'
37 );
38
39 protected $_query_vars = array(
40 'post_name' => 'name',
41 'ID' => 'p'
42 );
43
44 public function setup()
45 {
46 wp_defer_term_counting(true);
47 wp_defer_comment_counting(true);
48 }
49
50 public function teardown()
51 {
52 wp_defer_term_counting(false);
53 wp_defer_comment_counting(false);
54 }
55
56 public function exists(ParsedData $data)
57 {
58 $unique_fields = TemplateManager::get_template_unique_fields($this->template);
59
60 // allow user to set unique field name, get from importer setting
61 $unique_field = $this->importer->getSetting('unique_field');
62 if ($unique_field !== null) {
63 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
64 }
65
66 $unique_fields = $this->getUniqueIdentifiers($unique_fields);
67 $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
68
69 $unique_field_found = false;
70
71 $post_type = $this->importer->getSetting('post_type');
72 $post_status = 'any, trash, future';
73
74 $meta_args = array();
75 $query_args = array(
76 'post_type' => $post_type,
77 'post_status' => $post_status,
78 'fields' => 'ids',
79 'cache_results' => false,
80 'update_post_meta_cache' => false,
81 'update_post_term_cache' => false,
82 'no_found_rows' => true,
83 );
84
85 $has_unique_field = false;
86
87 foreach ($unique_fields as $field) {
88
89 // check all groups for a unique value
90 $unique_value = $data->getValue($field, '*');
91 if (empty($unique_value)) {
92 $cf = $data->getData('custom_fields');
93 if (!empty($cf)) {
94 $cf_index = intval($cf['custom_fields._index']);
95 if ($cf_index > 0) {
96 for ($i = 0; $i < $cf_index; $i++) {
97 $row = 'custom_fields.' . $i . '.';
98 $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
99 if ($custom_field_key !== $field) {
100 continue;
101 }
102 $unique_value = $cf[$row . 'value'];
103 break;
104 }
105 }
106 }
107 }
108
109 if (!empty($unique_value)) {
110 $has_unique_field = true;
111
112 if (in_array($field, $this->_post_fields, true)) {
113
114 if (array_key_exists($field, $this->_query_vars)) {
115 $query_args[$this->_query_vars[$field]] = $unique_value;
116 } else {
117 switch ($field) {
118 case 'post_title':
119 $query_args['title'] = $unique_value;
120 break;
121 default:
122 $query_args[$field] = $unique_value;
123 break;
124 }
125 }
126 } else {
127 $meta_args[] = array(
128 'key' => $field,
129 'value' => $unique_value
130 );
131 }
132 $unique_field_found = $field;
133 break;
134 }
135 }
136
137 if (!$has_unique_field) {
138
139 // fallback to post_title
140 $unique_value = $data->getValue('post_title');
141 if (empty($unique_value)) {
142 throw new MapperException("No Unique fields present.");
143 }
144
145 $query_args['title'] = $unique_value;
146 }
147
148 if (!empty($meta_args)) {
149 $query_args['meta_query'] = $meta_args;
150 }
151
152 $query = new \WP_Query($query_args);
153 if ($query->post_count > 1) {
154 throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $query->posts) . ").");
155 }
156
157 if ($query->post_count == 1) {
158 $this->ID = $query->posts[0];
159 return $this->ID;
160 }
161
162 return false;
163 }
164
165 public function insert(ParsedData $data)
166 {
167 $fields = $data->getData('default');
168
169 $post = array();
170 $meta = array();
171
172 // if we are trying to insert a post with a specific id then used import_id instead.
173 // if (isset($fields['ID']) && !empty($fields['ID'])) {
174 // $post['import_id'] = $fields['ID'];
175 // unset($fields['ID']);
176 // }
177
178 // we dont import the ID
179 if (isset($fields['ID'])) {
180 unset($fields['ID']);
181 }
182
183 $this->sortFields($fields, $post, $meta);
184
185 // set post_type
186 $post['post_type'] = $this->importer->getSetting('post_type');
187
188 $this->ID = $this->create_post($post, $data);
189
190 if (is_wp_error($this->ID)) {
191 throw new MapperException($this->ID->get_error_message());
192 }
193
194 // TODO: Do we merge in the custom fields, or do we process that in post_process
195 $this->template->process($this->ID, $data, $this->importer);
196
197 $meta = array_merge($meta, $data->getData('custom_fields'));
198
199 // create post meta
200 if ($this->ID && !empty($meta)) {
201 foreach ($meta as $key => $value) {
202 if (is_array($value)) {
203 $this->clear_custom_field($this->ID, $key);
204 foreach ($value as $v) {
205 $this->add_custom_field($this->ID, $key, $v);
206 }
207 } else {
208 $this->update_custom_field($this->ID, $key, $value);
209 }
210 }
211 }
212
213 $this->add_version_tag();
214 $this->template->post_process($this->ID, $data);
215
216 clean_post_cache($this->ID);
217
218 return $this->ID;
219 }
220
221 public function create_post($post, ParsedData $data)
222 {
223 return wp_insert_post($post, true);
224 }
225
226 public function update(ParsedData $data)
227 {
228 $fields = $data->getData('default');
229
230 $post_type = $this->importer->getSetting('post_type');
231
232 $post = array();
233 $meta = array();
234
235 // we dont import the ID
236 if (isset($fields['ID'])) {
237 unset($fields['ID']);
238 }
239
240 $this->sortFields($fields, $post, $meta);
241
242 if (!$this->ID) {
243 return false;
244 }
245
246 // Check to see if trash flag is set, only the importer that removed it can restore it
247 $trash_status = get_post_meta($this->ID, '_iwp_trash_status', true);
248 if (!empty($trash_status)) {
249 $trash_importer_id = get_post_meta($this->ID, '_iwp_trash_importer', true);
250 if ($trash_importer_id == $this->importer->getId()) {
251 $post['post_status'] = $trash_status;
252 } else {
253 $trash_status = false;
254 }
255 }
256
257 // update post type
258 if (!empty($post)) {
259
260 // check to see if fields need updating
261 $query = new \WP_Query(array(
262 'post_type' => $post_type,
263 'p' => $this->ID,
264 'posts_per_page' => 1,
265 'cache_results' => false,
266 'update_post_meta_cache' => false,
267 'update_post_term_cache' => false,
268 'no_found_rows' => true,
269 'post_status' => 'any, trash, future'
270 ));
271 if ($query->found_posts == 1) {
272 $old_post = $query->post;
273
274 foreach ($post as $k => $p) {
275 if ($p == $old_post->$k) {
276 unset($post[$k]);
277 }
278 }
279 }
280
281 if (!empty($post)) {
282 // update remaining
283 $post['ID'] = $this->ID;
284 $res = wp_update_post($post, true);
285 if (is_wp_error($res)) {
286 throw new MapperException($res->get_error_message());
287 }
288 }
289 }
290
291 // TODO: Do we merge in the custom fields, or do we process that in post_process
292 $this->template->process($this->ID, $data, $this->importer);
293
294 // update post meta
295 $meta = array_merge($meta, $data->getData('custom_fields'));
296 if (!empty($meta)) {
297 foreach ($meta as $key => $value) {
298 if (is_array($value)) {
299 $this->clear_custom_field($this->ID, $key);
300 foreach ($value as $v) {
301 $this->add_custom_field($this->ID, $key, $v);
302 }
303 } else {
304 $this->update_custom_field($this->ID, $key, $value);
305 }
306 }
307 }
308
309 $this->add_version_tag();
310 $this->template->post_process($this->ID, $data);
311
312 // Delete trash flag once post has been updated.
313 if (!empty($trash_status)) {
314 delete_post_meta($this->ID, '_iwp_trash_status');
315 delete_post_meta($this->ID, '_iwp_trash_importer');
316 }
317
318 clean_post_cache($this->ID);
319
320 return $this->ID;
321 }
322
323 public function get_objects_for_removal()
324 {
325 if ($this->is_session_tag_enabled()) {
326 return $this->get_ids_without_session_tag('pt-' . $this->importer->getSetting('post_type'));
327 } else {
328 $q = new \WP_Query(array(
329 'post_type' => $this->importer->getSetting('post_type'),
330 'meta_query' => array(
331 array(
332 'key' => '_iwp_session_' . $this->importer->getId(),
333 'value' => $this->importer->getStatusId(),
334 'compare' => '!='
335 )
336 ),
337 'fields' => 'ids',
338 'posts_per_page' => -1,
339 'cache_results' => false,
340 'update_post_meta_cache' => false,
341 'post_status' => 'any'
342 ));
343
344 if ($q->have_posts()) {
345 return $q->posts;
346 }
347 }
348
349 return false;
350 }
351
352 public function delete($id)
353 {
354 $permissions = $this->importer->getPermission('remove');
355 $force = isset($permissions['trash']) ? !$permissions['trash'] : true; // trash = true
356 if (!$force) {
357
358 // set trash flag
359 update_post_meta($id, '_iwp_trash_status', get_post_status($id));
360 update_post_meta($id, '_iwp_trash_importer', $this->importer->getId());
361 wp_trash_post($id);
362 } else {
363 wp_delete_post($id, $force);
364 }
365
366 $this->remove_session_tag($id, 'pt-' . $this->importer->getSetting('post_type'));
367 }
368
369 /**
370 * Sort fields into post and meta array
371 *
372 * @param array $fields list of fields
373 * @param array $post post_data pointer array
374 * @param array $meta post_meta pointer array
375 *
376 * @return void
377 */
378 function sortFields($fields = array(), &$post = array(), &$meta = array())
379 {
380
381 foreach ($fields as $id => $value) {
382
383 if (in_array($id, $this->_post_fields, true)) {
384
385 // post field
386 $post[$id] = $value;
387 } else {
388
389 // meta field
390 $meta[$id] = $value;
391 }
392 }
393 }
394
395 /**
396 * Clear all post meta before adding custom field
397 */
398 public function clear_custom_field($post_id, $key)
399 {
400 delete_post_meta($post_id, $key);
401 }
402
403 /**
404 * Add custom field, allow for multiple records using the same key
405 *
406 * @param int $post_id
407 * @param string $key
408 * @param string $value
409 * @return void
410 */
411 public function add_custom_field($post_id, $key, $value)
412 {
413 // Stop double serialization
414 if (is_serialized($value)) {
415 $value = unserialize($value);
416 }
417
418 add_post_meta($post_id, $key, $value);
419 }
420
421 public function update_custom_field($post_id, $key, $value, $unique = false, $skip_permissions = false)
422 {
423
424 $old_value = get_post_meta($post_id, $key, true);
425
426 // Stop double serialization
427 if (is_serialized($value)) {
428 $value = unserialize($value);
429 }
430
431 // check if new value
432 if ($old_value === $value) {
433 return;
434 }
435
436 if ($value !== '' && '' == $old_value) {
437 add_post_meta($post_id, $key, $value, $unique);
438 } elseif ($value !== '' && $value !== $old_value) {
439 update_post_meta($post_id, $key, $value);
440 } elseif ('' === $value && $old_value) {
441 delete_post_meta($post_id, $key, $value);
442 }
443 }
444
445 public function add_version_tag()
446 {
447 if ($this->is_session_tag_enabled()) {
448 $this->add_session_tag('pt-' . implode('|', (array)$this->importer->getSetting('post_type')));
449 } else {
450 update_post_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
451 }
452 }
453 }
454