wordpress-importer.php 41 KB

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