diff options
| author | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-10-12 16:21:15 +0000 |
|---|---|---|
| committer | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-10-12 16:21:15 +0000 |
| commit | 3a4570b0fc8b3d6339bef71d17d7701554e0bbf7 (patch) | |
| tree | 2a06e5261263c68d8afd95a6328879dc289cb909 /wp-admin/import/wordpress.php | |
| parent | b83c34a7010faee0223f6037025c350da12e05e6 (diff) | |
| download | wordpress-mu-3a4570b0fc8b3d6339bef71d17d7701554e0bbf7.tar.gz wordpress-mu-3a4570b0fc8b3d6339bef71d17d7701554e0bbf7.tar.xz wordpress-mu-3a4570b0fc8b3d6339bef71d17d7701554e0bbf7.zip | |
Merge with WP 2.3 - testing use only!
Move pluggable functions out of wpmu-functions and into pluggable.php, fixes #439
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@1069 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-admin/import/wordpress.php')
| -rw-r--r-- | wp-admin/import/wordpress.php | 176 |
1 files changed, 142 insertions, 34 deletions
diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php index a56e30a..f39e9bb 100644 --- a/wp-admin/import/wordpress.php +++ b/wp-admin/import/wordpress.php @@ -69,39 +69,52 @@ class WP_Import { function get_entries() { set_magic_quotes_runtime(0); - $importdata = array_map('rtrim', file($this->file)); // Read the file into an array $this->posts = array(); $this->categories = array(); + $this->tags = array(); $num = 0; $doing_entry = false; - foreach ($importdata as $importline) { - if ( false !== strpos($importline, '<wp:category>') ) { - preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); - $this->categories[] = $category[1]; - continue; - } - if ( false !== strpos($importline, '<item>') ) { - $this->posts[$num] = ''; - $doing_entry = true; - continue; - } - if ( false !== strpos($importline, '</item>') ) { - $num++; - $doing_entry = false; - continue; - } - if ( $doing_entry ) { - $this->posts[$num] .= $importline . "\n"; + + $fp = fopen($this->file, 'r'); + if ($fp) { + while ( !feof($fp) ) { + $importline = rtrim(fgets($fp)); + + if ( false !== strpos($importline, '<wp:category>') ) { + preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); + $this->categories[] = $category[1]; + continue; + } + if ( false !== strpos($importline, '<wp:tag>') ) { + preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag); + $this->tags[] = $tag[1]; + continue; + } + if ( false !== strpos($importline, '<item>') ) { + $this->posts[$num] = ''; + $doing_entry = true; + continue; + } + if ( false !== strpos($importline, '</item>') ) { + $num++; + $doing_entry = false; + continue; + } + if ( $doing_entry ) { + $this->posts[$num] .= $importline . "\n"; + } } - } - foreach ($this->posts as $post) { - $post_ID = (int) $this->get_tag( $post, 'wp:post_id' ); - if ($post_ID) { - $this->posts_processed[$post_ID][0] = &$post; - $this->posts_processed[$post_ID][1] = 0; + foreach ($this->posts as $post) { + $post_ID = (int) $this->get_tag( $post, 'wp:post_id' ); + if ($post_ID) { + $this->posts_processed[$post_ID][0] = &$post; + $this->posts_processed[$post_ID][1] = 0; + } } + + fclose($fp); } } @@ -128,6 +141,32 @@ class WP_Import { return $authors; } + function get_authors_from_post() { + $formnames = array (); + $selectnames = array (); + + foreach ($_POST['user'] as $key => $line) { + $newname = trim(stripslashes($line)); + if ($newname == '') + $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form. + array_push($formnames, "$newname"); + } // $formnames is the array with the form entered names + + foreach ($_POST['userselect'] as $user => $key) { + $selected = trim(stripslashes($key)); + array_push($selectnames, "$selected"); + } + + $count = count($formnames); + for ($i = 0; $i < $count; $i ++) { + if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form + array_push($this->newauthornames, "$selectnames[$i]"); + } else { + array_push($this->newauthornames, "$formnames[$i]"); + } + } + } + function wp_authors_form() { ?> <h2><?php _e('Assign Authors'); ?></h2> @@ -147,7 +186,7 @@ class WP_Import { echo '</li>'; } - echo '<input type="submit" value="Submit">'.'<br/>'; + echo '<input type="submit" value="Submit">'.'<br />'; echo '</form>'; echo '</ol>'; @@ -170,7 +209,7 @@ class WP_Import { function process_categories() { global $wpdb; - $cat_names = (array) $wpdb->get_col("SELECT cat_name FROM $wpdb->categories"); + $cat_names = (array) get_terms('category', 'fields=names'); while ( $c = array_shift($this->categories) ) { $cat_name = trim($this->get_tag( $c, 'wp:cat_name' )); @@ -196,12 +235,36 @@ class WP_Import { } } + function process_tags() { + global $wpdb; + + $tag_names = (array) get_terms('post_tag', 'fields=names'); + + while ( $c = array_shift($this->tags) ) { + $tag_name = trim($this->get_tag( $c, 'wp:tag_name' )); + + // If the category exists we leave it alone + if ( in_array($tag_name, $tag_names) ) + continue; + + $slug = $this->get_tag( $c, 'wp:tag_slug' ); + $description = $this->get_tag( $c, 'wp:tag_description' ); + + $tagarr = compact('slug', 'description'); + + $tag_ID = wp_insert_term($tag_name, 'post_tag', $tagarr); + } + } + function process_posts() { $i = -1; echo '<ol>'; - foreach ($this->posts as $post) - $this->process_post($post); + foreach ($this->posts as $post) { + $result = $this->process_post($post); + if ( is_wp_error( $result ) ) + return $result; + } echo '</ol>'; @@ -236,6 +299,15 @@ class WP_Import { $post_content = str_replace('<br>', '<br />', $post_content); $post_content = str_replace('<hr>', '<hr />', $post_content); + preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags); + $tags = $tags[1]; + + $tag_index = 0; + foreach ($tags as $tag) { + $tags[$tag_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $tag))); + $tag_index++; + } + preg_match_all('|<category>(.*?)</category>|is', $post, $categories); $categories = $categories[1]; @@ -253,7 +325,11 @@ class WP_Import { // If it has parent, process parent first. $post_parent = (int) $post_parent; if ($parent = $this->posts_processed[$post_parent]) { - if (!$parent[1]) $this->process_post($parent[0]); // If not yet, process the parent first. + if (!$parent[1]) { + $result = $this->process_post($parent[0]); // If not yet, process the parent first. + if ( is_wp_error( $result ) ) + return $result; + } $post_parent = $parent[1]; // New ID of the parent; } @@ -264,6 +340,8 @@ class WP_Import { $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'); $comment_post_ID = $post_id = wp_insert_post($postdata); + if ( is_wp_error( $post_id ) ) + return $post_id; // Memorize old and new ID. if ( $post_id && $post_ID && $this->posts_processed[$post_ID] ) @@ -273,14 +351,38 @@ class WP_Import { if (count($categories) > 0) { $post_cats = array(); foreach ($categories as $category) { - $cat_ID = (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name = '$category'"); + $slug = sanitize_term_field('slug', $category, 0, 'category', 'db'); + $cat = get_term_by('slug', $slug, 'category'); + $cat_ID = 0; + if ( ! empty($cat) ) + $cat_ID = $cat->term_id; if ($cat_ID == 0) { + $category = $wpdb->escape($category); $cat_ID = wp_insert_category(array('cat_name' => $category)); } $post_cats[] = $cat_ID; } wp_set_post_categories($post_id, $post_cats); - } + } + + // Add tags. + if (count($tags) > 0) { + $post_tags = array(); + foreach ($tags as $tag) { + $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); + $tag_obj = get_term_by('slug', $slug, 'post_tag'); + $tag_id = 0; + if ( ! empty($tag_obj) ) + $tag_id = $tag_obj->term_id; + if ( $tag_id == 0 ) { + $tag = $wpdb->escape($tag); + $tag_id = wp_insert_term($tag, 'post_tag'); + $tag_id = $tag_id['term_id']; + } + $post_tags[] = $tag_id; + } + wp_set_post_tags($post_id, $post_tags); + } } // Now for comments @@ -324,9 +426,13 @@ class WP_Import { $this->id = (int) $_GET['id']; $this->file = get_attached_file($this->id); + $this->get_authors_from_post(); $this->get_entries(); $this->process_categories(); - $this->process_posts(); + $this->process_tags(); + $result = $this->process_posts(); + if ( is_wp_error( $result ) ) + return $result; } function dispatch() { @@ -346,7 +452,9 @@ class WP_Import { break; case 2: check_admin_referer('import-wordpress'); - $this->import(); + $result = $this->import(); + if ( is_wp_error( $result ) ) + echo $result->get_error_message(); break; } $this->footer(); |
