WordPress Importer - Version 0.7

Version Description

  • Update minimum WordPress requirement to 3.7 and ensure compatibility with PHP 7.4.
  • Fix bug that caused not importing term meta.
  • Fix bug that caused slashes to be stripped from imported meta data.
  • Fix bug that prevented import of serialized meta data.
  • Fix file size check after download of remote files with HTTP compression enabled.
  • Improve accessibility of form fields by adding missing labels.
  • Improve imports for remote file URLs without name and/or extension.
  • Add support for wp:base_blog_url field to allow importing multiple files with WP-CLI.
  • Add support for term meta parsing when using the regular expressions or XML parser.
  • Developers: All PHP classes have been moved into their own files.
  • Developers: Allow to change IMPORT_DEBUG via wp-config.php and change default value to the value of WP_DEBUG.
Download this release

Release Info

Developer ocean90
Plugin Icon 128x128 WordPress Importer
Version 0.7
Comparing to
See all releases

Code changes from version 0.6.4 to 0.7

class-wp-import.php ADDED
@@ -0,0 +1,1407 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress Importer class for managing the import process of a WXR file
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WordPress importer class.
11
+ */
12
+ class WP_Import extends WP_Importer {
13
+ var $max_wxr_version = 1.2; // max. supported WXR version
14
+
15
+ var $id; // WXR attachment ID
16
+
17
+ // information to import from WXR file
18
+ var $version;
19
+ var $authors = array();
20
+ var $posts = array();
21
+ var $terms = array();
22
+ var $categories = array();
23
+ var $tags = array();
24
+ var $base_url = '';
25
+
26
+ // mappings from old information to new
27
+ var $processed_authors = array();
28
+ var $author_mapping = array();
29
+ var $processed_terms = array();
30
+ var $processed_posts = array();
31
+ var $post_orphans = array();
32
+ var $processed_menu_items = array();
33
+ var $menu_item_orphans = array();
34
+ var $missing_menu_items = array();
35
+
36
+ var $fetch_attachments = false;
37
+ var $url_remap = array();
38
+ var $featured_images = array();
39
+
40
+ /**
41
+ * Registered callback function for the WordPress Importer
42
+ *
43
+ * Manages the three separate stages of the WXR import process
44
+ */
45
+ function dispatch() {
46
+ $this->header();
47
+
48
+ $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
49
+ switch ( $step ) {
50
+ case 0:
51
+ $this->greet();
52
+ break;
53
+ case 1:
54
+ check_admin_referer( 'import-upload' );
55
+ if ( $this->handle_upload() )
56
+ $this->import_options();
57
+ break;
58
+ case 2:
59
+ check_admin_referer( 'import-wordpress' );
60
+ $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
61
+ $this->id = (int) $_POST['import_id'];
62
+ $file = get_attached_file( $this->id );
63
+ set_time_limit(0);
64
+ $this->import( $file );
65
+ break;
66
+ }
67
+
68
+ $this->footer();
69
+ }
70
+
71
+ /**
72
+ * The main controller for the actual import stage.
73
+ *
74
+ * @param string $file Path to the WXR file for importing
75
+ */
76
+ function import( $file ) {
77
+ add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
78
+ add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
79
+
80
+ $this->import_start( $file );
81
+
82
+ $this->get_author_mapping();
83
+
84
+ wp_suspend_cache_invalidation( true );
85
+ $this->process_categories();
86
+ $this->process_tags();
87
+ $this->process_terms();
88
+ $this->process_posts();
89
+ wp_suspend_cache_invalidation( false );
90
+
91
+ // update incorrect/missing information in the DB
92
+ $this->backfill_parents();
93
+ $this->backfill_attachment_urls();
94
+ $this->remap_featured_images();
95
+
96
+ $this->import_end();
97
+ }
98
+
99
+ /**
100
+ * Parses the WXR file and prepares us for the task of processing parsed data
101
+ *
102
+ * @param string $file Path to the WXR file for importing
103
+ */
104
+ function import_start( $file ) {
105
+ if ( ! is_file($file) ) {
106
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
107
+ echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
108
+ $this->footer();
109
+ die();
110
+ }
111
+
112
+ $import_data = $this->parse( $file );
113
+
114
+ if ( is_wp_error( $import_data ) ) {
115
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
116
+ echo esc_html( $import_data->get_error_message() ) . '</p>';
117
+ $this->footer();
118
+ die();
119
+ }
120
+
121
+ $this->version = $import_data['version'];
122
+ $this->get_authors_from_import( $import_data );
123
+ $this->posts = $import_data['posts'];
124
+ $this->terms = $import_data['terms'];
125
+ $this->categories = $import_data['categories'];
126
+ $this->tags = $import_data['tags'];
127
+ $this->base_url = esc_url( $import_data['base_url'] );
128
+
129
+ wp_defer_term_counting( true );
130
+ wp_defer_comment_counting( true );
131
+
132
+ do_action( 'import_start' );
133
+ }
134
+
135
+ /**
136
+ * Performs post-import cleanup of files and the cache
137
+ */
138
+ function import_end() {
139
+ wp_import_cleanup( $this->id );
140
+
141
+ wp_cache_flush();
142
+ foreach ( get_taxonomies() as $tax ) {
143
+ delete_option( "{$tax}_children" );
144
+ _get_term_hierarchy( $tax );
145
+ }
146
+
147
+ wp_defer_term_counting( false );
148
+ wp_defer_comment_counting( false );
149
+
150
+ echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
151
+ echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
152
+
153
+ do_action( 'import_end' );
154
+ }
155
+
156
+ /**
157
+ * Handles the WXR upload and initial parsing of the file to prepare for
158
+ * displaying author import options
159
+ *
160
+ * @return bool False if error uploading or invalid file, true otherwise
161
+ */
162
+ function handle_upload() {
163
+ $file = wp_import_handle_upload();
164
+
165
+ if ( isset( $file['error'] ) ) {
166
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
167
+ echo esc_html( $file['error'] ) . '</p>';
168
+ return false;
169
+ } else if ( ! file_exists( $file['file'] ) ) {
170
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
171
+ 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'] ) );
172
+ echo '</p>';
173
+ return false;
174
+ }
175
+
176
+ $this->id = (int) $file['id'];
177
+ $import_data = $this->parse( $file['file'] );
178
+ if ( is_wp_error( $import_data ) ) {
179
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
180
+ echo esc_html( $import_data->get_error_message() ) . '</p>';
181
+ return false;
182
+ }
183
+
184
+ $this->version = $import_data['version'];
185
+ if ( $this->version > $this->max_wxr_version ) {
186
+ echo '<div class="error"><p><strong>';
187
+ 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']) );
188
+ echo '</strong></p></div>';
189
+ }
190
+
191
+ $this->get_authors_from_import( $import_data );
192
+
193
+ return true;
194
+ }
195
+
196
+ /**
197
+ * Retrieve authors from parsed WXR data
198
+ *
199
+ * Uses the provided author information from WXR 1.1 files
200
+ * or extracts info from each post for WXR 1.0 files
201
+ *
202
+ * @param array $import_data Data returned by a WXR parser
203
+ */
204
+ function get_authors_from_import( $import_data ) {
205
+ if ( ! empty( $import_data['authors'] ) ) {
206
+ $this->authors = $import_data['authors'];
207
+ // no author information, grab it from the posts
208
+ } else {
209
+ foreach ( $import_data['posts'] as $post ) {
210
+ $login = sanitize_user( $post['post_author'], true );
211
+ if ( empty( $login ) ) {
212
+ printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
213
+ echo '<br />';
214
+ continue;
215
+ }
216
+
217
+ if ( ! isset($this->authors[$login]) )
218
+ $this->authors[$login] = array(
219
+ 'author_login' => $login,
220
+ 'author_display_name' => $post['post_author']
221
+ );
222
+ }
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Display pre-import options, author importing/mapping and option to
228
+ * fetch attachments
229
+ */
230
+ function import_options() {
231
+ $j = 0;
232
+ ?>
233
+ <form action="<?php echo admin_url( 'admin.php?import=wordpress&amp;step=2' ); ?>" method="post">
234
+ <?php wp_nonce_field( 'import-wordpress' ); ?>
235
+ <input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
236
+
237
+ <?php if ( ! empty( $this->authors ) ) : ?>
238
+ <h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
239
+ <p><?php _e( 'To make it simpler 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, such as your primary administrator account.', 'wordpress-importer' ); ?></p>
240
+ <?php if ( $this->allow_create_users() ) : ?>
241
+ <p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user&#8217;s role will be set as %s. Manually changing the new user&#8217;s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p>
242
+ <?php endif; ?>
243
+ <ol id="authors">
244
+ <?php foreach ( $this->authors as $author ) : ?>
245
+ <li><?php $this->author_select( $j++, $author ); ?></li>
246
+ <?php endforeach; ?>
247
+ </ol>
248
+ <?php endif; ?>
249
+
250
+ <?php if ( $this->allow_fetch_attachments() ) : ?>
251
+ <h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
252
+ <p>
253
+ <input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
254
+ <label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
255
+ </p>
256
+ <?php endif; ?>
257
+
258
+ <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
259
+ </form>
260
+ <?php
261
+ }
262
+
263
+ /**
264
+ * Display import options for an individual author. That is, either create
265
+ * a new user based on import info or map to an existing user
266
+ *
267
+ * @param int $n Index for each author in the form
268
+ * @param array $author Author information, e.g. login, display name, email
269
+ */
270
+ function author_select( $n, $author ) {
271
+ _e( 'Import author:', 'wordpress-importer' );
272
+ echo ' <strong>' . esc_html( $author['author_display_name'] );
273
+ if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
274
+ echo '</strong><br />';
275
+
276
+ if ( $this->version != '1.0' )
277
+ echo '<div style="margin-left:18px">';
278
+
279
+ $create_users = $this->allow_create_users();
280
+ if ( $create_users ) {
281
+ echo '<label for="user_new_'. $n . '">';
282
+ if ( $this->version != '1.0' ) {
283
+ _e( 'or create new user with login name:', 'wordpress-importer' );
284
+ $value = '';
285
+ } else {
286
+ _e( 'as a new user:', 'wordpress-importer' );
287
+ $value = esc_attr( sanitize_user( $author['author_login'], true ) );
288
+ }
289
+ echo '</label>';
290
+
291
+ echo ' <input type="text" id="user_new_' . $n . '" name="user_new['.$n.']" value="'. $value .'" /><br />';
292
+ }
293
+
294
+ echo '<label for="imported_authors_'. $n . '">';
295
+ if ( ! $create_users && $this->version == '1.0' ) {
296
+ _e( 'assign posts to an existing user:', 'wordpress-importer' );
297
+ } else {
298
+ _e( 'or assign posts to an existing user:', 'wordpress-importer' );
299
+ }
300
+ echo '</label>';
301
+
302
+ echo ' ' . wp_dropdown_users( array(
303
+ 'name' => "user_map[$n]",
304
+ 'id' => 'imported_authors_' . $n,
305
+ 'multi' => true,
306
+ 'show_option_all' => __( '- Select -', 'wordpress-importer' ),
307
+ 'show' => 'display_name_with_login',
308
+ 'echo' => 0,
309
+ ) );
310
+
311
+ echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
312
+
313
+ if ( $this->version != '1.0' )
314
+ echo '</div>';
315
+ }
316
+
317
+ /**
318
+ * Map old author logins to local user IDs based on decisions made
319
+ * in import options form. Can map to an existing user, create a new user
320
+ * or falls back to the current user in case of error with either of the previous
321
+ */
322
+ function get_author_mapping() {
323
+ if ( ! isset( $_POST['imported_authors'] ) )
324
+ return;
325
+
326
+ $create_users = $this->allow_create_users();
327
+
328
+ foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
329
+ // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
330
+ $santized_old_login = sanitize_user( $old_login, true );
331
+ $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false;
332
+
333
+ if ( ! empty( $_POST['user_map'][$i] ) ) {
334
+ $user = get_userdata( intval($_POST['user_map'][$i]) );
335
+ if ( isset( $user->ID ) ) {
336
+ if ( $old_id )
337
+ $this->processed_authors[$old_id] = $user->ID;
338
+ $this->author_mapping[$santized_old_login] = $user->ID;
339
+ }
340
+ } else if ( $create_users ) {
341
+ if ( ! empty($_POST['user_new'][$i]) ) {
342
+ $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() );
343
+ } else if ( $this->version != '1.0' ) {
344
+ $user_data = array(
345
+ 'user_login' => $old_login,
346
+ 'user_pass' => wp_generate_password(),
347
+ 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '',
348
+ 'display_name' => $this->authors[$old_login]['author_display_name'],
349
+ 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '',
350
+ 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '',
351
+ );
352
+ $user_id = wp_insert_user( $user_data );
353
+ }
354
+
355
+ if ( ! is_wp_error( $user_id ) ) {
356
+ if ( $old_id )
357
+ $this->processed_authors[$old_id] = $user_id;
358
+ $this->author_mapping[$santized_old_login] = $user_id;
359
+ } else {
360
+ 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']) );
361
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
362
+ echo ' ' . $user_id->get_error_message();
363
+ echo '<br />';
364
+ }
365
+ }
366
+
367
+ // failsafe: if the user_id was invalid, default to the current user
368
+ if ( ! isset( $this->author_mapping[$santized_old_login] ) ) {
369
+ if ( $old_id )
370
+ $this->processed_authors[$old_id] = (int) get_current_user_id();
371
+ $this->author_mapping[$santized_old_login] = (int) get_current_user_id();
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Create new categories based on import information
378
+ *
379
+ * Doesn't create a new category if its slug already exists
380
+ */
381
+ function process_categories() {
382
+ $this->categories = apply_filters( 'wp_import_categories', $this->categories );
383
+
384
+ if ( empty( $this->categories ) )
385
+ return;
386
+
387
+ foreach ( $this->categories as $cat ) {
388
+ // if the category already exists leave it alone
389
+ $term_id = term_exists( $cat['category_nicename'], 'category' );
390
+ if ( $term_id ) {
391
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
392
+ if ( isset($cat['term_id']) )
393
+ $this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
394
+ continue;
395
+ }
396
+
397
+ $parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
398
+ $description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
399
+
400
+ $data = array(
401
+ 'category_nicename' => $cat['category_nicename'],
402
+ 'category_parent' => $parent,
403
+ 'cat_name' => wp_slash( $cat['cat_name'] ),
404
+ 'category_description' => wp_slash( $description ),
405
+ );
406
+
407
+ $id = wp_insert_category( $data );
408
+ if ( ! is_wp_error( $id ) && $id > 0 ) {
409
+ if ( isset($cat['term_id']) )
410
+ $this->processed_terms[intval($cat['term_id'])] = $id;
411
+ } else {
412
+ printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
413
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
414
+ echo ': ' . $id->get_error_message();
415
+ echo '<br />';
416
+ continue;
417
+ }
418
+
419
+ $this->process_termmeta( $cat, $id );
420
+ }
421
+
422
+ unset( $this->categories );
423
+ }
424
+
425
+ /**
426
+ * Create new post tags based on import information
427
+ *
428
+ * Doesn't create a tag if its slug already exists
429
+ */
430
+ function process_tags() {
431
+ $this->tags = apply_filters( 'wp_import_tags', $this->tags );
432
+
433
+ if ( empty( $this->tags ) )
434
+ return;
435
+
436
+ foreach ( $this->tags as $tag ) {
437
+ // if the tag already exists leave it alone
438
+ $term_id = term_exists( $tag['tag_slug'], 'post_tag' );
439
+ if ( $term_id ) {
440
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
441
+ if ( isset($tag['term_id']) )
442
+ $this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
443
+ continue;
444
+ }
445
+
446
+ $description = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
447
+ $args = array(
448
+ 'slug' => $tag['tag_slug'],
449
+ 'description' => wp_slash( $description ),
450
+ );
451
+
452
+ $id = wp_insert_term( wp_slash( $tag['tag_name'] ), 'post_tag', $args );
453
+ if ( ! is_wp_error( $id ) ) {
454
+ if ( isset($tag['term_id']) )
455
+ $this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
456
+ } else {
457
+ printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
458
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
459
+ echo ': ' . $id->get_error_message();
460
+ echo '<br />';
461
+ continue;
462
+ }
463
+
464
+ $this->process_termmeta( $tag, $id['term_id'] );
465
+ }
466
+
467
+ unset( $this->tags );
468
+ }
469
+
470
+ /**
471
+ * Create new terms based on import information
472
+ *
473
+ * Doesn't create a term its slug already exists
474
+ */
475
+ function process_terms() {
476
+ $this->terms = apply_filters( 'wp_import_terms', $this->terms );
477
+
478
+ if ( empty( $this->terms ) )
479
+ return;
480
+
481
+ foreach ( $this->terms as $term ) {
482
+ // if the term already exists in the correct taxonomy leave it alone
483
+ $term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
484
+ if ( $term_id ) {
485
+ if ( is_array($term_id) ) $term_id = $term_id['term_id'];
486
+ if ( isset($term['term_id']) )
487
+ $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
488
+ continue;
489
+ }
490
+
491
+ if ( empty( $term['term_parent'] ) ) {
492
+ $parent = 0;
493
+ } else {
494
+ $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
495
+ if ( is_array( $parent ) ) {
496
+ $parent = $parent['term_id'];
497
+ }
498
+ }
499
+
500
+ $description = isset( $term['term_description'] ) ? $term['term_description'] : '';
501
+ $args = array(
502
+ 'slug' => $term['slug'],
503
+ 'description' => wp_slash( $description ),
504
+ 'parent' => (int) $parent
505
+ );
506
+
507
+ $id = wp_insert_term( wp_slash( $term['term_name'] ), $term['term_taxonomy'], $args );
508
+ if ( ! is_wp_error( $id ) ) {
509
+ if ( isset($term['term_id']) )
510
+ $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
511
+ } else {
512
+ printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
513
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
514
+ echo ': ' . $id->get_error_message();
515
+ echo '<br />';
516
+ continue;
517
+ }
518
+
519
+ $this->process_termmeta( $term, $id['term_id'] );
520
+ }
521
+
522
+ unset( $this->terms );
523
+ }
524
+
525
+ /**
526
+ * Add metadata to imported term.
527
+ *
528
+ * @since 0.6.2
529
+ *
530
+ * @param array $term Term data from WXR import.
531
+ * @param int $term_id ID of the newly created term.
532
+ */
533
+ protected function process_termmeta( $term, $term_id ) {
534
+ if ( ! function_exists( 'add_term_meta' ) ) {
535
+ return;
536
+ }
537
+
538
+ if ( ! isset( $term['termmeta'] ) ) {
539
+ $term['termmeta'] = array();
540
+ }
541
+
542
+ /**
543
+ * Filters the metadata attached to an imported term.
544
+ *
545
+ * @since 0.6.2
546
+ *
547
+ * @param array $termmeta Array of term meta.
548
+ * @param int $term_id ID of the newly created term.
549
+ * @param array $term Term data from the WXR import.
550
+ */
551
+ $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
552
+
553
+ if ( empty( $term['termmeta'] ) ) {
554
+ return;
555
+ }
556
+
557
+ foreach ( $term['termmeta'] as $meta ) {
558
+ /**
559
+ * Filters the meta key for an imported piece of term meta.
560
+ *
561
+ * @since 0.6.2
562
+ *
563
+ * @param string $meta_key Meta key.
564
+ * @param int $term_id ID of the newly created term.
565
+ * @param array $term Term data from the WXR import.
566
+ */
567
+ $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
568
+ if ( ! $key ) {
569
+ continue;
570
+ }
571
+
572
+ // Export gets meta straight from the DB so could have a serialized string
573
+ $value = maybe_unserialize( $meta['value'] );
574
+
575
+ add_term_meta( $term_id, wp_slash( $key ), wp_slash_strings_only( $value ) );
576
+
577
+ /**
578
+ * Fires after term meta is imported.
579
+ *
580
+ * @since 0.6.2
581
+ *
582
+ * @param int $term_id ID of the newly created term.
583
+ * @param string $key Meta key.
584
+ * @param mixed $value Meta value.
585
+ */
586
+ do_action( 'import_term_meta', $term_id, $key, $value );
587
+ }
588
+ }
589
+
590
+ /**
591
+ * Create new posts based on import information
592
+ *
593
+ * Posts marked as having a parent which doesn't exist will become top level items.
594
+ * Doesn't create a new post if: the post type doesn't exist, the given post ID
595
+ * is already noted as imported or a post with the same title and date already exists.
596
+ * Note that new/updated terms, comments and meta are imported for the last of the above.
597
+ */
598
+ function process_posts() {
599
+ $this->posts = apply_filters( 'wp_import_posts', $this->posts );
600
+
601
+ foreach ( $this->posts as $post ) {
602
+ $post = apply_filters( 'wp_import_post_data_raw', $post );
603
+
604
+ if ( ! post_type_exists( $post['post_type'] ) ) {
605
+ printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
606
+ esc_html($post['post_title']), esc_html($post['post_type']) );
607
+ echo '<br />';
608
+ do_action( 'wp_import_post_exists', $post );
609
+ continue;
610
+ }
611
+
612
+ if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
613
+ continue;
614
+
615
+ if ( $post['status'] == 'auto-draft' )
616
+ continue;
617
+
618
+ if ( 'nav_menu_item' == $post['post_type'] ) {
619
+ $this->process_menu_item( $post );
620
+ continue;
621
+ }
622
+
623
+ $post_type_object = get_post_type_object( $post['post_type'] );
624
+
625
+ $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
626
+
627
+ /**
628
+ * Filter ID of the existing post corresponding to post currently importing.
629
+ *
630
+ * Return 0 to force the post to be imported. Filter the ID to be something else
631
+ * to override which existing post is mapped to the imported post.
632
+ *
633
+ * @see post_exists()
634
+ * @since 0.6.2
635
+ *
636
+ * @param int $post_exists Post ID, or 0 if post did not exist.
637
+ * @param array $post The post array to be inserted.
638
+ */
639
+ $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
640
+
641
+ if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
642
+ printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
643
+ echo '<br />';
644
+ $comment_post_ID = $post_id = $post_exists;
645
+ $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
646
+ } else {
647
+ $post_parent = (int) $post['post_parent'];
648
+ if ( $post_parent ) {
649
+ // if we already know the parent, map it to the new local ID
650
+ if ( isset( $this->processed_posts[$post_parent] ) ) {
651
+ $post_parent = $this->processed_posts[$post_parent];
652
+ // otherwise record the parent for later
653
+ } else {
654
+ $this->post_orphans[intval($post['post_id'])] = $post_parent;
655
+ $post_parent = 0;
656
+ }
657
+ }
658
+
659
+ // map the post author
660
+ $author = sanitize_user( $post['post_author'], true );
661
+ if ( isset( $this->author_mapping[$author] ) )
662
+ $author = $this->author_mapping[$author];
663
+ else
664
+ $author = (int) get_current_user_id();
665
+
666
+ $postdata = array(
667
+ 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
668
+ 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
669
+ 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
670
+ 'post_status' => $post['status'], 'post_name' => $post['post_name'],
671
+ 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
672
+ 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
673
+ 'post_type' => $post['post_type'], 'post_password' => $post['post_password']
674
+ );
675
+
676
+ $original_post_ID = $post['post_id'];
677
+ $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
678
+
679
+ $postdata = wp_slash( $postdata );
680
+
681
+ if ( 'attachment' == $postdata['post_type'] ) {
682
+ $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
683
+
684
+ // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
685
+ // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
686
+ $postdata['upload_date'] = $post['post_date'];
687
+ if ( isset( $post['postmeta'] ) ) {
688
+ foreach( $post['postmeta'] as $meta ) {
689
+ if ( $meta['key'] == '_wp_attached_file' ) {
690
+ if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
691
+ $postdata['upload_date'] = $matches[0];
692
+ break;
693
+ }
694
+ }
695
+ }
696
+
697
+ $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
698
+ } else {
699
+ $comment_post_ID = $post_id = wp_insert_post( $postdata, true );
700
+ do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
701
+ }
702
+
703
+ if ( is_wp_error( $post_id ) ) {
704
+ printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
705
+ $post_type_object->labels->singular_name, esc_html($post['post_title']) );
706
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
707
+ echo ': ' . $post_id->get_error_message();
708
+ echo '<br />';
709
+ continue;
710
+ }
711
+
712
+ if ( $post['is_sticky'] == 1 )
713
+ stick_post( $post_id );
714
+ }
715
+
716
+ // map pre-import ID to local ID
717
+ $this->processed_posts[intval($post['post_id'])] = (int) $post_id;
718
+
719
+ if ( ! isset( $post['terms'] ) )
720
+ $post['terms'] = array();
721
+
722
+ $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
723
+
724
+ // add categories, tags and other terms
725
+ if ( ! empty( $post['terms'] ) ) {
726
+ $terms_to_set = array();
727
+ foreach ( $post['terms'] as $term ) {
728
+ // back compat with WXR 1.0 map 'tag' to 'post_tag'
729
+ $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
730
+ $term_exists = term_exists( $term['slug'], $taxonomy );
731
+ $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
732
+ if ( ! $term_id ) {
733
+ $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
734
+ if ( ! is_wp_error( $t ) ) {
735
+ $term_id = $t['term_id'];
736
+ do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
737
+ } else {
738
+ printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
739
+ if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
740
+ echo ': ' . $t->get_error_message();
741
+ echo '<br />';
742
+ do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
743
+ continue;
744
+ }
745
+ }
746
+ $terms_to_set[$taxonomy][] = intval( $term_id );
747
+ }
748
+
749
+ foreach ( $terms_to_set as $tax => $ids ) {
750
+ $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
751
+ do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
752
+ }
753
+ unset( $post['terms'], $terms_to_set );
754
+ }
755
+
756
+ if ( ! isset( $post['comments'] ) )
757
+ $post['comments'] = array();
758
+
759
+ $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
760
+
761
+ // add/update comments
762
+ if ( ! empty( $post['comments'] ) ) {
763
+ $num_comments = 0;
764
+ $inserted_comments = array();
765
+ foreach ( $post['comments'] as $comment ) {
766
+ $comment_id = $comment['comment_id'];
767
+ $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
768
+ $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
769
+ $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
770
+ $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
771
+ $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
772
+ $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
773
+ $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
774
+ $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
775
+ $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
776
+ $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
777
+ $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
778
+ $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
779
+ if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
780
+ $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
781
+ }
782
+ ksort( $newcomments );
783
+
784
+ foreach ( $newcomments as $key => $comment ) {
785
+ // if this is a new post we can skip the comment_exists() check
786
+ if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
787
+ if ( isset( $inserted_comments[$comment['comment_parent']] ) ) {
788
+ $comment['comment_parent'] = $inserted_comments[ $comment['comment_parent'] ];
789
+ }
790
+
791
+ $comment_data = wp_slash( $comment );
792
+ unset( $comment_data['commentmeta'] ); // Handled separately, wp_insert_comment() also expects `comment_meta`.
793
+ $comment_data = wp_filter_comment( $comment_data );
794
+
795
+ $inserted_comments[ $key ] = wp_insert_comment( $comment_data );
796
+
797
+ do_action( 'wp_import_insert_comment', $inserted_comments[ $key ], $comment, $comment_post_ID, $post );
798
+
799
+ foreach( $comment['commentmeta'] as $meta ) {
800
+ $value = maybe_unserialize( $meta['value'] );
801
+
802
+ add_comment_meta( $inserted_comments[ $key ], wp_slash( $meta['key'] ), wp_slash_strings_only( $value ) );
803
+ }
804
+
805
+ $num_comments++;
806
+ }
807
+ }
808
+ unset( $newcomments, $inserted_comments, $post['comments'] );
809
+ }
810
+
811
+ if ( ! isset( $post['postmeta'] ) )
812
+ $post['postmeta'] = array();
813
+
814
+ $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
815
+
816
+ // add/update post meta
817
+ if ( ! empty( $post['postmeta'] ) ) {
818
+ foreach ( $post['postmeta'] as $meta ) {
819
+ $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
820
+ $value = false;
821
+
822
+ if ( '_edit_last' == $key ) {
823
+ if ( isset( $this->processed_authors[intval($meta['value'])] ) )
824
+ $value = $this->processed_authors[intval($meta['value'])];
825
+ else
826
+ $key = false;
827
+ }
828
+
829
+ if ( $key ) {
830
+ // export gets meta straight from the DB so could have a serialized string
831
+ if ( ! $value ) {
832
+ $value = maybe_unserialize( $meta['value'] );
833
+ }
834
+
835
+ add_post_meta( $post_id, wp_slash( $key ), wp_slash_strings_only( $value ) );
836
+
837
+ do_action( 'import_post_meta', $post_id, $key, $value );
838
+
839
+ // if the post has a featured image, take note of this in case of remap
840
+ if ( '_thumbnail_id' == $key )
841
+ $this->featured_images[$post_id] = (int) $value;
842
+ }
843
+ }
844
+ }
845
+ }
846
+
847
+ unset( $this->posts );
848
+ }
849
+
850
+ /**
851
+ * Attempt to create a new menu item from import data
852
+ *
853
+ * Fails for draft, orphaned menu items and those without an associated nav_menu
854
+ * or an invalid nav_menu term. If the post type or term object which the menu item
855
+ * represents doesn't exist then the menu item will not be imported (waits until the
856
+ * end of the import to retry again before discarding).
857
+ *
858
+ * @param array $item Menu item details from WXR file
859
+ */
860
+ function process_menu_item( $item ) {
861
+ // skip draft, orphaned menu items
862
+ if ( 'draft' == $item['status'] )
863
+ return;
864
+
865
+ $menu_slug = false;
866
+ if ( isset($item['terms']) ) {
867
+ // loop through terms, assume first nav_menu term is correct menu
868
+ foreach ( $item['terms'] as $term ) {
869
+ if ( 'nav_menu' == $term['domain'] ) {
870
+ $menu_slug = $term['slug'];
871
+ break;
872
+ }
873
+ }
874
+ }
875
+
876
+ // no nav_menu term associated with this menu item
877
+ if ( ! $menu_slug ) {
878
+ _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
879
+ echo '<br />';
880
+ return;
881
+ }
882
+
883
+ $menu_id = term_exists( $menu_slug, 'nav_menu' );
884
+ if ( ! $menu_id ) {
885
+ printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
886
+ echo '<br />';
887
+ return;
888
+ } else {
889
+ $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
890
+ }
891
+
892
+ foreach ( $item['postmeta'] as $meta )
893
+ ${$meta['key']} = $meta['value'];
894
+
895
+ if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
896
+ $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
897
+ } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
898
+ $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
899
+ } else if ( 'custom' != $_menu_item_type ) {
900
+ // associated object is missing or not imported yet, we'll retry later
901
+ $this->missing_menu_items[] = $item;
902
+ return;
903
+ }
904
+
905
+ if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
906
+ $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
907
+ } else if ( $_menu_item_menu_item_parent ) {
908
+ $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
909
+ $_menu_item_menu_item_parent = 0;
910
+ }
911
+
912
+ // wp_update_nav_menu_item expects CSS classes as a space separated string
913
+ $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
914
+ if ( is_array( $_menu_item_classes ) )
915
+ $_menu_item_classes = implode( ' ', $_menu_item_classes );
916
+
917
+ $args = array(
918
+ 'menu-item-object-id' => $_menu_item_object_id,
919
+ 'menu-item-object' => $_menu_item_object,
920
+ 'menu-item-parent-id' => $_menu_item_menu_item_parent,
921
+ 'menu-item-position' => intval( $item['menu_order'] ),
922
+ 'menu-item-type' => $_menu_item_type,
923
+ 'menu-item-title' => $item['post_title'],
924
+ 'menu-item-url' => $_menu_item_url,
925
+ 'menu-item-description' => $item['post_content'],
926
+ 'menu-item-attr-title' => $item['post_excerpt'],
927
+ 'menu-item-target' => $_menu_item_target,
928
+ 'menu-item-classes' => $_menu_item_classes,
929
+ 'menu-item-xfn' => $_menu_item_xfn,
930
+ 'menu-item-status' => $item['status']
931
+ );
932
+
933
+ $id = wp_update_nav_menu_item( $menu_id, 0, $args );
934
+ if ( $id && ! is_wp_error( $id ) )
935
+ $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
936
+ }
937
+
938
+ /**
939
+ * If fetching attachments is enabled then attempt to create a new attachment
940
+ *
941
+ * @param array $post Attachment post details from WXR
942
+ * @param string $url URL to fetch attachment from
943
+ * @return int|WP_Error Post ID on success, WP_Error otherwise
944
+ */
945
+ function process_attachment( $post, $url ) {
946
+ if ( ! $this->fetch_attachments )
947
+ return new WP_Error( 'attachment_processing_error',
948
+ __( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
949
+
950
+ // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
951
+ if ( preg_match( '|^/[\w\W]+$|', $url ) )
952
+ $url = rtrim( $this->base_url, '/' ) . $url;
953
+
954
+ $upload = $this->fetch_remote_file( $url, $post );
955
+ if ( is_wp_error( $upload ) )
956
+ return $upload;
957
+
958
+ if ( $info = wp_check_filetype( $upload['file'] ) )
959
+ $post['post_mime_type'] = $info['type'];
960
+ else
961
+ return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
962
+
963
+ $post['guid'] = $upload['url'];
964
+
965
+ // as per wp-admin/includes/upload.php
966
+ $post_id = wp_insert_attachment( $post, $upload['file'] );
967
+ wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
968
+
969
+ // remap resized image URLs, works by stripping the extension and remapping the URL stub.
970
+ if ( preg_match( '!^image/!', $info['type'] ) ) {
971
+ $parts = pathinfo( $url );
972
+ $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
973
+
974
+ $parts_new = pathinfo( $upload['url'] );
975
+ $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
976
+
977
+ $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
978
+ }
979
+
980
+ return $post_id;
981
+ }
982
+
983
+ /**
984
+ * Attempt to download a remote file attachment
985
+ *
986
+ * @param string $url URL of item to fetch
987
+ * @param array $post Attachment details
988
+ * @return array|WP_Error Local file location details on success, WP_Error otherwise
989
+ */
990
+ function fetch_remote_file( $url, $post ) {
991
+ // Extract the file name from the URL.
992
+ $file_name = basename( parse_url( $url, PHP_URL_PATH ) );
993
+
994
+ if ( ! $file_name ) {
995
+ $file_name = md5( $url );
996
+ }
997
+
998
+ $tmp_file_name = wp_tempnam( $file_name );
999
+ if ( ! $tmp_file_name ) {
1000
+ return new WP_Error( 'import_no_file', __( 'Could not create temporary file.', 'wordpress-importer' ) );
1001
+ }
1002
+
1003
+ // Fetch the remote URL and write it to the placeholder file.
1004
+ $remote_response = wp_safe_remote_get( $url, array(
1005
+ 'timeout' => 300,
1006
+ 'stream' => true,
1007
+ 'filename' => $tmp_file_name,
1008
+ 'headers' => array(
1009
+ 'Accept-Encoding' => 'identity',
1010
+ ),
1011
+ ) );
1012
+
1013
+ if ( is_wp_error( $remote_response ) ) {
1014
+ @unlink( $tmp_file_name );
1015
+ return new WP_Error(
1016
+ 'import_file_error',
1017
+ sprintf(
1018
+ /* translators: 1: The WordPress error message. 2: The WordPress error code. */
1019
+ __( 'Request failed due to an error: %1$s (%2$s)', 'wordpress-importer' ),
1020
+ esc_html( $remote_response->get_error_message() ),
1021
+ esc_html( $remote_response->get_error_code() )
1022
+ )
1023
+ );
1024
+ }
1025
+
1026
+ $remote_response_code = (int) wp_remote_retrieve_response_code( $remote_response );
1027
+
1028
+ // Make sure the fetch was successful.
1029
+ if ( 200 !== $remote_response_code ) {
1030
+ @unlink( $tmp_file_name );
1031
+ return new WP_Error(
1032
+ 'import_file_error',
1033
+ sprintf(
1034
+ /* translators: 1: The HTTP error message. 2: The HTTP error code. */
1035
+ __( 'Remote server returned the following unexpected result: %1$s (%2$s)', 'wordpress-importer' ),
1036
+ get_status_header_desc( $remote_response_code ),
1037
+ esc_html( $remote_response_code )
1038
+ )
1039
+ );
1040
+ }
1041
+
1042
+ $headers = wp_remote_retrieve_headers( $remote_response );
1043
+
1044
+ // Request failed.
1045
+ if ( ! $headers ) {
1046
+ @unlink( $tmp_file_name );
1047
+ return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
1048
+ }
1049
+
1050
+ $filesize = (int) filesize( $tmp_file_name );
1051
+
1052
+ if ( 0 === $filesize ) {
1053
+ @unlink( $tmp_file_name );
1054
+ return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
1055
+ }
1056
+
1057
+ if ( ! isset( $headers['content-encoding'] ) && isset( $headers['content-length'] ) && $filesize !== (int) $headers['content-length'] ) {
1058
+ @unlink( $tmp_file_name );
1059
+ return new WP_Error( 'import_file_error', __('Downloaded file has incorrect size', 'wordpress-importer' ) );
1060
+ }
1061
+
1062
+ $max_size = (int) $this->max_attachment_size();
1063
+ if ( ! empty( $max_size ) && $filesize > $max_size ) {
1064
+ @unlink( $tmp_file_name );
1065
+ return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer' ), size_format($max_size) ) );
1066
+ }
1067
+
1068
+ // Override file name with Content-Disposition header value.
1069
+ if ( ! empty( $headers['content-disposition'] ) ) {
1070
+ $file_name_from_disposition = self::get_filename_from_disposition( (array) $headers['content-disposition'] );
1071
+ if ( $file_name_from_disposition ) {
1072
+ $file_name = $file_name_from_disposition;
1073
+ }
1074
+ }
1075
+
1076
+ // Set file extension if missing.
1077
+ $file_ext = pathinfo( $file_name, PATHINFO_EXTENSION );
1078
+ if ( ! $file_ext && ! empty( $headers['content-type'] ) ) {
1079
+ $extension = self::get_file_extension_by_mime_type( $headers['content-type'] );
1080
+ if ( $extension ) {
1081
+ $file_name = "{$file_name}.{$extension}";
1082
+ }
1083
+ }
1084
+
1085
+ // Handle the upload like _wp_handle_upload() does.
1086
+ $wp_filetype = wp_check_filetype_and_ext( $tmp_file_name, $file_name );
1087
+ $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
1088
+ $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
1089
+ $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
1090
+
1091
+ // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect.
1092
+ if ( $proper_filename ) {
1093
+ $file_name = $proper_filename;
1094
+ }
1095
+
1096
+ if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
1097
+ return new WP_Error( 'import_file_error', __( 'Sorry, this file type is not permitted for security reasons.', 'wordpress-importer' ) );
1098
+ }
1099
+
1100
+ $uploads = wp_upload_dir( $post['upload_date'] );
1101
+ if ( ! ( $uploads && false === $uploads['error'] ) ) {
1102
+ return new WP_Error( 'upload_dir_error', $uploads['error'] );
1103
+ }
1104
+
1105
+ // Move the file to the uploads dir.
1106
+ $file_name = wp_unique_filename( $uploads['path'], $file_name );
1107
+ $new_file = $uploads['path'] . "/$file_name";
1108
+ $move_new_file = copy( $tmp_file_name, $new_file );
1109
+
1110
+ if ( ! $move_new_file ) {
1111
+ @unlink( $tmp_file_name );
1112
+ return new WP_Error( 'import_file_error', __( 'The uploaded file could not be moved', 'wordpress-importer' ) );
1113
+ }
1114
+
1115
+ // Set correct file permissions.
1116
+ $stat = stat( dirname( $new_file ) );
1117
+ $perms = $stat['mode'] & 0000666;
1118
+ chmod( $new_file, $perms );
1119
+
1120
+ $upload = array(
1121
+ 'file' => $new_file,
1122
+ 'url' => $uploads['url'] . "/$file_name",
1123
+ 'type' => $wp_filetype['type'],
1124
+ 'error' => false,
1125
+ );
1126
+
1127
+ // keep track of the old and new urls so we can substitute them later
1128
+ $this->url_remap[$url] = $upload['url'];
1129
+ $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
1130
+ // keep track of the destination if the remote url is redirected somewhere else
1131
+ if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
1132
+ $this->url_remap[$headers['x-final-location']] = $upload['url'];
1133
+
1134
+ return $upload;
1135
+ }
1136
+
1137
+ /**
1138
+ * Attempt to associate posts and menu items with previously missing parents
1139
+ *
1140
+ * An imported post's parent may not have been imported when it was first created
1141
+ * so try again. Similarly for child menu items and menu items which were missing
1142
+ * the object (e.g. post) they represent in the menu
1143
+ */
1144
+ function backfill_parents() {
1145
+ global $wpdb;
1146
+
1147
+ // find parents for post orphans
1148
+ foreach ( $this->post_orphans as $child_id => $parent_id ) {
1149
+ $local_child_id = $local_parent_id = false;
1150
+ if ( isset( $this->processed_posts[$child_id] ) )
1151
+ $local_child_id = $this->processed_posts[$child_id];
1152
+ if ( isset( $this->processed_posts[$parent_id] ) )
1153
+ $local_parent_id = $this->processed_posts[$parent_id];
1154
+
1155
+ if ( $local_child_id && $local_parent_id ) {
1156
+ $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
1157
+ clean_post_cache( $local_child_id );
1158
+ }
1159
+ }
1160
+
1161
+ // all other posts/terms are imported, retry menu items with missing associated object
1162
+ $missing_menu_items = $this->missing_menu_items;
1163
+ foreach ( $missing_menu_items as $item )
1164
+ $this->process_menu_item( $item );
1165
+
1166
+ // find parents for menu item orphans
1167
+ foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
1168
+ $local_child_id = $local_parent_id = 0;
1169
+ if ( isset( $this->processed_menu_items[$child_id] ) )
1170
+ $local_child_id = $this->processed_menu_items[$child_id];
1171
+ if ( isset( $this->processed_menu_items[$parent_id] ) )
1172
+ $local_parent_id = $this->processed_menu_items[$parent_id];
1173
+
1174
+ if ( $local_child_id && $local_parent_id )
1175
+ update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
1176
+ }
1177
+ }
1178
+
1179
+ /**
1180
+ * Use stored mapping information to update old attachment URLs
1181
+ */
1182
+ function backfill_attachment_urls() {
1183
+ global $wpdb;
1184
+ // make sure we do the longest urls first, in case one is a substring of another
1185
+ uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
1186
+
1187
+ foreach ( $this->url_remap as $from_url => $to_url ) {
1188
+ // remap urls in post_content
1189
+ $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
1190
+ // remap enclosure urls
1191
+ $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
1192
+ }
1193
+ }
1194
+
1195
+ /**
1196
+ * Update _thumbnail_id meta to new, imported attachment IDs
1197
+ */
1198
+ function remap_featured_images() {
1199
+ // cycle through posts that have a featured image
1200
+ foreach ( $this->featured_images as $post_id => $value ) {
1201
+ if ( isset( $this->processed_posts[$value] ) ) {
1202
+ $new_id = $this->processed_posts[$value];
1203
+ // only update if there's a difference
1204
+ if ( $new_id != $value )
1205
+ update_post_meta( $post_id, '_thumbnail_id', $new_id );
1206
+ }
1207
+ }
1208
+ }
1209
+
1210
+ /**
1211
+ * Parse a WXR file
1212
+ *
1213
+ * @param string $file Path to WXR file for parsing
1214
+ * @return array Information gathered from the WXR file
1215
+ */
1216
+ function parse( $file ) {
1217
+ $parser = new WXR_Parser();
1218
+ return $parser->parse( $file );
1219
+ }
1220
+
1221
+ // Display import page title
1222
+ function header() {
1223
+ echo '<div class="wrap">';
1224
+ echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
1225
+
1226
+ $updates = get_plugin_updates();
1227
+ $basename = plugin_basename(__FILE__);
1228
+ if ( isset( $updates[$basename] ) ) {
1229
+ $update = $updates[$basename];
1230
+ echo '<div class="error"><p><strong>';
1231
+ 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 );
1232
+ echo '</strong></p></div>';
1233
+ }
1234
+ }
1235
+
1236
+ // Close div.wrap
1237
+ function footer() {
1238
+ echo '</div>';
1239
+ }
1240
+
1241
+ /**
1242
+ * Display introductory text and file upload form
1243
+ */
1244
+ function greet() {
1245
+ echo '<div class="narrow">';
1246
+ 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>';
1247
+ echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
1248
+ wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
1249
+ echo '</div>';
1250
+ }
1251
+
1252
+ /**
1253
+ * Decide if the given meta key maps to information we will want to import
1254
+ *
1255
+ * @param string $key The meta key to check
1256
+ * @return string|bool The key if we do want to import, false if not
1257
+ */
1258
+ function is_valid_meta_key( $key ) {
1259
+ // skip attachment metadata since we'll regenerate it from scratch
1260
+ // skip _edit_lock as not relevant for import
1261
+ if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
1262
+ return false;
1263
+ return $key;
1264
+ }
1265
+
1266
+ /**
1267
+ * Decide whether or not the importer is allowed to create users.
1268
+ * Default is true, can be filtered via import_allow_create_users
1269
+ *
1270
+ * @return bool True if creating users is allowed
1271
+ */
1272
+ function allow_create_users() {
1273
+ return apply_filters( 'import_allow_create_users', true );
1274
+ }
1275
+
1276
+ /**
1277
+ * Decide whether or not the importer should attempt to download attachment files.
1278
+ * Default is true, can be filtered via import_allow_fetch_attachments. The choice
1279
+ * made at the import options screen must also be true, false here hides that checkbox.
1280
+ *
1281
+ * @return bool True if downloading attachments is allowed
1282
+ */
1283
+ function allow_fetch_attachments() {
1284
+ return apply_filters( 'import_allow_fetch_attachments', true );
1285
+ }
1286
+
1287
+ /**
1288
+ * Decide what the maximum file size for downloaded attachments is.
1289
+ * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
1290
+ *
1291
+ * @return int Maximum attachment file size to import
1292
+ */
1293
+ function max_attachment_size() {
1294
+ return apply_filters( 'import_attachment_size_limit', 0 );
1295
+ }
1296
+
1297
+ /**
1298
+ * Added to http_request_timeout filter to force timeout at 60 seconds during import
1299
+ * @return int 60
1300
+ */
1301
+ function bump_request_timeout( $val ) {
1302
+ return 60;
1303
+ }
1304
+
1305
+ // return the difference in length between two strings
1306
+ function cmpr_strlen( $a, $b ) {
1307
+ return strlen($b) - strlen($a);
1308
+ }
1309
+
1310
+ /**
1311
+ * Parses filename from a Content-Disposition header value.
1312
+ *
1313
+ * As per RFC6266:
1314
+ *
1315
+ * content-disposition = "Content-Disposition" ":"
1316
+ * disposition-type *( ";" disposition-parm )
1317
+ *
1318
+ * disposition-type = "inline" | "attachment" | disp-ext-type
1319
+ * ; case-insensitive
1320
+ * disp-ext-type = token
1321
+ *
1322
+ * disposition-parm = filename-parm | disp-ext-parm
1323
+ *
1324
+ * filename-parm = "filename" "=" value
1325
+ * | "filename*" "=" ext-value
1326
+ *
1327
+ * disp-ext-parm = token "=" value
1328
+ * | ext-token "=" ext-value
1329
+ * ext-token = <the characters in token, followed by "*">
1330
+ *
1331
+ * @since 0.7.0
1332
+ *
1333
+ * @see WP_REST_Attachments_Controller::get_filename_from_disposition()
1334
+ *
1335
+ * @link http://tools.ietf.org/html/rfc2388
1336
+ * @link http://tools.ietf.org/html/rfc6266
1337
+ *
1338
+ * @param string[] $disposition_header List of Content-Disposition header values.
1339
+ * @return string|null Filename if available, or null if not found.
1340
+ */
1341
+ protected static function get_filename_from_disposition( $disposition_header ) {
1342
+ // Get the filename.
1343
+ $filename = null;
1344
+
1345
+ foreach ( $disposition_header as $value ) {
1346
+ $value = trim( $value );
1347
+
1348
+ if ( strpos( $value, ';' ) === false ) {
1349
+ continue;
1350
+ }
1351
+
1352
+ list( $type, $attr_parts ) = explode( ';', $value, 2 );
1353
+
1354
+ $attr_parts = explode( ';', $attr_parts );
1355
+ $attributes = array();
1356
+
1357
+ foreach ( $attr_parts as $part ) {
1358
+ if ( strpos( $part, '=' ) === false ) {
1359
+ continue;
1360
+ }
1361
+
1362
+ list( $key, $value ) = explode( '=', $part, 2 );
1363
+
1364
+ $attributes[ trim( $key ) ] = trim( $value );
1365
+ }
1366
+
1367
+ if ( empty( $attributes['filename'] ) ) {
1368
+ continue;
1369
+ }
1370
+
1371
+ $filename = trim( $attributes['filename'] );
1372
+
1373
+ // Unquote quoted filename, but after trimming.
1374
+ if ( substr( $filename, 0, 1 ) === '"' && substr( $filename, -1, 1 ) === '"' ) {
1375
+ $filename = substr( $filename, 1, -1 );
1376
+ }
1377
+ }
1378
+
1379
+ return $filename;
1380
+ }
1381
+
1382
+ /**
1383
+ * Retrieves file extension by mime type.
1384
+ *
1385
+ * @since 0.7.0
1386
+ *
1387
+ * @param string $mime_type Mime type to search extension for.
1388
+ * @return string|null File extension if available, or null if not found.
1389
+ */
1390
+ protected static function get_file_extension_by_mime_type( $mime_type ) {
1391
+ static $map = null;
1392
+
1393
+ if ( is_array( $map ) ) {
1394
+ return isset( $map[ $mime_type ] ) ? $map[ $mime_type ] : null;
1395
+ }
1396
+
1397
+ $mime_types = wp_get_mime_types();
1398
+ $map = array_flip( $mime_types );
1399
+
1400
+ // Some types have multiple extensions, use only the first one.
1401
+ foreach ( $map as $type => $extensions ) {
1402
+ $map[ $type ] = strtok( $extensions, '|' );
1403
+ }
1404
+
1405
+ return isset( $map[ $mime_type ] ) ? $map[ $mime_type ] : null;
1406
+ }
1407
+ }
compat.php ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Implementation for WordPress functions missing in older WordPress versions.
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ if ( ! function_exists( 'wp_slash_strings_only' ) ) {
10
+ /**
11
+ * Adds slashes to only string values in an array of values.
12
+ *
13
+ * Compat for WordPress < 5.3.0.
14
+ *
15
+ * @since 0.7.0
16
+ *
17
+ * @param mixed $value Scalar or array of scalars.
18
+ * @return mixed Slashes $value
19
+ */
20
+ function wp_slash_strings_only( $value ) {
21
+ return map_deep( $value, 'addslashes_strings_only' );
22
+ }
23
+ }
24
+
25
+ if ( ! function_exists( 'addslashes_strings_only' ) ) {
26
+ /**
27
+ * Adds slashes only if the provided value is a string.
28
+ *
29
+ * Compat for WordPress < 5.3.0.
30
+ *
31
+ * @since 0.7.0
32
+ *
33
+ * @param mixed $value
34
+ * @return mixed
35
+ */
36
+ function addslashes_strings_only( $value ) {
37
+ return is_string( $value ) ? addslashes( $value ) : $value;
38
+ }
39
+ }
40
+
41
+ if ( ! function_exists( 'map_deep' ) ) {
42
+ /**
43
+ * Maps a function to all non-iterable elements of an array or an object.
44
+ *
45
+ * Compat for WordPress < 4.4.0.
46
+ *
47
+ * @since 0.7.0
48
+ *
49
+ * @param mixed $value The array, object, or scalar.
50
+ * @param callable $callback The function to map onto $value.
51
+ * @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
52
+ */
53
+ function map_deep( $value, $callback ) {
54
+ if ( is_array( $value ) ) {
55
+ foreach ( $value as $index => $item ) {
56
+ $value[ $index ] = map_deep( $item, $callback );
57
+ }
58
+ } elseif ( is_object( $value ) ) {
59
+ $object_vars = get_object_vars( $value );
60
+ foreach ( $object_vars as $property_name => $property_value ) {
61
+ $value->$property_name = map_deep( $property_value, $callback );
62
+ }
63
+ } else {
64
+ $value = call_user_func( $callback, $value );
65
+ }
66
+
67
+ return $value;
68
+ }
69
+ }
parsers.php CHANGED
@@ -6,693 +6,16 @@
6
  * @subpackage Importer
