From 3d270053f2b0d3b063eb8f18a3803cbe282f5962 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 1 Dec 2009 02:12:42 +0000 Subject: [PATCH] Preserve parentage when importing comments. Props joostdevalk. fixes #8019 git-svn-id: https://develop.svn.wordpress.org/trunk@12301 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/import/wordpress.php | 47 +++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/wp-admin/import/wordpress.php b/wp-admin/import/wordpress.php index 9645ac817a..83c90acff1 100644 --- a/wp-admin/import/wordpress.php +++ b/wp-admin/import/wordpress.php @@ -559,26 +559,35 @@ class WP_Import { preg_match_all('|(.*?)|is', $post, $comments); $comments = $comments[1]; $num_comments = 0; - if ( $comments) { foreach ($comments as $comment) { - $comment_author = $this->get_tag( $comment, 'wp:comment_author'); - $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email'); - $comment_author_IP = $this->get_tag( $comment, 'wp:comment_author_IP'); - $comment_author_url = $this->get_tag( $comment, 'wp:comment_author_url'); - $comment_date = $this->get_tag( $comment, 'wp:comment_date'); - $comment_date_gmt = $this->get_tag( $comment, 'wp:comment_date_gmt'); - $comment_content = $this->get_tag( $comment, 'wp:comment_content'); - $comment_approved = $this->get_tag( $comment, 'wp:comment_approved'); - $comment_type = $this->get_tag( $comment, 'wp:comment_type'); - $comment_parent = $this->get_tag( $comment, 'wp:comment_parent'); - - // if this is a new post we can skip the comment_exists() check - if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) { - $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'); - $commentdata = wp_filter_comment($commentdata); - wp_insert_comment($commentdata); - $num_comments++; + $inserted_comments = array(); + if ( $comments) { + foreach ($comments as $comment) { + $comment_id = $this->get_tag( $comment, 'wp:comment_id'); + $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; + $newcomments[$comment_id]['comment_author'] = $this->get_tag( $comment, 'wp:comment_author'); + $newcomments[$comment_id]['comment_author_email'] = $this->get_tag( $comment, 'wp:comment_author_email'); + $newcomments[$comment_id]['comment_author_IP'] = $this->get_tag( $comment, 'wp:comment_author_IP'); + $newcomments[$comment_id]['comment_author_url'] = $this->get_tag( $comment, 'wp:comment_author_url'); + $newcomments[$comment_id]['comment_date'] = $this->get_tag( $comment, 'wp:comment_date'); + $newcomments[$comment_id]['comment_date_gmt'] = $this->get_tag( $comment, 'wp:comment_date_gmt'); + $newcomments[$comment_id]['comment_content'] = $this->get_tag( $comment, 'wp:comment_content'); + $newcomments[$comment_id]['comment_approved'] = $this->get_tag( $comment, 'wp:comment_approved'); + $newcomments[$comment_id]['comment_type'] = $this->get_tag( $comment, 'wp:comment_type'); + $newcomments[$comment_id]['comment_parent'] = $this->get_tag( $comment, 'wp:comment_parent'); } - } } + // Sort by comment ID, to make sure comment parents exist (if there at all) + ksort($newcomments); + foreach ($newcomments as $key => $comment) { + // if this is a new post we can skip the comment_exists() check + if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) { + if (isset($inserted_comments[$comment['comment_parent']])) + $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; + $comment = wp_filter_comment($comment); + $inserted_comments[$key] = wp_insert_comment($comment); + $num_comments++; + } + } + } if ( $num_comments ) printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);