Preserve page hierarchy. Props takayukister. fixes #4025
git-svn-id: https://develop.svn.wordpress.org/trunk@5245 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f8680b6c3a
commit
e58cfd6981
@ -3,6 +3,8 @@
|
||||
class WP_Import {
|
||||
|
||||
var $posts = array ();
|
||||
var $posts_processed = array ();
|
||||
// Array of arrays. [[0] => XML fragment, [1] => New post ID]
|
||||
var $file;
|
||||
var $id;
|
||||
var $mtnames = array ();
|
||||
@ -86,6 +88,12 @@ class WP_Import {
|
||||
$importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
|
||||
preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
|
||||
$this->posts = $this->posts[1];
|
||||
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;
|
||||
}
|
||||
preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
|
||||
$this->categories = $this->categories[1];
|
||||
}
|
||||
@ -208,10 +216,25 @@ class WP_Import {
|
||||
}
|
||||
|
||||
function process_posts() {
|
||||
global $wpdb;
|
||||
$i = -1;
|
||||
echo '<ol>';
|
||||
foreach ($this->posts as $post) {
|
||||
|
||||
foreach ($this->posts as $post)
|
||||
$this->process_post($post);
|
||||
|
||||
echo '</ol>';
|
||||
|
||||
wp_import_cleanup($this->id);
|
||||
|
||||
echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
|
||||
}
|
||||
|
||||
function process_post($post) {
|
||||
global $wpdb;
|
||||
|
||||
$post_ID = (int) $this->get_tag( $post, 'wp:post_id' );
|
||||
if ( $post_ID && !empty($this->posts_processed[$post_ID][1]) ) // Processed already
|
||||
return 0;
|
||||
|
||||
// There are only ever one of these
|
||||
$post_title = $this->get_tag( $post, 'title' );
|
||||
@ -246,6 +269,14 @@ class WP_Import {
|
||||
echo '<li>';
|
||||
printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
|
||||
} else {
|
||||
|
||||
// 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.
|
||||
$post_parent = $parent[1]; // New ID of the parent;
|
||||
}
|
||||
|
||||
echo '<li>';
|
||||
printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
|
||||
|
||||
@ -253,10 +284,15 @@ 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);
|
||||
|
||||
// Memorize old and new ID.
|
||||
if ( $post_id && $post_ID && $this->posts_processed[$post_ID] )
|
||||
$this->posts_processed[$post_ID][1] = $post_id; // New ID.
|
||||
|
||||
// Add categories.
|
||||
if (0 != count($categories)) {
|
||||
if ( 0 != count($categories) )
|
||||
wp_create_categories($categories, $post_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Now for comments
|
||||
@ -281,6 +317,7 @@ class WP_Import {
|
||||
$num_comments++;
|
||||
}
|
||||
} }
|
||||
|
||||
if ( $num_comments )
|
||||
printf(' '.__('(%s comments)'), $num_comments);
|
||||
|
||||
@ -292,15 +329,6 @@ class WP_Import {
|
||||
$value = $this->get_tag( $p, 'wp:meta_value' );
|
||||
add_post_meta( $post_id, $key, $value );
|
||||
} }
|
||||
|
||||
$index++;
|
||||
}
|
||||
|
||||
echo '</ol>';
|
||||
|
||||
wp_import_cleanup($this->id);
|
||||
|
||||
echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
|
||||
}
|
||||
|
||||
function import() {
|
||||
|
Loading…
Reference in New Issue
Block a user