7
  */
8
 
9
- /**
10
- * WordPress Importer class for managing parsing of WXR files.
11
- */
12
- class WXR_Parser {
13
- function parse( $file ) {
14
- // Attempt to use proper XML parsers first
15
- if ( extension_loaded( 'simplexml' ) ) {
16
- $parser = new WXR_Parser_SimpleXML;
17
- $result = $parser->parse( $file );
18
-
19
- // If SimpleXML succeeds or this is an invalid WXR file then return the results
20
- if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() )
21
- return $result;
22
- } else if ( extension_loaded( 'xml' ) ) {
23
- $parser = new WXR_Parser_XML;
24
- $result = $parser->parse( $file );
25
-
26
- // If XMLParser succeeds or this is an invalid WXR file then return the results
27
- if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() )
28
- return $result;
29
- }
30
-
31
- // We have a malformed XML file, so display the error and fallthrough to regex
32
- if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) {
33
- echo '<pre>';
34
- if ( 'SimpleXML_parse_error' == $result->get_error_code() ) {
35
- foreach ( $result->get_error_data() as $error )
36
- echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n";
37
- } else if ( 'XML_parse_error' == $result->get_error_code() ) {
38
- $error = $result->get_error_data();
39
- echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] );
40
- }
41
- echo '</pre>';
42
- echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />';
43
- echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>';
44
- }
45
-
46
- // use regular expressions if nothing else available or this is bad XML
47
- $parser = new WXR_Parser_Regex;
48
- return $parser->parse( $file );
49
- }
50
- }
51
-
52
- /**
53
- * WXR Parser that makes use of the SimpleXML PHP extension.
54
- */
55
- class WXR_Parser_SimpleXML {
56
- function parse( $file ) {
57
- $authors = $posts = $categories = $tags = $terms = array();
58
-
59
- $internal_errors = libxml_use_internal_errors(true);
60
-
61
- $dom = new DOMDocument;
62
- $old_value = null;
63
- if ( function_exists( 'libxml_disable_entity_loader' ) ) {
64
- $old_value = libxml_disable_entity_loader( true );
65
- }
66
- $success = $dom->loadXML( file_get_contents( $file ) );
67
- if ( ! is_null( $old_value ) ) {
68
- libxml_disable_entity_loader( $old_value );
69
- }
70
-
71
- if ( ! $success || isset( $dom->doctype ) ) {
72
- return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
73
- }
74
-
75
- $xml = simplexml_import_dom( $dom );
76
- unset( $dom );
77
-
78
- // halt if loading produces an error
79
- if ( ! $xml )
80
- return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
81
-
82
- $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
83
- if ( ! $wxr_version )
84
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
85
-
86
- $wxr_version = (string) trim( $wxr_version[0] );
87
- // confirm that we are dealing with the correct file format
88
- if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
89
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
90
-
91
- $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
92
- $base_url = (string) trim( $base_url[0] );
93
-
94
- $namespaces = $xml->getDocNamespaces();
95
- if ( ! isset( $namespaces['wp'] ) )
96
- $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
97
- if ( ! isset( $namespaces['excerpt'] ) )
98
- $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
99
-
100
- // grab authors
101
- foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
102
- $a = $author_arr->children( $namespaces['wp'] );
103
- $login = (string) $a->author_login;
104
- $authors[$login] = array(
105
- 'author_id' => (int) $a->author_id,
106
- 'author_login' => $login,
107
- 'author_email' => (string) $a->author_email,
108
- 'author_display_name' => (string) $a->author_display_name,
109
- 'author_first_name' => (string) $a->author_first_name,
110
- 'author_last_name' => (string) $a->author_last_name
111
- );
112
- }
113
-
114
- // grab cats, tags and terms
115
- foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
116
- $t = $term_arr->children( $namespaces['wp'] );
117
- $category = array(
118
- 'term_id' => (int) $t->term_id,
119
- 'category_nicename' => (string) $t->category_nicename,
120
- 'category_parent' => (string) $t->category_parent,
121
- 'cat_name' => (string) $t->cat_name,
122
- 'category_description' => (string) $t->category_description
123
- );
124
-
125
- foreach ( $t->termmeta as $meta ) {
126
- $category['termmeta'][] = array(
127
- 'key' => (string) $meta->meta_key,
128
- 'value' => (string) $meta->meta_value
129
- );
130
- }
131
-
132
- $categories[] = $category;
133
- }
134
-
135
- foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
136
- $t = $term_arr->children( $namespaces['wp'] );
137
- $tag = array(
138
- 'term_id' => (int) $t->term_id,
139
- 'tag_slug' => (string) $t->tag_slug,
140
- 'tag_name' => (string) $t->tag_name,
141
- 'tag_description' => (string) $t->tag_description
142
- );
143
-
144
- foreach ( $t->termmeta as $meta ) {
145
- $tag['termmeta'][] = array(
146
- 'key' => (string) $meta->meta_key,
147
- 'value' => (string) $meta->meta_value
148
- );
149
- }
150
-
151
- $tags[] = $tag;
152
- }
153
-
154
- foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
155
- $t = $term_arr->children( $namespaces['wp'] );
156
- $term = array(
157
- 'term_id' => (int) $t->term_id,
158
- 'term_taxonomy' => (string) $t->term_taxonomy,
159
- 'slug' => (string) $t->term_slug,
160
- 'term_parent' => (string) $t->term_parent,
161
- 'term_name' => (string) $t->term_name,
162
- 'term_description' => (string) $t->term_description
163
- );
164
-
165
- foreach ( $t->termmeta as $meta ) {
166
- $term['termmeta'][] = array(
167
- 'key' => (string) $meta->meta_key,
168
- 'value' => (string) $meta->meta_value
169
- );
170
- }
171
-
172
- $terms[] = $term;
173
- }
174
-
175
- // grab posts
176
- foreach ( $xml->channel->item as $item ) {
177
- $post = array(
178
- 'post_title' => (string) $item->title,
179
- 'guid' => (string) $item->guid,
180
- );
181
-
182
- $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
183
- $post['post_author'] = (string) $dc->creator;
184
-
185
- $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
186
- $excerpt = $item->children( $namespaces['excerpt'] );
187
- $post['post_content'] = (string) $content->encoded;
188
- $post['post_excerpt'] = (string) $excerpt->encoded;
189
-
190
- $wp = $item->children( $namespaces['wp'] );
191
- $post['post_id'] = (int) $wp->post_id;
192
- $post['post_date'] = (string) $wp->post_date;
193
- $post['post_date_gmt'] = (string) $wp->post_date_gmt;
194
- $post['comment_status'] = (string) $wp->comment_status;
195
- $post['ping_status'] = (string) $wp->ping_status;
196
- $post['post_name'] = (string) $wp->post_name;
197
- $post['status'] = (string) $wp->status;
198
- $post['post_parent'] = (int) $wp->post_parent;
199
- $post['menu_order'] = (int) $wp->menu_order;
200
- $post['post_type'] = (string) $wp->post_type;
201
- $post['post_password'] = (string) $wp->post_password;
202
- $post['is_sticky'] = (int) $wp->is_sticky;
203
-
204
- if ( isset($wp->attachment_url) )
205
- $post['attachment_url'] = (string) $wp->attachment_url;
206
-
207
- foreach ( $item->category as $c ) {
208
- $att = $c->attributes();
209
- if ( isset( $att['nicename'] ) )
210
- $post['terms'][] = array(
211
- 'name' => (string) $c,
212
- 'slug' => (string) $att['nicename'],
213
- 'domain' => (string) $att['domain']
214
- );
215
- }
216
-
217
- foreach ( $wp->postmeta as $meta ) {
218
- $post['postmeta'][] = array(
219
- 'key' => (string) $meta->meta_key,
220
- 'value' => (string) $meta->meta_value
221
- );
222
- }
223
-
224
- foreach ( $wp->comment as $comment ) {
225
- $meta = array();
226
- if ( isset( $comment->commentmeta ) ) {
227
- foreach ( $comment->commentmeta as $m ) {
228
- $meta[] = array(
229
- 'key' => (string) $m->meta_key,
230
- 'value' => (string) $m->meta_value
231
- );
232
- }
233
- }
234
-
235
- $post['comments'][] = array(
236
- 'comment_id' => (int) $comment->comment_id,
237
- 'comment_author' => (string) $comment->comment_author,
238
- 'comment_author_email' => (string) $comment->comment_author_email,
239
- 'comment_author_IP' => (string) $comment->comment_author_IP,
240
- 'comment_author_url' => (string) $comment->comment_author_url,
241
- 'comment_date' => (string) $comment->comment_date,
242
- 'comment_date_gmt' => (string) $comment->comment_date_gmt,
243
- 'comment_content' => (string) $comment->comment_content,
244
- 'comment_approved' => (string) $comment->comment_approved,
245
- 'comment_type' => (string) $comment->comment_type,
246
- 'comment_parent' => (string) $comment->comment_parent,
247
- 'comment_user_id' => (int) $comment->comment_user_id,
248
- 'commentmeta' => $meta,
249
- );
250
- }
251
-
252
- $posts[] = $post;
253
- }
254
-
255
- return array(
256
- 'authors' => $authors,
257
- 'posts' => $posts,
258
- 'categories' => $categories,
259
- 'tags' => $tags,
260
- 'terms' => $terms,
261
- 'base_url' => $base_url,
262
- 'version' => $wxr_version
263
- );
264
- }
265
- }
266
-
267
- /**
268
- * WXR Parser that makes use of the XML Parser PHP extension.
269
- */
270
- class WXR_Parser_XML {
271
- var $wp_tags = array(
272
- 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url',
273
- 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password',
274
- 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description',
275
- 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent',
276
- 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name',
277
- 'wp:author_first_name', 'wp:author_last_name',
278
- );
279
- var $wp_sub_tags = array(
280
- 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url',
281
- 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content',
282
- 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id',
283
- );
284
-
285
- function parse( $file ) {
286
- $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
287
- $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
288
-
289
- $xml = xml_parser_create( 'UTF-8' );
290
- xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 );
291
- xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 );
292
- xml_set_object( $xml, $this );
293
- xml_set_character_data_handler( $xml, 'cdata' );
294
- xml_set_element_handler( $xml, 'tag_open', 'tag_close' );
295
-
296
- if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) {
297
- $current_line = xml_get_current_line_number( $xml );
298
- $current_column = xml_get_current_column_number( $xml );
299
- $error_code = xml_get_error_code( $xml );
300
- $error_string = xml_error_string( $error_code );
301
- return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) );
302
- }
303
- xml_parser_free( $xml );
304
-
305
- if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) )
306
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
307
-
308
- return array(
309
- 'authors' => $this->authors,
310
- 'posts' => $this->posts,
311
- 'categories' => $this->category,
312
- 'tags' => $this->tag,
313
- 'terms' => $this->term,
314
- 'base_url' => $this->base_url,
315
- 'version' => $this->wxr_version
316
- );
317
- }
318
-
319
- function tag_open( $parse, $tag, $attr ) {
320
- if ( in_array( $tag, $this->wp_tags ) ) {
321
- $this->in_tag = substr( $tag, 3 );
322
- return;
323
- }
324
-
325
- if ( in_array( $tag, $this->wp_sub_tags ) ) {
326
- $this->in_sub_tag = substr( $tag, 3 );
327
- return;
328
- }
329
-
330
- switch ( $tag ) {
331
- case 'category':
332
- if ( isset($attr['domain'], $attr['nicename']) ) {
333
- $this->sub_data['domain'] = $attr['domain'];
334
- $this->sub_data['slug'] = $attr['nicename'];
335
- }
336
- break;
337
- case 'item': $this->in_post = true;
338
- case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break;
339
- case 'guid': $this->in_tag = 'guid'; break;
340
- case 'dc:creator': $this->in_tag = 'post_author'; break;
341
- case 'content:encoded': $this->in_tag = 'post_content'; break;
342
- case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break;
343
-
344
- case 'wp:term_slug': $this->in_tag = 'slug'; break;
345
- case 'wp:meta_key': $this->in_sub_tag = 'key'; break;
346
- case 'wp:meta_value': $this->in_sub_tag = 'value'; break;
347
- }
348
- }
349
-
350
- function cdata( $parser, $cdata ) {
351
- if ( ! trim( $cdata ) )
352
- return;
353
-
354
- if ( false !== $this->in_tag || false !== $this->in_sub_tag ) {
355
- $this->cdata .= $cdata;
356
- } else {
357
- $this->cdata .= trim( $cdata );
358
- }
359
- }
360
-
361
- function tag_close( $parser, $tag ) {
362
- switch ( $tag ) {
363
- case 'wp:comment':
364
- unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data
365
- if ( ! empty( $this->sub_data ) )
366
- $this->data['comments'][] = $this->sub_data;
367
- $this->sub_data = false;
368
- break;
369
- case 'wp:commentmeta':
370
- $this->sub_data['commentmeta'][] = array(
371
- 'key' => $this->sub_data['key'],
372
- 'value' => $this->sub_data['value']
373
- );
374
- break;
375
- case 'category':
376
- if ( ! empty( $this->sub_data ) ) {
377
- $this->sub_data['name'] = $this->cdata;
378
- $this->data['terms'][] = $this->sub_data;
379
- }
380
- $this->sub_data = false;
381
- break;
382
- case 'wp:postmeta':
383
- if ( ! empty( $this->sub_data ) )
384
- $this->data['postmeta'][] = $this->sub_data;
385
- $this->sub_data = false;
386
- break;
387
- case 'item':
388
- $this->posts[] = $this->data;
389
- $this->data = false;
390
- break;
391
- case 'wp:category':
392
- case 'wp:tag':
393
- case 'wp:term':
394
- $n = substr( $tag, 3 );
395
- array_push( $this->$n, $this->data );
396
- $this->data = false;
397
- break;
398
- case 'wp:author':
399
- if ( ! empty($this->data['author_login']) )
400
- $this->authors[$this->data['author_login']] = $this->data;
401
- $this->data = false;
402
- break;
403
- case 'wp:base_site_url':
404
- $this->base_url = $this->cdata;
405
- break;
406
- case 'wp:wxr_version':
407
- $this->wxr_version = $this->cdata;
408
- break;
409
-
410
- default:
411
- if ( $this->in_sub_tag ) {
412
- $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
413
- $this->in_sub_tag = false;
414
- } else if ( $this->in_tag ) {
415
- $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
416
- $this->in_tag = false;
417
- }
418
- }
419
-
420
- $this->cdata = false;
421
- }
422
- }
423
-
424
- /**
425
- * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
426
- */
427
- class WXR_Parser_Regex {
428
- var $authors = array();
429
- var $posts = array();
430
- var $categories = array();
431
- var $tags = array();
432
- var $terms = array();
433
- var $base_url = '';
434
-
435
- function __construct() {
436
- $this->has_gzip = is_callable( 'gzopen' );
437
- }
438
-
439
- function parse( $file ) {
440
- $wxr_version = $in_multiline = false;
441
-
442
- $multiline_content = '';
443
-
444
- $multiline_tags = array(
445
- 'item' => array( 'posts', array( $this, 'process_post' ) ),
446
- 'wp:category' => array( 'categories', array( $this, 'process_category' ) ),
447
- 'wp:tag' => array( 'tags', array( $this, 'process_tag' ) ),
448
- 'wp:term' => array( 'terms', array( $this, 'process_term' ) ),
449
- );
450
-
451
- $fp = $this->fopen( $file, 'r' );
452
- if ( $fp ) {
453
- while ( ! $this->feof( $fp ) ) {
454
- $importline = rtrim( $this->fgets( $fp ) );
455
-
456
- if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
457
- $wxr_version = $version[1];
458
-
459
- if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
460
- preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
461
- $this->base_url = $url[1];
462
- continue;
463
- }
464
-
465
- if ( false !== strpos( $importline, '<wp:author>' ) ) {
466
- preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
467
- $a = $this->process_author( $author[1] );
468
- $this->authors[$a['author_login']] = $a;
469
- continue;
470
- }
471
-
472
- foreach ( $multiline_tags as $tag => $handler ) {
473
- // Handle multi-line tags on a singular line
474
- if ( preg_match( '|<' . $tag . '>(.*?)</' . $tag . '>|is', $importline, $matches ) ) {
475
- $this->{$handler[0]}[] = call_user_func( $handler[1], $matches[1] );
476
-
477
- } elseif ( false !== ( $pos = strpos( $importline, "<$tag>" ) ) ) {
478
- // Take note of any content after the opening tag
479
- $multiline_content = trim( substr( $importline, $pos + strlen( $tag ) + 2 ) );
480
-
481
- // We don't want to have this line added to `$is_multiline` below.
482
- $importline = '';
483
- $in_multiline = $tag;
484
-
485
- } elseif ( false !== ( $pos = strpos( $importline, "</$tag>" ) ) ) {
486
- $in_multiline = false;
487
- $multiline_content .= trim( substr( $importline, 0, $pos ) );
488
-
489
- $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content );
490
- }
491
- }
492
-
493
- if ( $in_multiline && $importline ) {
494
- $multiline_content .= $importline . "\n";
495
- }
496
- }
497
-
498
- $this->fclose($fp);
499
- }
500
-
501
- if ( ! $wxr_version )
502
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
503
-
504
- return array(
505
- 'authors' => $this->authors,
506
- 'posts' => $this->posts,
507
- 'categories' => $this->categories,
508
- 'tags' => $this->tags,
509
- 'terms' => $this->terms,
510
- 'base_url' => $this->base_url,
511
- 'version' => $wxr_version
512
- );
513
- }
514
-
515
- function get_tag( $string, $tag ) {
516
- preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
517
- if ( isset( $return[1] ) ) {
518
- if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
519
- if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
520
- preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
521
- $return = '';
522
- foreach( $matches[1] as $match )
523
- $return .= $match;
524
- } else {
525
- $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
526
- }
527
- } else {
528
- $return = $return[1];
529
- }
530
- } else {
531
- $return = '';
532
- }
533
- return $return;
534
- }
535
-
536
- function process_category( $c ) {
537
- return array(
538
- 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
539
- 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
540
- 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
541
- 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
542
- 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
543
- );
544
- }
545
-
546
- function process_tag( $t ) {
547
- return array(
548
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
549
- 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
550
- 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
551
- 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
552
- );
553
- }
554
-
555
- function process_term( $t ) {
556
- return array(
557
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
558
- 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
559
- 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
560
- 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
561
- 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
562
- 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
563
- );
564
- }
565
-
566
- function process_author( $a ) {
567
- return array(
568
- 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
569
- 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
570
- 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
571
- 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
572
- 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
573
- 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
574
- );
575
- }
576
-
577
- function process_post( $post ) {
578
- $post_id = $this->get_tag( $post, 'wp:post_id' );
579
- $post_title = $this->get_tag( $post, 'title' );
580
- $post_date = $this->get_tag( $post, 'wp:post_date' );
581
- $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
582
- $comment_status = $this->get_tag( $post, 'wp:comment_status' );
583
- $ping_status = $this->get_tag( $post, 'wp:ping_status' );
584
- $status = $this->get_tag( $post, 'wp:status' );
585
- $post_name = $this->get_tag( $post, 'wp:post_name' );
586
- $post_parent = $this->get_tag( $post, 'wp:post_parent' );
587
- $menu_order = $this->get_tag( $post, 'wp:menu_order' );
588
- $post_type = $this->get_tag( $post, 'wp:post_type' );
589
- $post_password = $this->get_tag( $post, 'wp:post_password' );
590
- $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
591
- $guid = $this->get_tag( $post, 'guid' );
592
- $post_author = $this->get_tag( $post, 'dc:creator' );
593
-
594
- $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
595
- $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
596
- $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
597
- $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
598
-
599
- $post_content = $this->get_tag( $post, 'content:encoded' );
600
- $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
601
- $post_content = str_replace( '<br>', '<br />', $post_content );
602
- $post_content = str_replace( '<hr>', '<hr />', $post_content );
603
-
604
- $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
605
- 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
606
- 'menu_order', 'post_type', 'post_password', 'is_sticky'
607
- );
608
-
609
- $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
610
- if ( $attachment_url )
611
- $postdata['attachment_url'] = $attachment_url;
612
-
613
- preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
614
- foreach ( $terms as $t ) {
615
- $post_terms[] = array(
616
- 'slug' => $t[2],
617
- 'domain' => $t[1],
618
- 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
619
- );
620
- }
621
- if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
622
-
623
- preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
624
- $comments = $comments[1];
625
- if ( $comments ) {
626
- foreach ( $comments as $comment ) {
627
- preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
628
- $commentmeta = $commentmeta[1];
629
- $c_meta = array();
630
- foreach ( $commentmeta as $m ) {
631
- $c_meta[] = array(
632
- 'key' => $this->get_tag( $m, 'wp:meta_key' ),
633
- 'value' => $this->get_tag( $m, 'wp:meta_value' ),
634
- );
635
- }
636
-
637
- $post_comments[] = array(
638
- 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
639
- 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
640
- 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
641
- 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
642
- 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
643
- 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
644
- 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
645
- 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
646
- 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
647
- 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
648
- 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
649
- 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
650
- 'commentmeta' => $c_meta,
651
- );
652
- }
653
- }
654
- if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
655
-
656
- preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
657
- $postmeta = $postmeta[1];
658
- if ( $postmeta ) {
659
- foreach ( $postmeta as $p ) {
660
- $post_postmeta[] = array(
661
- 'key' => $this->get_tag( $p, 'wp:meta_key' ),
662
- 'value' => $this->get_tag( $p, 'wp:meta_value' ),
663
- );
664
- }
665
- }
666
- if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
667
-
668
- return $postdata;
669
- }
670
-
671
- function _normalize_tag( $matches ) {
672
- return '<' . strtolower( $matches[1] );
673
- }
674
 
