WordPress Importer - Version 0.2

Version Description

Download this release

Release Info

Developer nacin
Plugin Icon 128x128 WordPress Importer
Version 0.2
Comparing to
See all releases

Version 0.2

languages/wordpress-importer.pot ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Translation of the WordPress plugin WordPress Importer 0.2 by wordpressdotorg.
2
+ # Copyright (C) 2010 wordpressdotorg
3
+ # This file is distributed under the same license as the WordPress Importer package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, 2010.
5
+ #
6
+ #, fuzzy
7
+ msgid ""
8
+ msgstr ""
9
+ "Project-Id-Version: WordPress Importer 0.2\n"
10
+ "Report-Msgid-Bugs-To: http://wordpress.org/tag/wordpress-importer\n"
11
+ "POT-Creation-Date: 2010-06-01 13:26+0300\n"
12
+ "PO-Revision-Date: 2010-MO-DA HO:MI+ZONE\n"
13
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
+ "Language-Team: LANGUAGE <LL@li.org>\n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=utf-8\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
19
+
20
+ #: wordpress-importer.php:55
21
+ msgid "Import WordPress"
22
+ msgstr ""
23
+
24
+ #: wordpress-importer.php:64
25
+ msgid ""
26
+ "Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import "
27
+ "the posts, pages, comments, custom fields, categories, and tags into this "
28
+ "site."
29
+ msgstr ""
30
+
31
+ #: wordpress-importer.php:65
32
+ msgid ""
33
+ "Choose a WordPress WXR file to upload, then click Upload file and import."
34
+ msgstr ""
35
+
36
+ #: wordpress-importer.php:228
37
+ msgid "Assign Authors"
38
+ msgstr ""
39
+
40
+ #: wordpress-importer.php:229
41
+ msgid ""
42
+ "To make it easier for you to edit and save the imported posts and drafts, "
43
+ "you may want to change the name of the author of the posts. For example, you "
44
+ "may want to import all the entries as <code>admin</code>s entries."
45
+ msgstr ""
46
+
47
+ #: wordpress-importer.php:232
48
+ msgid ""
49
+ "If a new user is created by WordPress, a password will be randomly "
50
+ "generated. Manually change the user&#8217;s details if necessary."
51
+ msgstr ""
52
+
53
+ #: wordpress-importer.php:245
54
+ msgid "Import author:"
55
+ msgstr ""
56
+
57
+ #: wordpress-importer.php:253
58
+ msgid "Import Attachments"
59
+ msgstr ""
60
+
61
+ #: wordpress-importer.php:256
62
+ msgid "Download and import file attachments"
63
+ msgstr ""
64
+
65
+ #: wordpress-importer.php:263
66
+ msgid "Submit"
67
+ msgstr ""
68
+
69
+ #: wordpress-importer.php:272
70
+ #, php-format
71
+ msgid "Create user %1$s or map to existing"
72
+ msgstr ""
73
+
74
+ #: wordpress-importer.php:275
75
+ msgid "Map to existing"
76
+ msgstr ""
77
+
78
+ #: wordpress-importer.php:283
79
+ msgid "- Select -"
80
+ msgstr ""
81
+
82
+ #: wordpress-importer.php:299
83
+ msgid "Invalid file"
84
+ msgstr ""
85
+
86
+ #: wordpress-importer.php:300
87
+ msgid "Please upload a valid WXR (WordPress eXtended RSS) export file."
88
+ msgstr ""
89
+
90
+ #: wordpress-importer.php:343
91
+ #, php-format
92
+ msgid "Importing category <em>%s</em>&#8230;"
93
+ msgstr ""
94
+
95
+ #: wordpress-importer.php:365
96
+ #, php-format
97
+ msgid "Importing tag <em>%s</em>&#8230;"
98
+ msgstr ""
99
+
100
+ #: wordpress-importer.php:403
101
+ #, php-format
102
+ msgid "Importing <em>%s</em>&#8230;"
103
+ msgstr ""
104
+
105
+ #: wordpress-importer.php:424
106
+ msgid "All done."
107
+ msgstr ""
108
+
109
+ #: wordpress-importer.php:424
110
+ msgid "Have fun!"
111
+ msgstr ""
112
+
113
+ #: wordpress-importer.php:488
114
+ #, php-format
115
+ msgid "Post <em>%s</em> already exists."
116
+ msgstr ""
117
+
118
+ #: wordpress-importer.php:521
119
+ #, php-format
120
+ msgid "Importing post <em>%s</em>..."
121
+ msgstr ""
122
+
123
+ #: wordpress-importer.php:617
124
+ #, php-format
125
+ msgid "(%s comment)"
126
+ msgid_plural "(%s comments)"
127
+ msgstr[0] ""
128
+ msgstr[1] ""
129
+
130
+ #: wordpress-importer.php:645
131
+ #, php-format
132
+ msgid "Importing attachment <em>%s</em>... "
133
+ msgstr ""
134
+
135
+ #: wordpress-importer.php:653
136
+ #, php-format
137
+ msgid "Remote file error: %s"
138
+ msgstr ""
139
+
140
+ #: wordpress-importer.php:661
141
+ msgid "Zero length file, deleting"
142
+ msgstr ""
143
+
144
+ #: wordpress-importer.php:670
145
+ msgid "Invalid file type"
146
+ msgstr ""
147
+
148
+ #: wordpress-importer.php:691
149
+ #, php-format
150
+ msgid "Skipping attachment <em>%s</em>"
151
+ msgstr ""
152
+
153
+ #: wordpress-importer.php:716
154
+ msgid "Remote server did not respond"
155
+ msgstr ""
156
+
157
+ #: wordpress-importer.php:722
158
+ #, php-format
159
+ msgid "Remote file returned error response %1$d %2$s"
160
+ msgstr ""
161
+
162
+ #: wordpress-importer.php:726
163
+ msgid "Remote file is incorrect size"
164
+ msgstr ""
165
+
166
+ #: wordpress-importer.php:732
167
+ #, php-format
168
+ msgid "Remote file is too large, limit is %s"
169
+ msgstr ""
170
+
171
+ #: wordpress-importer.php:864
172
+ msgid "Sorry, there has been an error."
173
+ msgstr ""
174
+
175
+ #: wordpress-importer.php:914
176
+ msgid ""
177
+ "Import <strong>posts, pages, comments, custom fields, categories, and tags</"
178
+ "strong> from a WordPress export file."
179
+ msgstr ""
180
+
181
+ #. Plugin Name of the plugin/theme
182
+ msgid "WordPress Importer"
183
+ msgstr ""
184
+
185
+ #. Plugin URI of the plugin/theme
186
+ msgid "http://wordpress.org/extend/plugins/wordpress-importer/"
187
+ msgstr ""
188
+
189
+ #. Description of the plugin/theme
190
+ msgid ""
191
+ "Import posts, pages, comments, custom fields, categories, and tags from a "
192
+ "WordPress export file."
193
+ msgstr ""
194
+
195
+ #. Author of the plugin/theme
196
+ msgid "wordpressdotorg"
197
+ msgstr ""
198
+
199
+ #. Author URI of the plugin/theme
200
+ msgid "http://wordpress.org/"
201
+ msgstr ""
readme.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: wordpressdotorg
3
+ Donate link:
4
+ Tags: importer, wordpress
5
+ Requires at least: 3.0
6
+ Tested up to: 3.0
7
+ Stable tag: 0.2
8
+
9
+ Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.
10
+
11
+ == Description ==
12
+
13
+ Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.
14
+
15
+ == Installation ==
16
+
17
+ 1. Upload the `wordpress-importer` folder to the `/wp-content/plugins/` directory
18
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
19
+ 1. Go to the Tools -> Import screen, Click on WordPress
20
+
21
+ == Frequently Asked Questions ==
22
+
23
+ == Screenshots ==
24
+
25
+ == Changelog ==
26
+
27
+ = 0.1 =
28
+ * Initial release
wordpress-importer.php ADDED
@@ -0,0 +1,921 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, and tags from a WordPress export file.
6
+ Author: wordpressdotorg
7
+ Author URI: http://wordpress.org/
8
+ Version: 0.2
9
+ Stable tag: 0.2
10
+ License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11
+ */
12
+
13
+ if ( !defined('WP_LOAD_IMPORTERS') )
14
+ return;
15
+
16
+ // Load Importer API
17
+ require_once ABSPATH . 'wp-admin/includes/import.php';
18
+
19
+ if ( !class_exists( 'WP_Importer' ) ) {
20
+ $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
21
+ if ( file_exists( $class_wp_importer ) )
22
+ require_once $class_wp_importer;
23
+ }
24
+
25
+ /**
26
+ * WordPress Importer
27
+ *
28
+ * @package WordPress
29
+ * @subpackage Importer
30
+ */
31
+ if ( class_exists( 'WP_Importer' ) ) {
32
+ class WP_Import extends WP_Importer {
33
+
34
+ var $post_ids_processed = array ();
35
+ var $orphans = array ();
36
+ var $file;
37
+ var $id;
38
+ var $mtnames = array ();
39
+ var $newauthornames = array ();
40
+ var $allauthornames = array ();
41
+
42
+ var $author_ids = array ();
43
+ var $tags = array ();
44
+ var $categories = array ();
45
+ var $terms = array ();
46
+ var $authors = array ();
47
+
48
+ var $j = -1;
49
+ var $fetch_attachments = false;
50
+ var $url_remap = array ();
51
+
52
+ function header() {
53
+ echo '<div class="wrap">';
54
+ screen_icon();
55
+ echo '<h2>'.__('Import WordPress', 'wordpress-importer').'</h2>';
56
+ }
57
+
58
+ function footer() {
59
+ echo '</div>';
60
+ }
61
+
62
+ function greet() {
63
+ echo '<div class="narrow">';
64
+ echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer').'</p>';
65
+ echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.', 'wordpress-importer').'</p>';
66
+ wp_import_upload_form("admin.php?import=wordpress&amp;step=1");
67
+ echo '</div>';
68
+ }
69
+
70
+ function get_tag( $string, $tag ) {
71
+ global $wpdb;
72
+ preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
73
+ if ( isset($return[1]) ) {
74
+ $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
75
+ $return = $wpdb->escape( trim( $return ) );
76
+ } else {
77
+ $return = '';
78
+ }
79
+ return $return;
80
+ }
81
+
82
+ function has_gzip() {
83
+ return is_callable('gzopen');
84
+ }
85
+
86
+ function fopen($filename, $mode='r') {
87
+ if ( $this->has_gzip() )
88
+ return gzopen($filename, $mode);
89
+ return fopen($filename, $mode);
90
+ }
91
+
92
+ function feof($fp) {
93
+ if ( $this->has_gzip() )
94
+ return gzeof($fp);
95
+ return feof($fp);
96
+ }
97
+
98
+ function fgets($fp, $len=8192) {
99
+ if ( $this->has_gzip() )
100
+ return gzgets($fp, $len);
101
+ return fgets($fp, $len);
102
+ }
103
+
104
+ function fclose($fp) {
105
+ if ( $this->has_gzip() )
106
+ return gzclose($fp);
107
+ return fclose($fp);
108
+ }
109
+
110
+ function get_entries($process_post_func=NULL) {
111
+ set_magic_quotes_runtime(0);
112
+
113
+ $doing_entry = false;
114
+ $is_wxr_file = false;
115
+
116
+ $fp = $this->fopen($this->file, 'r');
117
+ if ($fp) {
118
+ while ( !$this->feof($fp) ) {
119
+ $importline = rtrim($this->fgets($fp));
120
+
121
+ // this doesn't check that the file is perfectly valid but will at least confirm that it's not the wrong format altogether
122
+ if ( !$is_wxr_file && preg_match('|xmlns:wp="http://wordpress[.]org/export/\d+[.]\d+/"|', $importline) )
123
+ $is_wxr_file = true;
124
+
125
+ if ( false !== strpos($importline, '<wp:base_site_url>') ) {
126
+ preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url);
127
+ $this->base_url = $url[1];
128
+ continue;
129
+ }
130
+ if ( false !== strpos($importline, '<wp:category>') ) {
131
+ preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
132
+ $this->categories[] = $category[1];
133
+ continue;
134
+ }
135
+ if ( false !== strpos($importline, '<wp:tag>') ) {
136
+ preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
137
+ $this->tags[] = $tag[1];
138
+ continue;
139
+ }
140
+ if ( false !== strpos($importline, '<wp:term>') ) {
141
+ preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
142
+ $this->terms[] = $term[1];
143
+ continue;
144
+ }
145
+ if ( false !== strpos($importline, '<wp:author>') ) {
146
+ preg_match('|<wp:author>(.*?)</wp:author>|is', $importline, $author);
147
+ $this->authors[] = $author[1];
148
+ continue;
149
+ }
150
+ if ( false !== strpos($importline, '<item>') ) {
151
+ $this->post = '';
152
+ $doing_entry = true;
153
+ continue;
154
+ }
155
+ if ( false !== strpos($importline, '</item>') ) {
156
+ $doing_entry = false;
157
+ if ($process_post_func)
158
+ call_user_func($process_post_func, $this->post);
159
+ continue;
160
+ }
161
+ if ( $doing_entry ) {
162
+ $this->post .= $importline . "\n";
163
+ }
164
+ }
165
+
166
+ $this->fclose($fp);
167
+ }
168
+
169
+ return $is_wxr_file;
170
+
171
+ }
172
+
173
+ function get_wp_authors() {
174
+ // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
175
+ $temp = $this->allauthornames;
176
+ $authors[0] = array_shift($temp);
177
+ $y = count($temp) + 1;
178
+ for ($x = 1; $x < $y; $x ++) {
179
+ $next = array_shift($temp);
180
+ if (!(in_array($next, $authors)))
181
+ array_push($authors, $next);
182
+ }
183
+
184
+ return $authors;
185
+ }
186
+
187
+ function get_authors_from_post() {
188
+ global $current_user;
189
+
190
+ // this will populate $this->author_ids with a list of author_names => user_ids
191
+
192
+ foreach ( (array) $_POST['author_in'] as $i => $in_author_name ) {
193
+
194
+ if ( !empty($_POST['user_select'][$i]) ) {
195
+ // an existing user was selected in the dropdown list
196
+ $user = get_userdata( intval($_POST['user_select'][$i]) );
197
+ if ( isset($user->ID) )
198
+ $this->author_ids[$in_author_name] = $user->ID;
199
+ }
200
+ elseif ( $this->allow_create_users() ) {
201
+ // nothing was selected in the dropdown list, so we'll use the name in the text field
202
+
203
+ $new_author_name = trim($_POST['user_create'][$i]);
204
+ // if the user didn't enter a name, assume they want to use the same name as in the import file
205
+ if ( empty($new_author_name) )
206
+ $new_author_name = $in_author_name;
207
+
208
+ $user_id = username_exists($new_author_name);
209
+ if ( !$user_id ) {
210
+ $user_id = wp_create_user($new_author_name, wp_generate_password());
211
+ }
212
+
213
+ if ( !is_wp_error( $user_id ) ) {
214
+ $this->author_ids[$in_author_name] = $user_id;
215
+ }
216
+ }
217
+
218
+ // failsafe: if the user_id was invalid, default to the current user
219
+ if ( empty($this->author_ids[$in_author_name]) ) {
220
+ $this->author_ids[$in_author_name] = intval($current_user->ID);
221
+ }
222
+ }
223
+
224
+ }
225
+
226
+ function wp_authors_form() {
227
+ ?>
228
+ <h2><?php _e('Assign Authors', 'wordpress-importer'); ?></h2>
229
+ <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer'); ?></p>
230
+ <?php
231
+ if ( $this->allow_create_users() ) {
232
+ echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.', 'wordpress-importer')."</p>\n";
233
+ }
234
+
235
+
236
+ $authors = $this->get_wp_authors();
237
+ echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
238
+ wp_nonce_field('import-wordpress');
239
+ ?>
240
+ <ol id="authors">
241
+ <?php
242
+ $j = -1;
243
+ foreach ($authors as $author) {
244
+ ++ $j;
245
+ echo '<li>'.__('Import author:', 'wordpress-importer').' <strong>'.$author.'</strong><br />';
246
+ $this->users_form($j, $author);
247
+ echo '</li>';
248
+ }
249
+
250
+ if ( $this->allow_fetch_attachments() ) {
251
+ ?>
252
+ </ol>
253
+ <h2><?php _e('Import Attachments', 'wordpress-importer'); ?></h2>
254
+ <p>
255
+ <input type="checkbox" value="1" name="attachments" id="import-attachments" />
256
+ <label for="import-attachments"><?php _e('Download and import file attachments', 'wordpress-importer') ?></label>
257
+ </p>
258
+
259
+ <?php
260
+ }
261
+
262
+ echo '<p class="submit">';
263
+ echo '<input type="submit" class="button" value="'. esc_attr__('Submit', 'wordpress-importer') .'" />'.'<br />';
264
+ echo '</p>';
265
+ echo '</form>';
266
+
267
+ }
268
+
269
+ function users_form($n, $author) {
270
+
271
+ if ( $this->allow_create_users() ) {
272
+ printf('<label>'.__('Create user %1$s or map to existing', 'wordpress-importer'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user_create['.intval($n).']'.'" maxlength="30" /></label> <br />');
273
+ }
274
+ else {
275
+ echo __('Map to existing', 'wordpress-importer').'<br />';
276
+ }
277
+
278
+ // keep track of $n => $author name
279
+ echo '<input type="hidden" name="author_in['.intval($n).']" value="' . esc_attr($author).'" />';
280
+
281
+ $users = get_users_of_blog();
282
+ ?><select name="user_select[<?php echo $n; ?>]">
283
+ <option value="0"><?php _e('- Select -', 'wordpress-importer'); ?></option>
284
+ <?php
285
+ foreach ($users as $user) {
286
+ echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
287
+ }
288
+ ?>
289
+ </select>
290
+ <?php
291
+ }
292
+
293
+ function select_authors() {
294
+ $is_wxr_file = $this->get_entries(array(&$this, 'process_author'));
295
+ if ( $is_wxr_file ) {
296
+ $this->wp_authors_form();
297
+ }
298
+ else {
299
+ echo '<h2>'.__('Invalid file', 'wordpress-importer').'</h2>';
300
+ echo '<p>'.__('Please upload a valid WXR (WordPress eXtended RSS) export file.', 'wordpress-importer').'</p>';
301
+ }
302
+ }
303
+
304
+ // fetch the user ID for a given author name, respecting the mapping preferences
305
+ function checkauthor($author) {
306
+ global $current_user;
307
+
308
+ if ( !empty($this->author_ids[$author]) )
309
+ return $this->author_ids[$author];
310
+
311
+ // failsafe: map to the current user
312
+ return $current_user->ID;
313
+ }
314
+
315
+
316
+
317
+ function process_categories() {
318
+ global $wpdb;
319
+
320
+ $cat_names = (array) get_terms('category', array('fields' => 'names'));
321
+
322
+ while ( $c = array_shift($this->categories) ) {
323
+ $cat_name = trim($this->get_tag( $c, 'wp:cat_name' ));
324
+
325
+ // If the category exists we leave it alone
326
+ if ( in_array($cat_name, $cat_names) )
327
+ continue;
328
+
329
+ $category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
330
+ $category_description = $this->get_tag( $c, 'wp:category_description' );
331
+ $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
332
+ $links_private = (int) $this->get_tag( $c, 'wp:links_private' );
333
+
334
+ $parent = $this->get_tag( $c, 'wp:category_parent' );
335
+
336
+ if ( empty($parent) )
337
+ $category_parent = '0';
338
+ else
339
+ $category_parent = category_exists($parent);
340
+
341
+ $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name', 'category_description');
342
+
343
+ print '<em>' . sprintf( __( 'Importing category <em>%s</em>&#8230;' , 'wordpress-importer'), esc_html($cat_name) ) . '</em><br />' . "\n";
344
+ $cat_ID = wp_insert_category($catarr);
345
+ }
346
+ }
347
+
348
+ function process_tags() {
349
+ global $wpdb;
350
+
351
+ $tag_names = (array) get_terms('post_tag', array('fields' => 'names'));
352
+
353
+ while ( $c = array_shift($this->tags) ) {
354
+ $tag_name = trim($this->get_tag( $c, 'wp:tag_name' ));
355
+
356
+ // If the category exists we leave it alone
357
+ if ( in_array($tag_name, $tag_names) )
358
+ continue;
359
+
360
+ $slug = $this->get_tag( $c, 'wp:tag_slug' );
361
+ $description = $this->get_tag( $c, 'wp:tag_description' );
362
+
363
+ $tagarr = compact('slug', 'description');
364
+
365
+ print '<em>' . sprintf( __( 'Importing tag <em>%s</em>&#8230;' , 'wordpress-importer'), esc_html($tag_name) ) . '</em><br />' . "\n";
366
+ $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
367
+ }
368
+ }
369
+
370
+ function process_terms() {
371
+ global $wpdb, $wp_taxonomies;
372
+
373
+ $custom_taxonomies = $wp_taxonomies;
374
+ // get rid of the standard taxonomies
375
+ unset( $custom_taxonomies['category'] );
376
+ unset( $custom_taxonomies['post_tag'] );
377
+ unset( $custom_taxonomies['link_category'] );
378
+
379
+ $custom_taxonomies = array_keys( $custom_taxonomies );
380
+ $current_terms = (array) get_terms( $custom_taxonomies, array('get' => 'all') );
381
+ $taxonomies = array();
382
+ foreach ( $current_terms as $term ) {
383
+ if ( isset( $_terms[$term->taxonomy] ) ) {
384
+ $taxonomies[$term->taxonomy] = array_merge( $taxonomies[$term->taxonomy], array($term->name) );
385
+ } else {
386
+ $taxonomies[$term->taxonomy] = array($term->name);
387
+ }
388
+ }
389
+
390
+ while ( $c = array_shift($this->terms) ) {
391
+ $term_name = trim($this->get_tag( $c, 'wp:term_name' ));
392
+ $term_taxonomy = trim($this->get_tag( $c, 'wp:term_taxonomy' ));
393
+
394
+ // If the term exists in the taxonomy we leave it alone
395
+ if ( isset($taxonomies[$term_taxonomy] ) && in_array( $term_name, $taxonomies[$term_taxonomy] ) )
396
+ continue;
397
+
398
+ $slug = $this->get_tag( $c, 'wp:term_slug' );
399
+ $description = $this->get_tag( $c, 'wp:term_description' );
400
+
401
+ $termarr = compact('slug', 'description');
402
+
403
+ print '<em>' . sprintf( __( 'Importing <em>%s</em>&#8230;' , 'wordpress-importer'), esc_html($term_name) ) . '</em><br />' . "\n";
404
+ $term_ID = wp_insert_term($term_name, $this->get_tag( $c, 'wp:term_taxonomy' ), $termarr);
405
+ }
406
+ }
407
+
408
+ function process_author($post) {
409
+ $author = $this->get_tag( $post, 'dc:creator' );
410
+ if ($author)
411
+ $this->allauthornames[] = $author;
412
+ }
413
+
414
+ function process_posts() {
415
+ echo '<ol>';
416
+
417
+ $this->get_entries(array(&$this, 'process_post'));
418
+
419
+ echo '</ol>';
420
+
421
+ wp_import_cleanup($this->id);
422
+ do_action('import_done', 'wordpress');
423
+
424
+ echo '<h3>'.sprintf(__('All done.', 'wordpress-importer').' <a href="%s">'.__('Have fun!', 'wordpress-importer').'</a>', get_option('home')).'</h3>';
425
+ }
426
+
427
+ function _normalize_tag( $matches ) {
428
+ return '<' . strtolower( $matches[1] );
429
+ }
430
+
431
+ function process_post($post) {
432
+ global $wpdb;
433
+
434
+ $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
435
+ if ( $post_ID && !empty($this->post_ids_processed[$post_ID]) ) // Processed already
436
+ return 0;
437
+
438
+ set_time_limit( 60 );
439
+
440
+ // There are only ever one of these
441
+ $post_title = $this->get_tag( $post, 'title' );
442
+ $post_date = $this->get_tag( $post, 'wp:post_date' );
443
+ $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
444
+ $comment_status = $this->get_tag( $post, 'wp:comment_status' );
445
+ $ping_status = $this->get_tag( $post, 'wp:ping_status' );
446
+ $post_status = $this->get_tag( $post, 'wp:status' );
447
+ $post_name = $this->get_tag( $post, 'wp:post_name' );
448
+ $post_parent = $this->get_tag( $post, 'wp:post_parent' );
449
+ $menu_order = $this->get_tag( $post, 'wp:menu_order' );
450
+ $post_type = $this->get_tag( $post, 'wp:post_type' );
451
+ $post_password = $this->get_tag( $post, 'wp:post_password' );
452
+ $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
453
+ $guid = $this->get_tag( $post, 'guid' );
454
+ $post_author = $this->get_tag( $post, 'dc:creator' );
455
+
456
+ $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
457
+ $post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt);
458
+ $post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
459
+ $post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
460
+
461
+ $post_content = $this->get_tag( $post, 'content:encoded' );
462
+ $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
463
+ $post_content = str_replace('<br>', '<br />', $post_content);
464
+ $post_content = str_replace('<hr>', '<hr />', $post_content);
465
+
466
+ preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
467
+ $tags = $tags[1];
468
+
469
+ $tag_index = 0;
470
+ foreach ($tags as $tag) {
471
+ $tags[$tag_index] = $wpdb->escape( html_entity_decode( str_replace(array( '<![CDATA[', ']]>' ), '', $tag ) ) );
472
+ $tag_index++;
473
+ }
474
+
475
+ preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
476
+ $categories = $categories[1];
477
+
478
+ $cat_index = 0;
479
+ foreach ($categories as $category) {
480
+ $categories[$cat_index] = $wpdb->escape( html_entity_decode( str_replace( array( '<![CDATA[', ']]>' ), '', $category ) ) );
481
+ $cat_index++;
482
+ }
483
+
484
+ $post_exists = post_exists($post_title, '', $post_date);
485
+
486
+ if ( $post_exists ) {
487
+ echo '<li>';
488
+ printf(__('Post <em>%s</em> already exists.', 'wordpress-importer'), stripslashes($post_title));
489
+ $comment_post_ID = $post_id = $post_exists;
490
+ } else {
491
+
492
+ // If it has parent, process parent first.
493
+ $post_parent = (int) $post_parent;
494
+ if ($post_parent) {
495
+ // if we already know the parent, map it to the local ID
496
+ if ( isset( $this->post_ids_processed[$post_parent] ) ) {
497
+ $post_parent = $this->post_ids_processed[$post_parent]; // new ID of the parent
498
+ }
499
+ else {
500
+ // record the parent for later
501
+ $this->orphans[intval($post_ID)] = $post_parent;
502
+ }
503
+ }
504
+
505
+ echo '<li>';
506
+
507
+ $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
508
+
509
+ $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'post_status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password');
510
+ $postdata['import_id'] = $post_ID;
511
+ if ($post_type == 'attachment') {
512
+ $remote_url = $this->get_tag( $post, 'wp:attachment_url' );
513
+ if ( !$remote_url )
514
+ $remote_url = $guid;
515
+
516
+ $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
517
+ if ( !$post_id or is_wp_error($post_id) )
518
+ return $post_id;
519
+ }
520
+ else {
521
+ printf(__('Importing post <em>%s</em>...', 'wordpress-importer') . "\n", stripslashes($post_title));
522
+ $comment_post_ID = $post_id = wp_insert_post($postdata);
523
+ if ( $post_id && $is_sticky == 1 )
524
+ stick_post( $post_id );
525
+
526
+ }
527
+
528
+ if ( is_wp_error( $post_id ) )
529
+ return $post_id;
530
+
531
+ // Memorize old and new ID.
532
+ if ( $post_id && $post_ID ) {
533
+ $this->post_ids_processed[intval($post_ID)] = intval($post_id);
534
+ }
535
+
536
+ // Add categories.
537
+ if (count($categories) > 0) {
538
+ $post_cats = array();
539
+ foreach ($categories as $category) {
540
+ if ( '' == $category )
541
+ continue;
542
+ $slug = sanitize_term_field('slug', $category, 0, 'category', 'db');
543
+ $cat = get_term_by('slug', $slug, 'category');
544
+ $cat_ID = 0;
545
+ if ( ! empty($cat) )
546
+ $cat_ID = $cat->term_id;
547
+ if ($cat_ID == 0) {
548
+ $category = $wpdb->escape($category);
549
+ $cat_ID = wp_insert_category(array('cat_name' => $category));
550
+ if ( is_wp_error($cat_ID) )
551
+ continue;
552
+ }
553
+ $post_cats[] = $cat_ID;
554
+ }
555
+ wp_set_post_categories($post_id, $post_cats);
556
+ }
557
+
558
+ // Add tags.
559
+ if (count($tags) > 0) {
560
+ $post_tags = array();
561
+ foreach ($tags as $tag) {
562
+ if ( '' == $tag )
563
+ continue;
564
+ $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
565
+ $tag_obj = get_term_by('slug', $slug, 'post_tag');
566
+ $tag_id = 0;
567
+ if ( ! empty($tag_obj) )
568
+ $tag_id = $tag_obj->term_id;
569
+ if ( $tag_id == 0 ) {
570
+ $tag = $wpdb->escape($tag);
571
+ $tag_id = wp_insert_term($tag, 'post_tag');
572
+ if ( is_wp_error($tag_id) )
573
+ continue;
574
+ $tag_id = $tag_id['term_id'];
575
+ }
576
+ $post_tags[] = intval($tag_id);
577
+ }
578
+ wp_set_post_tags($post_id, $post_tags);
579
+ }
580
+ }
581
+
582
+ // Now for comments
583
+ preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
584
+ $comments = $comments[1];
585
+ $num_comments = 0;
586
+ $inserted_comments = array();
587
+ if ( $comments) {
588
+ foreach ($comments as $comment) {
589
+ $comment_id = $this->get_tag( $comment, 'wp:comment_id');
590
+ $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
591
+ $newcomments[$comment_id]['comment_author'] = $this->get_tag( $comment, 'wp:comment_author');
592
+ $newcomments[$comment_id]['comment_author_email'] = $this->get_tag( $comment, 'wp:comment_author_email');
593
+ $newcomments[$comment_id]['comment_author_IP'] = $this->get_tag( $comment, 'wp:comment_author_IP');
594
+ $newcomments[$comment_id]['comment_author_url'] = $this->get_tag( $comment, 'wp:comment_author_url');
595
+ $newcomments[$comment_id]['comment_date'] = $this->get_tag( $comment, 'wp:comment_date');
596
+ $newcomments[$comment_id]['comment_date_gmt'] = $this->get_tag( $comment, 'wp:comment_date_gmt');
597
+ $newcomments[$comment_id]['comment_content'] = $this->get_tag( $comment, 'wp:comment_content');
598
+ $newcomments[$comment_id]['comment_approved'] = $this->get_tag( $comment, 'wp:comment_approved');
599
+ $newcomments[$comment_id]['comment_type'] = $this->get_tag( $comment, 'wp:comment_type');
600
+ $newcomments[$comment_id]['comment_parent'] = $this->get_tag( $comment, 'wp:comment_parent');
601
+ }
602
+ // Sort by comment ID, to make sure comment parents exist (if there at all)
603
+ ksort($newcomments);
604
+ foreach ($newcomments as $key => $comment) {
605
+ // if this is a new post we can skip the comment_exists() check
606
+ if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) {
607
+ if (isset($inserted_comments[$comment['comment_parent']]))
608
+ $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
609
+ $comment = wp_filter_comment($comment);
610
+ $inserted_comments[$key] = wp_insert_comment($comment);
611
+ $num_comments++;
612
+ }
613
+ }
614
+ }
615
+
616
+ if ( $num_comments )
617
+ printf(' '._n('(%s comment)', '(%s comments)', $num_comments, 'wordpress-importer'), $num_comments);
618
+
619
+ // Now for post meta
620
+ preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
621
+ $postmeta = $postmeta[1];
622
+ if ( $postmeta) { foreach ($postmeta as $p) {
623
+ $key = $this->get_tag( $p, 'wp:meta_key' );
624
+ $value = $this->get_tag( $p, 'wp:meta_value' );
625
+
626
+ $this->process_post_meta($post_id, $key, $value);
627
+
628
+ } }
629
+
630
+ do_action('import_post_added', $post_id);
631
+ print "</li>\n";
632
+ }
633
+
634
+ function process_post_meta($post_id, $key, $value) {
635
+ // the filter can return false to skip a particular metadata key
636
+ $_key = apply_filters('import_post_meta_key', $key);
637
+ if ( $_key ) {
638
+ add_post_meta( $post_id, $_key, $value );
639
+ do_action('import_post_meta', $post_id, $_key, $value);
640
+ }
641
+ }
642
+
643
+ function process_attachment($postdata, $remote_url) {
644
+ if ($this->fetch_attachments and $remote_url) {
645
+ printf( __('Importing attachment <em>%s</em>... ', 'wordpress-importer'), htmlspecialchars($remote_url) );
646
+
647
+ // If the URL is absolute, but does not contain http, upload it assuming the base_site_url variable
648
+ if ( preg_match('/^\/[\w\W]+$/', $remote_url) )
649
+ $remote_url = rtrim($this->base_url,'/').$remote_url;
650
+
651
+ $upload = $this->fetch_remote_file($postdata, $remote_url);
652
+ if ( is_wp_error($upload) ) {
653
+ printf( __('Remote file error: %s', 'wordpress-importer'), htmlspecialchars($upload->get_error_message()) );
654
+ return $upload;
655
+ }
656
+ else {
657
+ print '('.size_format(filesize($upload['file'])).')';
658
+ }
659
+
660
+ if ( 0 == filesize( $upload['file'] ) ) {
661
+ print __( "Zero length file, deleting" , 'wordpress-importer') . "\n";
662
+ unlink( $upload['file'] );
663
+ return;
664
+ }
665
+
666
+ if ( $info = wp_check_filetype($upload['file']) ) {
667
+ $postdata['post_mime_type'] = $info['type'];
668
+ }
669
+ else {
670
+ print __('Invalid file type', 'wordpress-importer');
671
+ return;
672
+ }
673
+
674
+ $postdata['guid'] = $upload['url'];
675
+
676
+ // as per wp-admin/includes/upload.php
677
+ $post_id = wp_insert_attachment($postdata, $upload['file']);
678
+ wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
679
+
680
+ // remap the thumbnail url. this isn't perfect because we're just guessing the original url.
681
+ if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
682
+ $parts = pathinfo($remote_url);
683
+ $ext = $parts['extension'];
684
+ $name = basename($parts['basename'], ".{$ext}");
685
+ $this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
686
+ }
687
+
688
+ return $post_id;
689
+ }
690
+ else {
691
+ printf( __('Skipping attachment <em>%s</em>', 'wordpress-importer'), htmlspecialchars($remote_url) );
692
+ }
693
+ }
694
+
695
+ function fetch_remote_file( $post, $url ) {
696
+ add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
697
+
698
+ $upload = wp_upload_dir($post['post_date']);
699
+
700
+ // extract the file name and extension from the url
701
+ $file_name = basename($url);
702
+
703
+ // get placeholder file in the upload dir with a unique sanitized filename
704
+ $upload = wp_upload_bits( $file_name, 0, '', $post['post_date']);
705
+ if ( $upload['error'] ) {
706
+ echo $upload['error'];
707
+ return new WP_Error( 'upload_dir_error', $upload['error'] );
708
+ }
709
+
710
+ // fetch the remote url and write it to the placeholder file
711
+ $headers = wp_get_http($url, $upload['file']);
712
+
713
+ //Request failed
714
+ if ( ! $headers ) {
715
+ @unlink($upload['file']);
716
+ return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
717
+ }
718
+
719
+ // make sure the fetch was successful
720
+ if ( $headers['response'] != '200' ) {
721
+ @unlink($upload['file']);
722
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %1$d %2$s', 'wordpress-importer'), $headers['response'], get_status_header_desc($headers['response']) ) );
723
+ }
724
+ elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) {
725
+ @unlink($upload['file']);
726
+ return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
727
+ }
728
+
729
+ $max_size = $this->max_attachment_size();
730
+ if ( !empty($max_size) and filesize($upload['file']) > $max_size ) {
731
+ @unlink($upload['file']);
732
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size), 'wordpress-importer')) );
733
+ }
734
+
735
+ // keep track of the old and new urls so we can substitute them later
736
+ $this->url_remap[$url] = $upload['url'];
737
+ $this->url_remap[$post['guid']] = $upload['url'];
738
+ // if the remote url is redirected somewhere else, keep track of the destination too
739
+ if ( $headers['x-final-location'] != $url )
740
+ $this->url_remap[$headers['x-final-location']] = $upload['url'];
741
+
742
+ return $upload;
743
+
744
+ }
745
+
746
+ /**
747
+ * Bump up the request timeout for http requests
748
+ *
749
+ * @param int $val
750
+ * @return int
751
+ */
752
+ function bump_request_timeout( $val ) {
753
+ return 60;
754
+ }
755
+
756
+ // sort by strlen, longest string first
757
+ function cmpr_strlen($a, $b) {
758
+ return strlen($b) - strlen($a);
759
+ }
760
+
761
+ // update url references in post bodies to point to the new local files
762
+ function backfill_attachment_urls() {
763
+
764
+ // make sure we do the longest urls first, in case one is a substring of another
765
+ uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
766
+
767
+ global $wpdb;
768
+ foreach ($this->url_remap as $from_url => $to_url) {
769
+ // remap urls in post_content
770
+ $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
771
+ // remap enclosure urls
772
+ $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
773
+ }
774
+ }
775
+
776
+ // update the post_parent of orphans now that we know the local id's of all parents
777
+ function backfill_parents() {
778
+ global $wpdb;
779
+
780
+ foreach ($this->orphans as $child_id => $parent_id) {
781
+ $local_child_id = $local_parent_id = false;
782
+ if ( isset( $this->post_ids_processed[$child_id] ) )
783
+ $local_child_id = $this->post_ids_processed[$child_id];
784
+ if ( isset( $this->post_ids_processed[$parent_id] ) )
785
+ $local_parent_id = $this->post_ids_processed[$parent_id];
786
+
787
+ if ($local_child_id and $local_parent_id) {
788
+ $wpdb->update($wpdb->posts, array('post_parent' => $local_parent_id), array('ID' => $local_child_id) );
789
+ }
790
+ }
791
+ }
792
+
793
+ function is_valid_meta_key($key) {
794
+ // skip attachment metadata since we'll regenerate it from scratch
795
+ if ( $key == '_wp_attached_file' || $key == '_wp_attachment_metadata' )
796
+ return false;
797
+ return $key;
798
+ }
799
+
800
+ // give the user the option of creating new users to represent authors in the import file?
801
+ function allow_create_users() {
802
+ return apply_filters('import_allow_create_users', true);
803
+ }
804
+
805
+ // give the user the option of downloading and importing attached files
806
+ function allow_fetch_attachments() {
807
+ return apply_filters('import_allow_fetch_attachments', true);
808
+ }
809
+
810
+ function max_attachment_size() {
811
+ // can be overridden with a filter - 0 means no limit
812
+ return apply_filters('import_attachment_size_limit', 0);
813
+ }
814
+
815
+ function import_start() {
816
+ wp_defer_term_counting(true);
817
+ wp_defer_comment_counting(true);
818
+ do_action('import_start');
819
+ }
820
+
821
+ function import_end() {
822
+ do_action('import_end');
823
+
824
+ // clear the caches after backfilling
825
+ foreach ($this->post_ids_processed as $post_id)
826
+ clean_post_cache($post_id);
827
+
828
+ wp_defer_term_counting(false);
829
+ wp_defer_comment_counting(false);
830
+ }
831
+
832
+ function import($id, $fetch_attachments = false) {
833
+ $this->id = (int) $id;
834
+ $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
835
+
836
+ add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
837
+ $file = get_attached_file($this->id);
838
+ $this->import_file($file);
839
+ }
840
+
841
+ function import_file($file) {
842
+ $this->file = $file;
843
+
844
+ $this->import_start();
845
+ $this->get_authors_from_post();
846
+ wp_suspend_cache_invalidation(true);
847
+ $this->get_entries();
848
+ $this->process_categories();
849
+ $this->process_tags();
850
+ $this->process_terms();
851
+ $result = $this->process_posts();
852
+ wp_suspend_cache_invalidation(false);
853
+ $this->backfill_parents();
854
+ $this->backfill_attachment_urls();
855
+ $this->import_end();
856
+
857
+ if ( is_wp_error( $result ) )
858
+ return $result;
859
+ }
860
+
861
+ function handle_upload() {
862
+ $file = wp_import_handle_upload();
863
+ if ( isset($file['error']) ) {
864
+ echo '<p>'.__('Sorry, there has been an error.', 'wordpress-importer').'</p>';
865
+ echo '<p><strong>' . $file['error'] . '</strong></p>';
866
+ return false;
867
+ }
868
+ $this->file = $file['file'];
869
+ $this->id = (int) $file['id'];
870
+ return true;
871
+ }
872
+
873
+ function dispatch() {
874
+ if (empty ($_GET['step']))
875
+ $step = 0;
876
+ else
877
+ $step = (int) $_GET['step'];
878
+
879
+ $this->header();
880
+ switch ($step) {
881
+ case 0 :
882
+ $this->greet();
883
+ break;
884
+ case 1 :
885
+ check_admin_referer('import-upload');
886
+ if ( $this->handle_upload() )
887
+ $this->select_authors();
888
+ break;
889
+ case 2:
890
+ check_admin_referer('import-wordpress');
891
+ $fetch_attachments = ! empty( $_POST['attachments'] );
892
+ $result = $this->import( $_GET['id'], $fetch_attachments);
893
+ if ( is_wp_error( $result ) )
894
+ echo $result->get_error_message();
895
+ break;
896
+ }
897
+ $this->footer();
898
+ }
899
+
900
+ function WP_Import() {
901
+ // Nothing.
902
+ }
903
+ }
904
+
905
+ /**
906
+ * Register WordPress Importer
907
+ *
908
+ * @since unknown
909
+ * @var WP_Import
910
+ * @name $wp_import
911
+ */
912
+ $wp_import = new WP_Import();
913
+
914
+ register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array ($wp_import, 'dispatch'));
915
+
916
+ } // class_exists( 'WP_Importer' )
917
+
918
+ function wordpress_importer_init() {
919
+ load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
920
+ }
921
+ add_action( 'init', 'wordpress_importer_init' );