WP_Import

The WordPress Importer WP Import class.

Defined (1)

The class is defined in the following location(s).

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