675
- function fopen( $filename, $mode = 'r' ) {
676
- if ( $this->has_gzip )
677
- return gzopen( $filename, $mode );
678
- return fopen( $filename, $mode );
679
- }
680
 
681
- function feof( $fp ) {
682
- if ( $this->has_gzip )
683
- return gzeof( $fp );
684
- return feof( $fp );
685
- }
686
 
687
- function fgets( $fp, $len = 8192 ) {
688
- if ( $this->has_gzip )
689
- return gzgets( $fp, $len );
690
- return fgets( $fp, $len );
691
- }
692
 
693
- function fclose( $fp ) {
694
- if ( $this->has_gzip )
695
- return gzclose( $fp );
696
- return fclose( $fp );
697
- }
698
- }
6
  * @subpackage Importer
7
  */
8
 
9
+ _deprecated_file( basename( __FILE__ ), '0.7.0' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ /** WXR_Parser class */
12
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser.php';
 
 
 
13
 
14
+ /** WXR_Parser_SimpleXML class */
15
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-simplexml.php';
 
 
 
16
 
17
+ /** WXR_Parser_XML class */
18
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-xml.php';
 
 
 
19
 
20
+ /** WXR_Parser_Regex class */
21
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-regex.php';
 
 
 
 
parsers/class-wxr-parser-regex.php ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress eXtended RSS file parser implementations
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
11
+ */
12
+ class WXR_Parser_Regex {
13
+ var $authors = array();
14
+ var $posts = array();
15
+ var $categories = array();
16
+ var $tags = array();
17
+ var $terms = array();
18
+ var $base_url = '';
19
+ var $base_blog_url = '';
20
+
21
+ function __construct() {
22
+ $this->has_gzip = is_callable( 'gzopen' );
23
+ }
24
+
25
+ function parse( $file ) {
26
+ $wxr_version = $in_multiline = false;
27
+
28
+ $multiline_content = '';
29
+
30
+ $multiline_tags = array(
31
+ 'item' => array( 'posts', array( $this, 'process_post' ) ),
32
+ 'wp:category' => array( 'categories', array( $this, 'process_category' ) ),
33
+ 'wp:tag' => array( 'tags', array( $this, 'process_tag' ) ),
34
+ 'wp:term' => array( 'terms', array( $this, 'process_term' ) ),
35
+ );
36
+
37
+ $fp = $this->fopen( $file, 'r' );
38
+ if ( $fp ) {
39
+ while ( ! $this->feof( $fp ) ) {
40
+ $importline = rtrim( $this->fgets( $fp ) );
41
+
42
+ if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
43
+ $wxr_version = $version[1];
44
+
45
+ if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
46
+ preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
47
+ $this->base_url = $url[1];
48
+ continue;
49
+ }
50
+
51
+ if ( false !== strpos( $importline, '<wp:base_blog_url>' ) ) {
52
+ preg_match( '|<wp:base_blog_url>(.*?)</wp:base_blog_url>|is', $importline, $blog_url );
53
+ $this->base_blog_url = $blog_url[1];
54
+ continue;
55
+ } else {
56
+ $this->base_blog_url = $this->base_url;
57
+ }
58
+
59
+ if ( false !== strpos( $importline, '<wp:author>' ) ) {
60
+ preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
61
+ $a = $this->process_author( $author[1] );
62
+ $this->authors[$a['author_login']] = $a;
63
+ continue;
64
+ }
65
+
66
+ foreach ( $multiline_tags as $tag => $handler ) {
67
+ // Handle multi-line tags on a singular line
68
+ if ( preg_match( '|<' . $tag . '>(.*?)</' . $tag . '>|is', $importline, $matches ) ) {
69
+ $this->{$handler[0]}[] = call_user_func( $handler[1], $matches[1] );
70
+
71
+ } elseif ( false !== ( $pos = strpos( $importline, "<$tag>" ) ) ) {
72
+ // Take note of any content after the opening tag
73
+ $multiline_content = trim( substr( $importline, $pos + strlen( $tag ) + 2 ) );
74
+
75
+ // We don't want to have this line added to `$is_multiline` below.
76
+ $importline = '';
77
+ $in_multiline = $tag;
78
+
79
+ } elseif ( false !== ( $pos = strpos( $importline, "</$tag>" ) ) ) {
80
+ $in_multiline = false;
81
+ $multiline_content .= trim( substr( $importline, 0, $pos ) );
82
+
83
+ $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content );
84
+ }
85
+ }
86
+
87
+ if ( $in_multiline && $importline ) {
88
+ $multiline_content .= $importline . "\n";
89
+ }
90
+ }
91
+
92
+ $this->fclose($fp);
93
+ }
94
+
95
+ if ( ! $wxr_version )
96
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
97
+
98
+ return array(
99
+ 'authors' => $this->authors,
100
+ 'posts' => $this->posts,
101
+ 'categories' => $this->categories,
102
+ 'tags' => $this->tags,
103
+ 'terms' => $this->terms,
104
+ 'base_url' => $this->base_url,
105
+ 'base_blog_url' => $this->base_blog_url,
106
+ 'version' => $wxr_version
107
+ );
108
+ }
109
+
110
+ function get_tag( $string, $tag ) {
111
+ preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
112
+ if ( isset( $return[1] ) ) {
113
+ if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
114
+ if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
115
+ preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
116
+ $return = '';
117
+ foreach( $matches[1] as $match )
118
+ $return .= $match;
119
+ } else {
120
+ $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
121
+ }
122
+ } else {
123
+ $return = $return[1];
124
+ }
125
+ } else {
126
+ $return = '';
127
+ }
128
+ return $return;
129
+ }
130
+
131
+ function process_category( $c ) {
132
+ $term = array(
133
+ 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
134
+ 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
135
+ 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
136
+ 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
137
+ 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
138
+ );
139
+
140
+ $term_meta = $this->process_meta( $c, 'wp:termmeta' );
141
+ if ( ! empty( $term_meta ) ) {
142
+ $term['termmeta'] = $term_meta;
143
+ }
144
+
145
+ return $term;
146
+ }
147
+
148
+ function process_tag( $t ) {
149
+ $term = array(
150
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
151
+ 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
152
+ 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
153
+ 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
154
+ );
155
+
156
+ $term_meta = $this->process_meta( $t, 'wp:termmeta' );
157
+ if ( ! empty( $term_meta ) ) {
158
+ $term['termmeta'] = $term_meta;
159
+ }
160
+
161
+ return $term;
162
+ }
163
+
164
+ function process_term( $t ) {
165
+ $term = array(
166
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
167
+ 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
168
+ 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
169
+ 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
170
+ 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
171
+ 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
172
+ );
173
+
174
+ $term_meta = $this->process_meta( $t, 'wp:termmeta' );
175
+ if ( ! empty( $term_meta ) ) {
176
+ $term['termmeta'] = $term_meta;
177
+ }
178
+
179
+ return $term;
180
+ }
181
+
182
+ function process_meta( $string, $tag ) {
183
+ $parsed_meta = array();
184
+
185
+ preg_match_all( "|<$tag>(.+?)</$tag>|is", $string, $meta );
186
+
187
+ if ( ! isset( $meta[1] ) ) {
188
+ return $parsed_meta;
189
+ }
190
+
191
+ foreach ( $meta[1] as $m ) {
192
+ $parsed_meta[] = array(
193
+ 'key' => $this->get_tag( $m, 'wp:meta_key' ),
194
+ 'value' => $this->get_tag( $m, 'wp:meta_value' ),
195
+ );
196
+ }
197
+
198
+ return $parsed_meta;
199
+ }
200
+
201
+ function process_author( $a ) {
202
+ return array(
203
+ 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
204
+ 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
205
+ 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
206
+ 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
207
+ 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
208
+ 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
209
+ );
210
+ }
211
+
212
+ function process_post( $post ) {
213
+ $post_id = $this->get_tag( $post, 'wp:post_id' );
214
+ $post_title = $this->get_tag( $post, 'title' );
215
+ $post_date = $this->get_tag( $post, 'wp:post_date' );
216
+ $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
217
+ $comment_status = $this->get_tag( $post, 'wp:comment_status' );
218
+ $ping_status = $this->get_tag( $post, 'wp:ping_status' );
219
+ $status = $this->get_tag( $post, 'wp:status' );
220
+ $post_name = $this->get_tag( $post, 'wp:post_name' );
221
+ $post_parent = $this->get_tag( $post, 'wp:post_parent' );
222
+ $menu_order = $this->get_tag( $post, 'wp:menu_order' );
223
+ $post_type = $this->get_tag( $post, 'wp:post_type' );
224
+ $post_password = $this->get_tag( $post, 'wp:post_password' );
225
+ $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
226
+ $guid = $this->get_tag( $post, 'guid' );
227
+ $post_author = $this->get_tag( $post, 'dc:creator' );
228
+
229
+ $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
230
+ $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
231
+ $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
232
+ $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
233
+
234
+ $post_content = $this->get_tag( $post, 'content:encoded' );
235
+ $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
236
+ $post_content = str_replace( '<br>', '<br />', $post_content );
237
+ $post_content = str_replace( '<hr>', '<hr />', $post_content );
238
+
239
+ $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
240
+ 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
241
+ 'menu_order', 'post_type', 'post_password', 'is_sticky'
242
+ );
243
+
244
+ $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
245
+ if ( $attachment_url )
246
+ $postdata['attachment_url'] = $attachment_url;
247
+
248
+ preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
249
+ foreach ( $terms as $t ) {
250
+ $post_terms[] = array(
251
+ 'slug' => $t[2],
252
+ 'domain' => $t[1],
253
+ 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
254
+ );
255
+ }
256
+ if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
257
+
258
+ preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
259
+ $comments = $comments[1];
260
+ if ( $comments ) {
261
+ foreach ( $comments as $comment ) {
262
+ $post_comments[] = array(
263
+ 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
264
+ 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
265
+ 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
266
+ 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
267
+ 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
268
+ 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
269
+ 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
270
+ 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
271
+ 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
272
+ 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
273
+ 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
274
+ 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
275
+ 'commentmeta' => $this->process_meta( $comment, 'wp:commentmeta' ),
276
+ );
277
+ }
278
+ }
279
+ if ( ! empty( $post_comments ) ) {
280
+ $postdata['comments'] = $post_comments;
281
+ }
282
+
283
+ $post_meta = $this->process_meta( $post, 'wp:postmeta' );
284
+ if ( ! empty( $post_meta ) ) {
285
+ $postdata['postmeta'] = $post_meta;
286
+ }
287
+
288
+ return $postdata;
289
+ }
290
+
291
+ function _normalize_tag( $matches ) {
292
+ return '<' . strtolower( $matches[1] );
293
+ }
294
+
295
+ function fopen( $filename, $mode = 'r' ) {
296
+ if ( $this->has_gzip )
297
+ return gzopen( $filename, $mode );
298
+ return fopen( $filename, $mode );
299
+ }
300
+
301
+ function feof( $fp ) {
302
+ if ( $this->has_gzip )
303
+ return gzeof( $fp );
304
+ return feof( $fp );
305
+ }
306
+
307
+ function fgets( $fp, $len = 8192 ) {
308
+ if ( $this->has_gzip )
309
+ return gzgets( $fp, $len );
310
+ return fgets( $fp, $len );
311
+ }
312
+
313
+ function fclose( $fp ) {
314
+ if ( $this->has_gzip )
315
+ return gzclose( $fp );
316
+ return fclose( $fp );
317
+ }
318
+ }
parsers/class-wxr-parser-simplexml.php ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress eXtended RSS file parser implementations
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WXR Parser that makes use of the SimpleXML PHP extension.
11
+ */
12
+ class WXR_Parser_SimpleXML {
13
+ function parse( $file ) {
14
+ $authors = $posts = $categories = $tags = $terms = array();
15
+
16
+ $internal_errors = libxml_use_internal_errors(true);
17
+
18
+ $dom = new DOMDocument;
19
+ $old_value = null;
20
+ if ( function_exists( 'libxml_disable_entity_loader' ) ) {
21
+ $old_value = libxml_disable_entity_loader( true );
22
+ }
23
+ $success = $dom->loadXML( file_get_contents( $file ) );
24
+ if ( ! is_null( $old_value ) ) {
25
+ libxml_disable_entity_loader( $old_value );
26
+ }
27
+
28
+ if ( ! $success || isset( $dom->doctype ) ) {
29
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
30
+ }
31
+
32
+ $xml = simplexml_import_dom( $dom );
33
+ unset( $dom );
34
+
35
+ // halt if loading produces an error
36
+ if ( ! $xml )
37
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
38
+
39
+ $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
40
+ if ( ! $wxr_version )
41
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
42
+
43
+ $wxr_version = (string) trim( $wxr_version[0] );
44
+ // confirm that we are dealing with the correct file format
45
+ if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
46
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
47
+
48
+ $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
49
+ $base_url = (string) trim( isset( $base_url[0] ) ? $base_url[0] : '' );
50
+
51
+
52
+ $base_blog_url = $xml->xpath('/rss/channel/wp:base_blog_url');
53
+ if ( $base_blog_url ) {
54
+ $base_blog_url = (string) trim( $base_blog_url[0] );
55
+ } else {
56
+ $base_blog_url = $base_url;
57
+ }
58
+
59
+ $namespaces = $xml->getDocNamespaces();
60
+ if ( ! isset( $namespaces['wp'] ) )
61
+ $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
62
+ if ( ! isset( $namespaces['excerpt'] ) )
63
+ $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
64
+
65
+ // grab authors
66
+ foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
67
+ $a = $author_arr->children( $namespaces['wp'] );
68
+ $login = (string) $a->author_login;
69
+ $authors[$login] = array(
70
+ 'author_id' => (int) $a->author_id,
71
+ 'author_login' => $login,
72
+ 'author_email' => (string) $a->author_email,
73
+ 'author_display_name' => (string) $a->author_display_name,
74
+ 'author_first_name' => (string) $a->author_first_name,
75
+ 'author_last_name' => (string) $a->author_last_name
76
+ );
77
+ }
78
+
79
+ // grab cats, tags and terms
80
+ foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
81
+ $t = $term_arr->children( $namespaces['wp'] );
82
+ $category = array(
83
+ 'term_id' => (int) $t->term_id,
84
+ 'category_nicename' => (string) $t->category_nicename,
85
+ 'category_parent' => (string) $t->category_parent,
86
+ 'cat_name' => (string) $t->cat_name,
87
+ 'category_description' => (string) $t->category_description
88
+ );
89
+
90
+ foreach ( $t->termmeta as $meta ) {
91
+ $category['termmeta'][] = array(
92
+ 'key' => (string) $meta->meta_key,
93
+ 'value' => (string) $meta->meta_value
94
+ );
95
+ }
96
+
97
+ $categories[] = $category;
98
+ }
99
+
100
+ foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
101
+ $t = $term_arr->children( $namespaces['wp'] );
102
+ $tag = array(
103
+ 'term_id' => (int) $t->term_id,
104
+ 'tag_slug' => (string) $t->tag_slug,
105
+ 'tag_name' => (string) $t->tag_name,
106
+ 'tag_description' => (string) $t->tag_description
107
+ );
108
+
109
+ foreach ( $t->termmeta as $meta ) {
110
+ $tag['termmeta'][] = array(
111
+ 'key' => (string) $meta->meta_key,
112
+ 'value' => (string) $meta->meta_value
113
+ );
114
+ }
115
+
116
+ $tags[] = $tag;
117
+ }
118
+
119
+ foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
120
+ $t = $term_arr->children( $namespaces['wp'] );
121
+ $term = array(
122
+ 'term_id' => (int) $t->term_id,
123
+ 'term_taxonomy' => (string) $t->term_taxonomy,
124
+ 'slug' => (string) $t->term_slug,
125
+ 'term_parent' => (string) $t->term_parent,
126
+ 'term_name' => (string) $t->term_name,
127
+ 'term_description' => (string) $t->term_description
128
+ );
129
+
130
+ foreach ( $t->termmeta as $meta ) {
131
+ $term['termmeta'][] = array(
132
+ 'key' => (string) $meta->meta_key,
133
+ 'value' => (string) $meta->meta_value
134
+ );
135
+ }
136
+
137
+ $terms[] = $term;
138
+ }
139
+
140
+ // grab posts
141
+ foreach ( $xml->channel->item as $item ) {
142
+ $post = array(
143
+ 'post_title' => (string) $item->title,
144
+ 'guid' => (string) $item->guid,
145
+ );
146
+
147
+ $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
148
+ $post['post_author'] = (string) $dc->creator;
149
+
150
+ $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
151
+ $excerpt = $item->children( $namespaces['excerpt'] );
152
+ $post['post_content'] = (string) $content->encoded;
153
+ $post['post_excerpt'] = (string) $excerpt->encoded;
154
+
155
+ $wp = $item->children( $namespaces['wp'] );
156
+ $post['post_id'] = (int) $wp->post_id;
157
+ $post['post_date'] = (string) $wp->post_date;
158
+ $post['post_date_gmt'] = (string) $wp->post_date_gmt;
159
+ $post['comment_status'] = (string) $wp->comment_status;
160
+ $post['ping_status'] = (string) $wp->ping_status;
161
+ $post['post_name'] = (string) $wp->post_name;
162
+ $post['status'] = (string) $wp->status;
163
+ $post['post_parent'] = (int) $wp->post_parent;
164
+ $post['menu_order'] = (int) $wp->menu_order;
165
+ $post['post_type'] = (string) $wp->post_type;
166
+ $post['post_password'] = (string) $wp->post_password;
167
+ $post['is_sticky'] = (int) $wp->is_sticky;
168
+
169
+ if ( isset($wp->attachment_url) )
170
+ $post['attachment_url'] = (string) $wp->attachment_url;
171
+
172
+ foreach ( $item->category as $c ) {
173
+ $att = $c->attributes();
174
+ if ( isset( $att['nicename'] ) )
175
+ $post['terms'][] = array(
176
+ 'name' => (string) $c,
177
+ 'slug' => (string) $att['nicename'],
178
+ 'domain' => (string) $att['domain']
179
+ );
180
+ }
181
+
182
+ foreach ( $wp->postmeta as $meta ) {
183
+ $post['postmeta'][] = array(
184
+ 'key' => (string) $meta->meta_key,
185
+ 'value' => (string) $meta->meta_value
186
+ );
187
+ }
188
+
189
+ foreach ( $wp->comment as $comment ) {
190
+ $meta = array();
191
+ if ( isset( $comment->commentmeta ) ) {
192
+ foreach ( $comment->commentmeta as $m ) {
193
+ $meta[] = array(
194
+ 'key' => (string) $m->meta_key,
195
+ 'value' => (string) $m->meta_value
196
+ );
197
+ }
198
+ }
199
+
200
+ $post['comments'][] = array(
201
+ 'comment_id' => (int) $comment->comment_id,
202
+ 'comment_author' => (string) $comment->comment_author,
203
+ 'comment_author_email' => (string) $comment->comment_author_email,
204
+ 'comment_author_IP' => (string) $comment->comment_author_IP,
205
+ 'comment_author_url' => (string) $comment->comment_author_url,
206
+ 'comment_date' => (string) $comment->comment_date,
207
+ 'comment_date_gmt' => (string) $comment->comment_date_gmt,
208
+ 'comment_content' => (string) $comment->comment_content,
209
+ 'comment_approved' => (string) $comment->comment_approved,
210
+ 'comment_type' => (string) $comment->comment_type,
211
+ 'comment_parent' => (string) $comment->comment_parent,
212
+ 'comment_user_id' => (int) $comment->comment_user_id,
213
+ 'commentmeta' => $meta,
214
+ );
215
+ }
216
+
217
+ $posts[] = $post;
218
+ }
219
+
220
+ return array(
221
+ 'authors' => $authors,
222
+ 'posts' => $posts,
223
+ 'categories' => $categories,
224
+ 'tags' => $tags,
225
+ 'terms' => $terms,
226
+ 'base_url' => $base_url,
227
+ 'base_blog_url' => $base_blog_url,
228
+ 'version' => $wxr_version
229
+ );
230
+ }
231
+ }
parsers/class-wxr-parser-xml.php ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress eXtended RSS file parser implementations
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WXR Parser that makes use of the XML Parser PHP extension.
11
+ */
12
+ class WXR_Parser_XML {
13
+ var $wp_tags = array(
14
+ 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url',
15
+ 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password',
16
+ 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description',
17
+ 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent',
18
+ 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name',
19
+ 'wp:author_first_name', 'wp:author_last_name',
20
+ );
21
+ var $wp_sub_tags = array(
22
+ 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url',
23
+ 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content',
24
+ 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id',
25
+ );
26
+
27
+ function parse( $file ) {
28
+ $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
29
+ $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
30
+
31
+ $xml = xml_parser_create( 'UTF-8' );
32
+ xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 );
33
+ xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 );
34
+ xml_set_object( $xml, $this );
35
+ xml_set_character_data_handler( $xml, 'cdata' );
36
+ xml_set_element_handler( $xml, 'tag_open', 'tag_close' );
37
+
38
+ if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) {
39
+ $current_line = xml_get_current_line_number( $xml );
40
+ $current_column = xml_get_current_column_number( $xml );
41
+ $error_code = xml_get_error_code( $xml );
42
+ $error_string = xml_error_string( $error_code );
43
+ return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) );
44
+ }
45
+ xml_parser_free( $xml );
46
+
47
+ if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) )
48
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
49
+
50
+ return array(
51
+ 'authors' => $this->authors,
52
+ 'posts' => $this->posts,
53
+ 'categories' => $this->category,
54
+ 'tags' => $this->tag,
55
+ 'terms' => $this->term,
56
+ 'base_url' => $this->base_url,
57
+ 'base_blog_url' => $this->base_blog_url,
58
+ 'version' => $this->wxr_version
59
+ );
60
+ }
61
+
62
+ function tag_open( $parse, $tag, $attr ) {
63
+ if ( in_array( $tag, $this->wp_tags ) ) {
64
+ $this->in_tag = substr( $tag, 3 );
65
+ return;
66
+ }
67
+
68
+ if ( in_array( $tag, $this->wp_sub_tags ) ) {
69
+ $this->in_sub_tag = substr( $tag, 3 );
70
+ return;
71
+ }
72
+
73
+ switch ( $tag ) {
74
+ case 'category':
75
+ if ( isset($attr['domain'], $attr['nicename']) ) {
76
+ $this->sub_data['domain'] = $attr['domain'];
77
+ $this->sub_data['slug'] = $attr['nicename'];
78
+ }
79
+ break;
80
+ case 'item': $this->in_post = true;
81
+ case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break;
82
+ case 'guid': $this->in_tag = 'guid'; break;
83
+ case 'dc:creator': $this->in_tag = 'post_author'; break;
84
+ case 'content:encoded': $this->in_tag = 'post_content'; break;
85
+ case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break;
86
+
87
+ case 'wp:term_slug': $this->in_tag = 'slug'; break;
88
+ case 'wp:meta_key': $this->in_sub_tag = 'key'; break;
89
+ case 'wp:meta_value': $this->in_sub_tag = 'value'; break;
90
+ }
91
+ }
92
+
93
+ function cdata( $parser, $cdata ) {
94
+ if ( ! trim( $cdata ) )
95
+ return;
96
+
97
+ if ( false !== $this->in_tag || false !== $this->in_sub_tag ) {
98
+ $this->cdata .= $cdata;
99
+ } else {
100
+ $this->cdata .= trim( $cdata );
101
+ }
102
+ }
103
+
104
+ function tag_close( $parser, $tag ) {
105
+ switch ( $tag ) {
106
+ case 'wp:comment':
107
+ unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data
108
+ if ( ! empty( $this->sub_data ) )
109
+ $this->data['comments'][] = $this->sub_data;
110
+ $this->sub_data = false;
111
+ break;
112
+ case 'wp:commentmeta':
113
+ $this->sub_data['commentmeta'][] = array(
114
+ 'key' => $this->sub_data['key'],
115
+ 'value' => $this->sub_data['value']
116
+ );
117
+ break;
118
+ case 'category':
119
+ if ( ! empty( $this->sub_data ) ) {
120
+ $this->sub_data['name'] = $this->cdata;
121
+ $this->data['terms'][] = $this->sub_data;
122
+ }
123
+ $this->sub_data = false;
124
+ break;
125
+ case 'wp:postmeta':
126
+ if ( ! empty( $this->sub_data ) )
127
+ $this->data['postmeta'][] = $this->sub_data;
128
+ $this->sub_data = false;
129
+ break;
130
+ case 'item':
131
+ $this->posts[] = $this->data;
132
+ $this->data = false;
133
+ break;
134
+ case 'wp:category':
135
+ case 'wp:tag':
136
+ case 'wp:term':
137
+ $n = substr( $tag, 3 );
138
+ array_push( $this->$n, $this->data );
139
+ $this->data = false;
140
+ break;
141
+ case 'wp:termmeta':
142
+ if ( ! empty( $this->sub_data ) ) {
143
+ $this->data['termmeta'][] = $this->sub_data;
144
+ }
145
+ $this->sub_data = false;
146
+ break;
147
+ case 'wp:author':
148
+ if ( ! empty($this->data['author_login']) )
149
+ $this->authors[$this->data['author_login']] = $this->data;
150
+ $this->data = false;
151
+ break;
152
+ case 'wp:base_site_url':
153
+ $this->base_url = $this->cdata;
154
+ if ( ! isset( $this->base_blog_url ) ) {
155
+ $this->base_blog_url = $this->cdata;
156
+ }
157
+ break;
158
+ case 'wp:base_blog_url':
159
+ $this->base_blog_url = $this->cdata;
160
+ break;
161
+ case 'wp:wxr_version':
162
+ $this->wxr_version = $this->cdata;
163
+ break;
164
+
165
+ default:
166
+ if ( $this->in_sub_tag ) {
167
+ $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
168
+ $this->in_sub_tag = false;
169
+ } else if ( $this->in_tag ) {
170
+ $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
171
+ $this->in_tag = false;
172
+ }
173
+ }
174
+
175
+ $this->cdata = false;
176
+ }
177
+ }
parsers/class-wxr-parser.php ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WordPress eXtended RSS file parser implementations
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Importer
7
+ */
8
+
9
+ /**
10
+ * WordPress Importer class for managing parsing of WXR files.
11
+ */
12
+ class WXR_Parser {
13
+ function parse( $file ) {
14
+ // Attempt to use proper XML parsers first
15
+ if ( extension_loaded( 'simplexml' ) ) {
16
+ $parser = new WXR_Parser_SimpleXML;
17
+ $result = $parser->parse( $file );
18
+
19
+ // If SimpleXML succeeds or this is an invalid WXR file then return the results
20
+ if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() )
21
+ return $result;
22
+ } else if ( extension_loaded( 'xml' ) ) {
23
+ $parser = new WXR_Parser_XML;
24
+ $result = $parser->parse( $file );
25
+
26
+ // If XMLParser succeeds or this is an invalid WXR file then return the results
27
+ if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() )
28
+ return $result;
29
+ }
30
+
31
+ // We have a malformed XML file, so display the error and fallthrough to regex
32
+ if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) {
33
+ echo '<pre>';
34
+ if ( 'SimpleXML_parse_error' == $result->get_error_code() ) {
35
+ foreach ( $result->get_error_data() as $error )
36
+ echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n";
37
+ } else if ( 'XML_parse_error' == $result->get_error_code() ) {
38
+ $error = $result->get_error_data();
39
+ echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] );
40
+ }
41
+ echo '</pre>';
42
+ echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />';
43
+ echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>';
44
+ }
45
+
46
+ // use regular expressions if nothing else available or this is bad XML
47
+ $parser = new WXR_Parser_Regex;
48
+ return $parser->parse( $file );
49
+ }
50
+ }
readme.txt CHANGED
@@ -1,122 +1,118 @@
1
- === WordPress Importer ===
2
- Contributors: wordpressdotorg
3
- Donate link: https://wordpressfoundation.org/donate/
4
- Tags: importer, wordpress
5
- Requires at least: 3.6
6
- Tested up to: 4.9
7
- Stable tag: 0.6.4
8
- License: GPLv2 or later
9
- License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
-
11
- Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
12
-
13
- == Description ==
14
-
15
- The WordPress Importer will import the following content from a WordPress export file:
16
-
17
- * Posts, pages and other custom post types
18
- * Comments
19
- * Custom fields and post meta
20
- * Categories, tags and terms from custom taxonomies
21
- * Authors
22
-
23
- For further information and instructions please see the [Codex page on Importing Content](https://codex.wordpress.org/Importing_Content#WordPress)
24
-
25
- == Installation ==
26
-
27
- The quickest method for installing the importer is:
28
-
29
- 1. Visit Tools -> Import in the WordPress dashboard
30
- 1. Click on the WordPress link in the list of importers
31
- 1. Click "Install Now"
32
- 1. Finally click "Activate Plugin & Run Importer"
33
-
34
- If you would prefer to do things manually then follow these instructions:
35
-
36
- 1. Upload the `wordpress-importer` folder to the `/wp-content/plugins/` directory
37
- 1. Activate the plugin through the 'Plugins' menu in WordPress
38
- 1. Go to the Tools -> Import screen, click on WordPress
39
-
40
- == Changelog ==
41
-
42
- = 0.6.4 =
43
- * Improve PHP7 compatibility.
44
- * Fix bug that caused slashes to be stripped from imported comments.
45
- * Fix for various deprecation notices including `wp_get_http()` and `screen_icon()`.
46
- * Fix for importing export files with multiline term meta data.
47
-
48
- = 0.6.3 =
49
- * Add support for import term metadata.
50
- * Fix bug that caused slashes to be stripped from imported content.
51
- * Fix bug that caused characters to be stripped inside of CDATA in some cases.
52
- * Fix PHP notices.
53
-
54
- = 0.6.2 =
55
- * Add `wp_import_existing_post` filter, see [Trac ticket #33721](https://core.trac.wordpress.org/ticket/33721).
56
-
57
- = 0.6 =
58
- * Support for WXR 1.2 and multiple CDATA sections
59
- * Post aren't duplicates if their post_type's are different
60
-
61
- = 0.5.2 =
62
- * Double check that the uploaded export file exists before processing it. This prevents incorrect error messages when
63
- an export file is uploaded to a server with bad permissions and WordPress 3.3 or 3.3.1 is being used.
64
-
65
- = 0.5 =
66
- * Import comment meta (requires export from WordPress 3.2)
67
- * Minor bugfixes and enhancements
68
-
69
- = 0.4 =
70
- * Map comment user_id where possible
71
- * Import attachments from `wp:attachment_url`
72
- * Upload attachments to correct directory
73
- * Remap resized image URLs correctly
74
-
75
- = 0.3 =
76
- * Use an XML Parser if possible
77
- * Proper import support for nav menus
78
- * ... and much more, see [Trac ticket #15197](https://core.trac.wordpress.org/ticket/15197)
79
-
80
- = 0.1 =
81
- * Initial release
82
-
83
- == Upgrade Notice ==
84
-
85
- = 0.6 =
86
- Support for exports from WordPress 3.4.
87
-
88
- = 0.5.2 =
89
- Fix incorrect error message when the export file could not be uploaded.
90
-
91
- = 0.5 =
92
- Import comment meta and other minor bugfixes and enhancements.
93
-
94
- = 0.4 =
95
- Bug fixes for attachment importing and other small enhancements.
96
-
97
- = 0.3 =
98
- Upgrade for a more robust and reliable experience when importing WordPress export files, and for compatibility with WordPress 3.1.
99
-
100
- == Frequently Asked Questions ==
101
-
102
- = Help! I'm getting out of memory errors or a blank screen. =
103
- If your exported file is very large, the import script may run into your host's configured memory limit for PHP.
104
-
105
- A message like "Fatal error: Allowed memory size of 8388608 bytes exhausted" indicates that the script can't successfully import your XML file under the current PHP memory limit. If you have access to the php.ini file, you can manually increase the limit; if you do not (your WordPress installation is hosted on a shared server, for instance), you might have to break your exported XML file into several smaller pieces and run the import script one at a time.
106
-
107
- For those with shared hosting, the best alternative may be to consult hosting support to determine the safest approach for running the import. A host may be willing to temporarily lift the memory limit and/or run the process directly from their end.
108
-
109
- -- [WordPress Codex: Importing Content](https://codex.wordpress.org/Importing_Content#Before_Importing)
110
-
111
- == Filters ==
112
-
113
- The importer has a couple of filters to allow you to completely enable/block certain features:
114
-
115
- * `import_allow_create_users`: return false if you only want to allow mapping to existing users
116
- * `import_allow_fetch_attachments`: return false if you do not wish to allow importing and downloading of attachments
117
- * `import_attachment_size_limit`: return an integer value for the maximum file size in bytes to save (default is 0, which is unlimited)
118
-
119
- There are also a few actions available to hook into:
120
-
121
- * `import_start`: occurs after the export file has been uploaded and author import settings have been chosen
122
- * `import_end`: called after the last output from the importer
1
+ === WordPress Importer ===
2
+ Contributors: wordpressdotorg
3
+ Donate link: https://wordpressfoundation.org/donate/
4
+ Tags: importer, wordpress
5
+ Requires at least: 3.7
6
+ Tested up to: 5.4
7
+ Stable tag: 0.7
8
+ License: GPLv2 or later
9
+ License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
12
+
13
+ == Description ==
14
+
15
+ The WordPress Importer will import the following content from a WordPress export file:
16
+
17
+ * Posts, pages and other custom post types
18
+ * Comments and comment meta
19
+ * Custom fields and post meta
20
+ * Categories, tags and terms from custom taxonomies and term meta
21
+ * Authors
22
+
23
+ For further information and instructions please see the [documention on Importing Content](https://wordpress.org/support/article/importing-content/#wordpress).
24
+
25
+ == Installation ==
26
+
27
+ The quickest method for installing the importer is:
28
+
29
+ 1. Visit Tools -> Import in the WordPress dashboard
30
+ 1. Click on the WordPress link in the list of importers
31
+ 1. Click "Install Now"
32
+ 1. Finally click "Activate Plugin & Run Importer"
33
+
34
+ If you would prefer to do things manually then follow these instructions:
35
+
36
+ 1. Upload the `wordpress-importer` folder to the `/wp-content/plugins/` directory
37
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
38
+ 1. Go to the Tools -> Import screen, click on WordPress
39
+
40
+ == Changelog ==
41
+
42
+ = 0.7 =
43
+ * Update minimum WordPress requirement to 3.7 and ensure compatibility with PHP 7.4.
44
+ * Fix bug that caused not importing term meta.
45
+ * Fix bug that caused slashes to be stripped from imported meta data.
46
+ * Fix bug that prevented import of serialized meta data.
47
+ * Fix file size check after download of remote files with HTTP compression enabled.
48
+ * Improve accessibility of form fields by adding missing labels.
49
+ * Improve imports for remote file URLs without name and/or extension.
50
+ * Add support for `wp:base_blog_url` field to allow importing multiple files with WP-CLI.
51
+ * Add support for term meta parsing when using the regular expressions or XML parser.
52
+ * Developers: All PHP classes have been moved into their own files.
53
+ * Developers: Allow to change `IMPORT_DEBUG` via `wp-config.php` and change default value to the value of `WP_DEBUG`.
54
+
55
+ = 0.6.4 =
56
+ * Improve PHP7 compatibility.
57
+ * Fix bug that caused slashes to be stripped from imported comments.
58
+ * Fix for various deprecation notices including `wp_get_http()` and `screen_icon()`.
59
+ * Fix for importing export files with multiline term meta data.
60
+
61
+ = 0.6.3 =
62
+ * Add support for import term metadata.
63
+ * Fix bug that caused slashes to be stripped from imported content.
64
+ * Fix bug that caused characters to be stripped inside of CDATA in some cases.
65
+ * Fix PHP notices.
66
+
67
+ = 0.6.2 =
68
+ * Add `wp_import_existing_post` filter, see [Trac ticket #33721](https://core.trac.wordpress.org/ticket/33721).
69
+
70
+ = 0.6 =
71
+ * Support for WXR 1.2 and multiple CDATA sections
72
+ * Post aren't duplicates if their post_type's are different
73
+
74
+ = 0.5.2 =
75
+ * Double check that the uploaded export file exists before processing it. This prevents incorrect error messages when
76
+ an export file is uploaded to a server with bad permissions and WordPress 3.3 or 3.3.1 is being used.
77
+
78
+ = 0.5 =
79
+ * Import comment meta (requires export from WordPress 3.2)
80
+ * Minor bugfixes and enhancements
81
+
82
+ = 0.4 =
83
+ * Map comment user_id where possible
84
+ * Import attachments from `wp:attachment_url`
85
+ * Upload attachments to correct directory
86
+ * Remap resized image URLs correctly
87
+
88
+ = 0.3 =
89
+ * Use an XML Parser if possible
90
+ * Proper import support for nav menus
91
+ * ... and much more, see [Trac ticket #15197](https://core.trac.wordpress.org/ticket/15197)
92
+
93
+ = 0.1 =
94
+ * Initial release
95
+
96
+ == Frequently Asked Questions ==
97
+
98
+ = Help! I'm getting out of memory errors or a blank screen. =
99
+ If your exported file is very large, the import script may run into your host's configured memory limit for PHP.
100
+
101
+ A message like "Fatal error: Allowed memory size of 8388608 bytes exhausted" indicates that the script can't successfully import your XML file under the current PHP memory limit. If you have access to the php.ini file, you can manually increase the limit; if you do not (your WordPress installation is hosted on a shared server, for instance), you might have to break your exported XML file into several smaller pieces and run the import script one at a time.
102
+
103
+ For those with shared hosting, the best alternative may be to consult hosting support to determine the safest approach for running the import. A host may be willing to temporarily lift the memory limit and/or run the process directly from their end.
104
+
105
+ -- [Support Article: Importing Content](https://wordpress.org/support/article/importing-content/#before-importing)
106
+
107
+ == Filters ==
108
+
109
+ The importer has a couple of filters to allow you to completely enable/block certain features:
110
+
111
+ * `import_allow_create_users`: return false if you only want to allow mapping to existing users
112
+ * `import_allow_fetch_attachments`: return false if you do not wish to allow importing and downloading of attachments
113
+ * `import_attachment_size_limit`: return an integer value for the maximum file size in bytes to save (default is 0, which is unlimited)
114
+
115
+ There are also a few actions available to hook into:
116
+
117
+ * `import_start`: occurs after the export file has been uploaded and author import settings have been chosen
118
+ * `import_end`: called after the last output from the importer
 
 
 
 
wordpress-importer.php CHANGED
@@ -5,18 +5,22 @@ Plugin URI: https://wordpress.org/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: https://wordpress.org/
8
- Version: 0.6.4
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' ) ) {
@@ -25,1195 +29,23 @@ if ( ! class_exists( '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&amp;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&#8217;s role will be set as %s. Manually changing the new user&#8217;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
- $catarr = wp_slash( $catarr );
418
-
419
- $id = wp_insert_category( $catarr );
420
- if ( ! is_wp_error( $id ) ) {
421
- if ( isset($cat['term_id']) )
422
- $this->processed_terms[intval($cat['term_id'])] = $id;
423
- } else {
424
- printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
425
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
426
- echo ': ' . $id->get_error_message();
427
- echo '<br />';
428
- continue;
429
- }
430
-
431
- $this->process_termmeta( $cat, $id['term_id'] );
432
- }
433
-
434
- unset( $this->categories );
435
- }
436
-
437
- /**
438
- * Create new post tags based on import information
439
- *
440
- * Doesn't create a tag if its slug already exists
441
- */
442
- function process_tags() {
443
- $this->tags = apply_filters( 'wp_import_tags', $this->tags );
444
-
445
- if ( empty( $this->tags ) )
446
- return;
447
-
448
- foreach ( $this->tags as $tag ) {
449
- // if the tag already exists leave it alone
450
- $term_id = term_exists( $tag['tag_slug'], 'post_tag' );
451
- if ( $term_id ) {
452
- if ( is_array($term_id) ) $term_id = $term_id['term_id'];
453
- if ( isset($tag['term_id']) )
454
- $this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
455
- continue;
456
- }
457
-
458
- $tag = wp_slash( $tag );
459
- $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
460
- $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
461
-
462
- $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
463
- if ( ! is_wp_error( $id ) ) {
464
- if ( isset($tag['term_id']) )
465
- $this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
466
- } else {
467
- printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
468
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
469
- echo ': ' . $id->get_error_message();
470
- echo '<br />';
471
- continue;
472
- }
473
-
474
- $this->process_termmeta( $tag, $id['term_id'] );
475
- }
476
-
477
- unset( $this->tags );
478
- }
479
-
480
- /**
481
- * Create new terms based on import information
482
- *
483
- * Doesn't create a term its slug already exists
484
- */
485
- function process_terms() {
486
- $this->terms = apply_filters( 'wp_import_terms', $this->terms );
487
-
488
- if ( empty( $this->terms ) )
489
- return;
490
-
491
- foreach ( $this->terms as $term ) {
492
- // if the term already exists in the correct taxonomy leave it alone
493
- $term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
494
- if ( $term_id ) {
495
- if ( is_array($term_id) ) $term_id = $term_id['term_id'];
496
- if ( isset($term['term_id']) )
497
- $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
498
- continue;
499
- }
500
-
501
- if ( empty( $term['term_parent'] ) ) {
502
- $parent = 0;
503
- } else {
504
- $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
505
- if ( is_array( $parent ) ) $parent = $parent['term_id'];
506
- }
507
- $term = wp_slash( $term );
508
- $description = isset( $term['term_description'] ) ? $term['term_description'] : '';
509
- $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
510
-
511
- $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
512
- if ( ! is_wp_error( $id ) ) {
513
- if ( isset($term['term_id']) )
514
- $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
515
- } else {
516
- printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
517
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
518
- echo ': ' . $id->get_error_message();
519
- echo '<br />';
520
- continue;
521
- }
522
-
523
- $this->process_termmeta( $term, $id['term_id'] );
524
- }
525
-
526
- unset( $this->terms );
527
- }
528
-
529
- /**
530
- * Add metadata to imported term.
531
- *
532
- * @since 0.6.2
533
- *
534
- * @param array $term Term data from WXR import.
535
- * @param int $term_id ID of the newly created term.
536
- */
537
- protected function process_termmeta( $term, $term_id ) {
538
- if ( ! isset( $term['termmeta'] ) ) {
539
- $term['termmeta'] = array();
540
- }
541
-
542
- /**
543
- * Filters the metadata attached to an imported term.
544
- *
545
- * @since 0.6.2
546
- *
547
- * @param array $termmeta Array of term meta.
548
- * @param int $term_id ID of the newly created term.
549
- * @param array $term Term data from the WXR import.
550
- */
551
- $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
552
-
553
- if ( empty( $term['termmeta'] ) ) {
554
- return;
555
- }
556
-
557
- foreach ( $term['termmeta'] as $meta ) {
558
- /**
559
- * Filters the meta key for an imported piece of term meta.
560
- *
561
- * @since 0.6.2
562
- *
563
- * @param string $meta_key Meta key.
564
- * @param int $term_id ID of the newly created term.
565
- * @param array $term Term data from the WXR import.
566
- */
567
- $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
568
- if ( ! $key ) {
569
- continue;
570
- }
571
-
572
- // Export gets meta straight from the DB so could have a serialized string
573
- $value = maybe_unserialize( $meta['value'] );
574
-
575
- add_term_meta( $term_id, $key, $value );
576
-
577
- /**
578
- * Fires after term meta is imported.
579
- *
580
- * @since 0.6.2
581
- *
582
- * @param int $term_id ID of the newly created term.
583
- * @param string $key Meta key.
584
- * @param mixed $value Meta value.
585
- */
586
- do_action( 'import_term_meta', $term_id, $key, $value );
587
- }
588
- }
589
-
590
- /**
591
- * Create new posts based on import information
592
- *
593
- * Posts marked as having a parent which doesn't exist will become top level items.
594
- * Doesn't create a new post if: the post type doesn't exist, the given post ID
595
- * is already noted as imported or a post with the same title and date already exists.
596
- * Note that new/updated terms, comments and meta are imported for the last of the above.
597
- */
598
- function process_posts() {
599
- $this->posts = apply_filters( 'wp_import_posts', $this->posts );
600
-
601
- foreach ( $this->posts as $post ) {
602
- $post = apply_filters( 'wp_import_post_data_raw', $post );
603
-
604
- if ( ! post_type_exists( $post['post_type'] ) ) {
605
- printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
606
- esc_html($post['post_title']), esc_html($post['post_type']) );
607
- echo '<br />';
608
- do_action( 'wp_import_post_exists', $post );
609
- continue;
610
- }
611
 
612
- if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
613
- continue;
614
 
615
- if ( $post['status'] == 'auto-draft' )
616
- continue;
617
 
618
- if ( 'nav_menu_item' == $post['post_type'] ) {
619
- $this->process_menu_item( $post );
620
- continue;
621
- }
622
 
623
- $post_type_object = get_post_type_object( $post['post_type'] );
624
-
625
- $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
626
-
627
- /**
628
- * Filter ID of the existing post corresponding to post currently importing.
629
- *
630
- * Return 0 to force the post to be imported. Filter the ID to be something else
631
- * to override which existing post is mapped to the imported post.
632
- *
633
- * @see post_exists()
634
- * @since 0.6.2
635
- *
636
- * @param int $post_exists Post ID, or 0 if post did not exist.
637
- * @param array $post The post array to be inserted.
638
- */
639
- $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
640
-
641
- if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
642
- printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
643
- echo '<br />';
644
- $comment_post_ID = $post_id = $post_exists;
645
- $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
646
- } else {
647
- $post_parent = (int) $post['post_parent'];
648
- if ( $post_parent ) {
649
- // if we already know the parent, map it to the new local ID
650
- if ( isset( $this->processed_posts[$post_parent] ) ) {
651
- $post_parent = $this->processed_posts[$post_parent];
652
- // otherwise record the parent for later
653
- } else {
654
- $this->post_orphans[intval($post['post_id'])] = $post_parent;
655
- $post_parent = 0;
656
- }
657
- }
658
-
659
- // map the post author
660
- $author = sanitize_user( $post['post_author'], true );
661
- if ( isset( $this->author_mapping[$author] ) )
662
- $author = $this->author_mapping[$author];
663
- else
664
- $author = (int) get_current_user_id();
665
-
666
- $postdata = array(
667
- 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
668
- 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
669
- 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
670
- 'post_status' => $post['status'], 'post_name' => $post['post_name'],
671
- 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
672
- 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
673
- 'post_type' => $post['post_type'], 'post_password' => $post['post_password']
674
- );
675
-
676
- $original_post_ID = $post['post_id'];
677
- $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
678
-
679
- $postdata = wp_slash( $postdata );
680
-
681
- if ( 'attachment' == $postdata['post_type'] ) {
682
- $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
683
-
684
- // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
685
- // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
686
- $postdata['upload_date'] = $post['post_date'];
687
- if ( isset( $post['postmeta'] ) ) {
688
- foreach( $post['postmeta'] as $meta ) {
689
- if ( $meta['key'] == '_wp_attached_file' ) {
690
- if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
691
- $postdata['upload_date'] = $matches[0];
692
- break;
693
- }
694
- }
695
- }
696
-
697
- $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
698
- } else {
699
- $comment_post_ID = $post_id = wp_insert_post( $postdata, true );
700
- do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
701
- }
702
-
703
- if ( is_wp_error( $post_id ) ) {
704
- printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
705
- $post_type_object->labels->singular_name, esc_html($post['post_title']) );
706
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
707
- echo ': ' . $post_id->get_error_message();
708
- echo '<br />';
709
- continue;
710
- }
711
-
712
- if ( $post['is_sticky'] == 1 )
713
- stick_post( $post_id );
714
- }
715
-
716
- // map pre-import ID to local ID
717
- $this->processed_posts[intval($post['post_id'])] = (int) $post_id;
718
-
719
- if ( ! isset( $post['terms'] ) )
720
- $post['terms'] = array();
721
-
722
- $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
723
-
724
- // add categories, tags and other terms
725
- if ( ! empty( $post['terms'] ) ) {
726
- $terms_to_set = array();
727
- foreach ( $post['terms'] as $term ) {
728
- // back compat with WXR 1.0 map 'tag' to 'post_tag'
729
- $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
730
- $term_exists = term_exists( $term['slug'], $taxonomy );
731
- $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
732
- if ( ! $term_id ) {
733
- $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
734
- if ( ! is_wp_error( $t ) ) {
735
- $term_id = $t['term_id'];
736
- do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
737
- } else {
738
- printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
739
- if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
740
- echo ': ' . $t->get_error_message();
741
- echo '<br />';
742
- do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
743
- continue;
744
- }
745
- }
746
- $terms_to_set[$taxonomy][] = intval( $term_id );
747
- }
748
-
749
- foreach ( $terms_to_set as $tax => $ids ) {
750
- $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
751
- do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
752
- }
753
- unset( $post['terms'], $terms_to_set );
754
- }
755
-
756
- if ( ! isset( $post['comments'] ) )
757
- $post['comments'] = array();
758
-
759
- $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
760
-
761
- // add/update comments
762
- if ( ! empty( $post['comments'] ) ) {
763
- $num_comments = 0;
764
- $inserted_comments = array();
765
- foreach ( $post['comments'] as $comment ) {
766
- $comment_id = $comment['comment_id'];
767
- $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
768
- $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
769
- $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
770
- $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
771
- $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
772
- $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
773
- $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
774
- $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
775
- $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
776
- $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
777
- $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
778
- $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
779
- if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
780
- $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
781
- }
782
- ksort( $newcomments );
783
-
784
- foreach ( $newcomments as $key => $comment ) {
785
- // if this is a new post we can skip the comment_exists() check
786
- if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
787
- if ( isset( $inserted_comments[$comment['comment_parent']] ) )
788
- $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
789
- $comment = wp_slash( $comment );
790
- $comment = wp_filter_comment( $comment );
791
- $inserted_comments[$key] = wp_insert_comment( $comment );
792
- do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
793
-
794
- foreach( $comment['commentmeta'] as $meta ) {
795
- $value = maybe_unserialize( $meta['value'] );
796
- add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
797
- }
798
-
799
- $num_comments++;
800
- }
801
- }
802
- unset( $newcomments, $inserted_comments, $post['comments'] );
803
- }
804
-
805
- if ( ! isset( $post['postmeta'] ) )
806
- $post['postmeta'] = array();
807
-
808
- $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
809
-
810
- // add/update post meta
811
- if ( ! empty( $post['postmeta'] ) ) {
812
- foreach ( $post['postmeta'] as $meta ) {
813
- $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
814
- $value = false;
815
-
816
- if ( '_edit_last' == $key ) {
817
- if ( isset( $this->processed_authors[intval($meta['value'])] ) )
818
- $value = $this->processed_authors[intval($meta['value'])];
819
- else
820
- $key = false;
821
- }
822
-
823
- if ( $key ) {
824
- // export gets meta straight from the DB so could have a serialized string
825
- if ( ! $value )
826
- $value = maybe_unserialize( $meta['value'] );
827
-
828
- add_post_meta( $post_id, $key, $value );
829
- do_action( 'import_post_meta', $post_id, $key, $value );
830
-
831
- // if the post has a featured image, take note of this in case of remap
832
- if ( '_thumbnail_id' == $key )
833
- $this->featured_images[$post_id] = (int) $value;
834
- }
835
- }
836
- }
837
- }
838
-
839
- unset( $this->posts );
840
- }
841
-
842
- /**
843
- * Attempt to create a new menu item from import data
844
- *
845
- * Fails for draft, orphaned menu items and those without an associated nav_menu
846
- * or an invalid nav_menu term. If the post type or term object which the menu item
847
- * represents doesn't exist then the menu item will not be imported (waits until the
848
- * end of the import to retry again before discarding).
849
- *
850
- * @param array $item Menu item details from WXR file
851
- */
852
- function process_menu_item( $item ) {
853
- // skip draft, orphaned menu items
854
- if ( 'draft' == $item['status'] )
855
- return;
856
-
857
- $menu_slug = false;
858
- if ( isset($item['terms']) ) {
859
- // loop through terms, assume first nav_menu term is correct menu
860
- foreach ( $item['terms'] as $term ) {
861
- if ( 'nav_menu' == $term['domain'] ) {
862
- $menu_slug = $term['slug'];
863
- break;
864
- }
865
- }
866
- }
867
-
868
- // no nav_menu term associated with this menu item
869
- if ( ! $menu_slug ) {
870
- _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
871
- echo '<br />';
872
- return;
873
- }
874
-
875
- $menu_id = term_exists( $menu_slug, 'nav_menu' );
876
- if ( ! $menu_id ) {
877
- printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
878
- echo '<br />';
879
- return;
880
- } else {
881
- $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
882
- }
883
-
884
- foreach ( $item['postmeta'] as $meta )
885
- ${$meta['key']} = $meta['value'];
886
-
887
- if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
888
- $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
889
- } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
890
- $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
891
- } else if ( 'custom' != $_menu_item_type ) {
892
- // associated object is missing or not imported yet, we'll retry later
893
- $this->missing_menu_items[] = $item;
894
- return;
895
- }
896
-
897
- if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
898
- $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
899
- } else if ( $_menu_item_menu_item_parent ) {
900
- $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
901
- $_menu_item_menu_item_parent = 0;
902
- }
903
-
904
- // wp_update_nav_menu_item expects CSS classes as a space separated string
905
- $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
906
- if ( is_array( $_menu_item_classes ) )
907
- $_menu_item_classes = implode( ' ', $_menu_item_classes );
908
-
909
- $args = array(
910
- 'menu-item-object-id' => $_menu_item_object_id,
911
- 'menu-item-object' => $_menu_item_object,
912
- 'menu-item-parent-id' => $_menu_item_menu_item_parent,
913
- 'menu-item-position' => intval( $item['menu_order'] ),
914
- 'menu-item-type' => $_menu_item_type,
915
- 'menu-item-title' => $item['post_title'],
916
- 'menu-item-url' => $_menu_item_url,
917
- 'menu-item-description' => $item['post_content'],
918
- 'menu-item-attr-title' => $item['post_excerpt'],
919
- 'menu-item-target' => $_menu_item_target,
920
- 'menu-item-classes' => $_menu_item_classes,
921
- 'menu-item-xfn' => $_menu_item_xfn,
922
- 'menu-item-status' => $item['status']
923
- );
924
-
925
- $id = wp_update_nav_menu_item( $menu_id, 0, $args );
926
- if ( $id && ! is_wp_error( $id ) )
927
- $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
928
- }
929
-
930
- /**
931
- * If fetching attachments is enabled then attempt to create a new attachment
932
- *
933
- * @param array $post Attachment post details from WXR
934
- * @param string $url URL to fetch attachment from
935
- * @return int|WP_Error Post ID on success, WP_Error otherwise
936
- */
937
- function process_attachment( $post, $url ) {
938
- if ( ! $this->fetch_attachments )
939
- return new WP_Error( 'attachment_processing_error',
940
- __( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
941
-
942
- // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
943
- if ( preg_match( '|^/[\w\W]+$|', $url ) )
944
- $url = rtrim( $this->base_url, '/' ) . $url;
945
-
946
- $upload = $this->fetch_remote_file( $url, $post );
947
- if ( is_wp_error( $upload ) )
948
- return $upload;
949
-
950
- if ( $info = wp_check_filetype( $upload['file'] ) )
951
- $post['post_mime_type'] = $info['type'];
952
- else
953
- return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
954
-
955
- $post['guid'] = $upload['url'];
956
-
957
- // as per wp-admin/includes/upload.php
958
- $post_id = wp_insert_attachment( $post, $upload['file'] );
959
- wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
960
-
961
- // remap resized image URLs, works by stripping the extension and remapping the URL stub.
962
- if ( preg_match( '!^image/!', $info['type'] ) ) {
963
- $parts = pathinfo( $url );
964
- $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
965
-
966
- $parts_new = pathinfo( $upload['url'] );
967
- $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
968
-
969
- $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
970
- }
971
-
972
- return $post_id;
973
- }
974
-
975
- /**
976
- * Attempt to download a remote file attachment
977
- *
978
- * @param string $url URL of item to fetch
979
- * @param array $post Attachment details
980
- * @return array|WP_Error Local file location details on success, WP_Error otherwise
981
- */
982
- function fetch_remote_file( $url, $post ) {
983
- // extract the file name and extension from the url
984
- $file_name = basename( $url );
985
-
986
- // get placeholder file in the upload dir with a unique, sanitized filename
987
- $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
988
- if ( $upload['error'] )
989
- return new WP_Error( 'upload_dir_error', $upload['error'] );
990
-
991
- // fetch the remote url and write it to the placeholder file
992
- $remote_response = wp_safe_remote_get( $url, array(
993
- 'timeout' => 300,
994
- 'stream' => true,
995
- 'filename' => $upload['file'],
996
- ) );
997
-
998
- $headers = wp_remote_retrieve_headers( $remote_response );
999
-
1000
- // request failed
1001
- if ( ! $headers ) {
1002
- @unlink( $upload['file'] );
1003
- return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
1004
- }
1005
-
1006
- $remote_response_code = wp_remote_retrieve_response_code( $remote_response );
1007
-
1008
- // make sure the fetch was successful
1009
- if ( $remote_response_code != '200' ) {
1010
- @unlink( $upload['file'] );
1011
- return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($remote_response_code), get_status_header_desc($remote_response_code) ) );
1012
- }
1013
-
1014
- $filesize = filesize( $upload['file'] );
1015
-
1016
- if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
1017
- @unlink( $upload['file'] );
1018
- return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
1019
- }
1020
-
1021
- if ( 0 == $filesize ) {
1022
- @unlink( $upload['file'] );
1023
- return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
1024
- }
1025
-
1026
- $max_size = (int) $this->max_attachment_size();
1027
- if ( ! empty( $max_size ) && $filesize > $max_size ) {
1028
- @unlink( $upload['file'] );
1029
- return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
1030
- }
1031
-
1032
- // keep track of the old and new urls so we can substitute them later
1033
- $this->url_remap[$url] = $upload['url'];
1034
- $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
1035
- // keep track of the destination if the remote url is redirected somewhere else
1036
- if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
1037
- $this->url_remap[$headers['x-final-location']] = $upload['url'];
1038
-
1039
- return $upload;
1040
- }
1041
-
1042
- /**
1043
- * Attempt to associate posts and menu items with previously missing parents
1044
- *
1045
- * An imported post's parent may not have been imported when it was first created
1046
- * so try again. Similarly for child menu items and menu items which were missing
1047
- * the object (e.g. post) they represent in the menu
1048
- */
1049
- function backfill_parents() {
1050
- global $wpdb;
1051
-
1052
- // find parents for post orphans
1053
- foreach ( $this->post_orphans as $child_id => $parent_id ) {
1054
- $local_child_id = $local_parent_id = false;
1055
- if ( isset( $this->processed_posts[$child_id] ) )
1056
- $local_child_id = $this->processed_posts[$child_id];
1057
- if ( isset( $this->processed_posts[$parent_id] ) )
1058
- $local_parent_id = $this->processed_posts[$parent_id];
1059
-
1060
- if ( $local_child_id && $local_parent_id ) {
1061
- $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
1062
- clean_post_cache( $local_child_id );
1063
- }
1064
- }
1065
-
1066
- // all other posts/terms are imported, retry menu items with missing associated object
1067
- $missing_menu_items = $this->missing_menu_items;
1068
- foreach ( $missing_menu_items as $item )
1069
- $this->process_menu_item( $item );
1070
-
1071
- // find parents for menu item orphans
1072
- foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
1073
- $local_child_id = $local_parent_id = 0;
1074
- if ( isset( $this->processed_menu_items[$child_id] ) )
1075
- $local_child_id = $this->processed_menu_items[$child_id];
1076
- if ( isset( $this->processed_menu_items[$parent_id] ) )
1077
- $local_parent_id = $this->processed_menu_items[$parent_id];
1078
-
1079
- if ( $local_child_id && $local_parent_id )
1080
- update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
1081
- }
1082
- }
1083
-
1084
- /**
1085
- * Use stored mapping information to update old attachment URLs
1086
- */
1087
- function backfill_attachment_urls() {
1088
- global $wpdb;
1089
- // make sure we do the longest urls first, in case one is a substring of another
1090
- uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
1091
-
1092
- foreach ( $this->url_remap as $from_url => $to_url ) {
1093
- // remap urls in post_content
1094
- $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
1095
- // remap enclosure urls
1096
- $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
1097
- }
1098
- }
1099
-
1100
- /**
1101
- * Update _thumbnail_id meta to new, imported attachment IDs
1102
- */
1103
- function remap_featured_images() {
1104
- // cycle through posts that have a featured image
1105
- foreach ( $this->featured_images as $post_id => $value ) {
1106
- if ( isset( $this->processed_posts[$value] ) ) {
1107
- $new_id = $this->processed_posts[$value];
1108
- // only update if there's a difference
1109
- if ( $new_id != $value )
1110
- update_post_meta( $post_id, '_thumbnail_id', $new_id );
1111
- }
1112
- }
1113
- }
1114
-
1115
- /**
1116
- * Parse a WXR file
1117
- *
1118
- * @param string $file Path to WXR file for parsing
1119
- * @return array Information gathered from the WXR file
1120
- */
1121
- function parse( $file ) {
1122
- $parser = new WXR_Parser();
1123
- return $parser->parse( $file );
1124
- }
1125
-
1126
- // Display import page title
1127
- function header() {
1128
- echo '<div class="wrap">';
1129
- echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
1130
-
1131
- $updates = get_plugin_updates();
1132
- $basename = plugin_basename(__FILE__);
1133
- if ( isset( $updates[$basename] ) ) {
1134
- $update = $updates[$basename];
1135
- echo '<div class="error"><p><strong>';
1136
- 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 );
1137
- echo '</strong></p></div>';
1138
- }
1139
- }
1140
-
1141
- // Close div.wrap
1142
- function footer() {
1143
- echo '</div>';
1144
- }
1145
-
1146
- /**
1147
- * Display introductory text and file upload form
1148
- */
1149
- function greet() {
1150
- echo '<div class="narrow">';
1151
- 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>';
1152
- echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
1153
- wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
1154
- echo '</div>';
1155
- }
1156
-
1157
- /**
1158
- * Decide if the given meta key maps to information we will want to import
1159
- *
1160
- * @param string $key The meta key to check
1161
- * @return string|bool The key if we do want to import, false if not
1162
- */
1163
- function is_valid_meta_key( $key ) {
1164
- // skip attachment metadata since we'll regenerate it from scratch
1165
- // skip _edit_lock as not relevant for import
1166
- if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
1167
- return false;
1168
- return $key;
1169
- }
1170
-
1171
- /**
1172
- * Decide whether or not the importer is allowed to create users.
1173
- * Default is true, can be filtered via import_allow_create_users
1174
- *
1175
- * @return bool True if creating users is allowed
1176
- */
1177
- function allow_create_users() {
1178
- return apply_filters( 'import_allow_create_users', true );
1179
- }
1180
-
1181
- /**
1182
- * Decide whether or not the importer should attempt to download attachment files.
1183
- * Default is true, can be filtered via import_allow_fetch_attachments. The choice
1184
- * made at the import options screen must also be true, false here hides that checkbox.
1185
- *
1186
- * @return bool True if downloading attachments is allowed
1187
- */
1188
- function allow_fetch_attachments() {
1189
- return apply_filters( 'import_allow_fetch_attachments', true );
1190
- }
1191
-
1192
- /**
1193
- * Decide what the maximum file size for downloaded attachments is.
1194
- * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
1195
- *
1196
- * @return int Maximum attachment file size to import
1197
- */
1198
- function max_attachment_size() {
1199
- return apply_filters( 'import_attachment_size_limit', 0 );
1200
- }
1201
-
1202
- /**
1203
- * Added to http_request_timeout filter to force timeout at 60 seconds during import
1204
- * @return int 60
1205
- */
1206
- function bump_request_timeout( $val ) {
1207
- return 60;
1208
- }
1209
-
1210
- // return the difference in length between two strings
1211
- function cmpr_strlen( $a, $b ) {
1212
- return strlen($b) - strlen($a);
1213
- }
1214
- }
1215
 
