Version Description
- Add wp_import_existing_post filter. See: https://core.trac.wordpress.org/ticket/33721
Download this release
Release Info
Developer | Otto42 |
Plugin | WordPress Importer |
Version | 0.6.2 |
Comparing to | |
See all releases |
Code changes from version 0.6.1 to 0.6.2
- parsers.php +4 -4
- readme.txt +5 -3
- wordpress-importer.php +1146 -1132
parsers.php
CHANGED
@@ -401,14 +401,14 @@ class WXR_Parser_Regex {
|
|
401 |
var $terms = array();
|
402 |
var $base_url = '';
|
403 |
|
404 |
-
function WXR_Parser_Regex() {
|
405 |
-
$this->__construct();
|
406 |
-
}
|
407 |
-
|
408 |
function __construct() {
|
409 |
$this->has_gzip = is_callable( 'gzopen' );
|
410 |
}
|
411 |
|
|
|
|
|
|
|
|
|
412 |
function parse( $file ) {
|
413 |
$wxr_version = $in_post = false;
|
414 |
|
401 |
var $terms = array();
|
402 |
var $base_url = '';
|
403 |
|
|
|
|
|
|
|
|
|
404 |
function __construct() {
|
405 |
$this->has_gzip = is_callable( 'gzopen' );
|
406 |
}
|
407 |
|
408 |
+
function WXR_Parser_Regex() {
|
409 |
+
$this->__construct();
|
410 |
+
}
|
411 |
+
|
412 |
function parse( $file ) {
|
413 |
$wxr_version = $in_post = false;
|
414 |
|
readme.txt
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
=== WordPress Importer ===
|
2 |
Contributors: wordpressdotorg
|
3 |
-
Donate link:
|
4 |
Tags: importer, wordpress
|
5 |
Requires at least: 3.0
|
6 |
-
Tested up to: 4.
|
7 |
-
Stable tag: 0.6.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -39,6 +38,9 @@ If you would prefer to do things manually then follow these instructions:
|
|
39 |
|
40 |
== Changelog ==
|
41 |
|
|
|
|
|
|
|
42 |
= 0.6 =
|
43 |
* Support for WXR 1.2 and multiple CDATA sections
|
44 |
* Post aren't duplicates if their post_type's are different
|
1 |
=== WordPress Importer ===
|
2 |
Contributors: wordpressdotorg
|
|
|
3 |
Tags: importer, wordpress
|
4 |
Requires at least: 3.0
|
5 |
+
Tested up to: 4.5
|
6 |
+
Stable tag: 0.6.2
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
38 |
|
39 |
== Changelog ==
|
40 |
|
41 |
+
= 0.6.2 =
|
42 |
+
* Add wp_import_existing_post filter. See: https://core.trac.wordpress.org/ticket/33721
|
43 |
+
|
44 |
= 0.6 =
|
45 |
* Support for WXR 1.2 and multiple CDATA sections
|
46 |
* Post aren't duplicates if their post_type's are different
|
wordpress-importer.php
CHANGED
@@ -1,1132 +1,1146 @@
|
|
1 |
-
<?php
|
2 |
-
/*
|
3 |
-
Plugin Name: WordPress Importer
|
4 |
-
Plugin URI: http://wordpress.org/extend/plugins/wordpress-importer/
|
5 |
-
Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
|
6 |
-
Author: wordpressdotorg
|
7 |
-
Author URI: http://wordpress.org/
|
8 |
-
Version: 0.6.
|
9 |
-
Text Domain: wordpress-importer
|
10 |
-
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
11 |
-
*/
|
12 |
-
|
13 |
-
if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
|
14 |
-
return;
|
15 |
-
|
16 |
-
/** Display verbose errors */
|
17 |
-
define( 'IMPORT_DEBUG', false );
|
18 |
-
|
19 |
-
// Load Importer API
|
20 |
-
require_once ABSPATH . 'wp-admin/includes/import.php';
|
21 |
-
|
22 |
-
if ( ! class_exists( 'WP_Importer' ) ) {
|
23 |
-
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
|
24 |
-
if ( file_exists( $class_wp_importer ) )
|
25 |
-
require $class_wp_importer;
|
26 |
-
}
|
27 |
-
|
28 |
-
// include WXR file parsers
|
29 |
-
require dirname( __FILE__ ) . '/parsers.php';
|
30 |
-
|
31 |
-
/**
|
32 |
-
* WordPress Importer class for managing the import process of a WXR file
|
33 |
-
*
|
34 |
-
* @package WordPress
|
35 |
-
* @subpackage Importer
|
36 |
-
*/
|
37 |
-
if ( class_exists( 'WP_Importer' ) ) {
|
38 |
-
class WP_Import extends WP_Importer {
|
39 |
-
var $max_wxr_version = 1.2; // max. supported WXR version
|
40 |
-
|
41 |
-
var $id; // WXR attachment ID
|
42 |
-
|
43 |
-
// information to import from WXR file
|
44 |
-
var $version;
|
45 |
-
var $authors = array();
|
46 |
-
var $posts = array();
|
47 |
-
var $terms = array();
|
48 |
-
var $categories = array();
|
49 |
-
var $tags = array();
|
50 |
-
var $base_url = '';
|
51 |
-
|
52 |
-
// mappings from old information to new
|
53 |
-
var $processed_authors = array();
|
54 |
-
var $author_mapping = array();
|
55 |
-
var $processed_terms = array();
|
56 |
-
var $processed_posts = array();
|
57 |
-
var $post_orphans = array();
|
58 |
-
var $processed_menu_items = array();
|
59 |
-
var $menu_item_orphans = array();
|
60 |
-
var $missing_menu_items = array();
|
61 |
-
|
62 |
-
var $fetch_attachments = false;
|
63 |
-
var $url_remap = array();
|
64 |
-
var $featured_images = array();
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
*
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
$
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
$
|
89 |
-
|
90 |
-
$
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
*
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
$this->
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
$this->
|
114 |
-
$this->
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
$this->
|
121 |
-
|
122 |
-
$this->
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
*
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
$this->
|
150 |
-
$this->
|
151 |
-
$this->
|
152 |
-
$this->
|
153 |
-
$this->
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
*
|
186 |
-
*
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
$file
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
echo '
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
echo '
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
*
|
226 |
-
*
|
227 |
-
*
|
228 |
-
*
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
?>
|
261 |
-
<
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
<p><?php
|
268 |
-
<?php
|
269 |
-
<
|
270 |
-
<?php
|
271 |
-
|
272 |
-
<?php
|
273 |
-
|
274 |
-
<?php
|
275 |
-
|
276 |
-
<?php
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
<?php
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
*
|
293 |
-
*
|
294 |
-
*
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
echo '
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
_e( 'assign posts to an existing user:', 'wordpress-importer' );
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
*
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
$
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
if (
|
356 |
-
$
|
357 |
-
|
358 |
-
|
359 |
-
'
|
360 |
-
'
|
361 |
-
'
|
362 |
-
'
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
$this->
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
*
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
$
|
412 |
-
|
413 |
-
|
414 |
-
'
|
415 |
-
'
|
416 |
-
|
417 |
-
|
418 |
-
);
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
*
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
$
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
*
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
$parent =
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
$
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
*
|
524 |
-
*
|
525 |
-
*
|
526 |
-
*
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
$
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
);
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
$
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
$
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
$
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
|
691 |
-
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
}
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
$
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
'
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
return new WP_Error( 'attachment_processing_error',
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
//
|
914 |
-
|
915 |
-
|
916 |
-
return new WP_Error( '
|
917 |
-
|
918 |
-
|
919 |
-
$
|
920 |
-
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
if (
|
980 |
-
$local_parent_id
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
1022 |
-
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
1026 |
-
|
1027 |
-
|
1028 |
-
|
1029 |
-
|
1030 |
-
|
1031 |
-
|
1032 |
-
|
1033 |
-
|
1034 |
-
|
1035 |
-
|
1036 |
-
|
1037 |
-
|
1038 |
-
|
1039 |
-
|
1040 |
-
|
1041 |
-
|
1042 |
-
|
1043 |
-
|
1044 |
-
|
1045 |
-
|
1046 |
-
|
1047 |
-
echo '</
|
1048 |
-
|
1049 |
-
|
1050 |
-
|
1051 |
-
|
1052 |
-
|
1053 |
-
|
1054 |
-
|
1055 |
-
|
1056 |
-
|
1057 |
-
|
1058 |
-
|
1059 |
-
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
*
|
1066 |
-
*/
|
1067 |
-
function
|
1068 |
-
|
1069 |
-
|
1070 |
-
|
1071 |
-
|
1072 |
-
|
1073 |
-
}
|
1074 |
-
|
1075 |
-
/**
|
1076 |
-
* Decide
|
1077 |
-
*
|
1078 |
-
*
|
1079 |
-
* @return bool
|
1080 |
-
*/
|
1081 |
-
function
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
*
|
1091 |
-
|
1092 |
-
|
1093 |
-
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
1099 |
-
|
1100 |
-
*
|
1101 |
-
|
1102 |
-
|
1103 |
-
|
1104 |
-
|
1105 |
-
|
1106 |
-
|
1107 |
-
|
1108 |
-
|
1109 |
-
|
1110 |
-
|
1111 |
-
|
1112 |
-
|
1113 |
-
|
1114 |
-
|
1115 |
-
|
1116 |
-
|
1117 |
-
|
1118 |
-
}
|
1119 |
-
|
1120 |
-
|
1121 |
-
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
1125 |
-
|
1126 |
-
|
1127 |
-
|
1128 |
-
|
1129 |
-
$
|
1130 |
-
|
1131 |
-
}
|
1132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: WordPress Importer
|
4 |
+
Plugin URI: http://wordpress.org/extend/plugins/wordpress-importer/
|
5 |
+
Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
|
6 |
+
Author: wordpressdotorg
|
7 |
+
Author URI: http://wordpress.org/
|
8 |
+
Version: 0.6.2
|
9 |
+
Text Domain: wordpress-importer
|
10 |
+
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
11 |
+
*/
|
12 |
+
|
13 |
+
if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
|
14 |
+
return;
|
15 |
+
|
16 |
+
/** Display verbose errors */
|
17 |
+
define( 'IMPORT_DEBUG', false );
|
18 |
+
|
19 |
+
// Load Importer API
|
20 |
+
require_once ABSPATH . 'wp-admin/includes/import.php';
|
21 |
+
|
22 |
+
if ( ! class_exists( 'WP_Importer' ) ) {
|
23 |
+
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
|
24 |
+
if ( file_exists( $class_wp_importer ) )
|
25 |
+
require $class_wp_importer;
|
26 |
+
}
|
27 |
+
|
28 |
+
// include WXR file parsers
|
29 |
+
require dirname( __FILE__ ) . '/parsers.php';
|
30 |
+
|
31 |
+
/**
|
32 |
+
* WordPress Importer class for managing the import process of a WXR file
|
33 |
+
*
|
34 |
+
* @package WordPress
|
35 |
+
* @subpackage Importer
|
36 |
+
*/
|
37 |
+
if ( class_exists( 'WP_Importer' ) ) {
|
38 |
+
class WP_Import extends WP_Importer {
|
39 |
+
var $max_wxr_version = 1.2; // max. supported WXR version
|
40 |
+
|
41 |
+
var $id; // WXR attachment ID
|
42 |
+
|
43 |
+
// information to import from WXR file
|
44 |
+
var $version;
|
45 |
+
var $authors = array();
|
46 |
+
var $posts = array();
|
47 |
+
var $terms = array();
|
48 |
+
var $categories = array();
|
49 |
+
var $tags = array();
|
50 |
+
var $base_url = '';
|
51 |
+
|
52 |
+
// mappings from old information to new
|
53 |
+
var $processed_authors = array();
|
54 |
+
var $author_mapping = array();
|
55 |
+
var $processed_terms = array();
|
56 |
+
var $processed_posts = array();
|
57 |
+
var $post_orphans = array();
|
58 |
+
var $processed_menu_items = array();
|
59 |
+
var $menu_item_orphans = array();
|
60 |
+
var $missing_menu_items = array();
|
61 |
+
|
62 |
+
var $fetch_attachments = false;
|
63 |
+
var $url_remap = array();
|
64 |
+
var $featured_images = array();
|
65 |
+
|
66 |
+
/**
|
67 |
+
* Registered callback function for the WordPress Importer
|
68 |
+
*
|
69 |
+
* Manages the three separate stages of the WXR import process
|
70 |
+
*/
|
71 |
+
function dispatch() {
|
72 |
+
$this->header();
|
73 |
+
|
74 |
+
$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
|
75 |
+
switch ( $step ) {
|
76 |
+
case 0:
|
77 |
+
$this->greet();
|
78 |
+
break;
|
79 |
+
case 1:
|
80 |
+
check_admin_referer( 'import-upload' );
|
81 |
+
if ( $this->handle_upload() )
|
82 |
+
$this->import_options();
|
83 |
+
break;
|
84 |
+
case 2:
|
85 |
+
check_admin_referer( 'import-wordpress' );
|
86 |
+
$this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
|
87 |
+
$this->id = (int) $_POST['import_id'];
|
88 |
+
$file = get_attached_file( $this->id );
|
89 |
+
set_time_limit(0);
|
90 |
+
$this->import( $file );
|
91 |
+
break;
|
92 |
+
}
|
93 |
+
|
94 |
+
$this->footer();
|
95 |
+
}
|
96 |
+
|
97 |
+
/**
|
98 |
+
* The main controller for the actual import stage.
|
99 |
+
*
|
100 |
+
* @param string $file Path to the WXR file for importing
|
101 |
+
*/
|
102 |
+
function import( $file ) {
|
103 |
+
add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
|
104 |
+
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
|
105 |
+
|
106 |
+
$this->import_start( $file );
|
107 |
+
|
108 |
+
$this->get_author_mapping();
|
109 |
+
|
110 |
+
wp_suspend_cache_invalidation( true );
|
111 |
+
$this->process_categories();
|
112 |
+
$this->process_tags();
|
113 |
+
$this->process_terms();
|
114 |
+
$this->process_posts();
|
115 |
+
wp_suspend_cache_invalidation( false );
|
116 |
+
|
117 |
+
// update incorrect/missing information in the DB
|
118 |
+
$this->backfill_parents();
|
119 |
+
$this->backfill_attachment_urls();
|
120 |
+
$this->remap_featured_images();
|
121 |
+
|
122 |
+
$this->import_end();
|
123 |
+
}
|
124 |
+
|
125 |
+
/**
|
126 |
+
* Parses the WXR file and prepares us for the task of processing parsed data
|
127 |
+
*
|
128 |
+
* @param string $file Path to the WXR file for importing
|
129 |
+
*/
|
130 |
+
function import_start( $file ) {
|
131 |
+
if ( ! is_file($file) ) {
|
132 |
+
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
|
133 |
+
echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
|
134 |
+
$this->footer();
|
135 |
+
die();
|
136 |
+
}
|
137 |
+
|
138 |
+
$import_data = $this->parse( $file );
|
139 |
+
|
140 |
+
if ( is_wp_error( $import_data ) ) {
|
141 |
+
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
|
142 |
+
echo esc_html( $import_data->get_error_message() ) . '</p>';
|
143 |
+
$this->footer();
|
144 |
+
die();
|
145 |
+
}
|
146 |
+
|
147 |
+
$this->version = $import_data['version'];
|
148 |
+
$this->get_authors_from_import( $import_data );
|
149 |
+
$this->posts = $import_data['posts'];
|
150 |
+
$this->terms = $import_data['terms'];
|
151 |
+
$this->categories = $import_data['categories'];
|
152 |
+
$this->tags = $import_data['tags'];
|
153 |
+
$this->base_url = esc_url( $import_data['base_url'] );
|
154 |
+
|
155 |
+
wp_defer_term_counting( true );
|
156 |
+
wp_defer_comment_counting( true );
|
157 |
+
|
158 |
+
do_action( 'import_start' );
|
159 |
+
}
|
160 |
+
|
161 |
+
/**
|
162 |
+
* Performs post-import cleanup of files and the cache
|
163 |
+
*/
|
164 |
+
function import_end() {
|
165 |
+
wp_import_cleanup( $this->id );
|
166 |
+
|
167 |
+
wp_cache_flush();
|
168 |
+
foreach ( get_taxonomies() as $tax ) {
|
169 |
+
delete_option( "{$tax}_children" );
|
170 |
+
_get_term_hierarchy( $tax );
|
171 |
+
}
|
172 |
+
|
173 |
+
wp_defer_term_counting( false );
|
174 |
+
wp_defer_comment_counting( false );
|
175 |
+
|
176 |
+
echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
|
177 |
+
echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
|
178 |
+
|
179 |
+
do_action( 'import_end' );
|
180 |
+
}
|
181 |
+
|
182 |
+
/**
|
183 |
+
* Handles the WXR upload and initial parsing of the file to prepare for
|
184 |
+
* displaying author import options
|
185 |
+
*
|
186 |
+
* @return bool False if error uploading or invalid file, true otherwise
|
187 |
+
*/
|
188 |
+
function handle_upload() {
|
189 |
+
$file = wp_import_handle_upload();
|
190 |
+
|
191 |
+
if ( isset( $file['error'] ) ) {
|
192 |
+
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
|
193 |
+
echo esc_html( $file['error'] ) . '</p>';
|
194 |
+
return false;
|
195 |
+
} else if ( ! file_exists( $file['file'] ) ) {
|
196 |
+
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
|
197 |
+
printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) );
|
198 |
+
echo '</p>';
|
199 |
+
return false;
|
200 |
+
}
|
201 |
+
|
202 |
+
$this->id = (int) $file['id'];
|
203 |
+
$import_data = $this->parse( $file['file'] );
|
204 |
+
if ( is_wp_error( $import_data ) ) {
|
205 |
+
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
|
206 |
+
echo esc_html( $import_data->get_error_message() ) . '</p>';
|
207 |
+
return false;
|
208 |
+
}
|
209 |
+
|
210 |
+
$this->version = $import_data['version'];
|
211 |
+
if ( $this->version > $this->max_wxr_version ) {
|
212 |
+
echo '<div class="error"><p><strong>';
|
213 |
+
printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) );
|
214 |
+
echo '</strong></p></div>';
|
215 |
+
}
|
216 |
+
|
217 |
+
$this->get_authors_from_import( $import_data );
|
218 |
+
|
219 |
+
return true;
|
220 |
+
}
|
221 |
+
|
222 |
+
/**
|
223 |
+
* Retrieve authors from parsed WXR data
|
224 |
+
*
|
225 |
+
* Uses the provided author information from WXR 1.1 files
|
226 |
+
* or extracts info from each post for WXR 1.0 files
|
227 |
+
*
|
228 |
+
* @param array $import_data Data returned by a WXR parser
|
229 |
+
*/
|
230 |
+
function get_authors_from_import( $import_data ) {
|
231 |
+
if ( ! empty( $import_data['authors'] ) ) {
|
232 |
+
$this->authors = $import_data['authors'];
|
233 |
+
// no author information, grab it from the posts
|
234 |
+
} else {
|
235 |
+
foreach ( $import_data['posts'] as $post ) {
|
236 |
+
$login = sanitize_user( $post['post_author'], true );
|
237 |
+
if ( empty( $login ) ) {
|
238 |
+
printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
|
239 |
+
echo '<br />';
|
240 |
+
continue;
|
241 |
+
}
|
242 |
+
|
243 |
+
if ( ! isset($this->authors[$login]) )
|
244 |
+
$this->authors[$login] = array(
|
245 |
+
'author_login' => $login,
|
246 |
+
'author_display_name' => $post['post_author']
|
247 |
+
);
|
248 |
+
}
|
249 |
+
}
|
250 |
+
}
|
251 |
+
|
252 |
+
/**
|
253 |
+
* Display pre-import options, author importing/mapping and option to
|
254 |
+
* fetch attachments
|
255 |
+
*/
|
256 |
+
function import_options() {
|
257 |
+
$j = 0;
|
258 |
+
?>
|
259 |
+
<form action="<?php echo admin_url( 'admin.php?import=wordpress&step=2' ); ?>" method="post">
|
260 |
+
<?php wp_nonce_field( 'import-wordpress' ); ?>
|
261 |
+
<input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
|
262 |
+
|
263 |
+
<?php if ( ! empty( $this->authors ) ) : ?>
|
264 |
+
<h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
|
265 |
+
<p><?php _e( 'To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer' ); ?></p>
|
266 |
+
<?php if ( $this->allow_create_users() ) : ?>
|
267 |
+
<p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user’s role will be set as %s. Manually changing the new user’s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p>
|
268 |
+
<?php endif; ?>
|
269 |
+
<ol id="authors">
|
270 |
+
<?php foreach ( $this->authors as $author ) : ?>
|
271 |
+
<li><?php $this->author_select( $j++, $author ); ?></li>
|
272 |
+
<?php endforeach; ?>
|
273 |
+
</ol>
|
274 |
+
<?php endif; ?>
|
275 |
+
|
276 |
+
<?php if ( $this->allow_fetch_attachments() ) : ?>
|
277 |
+
<h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
|
278 |
+
<p>
|
279 |
+
<input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
|
280 |
+
<label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
|
281 |
+
</p>
|
282 |
+
<?php endif; ?>
|
283 |
+
|
284 |
+
<p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
|
285 |
+
</form>
|
286 |
+
<?php
|
287 |
+
}
|
288 |
+
|
289 |
+
/**
|
290 |
+
* Display import options for an individual author. That is, either create
|
291 |
+
* a new user based on import info or map to an existing user
|
292 |
+
*
|
293 |
+
* @param int $n Index for each author in the form
|
294 |
+
* @param array $author Author information, e.g. login, display name, email
|
295 |
+
*/
|
296 |
+
function author_select( $n, $author ) {
|
297 |
+
_e( 'Import author:', 'wordpress-importer' );
|
298 |
+
echo ' <strong>' . esc_html( $author['author_display_name'] );
|
299 |
+
if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
|
300 |
+
echo '</strong><br />';
|
301 |
+
|
302 |
+
if ( $this->version != '1.0' )
|
303 |
+
echo '<div style="margin-left:18px">';
|
304 |
+
|
305 |
+
$create_users = $this->allow_create_users();
|
306 |
+
if ( $create_users ) {
|
307 |
+
if ( $this->version != '1.0' ) {
|
308 |
+
_e( 'or create new user with login name:', 'wordpress-importer' );
|
309 |
+
$value = '';
|
310 |
+
} else {
|
311 |
+
_e( 'as a new user:', 'wordpress-importer' );
|
312 |
+
$value = esc_attr( sanitize_user( $author['author_login'], true ) );
|
313 |
+
}
|
314 |
+
|
315 |
+
echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
|
316 |
+
}
|
317 |
+
|
318 |
+
if ( ! $create_users && $this->version == '1.0' )
|
319 |
+
_e( 'assign posts to an existing user:', 'wordpress-importer' );
|
320 |
+
else
|
321 |
+
_e( 'or assign posts to an existing user:', 'wordpress-importer' );
|
322 |
+
wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );
|
323 |
+
echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
|
324 |
+
|
325 |
+
if ( $this->version != '1.0' )
|
326 |
+
echo '</div>';
|
327 |
+
}
|
328 |
+
|
329 |
+
/**
|
330 |
+
* Map old author logins to local user IDs based on decisions made
|
331 |
+
* in import options form. Can map to an existing user, create a new user
|
332 |
+
* or falls back to the current user in case of error with either of the previous
|
333 |
+
*/
|
334 |
+
function get_author_mapping() {
|
335 |
+
if ( ! isset( $_POST['imported_authors'] ) )
|
336 |
+
return;
|
337 |
+
|
338 |
+
$create_users = $this->allow_create_users();
|
339 |
+
|
340 |
+
foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
|
341 |
+
// Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
|
342 |
+
$santized_old_login = sanitize_user( $old_login, true );
|
343 |
+
$old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false;
|
344 |
+
|
345 |
+
if ( ! empty( $_POST['user_map'][$i] ) ) {
|
346 |
+
$user = get_userdata( intval($_POST['user_map'][$i]) );
|
347 |
+
if ( isset( $user->ID ) ) {
|
348 |
+
if ( $old_id )
|
349 |
+
$this->processed_authors[$old_id] = $user->ID;
|
350 |
+
$this->author_mapping[$santized_old_login] = $user->ID;
|
351 |
+
}
|
352 |
+
} else if ( $create_users ) {
|
353 |
+
if ( ! empty($_POST['user_new'][$i]) ) {
|
354 |
+
$user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
|
355 |
+
} else if ( $this->version != '1.0' ) {
|
356 |
+
$user_data = array(
|
357 |
+
'user_login' => $old_login,
|
358 |
+
'user_pass' => wp_generate_password(),
|
359 |
+
'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '',
|
360 |
+
'display_name' => $this->authors[$old_login]['author_display_name'],
|
361 |
+
'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '',
|
362 |
+
'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '',
|
363 |
+
);
|
364 |
+
$user_id = wp_insert_user( $user_data );
|
365 |
+
}
|
366 |
+
|
367 |
+
if ( ! is_wp_error( $user_id ) ) {
|
368 |
+
if ( $old_id )
|
369 |
+
$this->processed_authors[$old_id] = $user_id;
|
370 |
+
$this->author_mapping[$santized_old_login] = $user_id;
|
371 |
+
} else {
|
372 |
+
printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) );
|
373 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
374 |
+
echo ' ' . $user_id->get_error_message();
|
375 |
+
echo '<br />';
|
376 |
+
}
|
377 |
+
}
|
378 |
+
|
379 |
+
// failsafe: if the user_id was invalid, default to the current user
|
380 |
+
if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
|
381 |
+
if ( $old_id )
|
382 |
+
$this->processed_authors[$old_id] = (int) get_current_user_id();
|
383 |
+
$this->author_mapping[$santized_old_login] = (int) get_current_user_id();
|
384 |
+
}
|
385 |
+
}
|
386 |
+
}
|
387 |
+
|
388 |
+
/**
|
389 |
+
* Create new categories based on import information
|
390 |
+
*
|
391 |
+
* Doesn't create a new category if its slug already exists
|
392 |
+
*/
|
393 |
+
function process_categories() {
|
394 |
+
$this->categories = apply_filters( 'wp_import_categories', $this->categories );
|
395 |
+
|
396 |
+
if ( empty( $this->categories ) )
|
397 |
+
return;
|
398 |
+
|
399 |
+
foreach ( $this->categories as $cat ) {
|
400 |
+
// if the category already exists leave it alone
|
401 |
+
$term_id = term_exists( $cat['category_nicename'], 'category' );
|
402 |
+
if ( $term_id ) {
|
403 |
+
if ( is_array($term_id) ) $term_id = $term_id['term_id'];
|
404 |
+
if ( isset($cat['term_id']) )
|
405 |
+
$this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
|
406 |
+
continue;
|
407 |
+
}
|
408 |
+
|
409 |
+
$category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
|
410 |
+
$category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
|
411 |
+
$catarr = array(
|
412 |
+
'category_nicename' => $cat['category_nicename'],
|
413 |
+
'category_parent' => $category_parent,
|
414 |
+
'cat_name' => $cat['cat_name'],
|
415 |
+
'category_description' => $category_description
|
416 |
+
);
|
417 |
+
|
418 |
+
$id = wp_insert_category( $catarr );
|
419 |
+
if ( ! is_wp_error( $id ) ) {
|
420 |
+
if ( isset($cat['term_id']) )
|
421 |
+
$this->processed_terms[intval($cat['term_id'])] = $id;
|
422 |
+
} else {
|
423 |
+
printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
|
424 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
425 |
+
echo ': ' . $id->get_error_message();
|
426 |
+
echo '<br />';
|
427 |
+
continue;
|
428 |
+
}
|
429 |
+
}
|
430 |
+
|
431 |
+
unset( $this->categories );
|
432 |
+
}
|
433 |
+
|
434 |
+
/**
|
435 |
+
* Create new post tags based on import information
|
436 |
+
*
|
437 |
+
* Doesn't create a tag if its slug already exists
|
438 |
+
*/
|
439 |
+
function process_tags() {
|
440 |
+
$this->tags = apply_filters( 'wp_import_tags', $this->tags );
|
441 |
+
|
442 |
+
if ( empty( $this->tags ) )
|
443 |
+
return;
|
444 |
+
|
445 |
+
foreach ( $this->tags as $tag ) {
|
446 |
+
// if the tag already exists leave it alone
|
447 |
+
$term_id = term_exists( $tag['tag_slug'], 'post_tag' );
|
448 |
+
if ( $term_id ) {
|
449 |
+
if ( is_array($term_id) ) $term_id = $term_id['term_id'];
|
450 |
+
if ( isset($tag['term_id']) )
|
451 |
+
$this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
|
452 |
+
continue;
|
453 |
+
}
|
454 |
+
|
455 |
+
$tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
|
456 |
+
$tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
|
457 |
+
|
458 |
+
$id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
|
459 |
+
if ( ! is_wp_error( $id ) ) {
|
460 |
+
if ( isset($tag['term_id']) )
|
461 |
+
$this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
|
462 |
+
} else {
|
463 |
+
printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
|
464 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
465 |
+
echo ': ' . $id->get_error_message();
|
466 |
+
echo '<br />';
|
467 |
+
continue;
|
468 |
+
}
|
469 |
+
}
|
470 |
+
|
471 |
+
unset( $this->tags );
|
472 |
+
}
|
473 |
+
|
474 |
+
/**
|
475 |
+
* Create new terms based on import information
|
476 |
+
*
|
477 |
+
* Doesn't create a term its slug already exists
|
478 |
+
*/
|
479 |
+
function process_terms() {
|
480 |
+
$this->terms = apply_filters( 'wp_import_terms', $this->terms );
|
481 |
+
|
482 |
+
if ( empty( $this->terms ) )
|
483 |
+
return;
|
484 |
+
|
485 |
+
foreach ( $this->terms as $term ) {
|
486 |
+
// if the term already exists in the correct taxonomy leave it alone
|
487 |
+
$term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
|
488 |
+
if ( $term_id ) {
|
489 |
+
if ( is_array($term_id) ) $term_id = $term_id['term_id'];
|
490 |
+
if ( isset($term['term_id']) )
|
491 |
+
$this->processed_terms[intval($term['term_id'])] = (int) $term_id;
|
492 |
+
continue;
|
493 |
+
}
|
494 |
+
|
495 |
+
if ( empty( $term['term_parent'] ) ) {
|
496 |
+
$parent = 0;
|
497 |
+
} else {
|
498 |
+
$parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
|
499 |
+
if ( is_array( $parent ) ) $parent = $parent['term_id'];
|
500 |
+
}
|
501 |
+
$description = isset( $term['term_description'] ) ? $term['term_description'] : '';
|
502 |
+
$termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
|
503 |
+
|
504 |
+
$id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
|
505 |
+
if ( ! is_wp_error( $id ) ) {
|
506 |
+
if ( isset($term['term_id']) )
|
507 |
+
$this->processed_terms[intval($term['term_id'])] = $id['term_id'];
|
508 |
+
} else {
|
509 |
+
printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
|
510 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
511 |
+
echo ': ' . $id->get_error_message();
|
512 |
+
echo '<br />';
|
513 |
+
continue;
|
514 |
+
}
|
515 |
+
}
|
516 |
+
|
517 |
+
unset( $this->terms );
|
518 |
+
}
|
519 |
+
|
520 |
+
/**
|
521 |
+
* Create new posts based on import information
|
522 |
+
*
|
523 |
+
* Posts marked as having a parent which doesn't exist will become top level items.
|
524 |
+
* Doesn't create a new post if: the post type doesn't exist, the given post ID
|
525 |
+
* is already noted as imported or a post with the same title and date already exists.
|
526 |
+
* Note that new/updated terms, comments and meta are imported for the last of the above.
|
527 |
+
*/
|
528 |
+
function process_posts() {
|
529 |
+
$this->posts = apply_filters( 'wp_import_posts', $this->posts );
|
530 |
+
|
531 |
+
foreach ( $this->posts as $post ) {
|
532 |
+
$post = apply_filters( 'wp_import_post_data_raw', $post );
|
533 |
+
|
534 |
+
if ( ! post_type_exists( $post['post_type'] ) ) {
|
535 |
+
printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ),
|
536 |
+
esc_html($post['post_title']), esc_html($post['post_type']) );
|
537 |
+
echo '<br />';
|
538 |
+
do_action( 'wp_import_post_exists', $post );
|
539 |
+
continue;
|
540 |
+
}
|
541 |
+
|
542 |
+
if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
|
543 |
+
continue;
|
544 |
+
|
545 |
+
if ( $post['status'] == 'auto-draft' )
|
546 |
+
continue;
|
547 |
+
|
548 |
+
if ( 'nav_menu_item' == $post['post_type'] ) {
|
549 |
+
$this->process_menu_item( $post );
|
550 |
+
continue;
|
551 |
+
}
|
552 |
+
|
553 |
+
$post_type_object = get_post_type_object( $post['post_type'] );
|
554 |
+
|
555 |
+
$post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
|
556 |
+
|
557 |
+
/**
|
558 |
+
* Filter ID of the existing post corresponding to post currently importing.
|
559 |
+
*
|
560 |
+
* Return 0 to force the post to be imported. Filter the ID to be something else
|
561 |
+
* to override which existing post is mapped to the imported post.
|
562 |
+
*
|
563 |
+
* @see post_exists()
|
564 |
+
* @since 0.6.2
|
565 |
+
*
|
566 |
+
* @param int $post_exists Post ID, or 0 if post did not exist.
|
567 |
+
* @param array $post The post array to be inserted.
|
568 |
+
*/
|
569 |
+
$post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
|
570 |
+
|
571 |
+
if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
|
572 |
+
printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
|
573 |
+
echo '<br />';
|
574 |
+
$comment_post_ID = $post_id = $post_exists;
|
575 |
+
$this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
|
576 |
+
} else {
|
577 |
+
$post_parent = (int) $post['post_parent'];
|
578 |
+
if ( $post_parent ) {
|
579 |
+
// if we already know the parent, map it to the new local ID
|
580 |
+
if ( isset( $this->processed_posts[$post_parent] ) ) {
|
581 |
+
$post_parent = $this->processed_posts[$post_parent];
|
582 |
+
// otherwise record the parent for later
|
583 |
+
} else {
|
584 |
+
$this->post_orphans[intval($post['post_id'])] = $post_parent;
|
585 |
+
$post_parent = 0;
|
586 |
+
}
|
587 |
+
}
|
588 |
+
|
589 |
+
// map the post author
|
590 |
+
$author = sanitize_user( $post['post_author'], true );
|
591 |
+
if ( isset( $this->author_mapping[$author] ) )
|
592 |
+
$author = $this->author_mapping[$author];
|
593 |
+
else
|
594 |
+
$author = (int) get_current_user_id();
|
595 |
+
|
596 |
+
$postdata = array(
|
597 |
+
'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
|
598 |
+
'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
|
599 |
+
'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
|
600 |
+
'post_status' => $post['status'], 'post_name' => $post['post_name'],
|
601 |
+
'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
|
602 |
+
'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
|
603 |
+
'post_type' => $post['post_type'], 'post_password' => $post['post_password']
|
604 |
+
);
|
605 |
+
|
606 |
+
$original_post_ID = $post['post_id'];
|
607 |
+
$postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
|
608 |
+
|
609 |
+
if ( 'attachment' == $postdata['post_type'] ) {
|
610 |
+
$remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
|
611 |
+
|
612 |
+
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
|
613 |
+
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
|
614 |
+
$postdata['upload_date'] = $post['post_date'];
|
615 |
+
if ( isset( $post['postmeta'] ) ) {
|
616 |
+
foreach( $post['postmeta'] as $meta ) {
|
617 |
+
if ( $meta['key'] == '_wp_attached_file' ) {
|
618 |
+
if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
|
619 |
+
$postdata['upload_date'] = $matches[0];
|
620 |
+
break;
|
621 |
+
}
|
622 |
+
}
|
623 |
+
}
|
624 |
+
|
625 |
+
$comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
|
626 |
+
} else {
|
627 |
+
$comment_post_ID = $post_id = wp_insert_post( $postdata, true );
|
628 |
+
do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
|
629 |
+
}
|
630 |
+
|
631 |
+
if ( is_wp_error( $post_id ) ) {
|
632 |
+
printf( __( 'Failed to import %s “%s”', 'wordpress-importer' ),
|
633 |
+
$post_type_object->labels->singular_name, esc_html($post['post_title']) );
|
634 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
635 |
+
echo ': ' . $post_id->get_error_message();
|
636 |
+
echo '<br />';
|
637 |
+
continue;
|
638 |
+
}
|
639 |
+
|
640 |
+
if ( $post['is_sticky'] == 1 )
|
641 |
+
stick_post( $post_id );
|
642 |
+
}
|
643 |
+
|
644 |
+
// map pre-import ID to local ID
|
645 |
+
$this->processed_posts[intval($post['post_id'])] = (int) $post_id;
|
646 |
+
|
647 |
+
if ( ! isset( $post['terms'] ) )
|
648 |
+
$post['terms'] = array();
|
649 |
+
|
650 |
+
$post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
|
651 |
+
|
652 |
+
// add categories, tags and other terms
|
653 |
+
if ( ! empty( $post['terms'] ) ) {
|
654 |
+
$terms_to_set = array();
|
655 |
+
foreach ( $post['terms'] as $term ) {
|
656 |
+
// back compat with WXR 1.0 map 'tag' to 'post_tag'
|
657 |
+
$taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
|
658 |
+
$term_exists = term_exists( $term['slug'], $taxonomy );
|
659 |
+
$term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
|
660 |
+
if ( ! $term_id ) {
|
661 |
+
$t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
|
662 |
+
if ( ! is_wp_error( $t ) ) {
|
663 |
+
$term_id = $t['term_id'];
|
664 |
+
do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
|
665 |
+
} else {
|
666 |
+
printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
|
667 |
+
if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
|
668 |
+
echo ': ' . $t->get_error_message();
|
669 |
+
echo '<br />';
|
670 |
+
do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
|
671 |
+
continue;
|
672 |
+
}
|
673 |
+
}
|
674 |
+
$terms_to_set[$taxonomy][] = intval( $term_id );
|
675 |
+
}
|
676 |
+
|
677 |
+
foreach ( $terms_to_set as $tax => $ids ) {
|
678 |
+
$tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
|
679 |
+
do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
|
680 |
+
}
|
681 |
+
unset( $post['terms'], $terms_to_set );
|
682 |
+
}
|
683 |
+
|
684 |
+
if ( ! isset( $post['comments'] ) )
|
685 |
+
$post['comments'] = array();
|
686 |
+
|
687 |
+
$post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
|
688 |
+
|
689 |
+
// add/update comments
|
690 |
+
if ( ! empty( $post['comments'] ) ) {
|
691 |
+
$num_comments = 0;
|
692 |
+
$inserted_comments = array();
|
693 |
+
foreach ( $post['comments'] as $comment ) {
|
694 |
+
$comment_id = $comment['comment_id'];
|
695 |
+
$newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
|
696 |
+
$newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
|
697 |
+
$newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
|
698 |
+
$newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
|
699 |
+
$newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
|
700 |
+
$newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
|
701 |
+
$newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
|
702 |
+
$newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
|
703 |
+
$newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
|
704 |
+
$newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
|
705 |
+
$newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
|
706 |
+
$newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
|
707 |
+
if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
|
708 |
+
$newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
|
709 |
+
}
|
710 |
+
ksort( $newcomments );
|
711 |
+
|
712 |
+
foreach ( $newcomments as $key => $comment ) {
|
713 |
+
// if this is a new post we can skip the comment_exists() check
|
714 |
+
if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
|
715 |
+
if ( isset( $inserted_comments[$comment['comment_parent']] ) )
|
716 |
+
$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
|
717 |
+
$comment = wp_filter_comment( $comment );
|
718 |
+
$inserted_comments[$key] = wp_insert_comment( $comment );
|
719 |
+
do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
|
720 |
+
|
721 |
+
foreach( $comment['commentmeta'] as $meta ) {
|
722 |
+
$value = maybe_unserialize( $meta['value'] );
|
723 |
+
add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
|
724 |
+
}
|
725 |
+
|
726 |
+
$num_comments++;
|
727 |
+
}
|
728 |
+
}
|
729 |
+
unset( $newcomments, $inserted_comments, $post['comments'] );
|
730 |
+
}
|
731 |
+
|
732 |
+
if ( ! isset( $post['postmeta'] ) )
|
733 |
+
$post['postmeta'] = array();
|
734 |
+
|
735 |
+
$post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
|
736 |
+
|
737 |
+
// add/update post meta
|
738 |
+
if ( ! empty( $post['postmeta'] ) ) {
|
739 |
+
foreach ( $post['postmeta'] as $meta ) {
|
740 |
+
$key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
|
741 |
+
$value = false;
|
742 |
+
|
743 |
+
if ( '_edit_last' == $key ) {
|
744 |
+
if ( isset( $this->processed_authors[intval($meta['value'])] ) )
|
745 |
+
$value = $this->processed_authors[intval($meta['value'])];
|
746 |
+
else
|
747 |
+
$key = false;
|
748 |
+
}
|
749 |
+
|
750 |
+
if ( $key ) {
|
751 |
+
// export gets meta straight from the DB so could have a serialized string
|
752 |
+
if ( ! $value )
|
753 |
+
$value = maybe_unserialize( $meta['value'] );
|
754 |
+
|
755 |
+
add_post_meta( $post_id, $key, $value );
|
756 |
+
do_action( 'import_post_meta', $post_id, $key, $value );
|
757 |
+
|
758 |
+
// if the post has a featured image, take note of this in case of remap
|
759 |
+
if ( '_thumbnail_id' == $key )
|
760 |
+
$this->featured_images[$post_id] = (int) $value;
|
761 |
+
}
|
762 |
+
}
|
763 |
+
}
|
764 |
+
}
|
765 |
+
|
766 |
+
unset( $this->posts );
|
767 |
+
}
|
768 |
+
|
769 |
+
/**
|
770 |
+
* Attempt to create a new menu item from import data
|
771 |
+
*
|
772 |
+
* Fails for draft, orphaned menu items and those without an associated nav_menu
|
773 |
+
* or an invalid nav_menu term. If the post type or term object which the menu item
|
774 |
+
* represents doesn't exist then the menu item will not be imported (waits until the
|
775 |
+
* end of the import to retry again before discarding).
|
776 |
+
*
|
777 |
+
* @param array $item Menu item details from WXR file
|
778 |
+
*/
|
779 |
+
function process_menu_item( $item ) {
|
780 |
+
// skip draft, orphaned menu items
|
781 |
+
if ( 'draft' == $item['status'] )
|
782 |
+
return;
|
783 |
+
|
784 |
+
$menu_slug = false;
|
785 |
+
if ( isset($item['terms']) ) {
|
786 |
+
// loop through terms, assume first nav_menu term is correct menu
|
787 |
+
foreach ( $item['terms'] as $term ) {
|
788 |
+
if ( 'nav_menu' == $term['domain'] ) {
|
789 |
+
$menu_slug = $term['slug'];
|
790 |
+
break;
|
791 |
+
}
|
792 |
+
}
|
793 |
+
}
|
794 |
+
|
795 |
+
// no nav_menu term associated with this menu item
|
796 |
+
if ( ! $menu_slug ) {
|
797 |
+
_e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
|
798 |
+
echo '<br />';
|
799 |
+
return;
|
800 |
+
}
|
801 |
+
|
802 |
+
$menu_id = term_exists( $menu_slug, 'nav_menu' );
|
803 |
+
if ( ! $menu_id ) {
|
804 |
+
printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
|
805 |
+
echo '<br />';
|
806 |
+
return;
|
807 |
+
} else {
|
808 |
+
$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
|
809 |
+
}
|
810 |
+
|
811 |
+
foreach ( $item['postmeta'] as $meta )
|
812 |
+
$$meta['key'] = $meta['value'];
|
813 |
+
|
814 |
+
if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
|
815 |
+
$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
|
816 |
+
} else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
|
817 |
+
$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
|
818 |
+
} else if ( 'custom' != $_menu_item_type ) {
|
819 |
+
// associated object is missing or not imported yet, we'll retry later
|
820 |
+
$this->missing_menu_items[] = $item;
|
821 |
+
return;
|
822 |
+
}
|
823 |
+
|
824 |
+
if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
|
825 |
+
$_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
|
826 |
+
} else if ( $_menu_item_menu_item_parent ) {
|
827 |
+
$this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
|
828 |
+
$_menu_item_menu_item_parent = 0;
|
829 |
+
}
|
830 |
+
|
831 |
+
// wp_update_nav_menu_item expects CSS classes as a space separated string
|
832 |
+
$_menu_item_classes = maybe_unserialize( $_menu_item_classes );
|
833 |
+
if ( is_array( $_menu_item_classes ) )
|
834 |
+
$_menu_item_classes = implode( ' ', $_menu_item_classes );
|
835 |
+
|
836 |
+
$args = array(
|
837 |
+
'menu-item-object-id' => $_menu_item_object_id,
|
838 |
+
'menu-item-object' => $_menu_item_object,
|
839 |
+
'menu-item-parent-id' => $_menu_item_menu_item_parent,
|
840 |
+
'menu-item-position' => intval( $item['menu_order'] ),
|
841 |
+
'menu-item-type' => $_menu_item_type,
|
842 |
+
'menu-item-title' => $item['post_title'],
|
843 |
+
'menu-item-url' => $_menu_item_url,
|
844 |
+
'menu-item-description' => $item['post_content'],
|
845 |
+
'menu-item-attr-title' => $item['post_excerpt'],
|
846 |
+
'menu-item-target' => $_menu_item_target,
|
847 |
+
'menu-item-classes' => $_menu_item_classes,
|
848 |
+
'menu-item-xfn' => $_menu_item_xfn,
|
849 |
+
'menu-item-status' => $item['status']
|
850 |
+
);
|
851 |
+
|
852 |
+
$id = wp_update_nav_menu_item( $menu_id, 0, $args );
|
853 |
+
if ( $id && ! is_wp_error( $id ) )
|
854 |
+
$this->processed_menu_items[intval($item['post_id'])] = (int) $id;
|
855 |
+
}
|
856 |
+
|
857 |
+
/**
|
858 |
+
* If fetching attachments is enabled then attempt to create a new attachment
|
859 |
+
*
|
860 |
+
* @param array $post Attachment post details from WXR
|
861 |
+
* @param string $url URL to fetch attachment from
|
862 |
+
* @return int|WP_Error Post ID on success, WP_Error otherwise
|
863 |
+
*/
|
864 |
+
function process_attachment( $post, $url ) {
|
865 |
+
if ( ! $this->fetch_attachments )
|
866 |
+
return new WP_Error( 'attachment_processing_error',
|
867 |
+
__( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
|
868 |
+
|
869 |
+
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
|
870 |
+
if ( preg_match( '|^/[\w\W]+$|', $url ) )
|
871 |
+
$url = rtrim( $this->base_url, '/' ) . $url;
|
872 |
+
|
873 |
+
$upload = $this->fetch_remote_file( $url, $post );
|
874 |
+
if ( is_wp_error( $upload ) )
|
875 |
+
return $upload;
|
876 |
+
|
877 |
+
if ( $info = wp_check_filetype( $upload['file'] ) )
|
878 |
+
$post['post_mime_type'] = $info['type'];
|
879 |
+
else
|
880 |
+
return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
|
881 |
+
|
882 |
+
$post['guid'] = $upload['url'];
|
883 |
+
|
884 |
+
// as per wp-admin/includes/upload.php
|
885 |
+
$post_id = wp_insert_attachment( $post, $upload['file'] );
|
886 |
+
wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
|
887 |
+
|
888 |
+
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
|
889 |
+
if ( preg_match( '!^image/!', $info['type'] ) ) {
|
890 |
+
$parts = pathinfo( $url );
|
891 |
+
$name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
|
892 |
+
|
893 |
+
$parts_new = pathinfo( $upload['url'] );
|
894 |
+
$name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
|
895 |
+
|
896 |
+
$this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
|
897 |
+
}
|
898 |
+
|
899 |
+
return $post_id;
|
900 |
+
}
|
901 |
+
|
902 |
+
/**
|
903 |
+
* Attempt to download a remote file attachment
|
904 |
+
*
|
905 |
+
* @param string $url URL of item to fetch
|
906 |
+
* @param array $post Attachment details
|
907 |
+
* @return array|WP_Error Local file location details on success, WP_Error otherwise
|
908 |
+
*/
|
909 |
+
function fetch_remote_file( $url, $post ) {
|
910 |
+
// extract the file name and extension from the url
|
911 |
+
$file_name = basename( $url );
|
912 |
+
|
913 |
+
// get placeholder file in the upload dir with a unique, sanitized filename
|
914 |
+
$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
|
915 |
+
if ( $upload['error'] )
|
916 |
+
return new WP_Error( 'upload_dir_error', $upload['error'] );
|
917 |
+
|
918 |
+
// fetch the remote url and write it to the placeholder file
|
919 |
+
$headers = wp_get_http( $url, $upload['file'] );
|
920 |
+
|
921 |
+
// request failed
|
922 |
+
if ( ! $headers ) {
|
923 |
+
@unlink( $upload['file'] );
|
924 |
+
return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
|
925 |
+
}
|
926 |
+
|
927 |
+
// make sure the fetch was successful
|
928 |
+
if ( $headers['response'] != '200' ) {
|
929 |
+
@unlink( $upload['file'] );
|
930 |
+
return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
|
931 |
+
}
|
932 |
+
|
933 |
+
$filesize = filesize( $upload['file'] );
|
934 |
+
|
935 |
+
if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
|
936 |
+
@unlink( $upload['file'] );
|
937 |
+
return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
|
938 |
+
}
|
939 |
+
|
940 |
+
if ( 0 == $filesize ) {
|
941 |
+
@unlink( $upload['file'] );
|
942 |
+
return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
|
943 |
+
}
|
944 |
+
|
945 |
+
$max_size = (int) $this->max_attachment_size();
|
946 |
+
if ( ! empty( $max_size ) && $filesize > $max_size ) {
|
947 |
+
@unlink( $upload['file'] );
|
948 |
+
return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
|
949 |
+
}
|
950 |
+
|
951 |
+
// keep track of the old and new urls so we can substitute them later
|
952 |
+
$this->url_remap[$url] = $upload['url'];
|
953 |
+
$this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
|
954 |
+
// keep track of the destination if the remote url is redirected somewhere else
|
955 |
+
if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
|
956 |
+
$this->url_remap[$headers['x-final-location']] = $upload['url'];
|
957 |
+
|
958 |
+
return $upload;
|
959 |
+
}
|
960 |
+
|
961 |
+
/**
|
962 |
+
* Attempt to associate posts and menu items with previously missing parents
|
963 |
+
*
|
964 |
+
* An imported post's parent may not have been imported when it was first created
|
965 |
+
* so try again. Similarly for child menu items and menu items which were missing
|
966 |
+
* the object (e.g. post) they represent in the menu
|
967 |
+
*/
|
968 |
+
function backfill_parents() {
|
969 |
+
global $wpdb;
|
970 |
+
|
971 |
+
// find parents for post orphans
|
972 |
+
foreach ( $this->post_orphans as $child_id => $parent_id ) {
|
973 |
+
$local_child_id = $local_parent_id = false;
|
974 |
+
if ( isset( $this->processed_posts[$child_id] ) )
|
975 |
+
$local_child_id = $this->processed_posts[$child_id];
|
976 |
+
if ( isset( $this->processed_posts[$parent_id] ) )
|
977 |
+
$local_parent_id = $this->processed_posts[$parent_id];
|
978 |
+
|
979 |
+
if ( $local_child_id && $local_parent_id )
|
980 |
+
$wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
|
981 |
+
}
|
982 |
+
|
983 |
+
// all other posts/terms are imported, retry menu items with missing associated object
|
984 |
+
$missing_menu_items = $this->missing_menu_items;
|
985 |
+
foreach ( $missing_menu_items as $item )
|
986 |
+
$this->process_menu_item( $item );
|
987 |
+
|
988 |
+
// find parents for menu item orphans
|
989 |
+
foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
|
990 |
+
$local_child_id = $local_parent_id = 0;
|
991 |
+
if ( isset( $this->processed_menu_items[$child_id] ) )
|
992 |
+
$local_child_id = $this->processed_menu_items[$child_id];
|
993 |
+
if ( isset( $this->processed_menu_items[$parent_id] ) )
|
994 |
+
$local_parent_id = $this->processed_menu_items[$parent_id];
|
995 |
+
|
996 |
+
if ( $local_child_id && $local_parent_id )
|
997 |
+
update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
|
998 |
+
}
|
999 |
+
}
|
1000 |
+
|
1001 |
+
/**
|
1002 |
+
* Use stored mapping information to update old attachment URLs
|
1003 |
+
*/
|
1004 |
+
function backfill_attachment_urls() {
|
1005 |
+
global $wpdb;
|
1006 |
+
// make sure we do the longest urls first, in case one is a substring of another
|
1007 |
+
uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
|
1008 |
+
|
1009 |
+
foreach ( $this->url_remap as $from_url => $to_url ) {
|
1010 |
+
// remap urls in post_content
|
1011 |
+
$wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
|
1012 |
+
// remap enclosure urls
|
1013 |
+
$result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
|
1014 |
+
}
|
1015 |
+
}
|
1016 |
+
|
1017 |
+
/**
|
1018 |
+
* Update _thumbnail_id meta to new, imported attachment IDs
|
1019 |
+
*/
|
1020 |
+
function remap_featured_images() {
|
1021 |
+
// cycle through posts that have a featured image
|
1022 |
+
foreach ( $this->featured_images as $post_id => $value ) {
|
1023 |
+
if ( isset( $this->processed_posts[$value] ) ) {
|
1024 |
+
$new_id = $this->processed_posts[$value];
|
1025 |
+
// only update if there's a difference
|
1026 |
+
if ( $new_id != $value )
|
1027 |
+
update_post_meta( $post_id, '_thumbnail_id', $new_id );
|
1028 |
+
}
|
1029 |
+
}
|
1030 |
+
}
|
1031 |
+
|
1032 |
+
/**
|
1033 |
+
* Parse a WXR file
|
1034 |
+
*
|
1035 |
+
* @param string $file Path to WXR file for parsing
|
1036 |
+
* @return array Information gathered from the WXR file
|
1037 |
+
*/
|
1038 |
+
function parse( $file ) {
|
1039 |
+
$parser = new WXR_Parser();
|
1040 |
+
return $parser->parse( $file );
|
1041 |
+
}
|
1042 |
+
|
1043 |
+
// Display import page title
|
1044 |
+
function header() {
|
1045 |
+
echo '<div class="wrap">';
|
1046 |
+
screen_icon();
|
1047 |
+
echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
|
1048 |
+
|
1049 |
+
$updates = get_plugin_updates();
|
1050 |
+
$basename = plugin_basename(__FILE__);
|
1051 |
+
if ( isset( $updates[$basename] ) ) {
|
1052 |
+
$update = $updates[$basename];
|
1053 |
+
echo '<div class="error"><p><strong>';
|
1054 |
+
printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version );
|
1055 |
+
echo '</strong></p></div>';
|
1056 |
+
}
|
1057 |
+
}
|
1058 |
+
|
1059 |
+
// Close div.wrap
|
1060 |
+
function footer() {
|
1061 |
+
echo '</div>';
|
1062 |
+
}
|
1063 |
+
|
1064 |
+
/**
|
1065 |
+
* Display introductory text and file upload form
|
1066 |
+
*/
|
1067 |
+
function greet() {
|
1068 |
+
echo '<div class="narrow">';
|
1069 |
+
echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>';
|
1070 |
+
echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
|
1071 |
+
wp_import_upload_form( 'admin.php?import=wordpress&step=1' );
|
1072 |
+
echo '</div>';
|
1073 |
+
}
|
1074 |
+
|
1075 |
+
/**
|
1076 |
+
* Decide if the given meta key maps to information we will want to import
|
1077 |
+
*
|
1078 |
+
* @param string $key The meta key to check
|
1079 |
+
* @return string|bool The key if we do want to import, false if not
|
1080 |
+
*/
|
1081 |
+
function is_valid_meta_key( $key ) {
|
1082 |
+
// skip attachment metadata since we'll regenerate it from scratch
|
1083 |
+
// skip _edit_lock as not relevant for import
|
1084 |
+
if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
|
1085 |
+
return false;
|
1086 |
+
return $key;
|
1087 |
+
}
|
1088 |
+
|
1089 |
+
/**
|
1090 |
+
* Decide whether or not the importer is allowed to create users.
|
1091 |
+
* Default is true, can be filtered via import_allow_create_users
|
1092 |
+
*
|
1093 |
+
* @return bool True if creating users is allowed
|
1094 |
+
*/
|
1095 |
+
function allow_create_users() {
|
1096 |
+
return apply_filters( 'import_allow_create_users', true );
|
1097 |
+
}
|
1098 |
+
|
1099 |
+
/**
|
1100 |
+
* Decide whether or not the importer should attempt to download attachment files.
|
1101 |
+
* Default is true, can be filtered via import_allow_fetch_attachments. The choice
|
1102 |
+
* made at the import options screen must also be true, false here hides that checkbox.
|
1103 |
+
*
|
1104 |
+
* @return bool True if downloading attachments is allowed
|
1105 |
+
*/
|
1106 |
+
function allow_fetch_attachments() {
|
1107 |
+
return apply_filters( 'import_allow_fetch_attachments', true );
|
1108 |
+
}
|
1109 |
+
|
1110 |
+
/**
|
1111 |
+
* Decide what the maximum file size for downloaded attachments is.
|
1112 |
+
* Default is 0 (unlimited), can be filtered via import_attachment_size_limit
|
1113 |
+
*
|
1114 |
+
* @return int Maximum attachment file size to import
|
1115 |
+
*/
|
1116 |
+
function max_attachment_size() {
|
1117 |
+
return apply_filters( 'import_attachment_size_limit', 0 );
|
1118 |
+
}
|
1119 |
+
|
1120 |
+
/**
|
1121 |
+
* Added to http_request_timeout filter to force timeout at 60 seconds during import
|
1122 |
+
* @return int 60
|
1123 |
+
*/
|
1124 |
+
function bump_request_timeout( $val ) {
|
1125 |
+
return 60;
|
1126 |
+
}
|
1127 |
+
|
1128 |
+
// return the difference in length between two strings
|
1129 |
+
function cmpr_strlen( $a, $b ) {
|
1130 |
+
return strlen($b) - strlen($a);
|
1131 |
+
}
|
1132 |
+
}
|
1133 |
+
|
1134 |
+
} // class_exists( 'WP_Importer' )
|
1135 |
+
|
1136 |
+
function wordpress_importer_init() {
|
1137 |
+
load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
|
1138 |
+
|
1139 |
+
/**
|
1140 |
+
* WordPress Importer object for registering the import callback
|
1141 |
+
* @global WP_Import $wp_import
|
1142 |
+
*/
|
1143 |
+
$GLOBALS['wp_import'] = new WP_Import();
|
1144 |
+
register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) );
|
1145 |
+
}
|
1146 |
+
add_action( 'admin_init', 'wordpress_importer_init' );
|