forked from damieng/jekyll-blog-comments
/
export-blog-comments.php
38 lines (38 loc) · 3.76 KB
/
export-blog-comments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<html>
<title>Export WordPress blog comments</title>
<body>
<pre>
<?php error_reporting(-1);
require('wp-blog-header.php');
$comments=get_comments(array('status' => 'approve', 'type' => 'comment', 'orderby'=>'comment_post_ID'));
foreach($comments as $comment) :
$url=get_post_field('post_name', $comment->comment_post_ID);
$folder = dirname(__FILE__) . "/comments/{$url}";
if (!file_exists($folder)) {
echo "Creating folder {$folder}\n";
mkdir($folder, 0777, true) or die(print_r(error_get_last(),true));
}
$filename = "{$folder}/{$comment->comment_ID}.yml";
echo "Writing to {$filename}\n";
$file = fopen($filename, 'w') or die(print_r(error_get_last(),true));
fputs($file, "id: {$comment->comment_ID}\n");
fputs($file, "name: {$comment->comment_author}\n");
if ($comment->comment_author_email) {
fputs($file, "email: {$comment->comment_author_email}\n");
$avatarmd5 = md5($comment->comment_author_email);
fputs($file, "avatar: https://www.gravatar.com/avatar/{$avatarmd5}?r=pg&d=retro\n");
}
if ($comment->comment_author_url) {
fputs($file, "url: {$comment->comment_author_url}\n");
}
fputs($file, "date: {$comment->comment_date}\n");
$encodedComment = $comment->comment_content;
$encodedComment=str_replace("\\", "\\\\", $encodedComment);
$encodedComment=str_replace('"', '\"', $encodedComment);
fputs($file, "message: \"{$encodedComment}\"\n");
fclose($file);
endforeach; ?>
Complete!
</pre>
</body>
</html>