1216
- } // class_exists( 'WP_Importer' )
 
1217
 
1218
  function wordpress_importer_init() {
1219
  load_plugin_textdomain( '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: https://wordpress.org/
8
+ Version: 0.7
9
  Text Domain: wordpress-importer
10
+ License: GPLv2 or later
11
+ License URI: https://www.gnu.org/licenses/gpl-2.0.html
12
  */
13
 
14
+ if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) {
15
  return;
16
+ }
17
 
18
  /** Display verbose errors */
19
+ if ( ! defined( 'IMPORT_DEBUG' ) ) {
20
+ define( 'IMPORT_DEBUG', WP_DEBUG );
21
+ }
22
 
23
+ /** WordPress Import Administration API */
24
  require_once ABSPATH . 'wp-admin/includes/import.php';
25
 
26
  if ( ! class_exists( 'WP_Importer' ) ) {
29
  require $class_wp_importer;
30
  }
31
 
32
+ /** Functions missing in older WordPress versions. */
33
+ require_once dirname( __FILE__ ) . '/compat.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ /** WXR_Parser class */
36
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser.php';
37
 
38
+ /** WXR_Parser_SimpleXML class */
39
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-simplexml.php';
40
 
41
+ /** WXR_Parser_XML class */
42
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-xml.php';
 
 
43
 
44
+ /** WXR_Parser_Regex class */
45
+ require_once dirname( __FILE__ ) . '/parsers/class-wxr-parser-regex.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ /** WP_Import class */
48
+ require_once dirname( __FILE__ ) . '/class-wp-import.php';
49
 
50
  function wordpress_importer_init() {
51
  load_plugin_textdomain( 'wordpress-importer' );