-
+ EFAE5B996DE32533FA30DECA52C2BD9912E8C34EC533ABCEBF2AE77FDCA06740DF26BAF51DC54EC2B9F44A787936262047AC7750643FA60F30233F13BC774633
mp-wp/wp-admin/import/wordpress.php
(0 . 0)(1 . 806)
21352 <?php
21353 /**
21354 * WordPress Importer
21355 *
21356 * @package WordPress
21357 * @subpackage Importer
21358 */
21359
21360 /**
21361 * WordPress Importer
21362 *
21363 * Will process the WordPress eXtended RSS files that you upload from the export
21364 * file.
21365 *
21366 * @since unknown
21367 */
21368 class WP_Import {
21369
21370 var $post_ids_processed = array ();
21371 var $orphans = array ();
21372 var $file;
21373 var $id;
21374 var $mtnames = array ();
21375 var $newauthornames = array ();
21376 var $allauthornames = array ();
21377
21378 var $author_ids = array ();
21379 var $tags = array ();
21380 var $categories = array ();
21381
21382 var $j = -1;
21383 var $fetch_attachments = false;
21384 var $url_remap = array ();
21385
21386 function header() {
21387 echo '<div class="wrap">';
21388 screen_icon();
21389 echo '<h2>'.__('Import WordPress').'</h2>';
21390 }
21391
21392 function footer() {
21393 echo '</div>';
21394 }
21395
21396 function unhtmlentities($string) { // From php.net for < 4.3 compat
21397 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
21398 $trans_tbl = array_flip($trans_tbl);
21399 return strtr($string, $trans_tbl);
21400 }
21401
21402 function greet() {
21403 echo '<div class="narrow">';
21404 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 blog.').'</p>';
21405 echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>';
21406 wp_import_upload_form("admin.php?import=wordpress&step=1");
21407 echo '</div>';
21408 }
21409
21410 function get_tag( $string, $tag ) {
21411 global $wpdb;
21412 preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
21413 $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
21414 $return = $wpdb->escape( trim( $return ) );
21415 return $return;
21416 }
21417
21418 function has_gzip() {
21419 return is_callable('gzopen');
21420 }
21421
21422 function fopen($filename, $mode='r') {
21423 if ( $this->has_gzip() )
21424 return gzopen($filename, $mode);
21425 return fopen($filename, $mode);
21426 }
21427
21428 function feof($fp) {
21429 if ( $this->has_gzip() )
21430 return gzeof($fp);
21431 return feof($fp);
21432 }
21433
21434 function fgets($fp, $len=8192) {
21435 if ( $this->has_gzip() )
21436 return gzgets($fp, $len);
21437 return fgets($fp, $len);
21438 }
21439
21440 function fclose($fp) {
21441 if ( $this->has_gzip() )
21442 return gzclose($fp);
21443 return fclose($fp);
21444 }
21445
21446 function get_entries($process_post_func=NULL) {
21447 set_magic_quotes_runtime(0);
21448
21449 $doing_entry = false;
21450 $is_wxr_file = false;
21451
21452 $fp = $this->fopen($this->file, 'r');
21453 if ($fp) {
21454 while ( !$this->feof($fp) ) {
21455 $importline = rtrim($this->fgets($fp));
21456
21457 // this doesn't check that the file is perfectly valid but will at least confirm that it's not the wrong format altogether
21458 if ( !$is_wxr_file && preg_match('|xmlns:wp="http://wordpress[.]org/export/\d+[.]\d+/"|', $importline) )
21459 $is_wxr_file = true;
21460
21461 if ( false !== strpos($importline, '<wp:base_site_url>') ) {
21462 preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url);
21463 $this->base_url = $url[1];
21464 continue;
21465 }
21466 if ( false !== strpos($importline, '<wp:category>') ) {
21467 preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
21468 $this->categories[] = $category[1];
21469 continue;
21470 }
21471 if ( false !== strpos($importline, '<wp:tag>') ) {
21472 preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
21473 $this->tags[] = $tag[1];
21474 continue;
21475 }
21476 if ( false !== strpos($importline, '<item>') ) {
21477 $this->post = '';
21478 $doing_entry = true;
21479 continue;
21480 }
21481 if ( false !== strpos($importline, '</item>') ) {
21482 $doing_entry = false;
21483 if ($process_post_func)
21484 call_user_func($process_post_func, $this->post);
21485 continue;
21486 }
21487 if ( $doing_entry ) {
21488 $this->post .= $importline . "\n";
21489 }
21490 }
21491
21492 $this->fclose($fp);
21493 }
21494
21495 return $is_wxr_file;
21496
21497 }
21498
21499 function get_wp_authors() {
21500 // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
21501 $temp = $this->allauthornames;
21502 $authors[0] = array_shift($temp);
21503 $y = count($temp) + 1;
21504 for ($x = 1; $x < $y; $x ++) {
21505 $next = array_shift($temp);
21506 if (!(in_array($next, $authors)))
21507 array_push($authors, "$next");
21508 }
21509
21510 return $authors;
21511 }
21512
21513 function get_authors_from_post() {
21514 global $current_user;
21515
21516 // this will populate $this->author_ids with a list of author_names => user_ids
21517
21518 foreach ( $_POST['author_in'] as $i => $in_author_name ) {
21519
21520 if ( !empty($_POST['user_select'][$i]) ) {
21521 // an existing user was selected in the dropdown list
21522 $user = get_userdata( intval($_POST['user_select'][$i]) );
21523 if ( isset($user->ID) )
21524 $this->author_ids[$in_author_name] = $user->ID;
21525 }
21526 elseif ( $this->allow_create_users() ) {
21527 // nothing was selected in the dropdown list, so we'll use the name in the text field
21528
21529 $new_author_name = trim($_POST['user_create'][$i]);
21530 // if the user didn't enter a name, assume they want to use the same name as in the import file
21531 if ( empty($new_author_name) )
21532 $new_author_name = $in_author_name;
21533
21534 $user_id = username_exists($new_author_name);
21535 if ( !$user_id ) {
21536 $user_id = wp_create_user($new_author_name, wp_generate_password());
21537 }
21538
21539 $this->author_ids[$in_author_name] = $user_id;
21540 }
21541
21542 // failsafe: if the user_id was invalid, default to the current user
21543 if ( empty($this->author_ids[$in_author_name]) ) {
21544 $this->author_ids[$in_author_name] = intval($current_user->ID);
21545 }
21546 }
21547
21548 }
21549
21550 function wp_authors_form() {
21551 ?>
21552 <?php screen_icon(); ?>
21553 <h2><?php _e('Assign Authors'); ?></h2>
21554 <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p>
21555 <?php
21556 if ( $this->allow_create_users() ) {
21557 echo '<p>'.__('If a new user is created by WordPress, a password will be randomly generated. Manually change the user\'s details if necessary.')."</p>\n";
21558 }
21559
21560
21561 $authors = $this->get_wp_authors();
21562 echo '<form action="?import=wordpress&step=2&id=' . $this->id . '" method="post">';
21563 wp_nonce_field('import-wordpress');
21564 echo '<ol id="authors">';
21565 $j = -1;
21566 foreach ($authors as $author) {
21567 ++ $j;
21568 echo '<li>'.__('Import author:').' <strong>'.$author.'</strong><br />';
21569 $this->users_form($j, $author);
21570 echo '</li>';
21571 }
21572
21573 if ( $this->allow_fetch_attachments() ) {
21574 ?>
21575 </ol>
21576 <?php screen_icon(); ?>
21577 <h2><?php _e('Import Attachments'); ?></h2>
21578 <p>
21579 <input type="checkbox" value="1" name="attachments" id="import-attachments" />
21580 <label for="import-attachments"><?php _e('Download and import file attachments') ?></label>
21581 </p>
21582
21583 <?php
21584 }
21585
21586 echo '<p class="submit">';
21587 echo '<input type="submit" class="button" value="'.attribute_escape( __('Submit') ).'" />'.'<br />';
21588 echo '</p>';
21589 echo '</form>';
21590
21591 }
21592
21593 function users_form($n, $author) {
21594
21595 if ( $this->allow_create_users() ) {
21596 printf('<label>'.__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user_create['.intval($n).']'.'" maxlength="30" /></label> <br />');
21597 }
21598 else {
21599 echo __('Map to existing').'<br />';
21600 }
21601
21602 // keep track of $n => $author name
21603 echo '<input type="hidden" name="author_in['.intval($n).']" value="'.htmlspecialchars($author).'" />';
21604
21605 $users = get_users_of_blog();
21606 ?><select name="user_select[<?php echo $n; ?>]">
21607 <option value="0"><?php _e('- Select -'); ?></option>
21608 <?php
21609 foreach ($users as $user) {
21610 echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
21611 }
21612 ?>
21613 </select>
21614 <?php
21615 }
21616
21617 function select_authors() {
21618 $is_wxr_file = $this->get_entries(array(&$this, 'process_author'));
21619 if ( $is_wxr_file ) {
21620 $this->wp_authors_form();
21621 }
21622 else {
21623 echo '<h2>'.__('Invalid file').'</h2>';
21624 echo '<p>'.__('Please upload a valid WXR (WordPress eXtended RSS) export file.').'</p>';
21625 }
21626 }
21627
21628 // fetch the user ID for a given author name, respecting the mapping preferences
21629 function checkauthor($author) {
21630 global $current_user;
21631
21632 if ( !empty($this->author_ids[$author]) )
21633 return $this->author_ids[$author];
21634
21635 // failsafe: map to the current user
21636 return $current_user->ID;
21637 }
21638
21639
21640
21641 function process_categories() {
21642 global $wpdb;
21643
21644 $cat_names = (array) get_terms('category', 'fields=names');
21645
21646 while ( $c = array_shift($this->categories) ) {
21647 $cat_name = trim($this->get_tag( $c, 'wp:cat_name' ));
21648
21649 // If the category exists we leave it alone
21650 if ( in_array($cat_name, $cat_names) )
21651 continue;
21652
21653 $category_nicename = $this->get_tag( $c, 'wp:category_nicename' );
21654 $posts_private = (int) $this->get_tag( $c, 'wp:posts_private' );
21655 $links_private = (int) $this->get_tag( $c, 'wp:links_private' );
21656
21657 $parent = $this->get_tag( $c, 'wp:category_parent' );
21658
21659 if ( empty($parent) )
21660 $category_parent = '0';
21661 else
21662 $category_parent = category_exists($parent);
21663
21664 $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
21665
21666 $cat_ID = wp_insert_category($catarr);
21667 }
21668 }
21669
21670 function process_tags() {
21671 global $wpdb;
21672
21673 $tag_names = (array) get_terms('post_tag', 'fields=names');
21674
21675 while ( $c = array_shift($this->tags) ) {
21676 $tag_name = trim($this->get_tag( $c, 'wp:tag_name' ));
21677
21678 // If the category exists we leave it alone
21679 if ( in_array($tag_name, $tag_names) )
21680 continue;
21681
21682 $slug = $this->get_tag( $c, 'wp:tag_slug' );
21683 $description = $this->get_tag( $c, 'wp:tag_description' );
21684
21685 $tagarr = compact('slug', 'description');
21686
21687 $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr);
21688 }
21689 }
21690
21691 function process_author($post) {
21692 $author = $this->get_tag( $post, 'dc:creator' );
21693 if ($author)
21694 $this->allauthornames[] = $author;
21695 }
21696
21697 function process_posts() {
21698 echo '<ol>';
21699
21700 $this->get_entries(array(&$this, 'process_post'));
21701
21702 echo '</ol>';
21703
21704 wp_import_cleanup($this->id);
21705 do_action('import_done', 'wordpress');
21706
21707 echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
21708 }
21709
21710 function process_post($post) {
21711 global $wpdb;
21712
21713 $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
21714 if ( $post_ID && !empty($this->post_ids_processed[$post_ID]) ) // Processed already
21715 return 0;
21716
21717 set_time_limit( 60 );
21718
21719 // There are only ever one of these
21720 $post_title = $this->get_tag( $post, 'title' );
21721 $post_date = $this->get_tag( $post, 'wp:post_date' );
21722 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
21723 $comment_status = $this->get_tag( $post, 'wp:comment_status' );
21724 $ping_status = $this->get_tag( $post, 'wp:ping_status' );
21725 $post_status = $this->get_tag( $post, 'wp:status' );
21726 $post_name = $this->get_tag( $post, 'wp:post_name' );
21727 $post_parent = $this->get_tag( $post, 'wp:post_parent' );
21728 $menu_order = $this->get_tag( $post, 'wp:menu_order' );
21729 $post_type = $this->get_tag( $post, 'wp:post_type' );
21730 $post_password = $this->get_tag( $post, 'wp:post_password' );
21731 $guid = $this->get_tag( $post, 'guid' );
21732 $post_author = $this->get_tag( $post, 'dc:creator' );
21733
21734 $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
21735 $post_excerpt = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_excerpt);
21736 $post_excerpt = str_replace('<br>', '<br />', $post_excerpt);
21737 $post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt);
21738
21739 $post_content = $this->get_tag( $post, 'content:encoded' );
21740 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
21741 $post_content = str_replace('<br>', '<br />', $post_content);
21742 $post_content = str_replace('<hr>', '<hr />', $post_content);
21743
21744 preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
21745 $tags = $tags[1];
21746
21747 $tag_index = 0;
21748 foreach ($tags as $tag) {
21749 $tags[$tag_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $tag)));
21750 $tag_index++;
21751 }
21752
21753 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
21754 $categories = $categories[1];
21755
21756 $cat_index = 0;
21757 foreach ($categories as $category) {
21758 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
21759 $cat_index++;
21760 }
21761
21762 $post_exists = post_exists($post_title, '', $post_date);
21763
21764 if ( $post_exists ) {
21765 echo '<li>';
21766 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
21767 $comment_post_ID = $post_id = $post_exists;
21768 } else {
21769
21770 // If it has parent, process parent first.
21771 $post_parent = (int) $post_parent;
21772 if ($post_parent) {
21773 // if we already know the parent, map it to the local ID
21774 if ( $parent = $this->post_ids_processed[$post_parent] ) {
21775 $post_parent = $parent; // new ID of the parent
21776 }
21777 else {
21778 // record the parent for later
21779 $this->orphans[intval($post_ID)] = $post_parent;
21780 }
21781 }
21782
21783 echo '<li>';
21784
21785 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
21786
21787 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'post_status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password');
21788 $postdata['import_id'] = $post_ID;
21789 if ($post_type == 'attachment') {
21790 $remote_url = $this->get_tag( $post, 'wp:attachment_url' );
21791 if ( !$remote_url )
21792 $remote_url = $guid;
21793
21794 $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
21795 if ( !$post_id or is_wp_error($post_id) )
21796 return $post_id;
21797 }
21798 else {
21799 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
21800 $comment_post_ID = $post_id = wp_insert_post($postdata);
21801 }
21802
21803 if ( is_wp_error( $post_id ) )
21804 return $post_id;
21805
21806 // Memorize old and new ID.
21807 if ( $post_id && $post_ID ) {
21808 $this->post_ids_processed[intval($post_ID)] = intval($post_id);
21809 }
21810
21811 // Add categories.
21812 if (count($categories) > 0) {
21813 $post_cats = array();
21814 foreach ($categories as $category) {
21815 if ( '' == $category )
21816 continue;
21817 $slug = sanitize_term_field('slug', $category, 0, 'category', 'db');
21818 $cat = get_term_by('slug', $slug, 'category');
21819 $cat_ID = 0;
21820 if ( ! empty($cat) )
21821 $cat_ID = $cat->term_id;
21822 if ($cat_ID == 0) {
21823 $category = $wpdb->escape($category);
21824 $cat_ID = wp_insert_category(array('cat_name' => $category));
21825 if ( is_wp_error($cat_ID) )
21826 continue;
21827 }
21828 $post_cats[] = $cat_ID;
21829 }
21830 wp_set_post_categories($post_id, $post_cats);
21831 }
21832
21833 // Add tags.
21834 if (count($tags) > 0) {
21835 $post_tags = array();
21836 foreach ($tags as $tag) {
21837 if ( '' == $tag )
21838 continue;
21839 $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
21840 $tag_obj = get_term_by('slug', $slug, 'post_tag');
21841 $tag_id = 0;
21842 if ( ! empty($tag_obj) )
21843 $tag_id = $tag_obj->term_id;
21844 if ( $tag_id == 0 ) {
21845 $tag = $wpdb->escape($tag);
21846 $tag_id = wp_insert_term($tag, 'post_tag');
21847 if ( is_wp_error($tag_id) )
21848 continue;
21849 $tag_id = $tag_id['term_id'];
21850 }
21851 $post_tags[] = intval($tag_id);
21852 }
21853 wp_set_post_tags($post_id, $post_tags);
21854 }
21855 }
21856
21857 // Now for comments
21858 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
21859 $comments = $comments[1];
21860 $num_comments = 0;
21861 if ( $comments) { foreach ($comments as $comment) {
21862 $comment_author = $this->get_tag( $comment, 'wp:comment_author');
21863 $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
21864 $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP');
21865 $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url');
21866 $comment_date = $this->get_tag( $comment, 'wp:comment_date');
21867 $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt');
21868 $comment_content = $this->get_tag( $comment, 'wp:comment_content');
21869 $comment_approved = $this->get_tag( $comment, 'wp:comment_approved');
21870 $comment_type = $this->get_tag( $comment, 'wp:comment_type');
21871 $comment_parent = $this->get_tag( $comment, 'wp:comment_parent');
21872
21873 // if this is a new post we can skip the comment_exists() check
21874 if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) {
21875 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');
21876 wp_insert_comment($commentdata);
21877 $num_comments++;
21878 }
21879 } }
21880
21881 if ( $num_comments )
21882 printf(' '.__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
21883
21884 // Now for post meta
21885 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
21886 $postmeta = $postmeta[1];
21887 if ( $postmeta) { foreach ($postmeta as $p) {
21888 $key = $this->get_tag( $p, 'wp:meta_key' );
21889 $value = $this->get_tag( $p, 'wp:meta_value' );
21890 $value = stripslashes($value); // add_post_meta() will escape.
21891
21892 $this->process_post_meta($post_id, $key, $value);
21893
21894 } }
21895
21896 do_action('import_post_added', $post_id);
21897 print "</li>\n";
21898 }
21899
21900 function process_post_meta($post_id, $key, $value) {
21901 // the filter can return false to skip a particular metadata key
21902 $_key = apply_filters('import_post_meta_key', $key);
21903 if ( $_key ) {
21904 add_post_meta( $post_id, $_key, $value );
21905 do_action('import_post_meta', $post_id, $_key, $value);
21906 }
21907 }
21908
21909 function process_attachment($postdata, $remote_url) {
21910 if ($this->fetch_attachments and $remote_url) {
21911 printf( __('Importing attachment <em>%s</em>... '), htmlspecialchars($remote_url) );
21912
21913 // If the URL is absolute, but does not contain http, upload it assuming the base_site_url variable
21914 if ( preg_match('/^\/[\w\W]+$/', $remote_url) )
21915 $remote_url = rtrim($this->base_url,'/').$remote_url;
21916
21917 $upload = $this->fetch_remote_file($postdata, $remote_url);
21918 if ( is_wp_error($upload) ) {
21919 printf( __('Remote file error: %s'), htmlspecialchars($upload->get_error_message()) );
21920 return $upload;
21921 }
21922 else {
21923 print '('.size_format(filesize($upload['file'])).')';
21924 }
21925
21926 if ( $info = wp_check_filetype($upload['file']) ) {
21927 $postdata['post_mime_type'] = $info['type'];
21928 }
21929 else {
21930 print __('Invalid file type');
21931 return;
21932 }
21933
21934 $postdata['guid'] = $upload['url'];
21935
21936 // as per wp-admin/includes/upload.php
21937 $post_id = wp_insert_attachment($postdata, $upload['file']);
21938 wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
21939
21940 // remap the thumbnail url. this isn't perfect because we're just guessing the original url.
21941 if ( preg_match('@^image/@', $info['type']) && $thumb_url = wp_get_attachment_thumb_url($post_id) ) {
21942 $parts = pathinfo($remote_url);
21943 $ext = $parts['extension'];
21944 $name = basename($parts['basename'], ".{$ext}");
21945 $this->url_remap[$parts['dirname'] . '/' . $name . '.thumbnail.' . $ext] = $thumb_url;
21946 }
21947
21948 return $post_id;
21949 }
21950 else {
21951 printf( __('Skipping attachment <em>%s</em>'), htmlspecialchars($remote_url) );
21952 }
21953 }
21954
21955 function fetch_remote_file($post, $url) {
21956 $upload = wp_upload_dir($post['post_date']);
21957
21958 // extract the file name and extension from the url
21959 $file_name = basename($url);
21960
21961 // get placeholder file in the upload dir with a unique sanitized filename
21962 $upload = wp_upload_bits( $file_name, 0, '', $post['post_date']);
21963 if ( $upload['error'] ) {
21964 echo $upload['error'];
21965 return new WP_Error( 'upload_dir_error', $upload['error'] );
21966 }
21967
21968 // fetch the remote url and write it to the placeholder file
21969 $headers = wp_get_http($url, $upload['file']);
21970
21971 //Request failed
21972 if ( ! $headers ) {
21973 @unlink($upload['file']);
21974 return new WP_Error( 'import_file_error', __('Remote server did not respond') );
21975 }
21976
21977 // make sure the fetch was successful
21978 if ( $headers['response'] != '200' ) {
21979 @unlink($upload['file']);
21980 return new WP_Error( 'import_file_error', sprintf(__('Remote file returned error response %1$d %2$s'), $headers['response'], get_status_header_desc($headers['response']) ) );
21981 }
21982 elseif ( isset($headers['content-length']) && filesize($upload['file']) != $headers['content-length'] ) {
21983 @unlink($upload['file']);
21984 return new WP_Error( 'import_file_error', __('Remote file is incorrect size') );
21985 }
21986
21987 $max_size = $this->max_attachment_size();
21988 if ( !empty($max_size) and filesize($upload['file']) > $max_size ) {
21989 @unlink($upload['file']);
21990 return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', size_format($max_size))) );
21991 }
21992
21993 // keep track of the old and new urls so we can substitute them later
21994 $this->url_remap[$url] = $upload['url'];
21995 // if the remote url is redirected somewhere else, keep track of the destination too
21996 if ( $headers['x-final-location'] != $url )
21997 $this->url_remap[$headers['x-final-location']] = $upload['url'];
21998
21999 return $upload;
22000
22001 }
22002
22003 // sort by strlen, longest string first
22004 function cmpr_strlen($a, $b) {
22005 return strlen($b) - strlen($a);
22006 }
22007
22008 // update url references in post bodies to point to the new local files
22009 function backfill_attachment_urls() {
22010
22011 // make sure we do the longest urls first, in case one is a substring of another
22012 uksort($this->url_remap, array(&$this, 'cmpr_strlen'));
22013
22014 global $wpdb;
22015 foreach ($this->url_remap as $from_url => $to_url) {
22016 // remap urls in post_content
22017 $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, '%s', '%s')", $from_url, $to_url) );
22018 // remap enclosure urls
22019 $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, '%s', '%s') WHERE meta_key='enclosure'", $from_url, $to_url) );
22020 }
22021 }
22022
22023 // update the post_parent of orphans now that we know the local id's of all parents
22024 function backfill_parents() {
22025 global $wpdb;
22026
22027 foreach ($this->orphans as $child_id => $parent_id) {
22028 $local_child_id = $this->post_ids_processed[$child_id];
22029 $local_parent_id = $this->post_ids_processed[$parent_id];
22030 if ($local_child_id and $local_parent_id) {
22031 $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID = %d", $local_parent_id, $local_child_id));
22032 }
22033 }
22034 }
22035
22036 function is_valid_meta_key($key) {
22037 // skip _wp_attached_file metadata since we'll regenerate it from scratch
22038 if ( $key == '_wp_attached_file' )
22039 return false;
22040 return $key;
22041 }
22042
22043 // give the user the option of creating new users to represent authors in the import file?
22044 function allow_create_users() {
22045 return apply_filters('import_allow_create_users', true);
22046 }
22047
22048 // give the user the option of downloading and importing attached files
22049 function allow_fetch_attachments() {
22050 return apply_filters('import_allow_fetch_attachments', true);
22051 }
22052
22053 function max_attachment_size() {
22054 // can be overridden with a filter - 0 means no limit
22055 return apply_filters('import_attachment_size_limit', 0);
22056 }
22057
22058 function import_start() {
22059 wp_defer_term_counting(true);
22060 wp_defer_comment_counting(true);
22061 do_action('import_start');
22062 }
22063
22064 function import_end() {
22065 do_action('import_end');
22066
22067 // clear the caches after backfilling
22068 foreach ($this->post_ids_processed as $post_id)
22069 clean_post_cache($post_id);
22070
22071 wp_defer_term_counting(false);
22072 wp_defer_comment_counting(false);
22073 }
22074
22075 function import($id, $fetch_attachments = false) {
22076 $this->id = (int) $id;
22077 $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments);
22078
22079 add_filter('import_post_meta_key', array($this, 'is_valid_meta_key'));
22080 $file = get_attached_file($this->id);
22081 $this->import_file($file);
22082 }
22083
22084 function import_file($file) {
22085 $this->file = $file;
22086
22087 $this->import_start();
22088 $this->get_authors_from_post();
22089 wp_suspend_cache_invalidation(true);
22090 $this->get_entries();
22091 $this->process_categories();
22092 $this->process_tags();
22093 $result = $this->process_posts();
22094 wp_suspend_cache_invalidation(false);
22095 $this->backfill_parents();
22096 $this->backfill_attachment_urls();
22097 $this->import_end();
22098
22099 if ( is_wp_error( $result ) )
22100 return $result;
22101 }
22102
22103 function handle_upload() {
22104 $file = wp_import_handle_upload();
22105 if ( isset($file['error']) ) {
22106 echo '<p>'.__('Sorry, there has been an error.').'</p>';
22107 echo '<p><strong>' . $file['error'] . '</strong></p>';
22108 return false;
22109 }
22110 $this->file = $file['file'];
22111 $this->id = (int) $file['id'];
22112 return true;
22113 }
22114
22115 function dispatch() {
22116 if (empty ($_GET['step']))
22117 $step = 0;
22118 else
22119 $step = (int) $_GET['step'];
22120
22121 $this->header();
22122 switch ($step) {
22123 case 0 :
22124 $this->greet();
22125 break;
22126 case 1 :
22127 check_admin_referer('import-upload');
22128 if ( $this->handle_upload() )
22129 $this->select_authors();
22130 break;
22131 case 2:
22132 check_admin_referer('import-wordpress');
22133 $result = $this->import( $_GET['id'], $_POST['attachments'] );
22134 if ( is_wp_error( $result ) )
22135 echo $result->get_error_message();
22136 break;
22137 }
22138 $this->footer();
22139 }
22140
22141 function WP_Import() {
22142 // Nothing.
22143 }
22144 }
22145
22146 /**
22147 * Register WordPress Importer
22148 *
22149 * @since unknown
22150 * @var WP_Import
22151 * @name $wp_import
22152 */
22153 $wp_import = new WP_Import();
22154
22155 register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.'), array ($wp_import, 'dispatch'));
22156
22157 ?>