';
echo '
'.__('Import Comments from CSV').'
';
}
function footer() {
echo '';
}
function unhtmlentities($string) { // From php.net for < 4.3 compat
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
function greet() {
echo ''.__('Hello. This importer will load the comments from a previous blog that you have dumped to a CSV file into Wordpress. Find your special CSV file to upload and click Import.').'
';
wp_import_upload_form("admin.php?import=commentscsv&step=1");
}
function import_comments() {
global $wpdb;
/*
open up our file and loop through it...
the CSV file is expected to have these columns
0 = Original Post ID
1 = Post Title
2 = Post Date
3 = Original Comment ID
4 = Comment Title
5 = Comment Date
6 = Comment Author
7 = Comment Email
8 = Comment Author Website
9 = Comment Text
*/
set_magic_quotes_runtime(0);
$row = 1;
$handle = fopen($this->file, "r");
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
echo " Reading row $row.
\n";
$data[1] = str_replace('\'', '\\\'', $data[1]);
echo " Comment ID $data[3] ($data[4]) for Post ID $data[0] ($data[1]).
\n";
// get the ID of the post this comment is for
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$data[1]'");
echo " Found new ID of $post_id.
\n";
//get the dates in the right format
echo " Comment from $data[6] ($data[7], $data[8]) on $data[5].
\n";
/*if ($post_date_gmt) {
$post_date_gmt = strtotime($post_date_gmt[1]);
} else {
// if we don't already have something from pubDate
preg_match('|(.*?)|is', $post, $post_date_gmt);
$post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
$post_date_gmt = str_replace('T', ' ', $post_date_gmt);
$post_date_gmt = strtotime($post_date_gmt);
}
$post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
$post_date = get_date_from_gmt( $post_date_gmt );
*/
// cleanup the comment text
// Clean up content
//$post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
$data[9] = str_replace('\'', '\\\'', $data[9]);
$data[6] = str_replace('\'', '\\\'', $data[6]);
$data[9] = str_replace('
', '
', $data[9]);
$data[9] = str_replace('
', '
', $data[9]);
echo " Comment:
$data[9]
\n";
// prepare the comment for insertion
// insert comment
//if ($row == 1) {
$ret_id = wp_insert_comment(array(
'comment_post_ID' => $post_id,
'comment_author' => $data[6],
'comment_author_email' => $data[7],
'comment_author_url' => $data[8],
'comment_author_IP' => '0.0.0.0',
'comment_date' => $data[5],
'comment_content' => $data[9],
'comment_approved' => 1,
'user_id' => 0)
);
//}
// increment counter
$row++;
}
fclose($handle);
// $post_author = 1;
// $post_status = 'publish';
// $this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
}
function import() {
$file = wp_import_handle_upload();
if ( isset($file['error']) ) {
echo $file['error'];
return;
}
$this->file = $file['file'];
$this->import_comments();
wp_import_cleanup($file['id']);
echo '';
printf(__('All done. Have fun!'), get_option('home'));
echo '
';
}
function dispatch() {
if (empty ($_GET['step']))
$step = 0;
else
$step = (int) $_GET['step'];
$this->header();
switch ($step) {
case 0 :
$this->greet();
break;
case 1 :
$this->import();
break;
}
$this->footer();
}
function CommentsCSV_Import() {
// Nothing.
}
}
$commentscsv_import = new CommentsCSV_Import();
register_importer('commentscsv', __('CommentsCSV'), __('Import posts from an CSV file of comments'), array ($commentscsv_import, 'dispatch'));
?>