Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/wp-admin/import/wordpress.php

    r4742 r5426  
    44
    55    var $posts = array ();
     6    var $posts_processed = array ();
     7    // Array of arrays. [[0] => XML fragment, [1] => New post ID]
    68    var $file;
    79    var $id;
     
    3436
    3537    function get_tag( $string, $tag ) {
     38        global $wpdb;
    3639        preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
    37         $return = addslashes( trim( $return[1] ) );
     40        $return = $wpdb->escape( trim( $return[1] ) );
    3841        return $return;
    3942    }
     
    6366            $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
    6467            if (!$user_id) { //banging my head against the desk now.
    65                 if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
     68                if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
    6669                    $user_id = wp_create_user($author, $pass);
    6770                    $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
     
    8285    function get_entries() {
    8386        set_magic_quotes_runtime(0);
    84         $importdata = file($this->file); // Read the file into an array
    85         $importdata = implode('', $importdata); // squish it
    86         $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
    87         preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
    88         $this->posts = $this->posts[1];
    89         preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
    90         $this->categories = $this->categories[1];
     87        $importdata = array_map('rtrim', file($this->file)); // Read the file into an array
     88
     89        $this->posts = array();
     90        $this->categories = array();
     91        $num = 0;
     92        $doing_entry = false;
     93        foreach ($importdata as $importline) {
     94            if ( false !== strpos($importline, '<wp:category>') ) {
     95                preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
     96                $this->categories[] = $category[1];
     97                continue;
     98            }
     99            if ( false !== strpos($importline, '<item>') ) {
     100                $this->posts[$num] = '';
     101                $doing_entry = true;
     102                continue;   
     103            }
     104            if ( false !== strpos($importline, '</item>') ) {
     105                $num++;
     106                $doing_entry = false;
     107                continue;   
     108            }
     109            if ( $doing_entry ) {
     110                $this->posts[$num] .= $importline . "\n";
     111            }
     112        }
     113
     114        foreach ($this->posts as $post) {
     115            $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
     116            if ($post_ID) {
     117                $this->posts_processed[$post_ID][0] = &$post;
     118                $this->posts_processed[$post_ID][1] = 0;
     119            }
     120        }
    91121    }
    92122
     
    151181        echo '<ol id="authors">';
    152182        echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
     183        wp_nonce_field('import-wordpress');
    153184        $j = -1;
    154185        foreach ($authors as $author) {
     
    168199        $file = wp_import_handle_upload();
    169200        if ( isset($file['error']) ) {
    170             $this->header();
    171201            echo '<p>'.__('Sorry, there has been an error.').'</p>';
    172202            echo '<p><strong>' . $file['error'] . '</strong></p>';
    173             $this->footer();
    174203            return;
    175204        }
    176205        $this->file = $file['file'];
    177         $this->id = $file['id'];
     206        $this->id = (int) $file['id'];
    178207
    179208        $this->get_entries();
     
    202231                $category_parent = '0';
    203232            else
    204                 $category_parent = (int) category_exists($parent);
     233                $category_parent = category_exists($parent);
    205234
    206235            $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
     
    211240
    212241    function process_posts() {
    213         global $wpdb;
    214242        $i = -1;
    215243        echo '<ol>';
    216         foreach ($this->posts as $post) {
    217 
    218             // There are only ever one of these
    219             $post_title     = $this->get_tag( $post, 'title' );
    220             $post_date      = $this->get_tag( $post, 'wp:post_date' );
    221             $post_date_gmt  = $this->get_tag( $post, 'wp:post_date_gmt' );
    222             $comment_status = $this->get_tag( $post, 'wp:comment_status' );
    223             $ping_status    = $this->get_tag( $post, 'wp:ping_status' );
    224             $post_status    = $this->get_tag( $post, 'wp:status' );
    225             $post_parent    = $this->get_tag( $post, 'wp:post_parent' );
    226             $post_type      = $this->get_tag( $post, 'wp:post_type' );
    227             $guid           = $this->get_tag( $post, 'guid' );
    228             $post_author    = $this->get_tag( $post, 'dc:creator' );
    229 
    230             $post_content = $this->get_tag( $post, 'content:encoded' );
    231             $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);
    232             $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
    233             $post_content = str_replace('<br>', '<br />', $post_content);
    234             $post_content = str_replace('<hr>', '<hr />', $post_content);
    235 
    236             preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
    237             $categories = $categories[1];
    238 
    239             $cat_index = 0;
    240             foreach ($categories as $category) {
    241                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
    242                 $cat_index++;
    243             }
    244 
    245             if ($post_id = post_exists($post_title, '', $post_date)) {
    246                 echo '<li>';
    247                 printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
    248             } else {
    249                 echo '<li>';
    250                 printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
    251 
    252                 $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
    253 
    254                 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'post_type');
    255                 $comment_post_ID = $post_id = wp_insert_post($postdata);
    256                 // Add categories.
    257                 if (0 != count($categories)) {
    258                     wp_create_categories($categories, $post_id);
     244
     245        foreach ($this->posts as $post)
     246            $this->process_post($post);
     247
     248        echo '</ol>';
     249
     250        wp_import_cleanup($this->id);
     251
     252        echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
     253    }
     254 
     255    function process_post($post) {
     256        global $wpdb;
     257
     258        $post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
     259        if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already
     260            return 0;
     261     
     262        // There are only ever one of these
     263        $post_title     = $this->get_tag( $post, 'title' );
     264        $post_date      = $this->get_tag( $post, 'wp:post_date' );
     265        $post_date_gmt  = $this->get_tag( $post, 'wp:post_date_gmt' );
     266        $comment_status = $this->get_tag( $post, 'wp:comment_status' );
     267        $ping_status    = $this->get_tag( $post, 'wp:ping_status' );
     268        $post_status    = $this->get_tag( $post, 'wp:status' );
     269        $post_name      = $this->get_tag( $post, 'wp:post_name' );
     270        $post_parent    = $this->get_tag( $post, 'wp:post_parent' );
     271        $menu_order     = $this->get_tag( $post, 'wp:menu_order' );
     272        $post_type      = $this->get_tag( $post, 'wp:post_type' );
     273        $guid           = $this->get_tag( $post, 'guid' );
     274        $post_author    = $this->get_tag( $post, 'dc:creator' );
     275
     276        $post_content = $this->get_tag( $post, 'content:encoded' );
     277        $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);
     278        $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
     279        $post_content = str_replace('<br>', '<br />', $post_content);
     280        $post_content = str_replace('<hr>', '<hr />', $post_content);
     281
     282        preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
     283        $categories = $categories[1];
     284
     285        $cat_index = 0;
     286        foreach ($categories as $category) {
     287            $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
     288            $cat_index++;
     289        }
     290
     291        if ($post_id = post_exists($post_title, '', $post_date)) {
     292            echo '<li>';
     293            printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
     294        } else {
     295
     296            // If it has parent, process parent first.
     297            $post_parent = (int) $post_parent;
     298            if ($parent = $this->posts_processed[$post_parent]) {
     299                if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first.
     300                $post_parent = $parent[1]; // New ID of the parent;
     301            }
     302
     303            echo '<li>';
     304            printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
     305
     306            $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
     307
     308            $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'post_name', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'menu_order', 'post_type');
     309            $comment_post_ID = $post_id = wp_insert_post($postdata);
     310
     311            // Memorize old and new ID.
     312            if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
     313                $this->posts_processed[$post_ID][1] = $post_id; // New ID.
     314           
     315            // Add categories.
     316            if (count($categories) > 0) {
     317                $post_cats = array();
     318                foreach ($categories as $category) {
     319                    $cat_ID = (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$category'");
     320                    if ($cat_ID == 0) {
     321                        $cat_ID = wp_insert_category(array('cat_name' => $category));
     322                    }
     323                    $post_cats[] = $cat_ID;
    259324                }
    260             }
    261 
    262                 // Now for comments
    263                 preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
    264                 $comments = $comments[1];
    265                 $num_comments = 0;
    266                 if ( $comments) { foreach ($comments as $comment) {
    267                     $comment_author       = $this->get_tag( $comment, 'wp:comment_author');
    268                     $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
    269                     $comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
    270                     $comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
    271                     $comment_date         = $this->get_tag( $comment, 'wp:comment_date');
    272                     $comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
    273                     $comment_content      = $this->get_tag( $comment, 'wp:comment_content');
    274                     $comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
    275                     $comment_type         = $this->get_tag( $comment, 'wp:comment_type');
    276                     $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
    277 
    278                     if ( !comment_exists($comment_author, $comment_date) ) {
    279                         $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');
    280                         wp_insert_comment($commentdata);
    281                         $num_comments++;
    282                     }
    283                 } }
    284                 if ( $num_comments )
    285                     printf(' '.__('(%s comments)'), $num_comments);
    286 
    287                 // Now for post meta
    288                 preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
    289                 $postmeta = $postmeta[1];
    290                 if ( $postmeta) { foreach ($postmeta as $p) {
    291                     $key   = $this->get_tag( $p, 'wp:meta_key' );
    292                     $value = $this->get_tag( $p, 'wp:meta_value' );
    293                     add_post_meta( $post_id, $key, $value );
    294                 } }
    295 
    296             $index++;
    297         }
    298 
    299         echo '</ol>';
    300 
    301         wp_import_cleanup($this->id);
    302 
    303         echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
     325                wp_set_post_categories($post_id, $post_cats);
     326            }   
     327        }
     328
     329        // Now for comments
     330        preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
     331        $comments = $comments[1];
     332        $num_comments = 0;
     333        if ( $comments) { foreach ($comments as $comment) {
     334            $comment_author       = $this->get_tag( $comment, 'wp:comment_author');
     335            $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
     336            $comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
     337            $comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
     338            $comment_date         = $this->get_tag( $comment, 'wp:comment_date');
     339            $comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
     340            $comment_content      = $this->get_tag( $comment, 'wp:comment_content');
     341            $comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
     342            $comment_type         = $this->get_tag( $comment, 'wp:comment_type');
     343            $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
     344
     345            if ( !comment_exists($comment_author, $comment_date) ) {
     346                $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');
     347                wp_insert_comment($commentdata);
     348                $num_comments++;
     349            }
     350        } }
     351
     352        if ( $num_comments )
     353            printf(' '.__('(%s comments)'), $num_comments);
     354
     355        // Now for post meta
     356        preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
     357        $postmeta = $postmeta[1];
     358        if ( $postmeta) { foreach ($postmeta as $p) {
     359            $key   = $this->get_tag( $p, 'wp:meta_key' );
     360            $value = $this->get_tag( $p, 'wp:meta_value' );
     361            $value = stripslashes($value); // add_post_meta() will escape.
     362            add_post_meta( $post_id, $key, $value );
     363        } }
    304364    }
    305365
     
    326386                break;
    327387            case 1 :
     388                check_admin_referer('import-upload');
    328389                $this->select_authors();
    329390                break;
    330391            case 2:
     392                check_admin_referer('import-wordpress');
    331393                $this->import();
    332394                break;
Note: See TracChangeset for help on using the changeset viewer.