Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mt8
Last active July 29, 2019 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mt8/a74cd39aaf72c29c769c to your computer and use it in GitHub Desktop.
Save mt8/a74cd39aaf72c29c769c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FC2ブログからWordPressにインポートしたらコメント文が変になった人が自己責任で実行するやつ</title>
<link rel="stylesheet" href="wp-admin/css/install.css" type="text/css" />
</head>
<body>
<h1>FC2ブログからWordPressにインポートしたらコメント文が変になった人が自己責任で実行するやつ</h1>
<h2>SECRET: *とか、PASS: *******とかが出ちゃう人へ。。。</h2>
<hr />
<h3>このスクリプトがやってくれること</h3>
<ul>
<li>非公開コメントは、WordPressの「承認待ち」にします。</li>
<li>SECRET: *とか、PASS: ******を消します。</li>
</ul>
<p><strong>※データベースのバックアップをしてから実行して下さい。</strong></p>
<p><strong>※このスクリプトで発生したいかなるトラブルも作成者は責任を負いません。自己責任で実行して下さい。</strong></p>
<form method="POST">
<input type="submit" value="実行する" />
<input type="hidden" name="go" value="1" />
</form>
<?php
if ($_POST['go'] == '1') {
fc2_comment_clean_4_wordpress();
}
function fc2_comment_clean_4_wordpress() {
//set_time_limit(0);
require( dirname(__FILE__) . '/wp-blog-header.php' );
$replaced = 0;
$regx_check_open = "/SECRET: 0/";
$regx_replace_open = array(
"/SECRET: 0/",
"/PASS: [a-f0-9]{128}/",
);
$regx_check_secret = "/SECRET: 1/";
$regx_replace_secret = array(
"/SECRET: 1/",
"/PASS: [a-f0-9]{128}/",
);
global $wpdb;
$select_sql = "SELECT comment_ID, comment_content FROM $wpdb->comments";
$results = $wpdb->get_results($select_sql);
if (!empty($results)) {
$regx_pattern = null;
$comment_approved = null;
foreach ($results as $r) {
if (!preg_match($regx_check_secret, $r->comment_content) && !preg_match($regx_check_open, $r->comment_content)) {
continue;
}
if (preg_match($regx_check_secret, $r->comment_content)) {
$comment_approved = '0';
$regx_pattern = $regx_replace_secret;
} else {
$comment_approved = '1';
$regx_pattern = $regx_replace_open;
}
$comment_content = preg_replace($regx_pattern, '', $r->comment_content);
$update_sql = "UPDATE $wpdb->comments SET comment_approved = %s, comment_content = %s WHERE comment_ID = %d";
$wpdb->query($wpdb->prepare($update_sql, $comment_approved, $comment_content, $r->comment_ID));
$replaced++;
?>
<div>
<p>置換しました:</p>
<p><strong>(置換前):</strong><?php echo esc_html($r->comment_content) ?></p>
<p><strong>(置換後):</strong><?php echo esc_html($comment_content) ?></p>
<?php if ($comment_approved == '0') : ?>
<p><strong>このコメントは元は非公開だったので「承認待ち」にしておきました。</strong></p>
<?php endif; ?>
</div>
<hr />
<?php
}
}
?>
<p><strong><?php echo number_format($replaced) ?>件</strong>置換しました。このファイルは消しておいて下さい。</p>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment