/
captcha-wp-comment-form.php
164 lines (127 loc) · 4.27 KB
/
captcha-wp-comment-form.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
Plugin Name: Coinhive Captcha for WordPress Comments
Plugin URI: https://blanboom.org/2018/coinhive-captcha
Description: 发表评论时,使用 Coinhive 验证码进行验证
Version: 1.2
Author: Blanboom
Author URI: http://blanboom.org
License: GPL2
*/
/* 原插件信息:
* Plugin Name: Add reCAPTCHA to comment form
* Plugin URI: http://sitepoint.com
* Description: Add Google's reCAPTCHA to WordPress comment form
* Version: 1.0
* Author: Agbonghama Collins
* Author URI: http://w3guy.com
* License: GPL2
*/
class Captcha_Comment_Form {
/** @type string private key|public key */
private $public_key, $private_key;
/** @type string captcha errors */
private static $captcha_error;
/** class constructor */
public function __construct() {
$this->public_key = '1Egl6gZ5ifNkxgpHDFuwtT5eifXFoPYg';
$this->private_key = '!!!!!!!!!!!!!!!!!!!!!!YOUR_PRIVATE_KEY!!!!!!!!!!!!!!!!!!!!!!';
// adds the captcha to the comment form
add_action( 'comment_form', array( $this, 'captcha_display' ) );
// delete comment that fail the captcha challenge
add_action( 'wp_head', array( $this, 'delete_failed_captcha_comment' ) );
// authenticate the captcha answer
add_filter( 'preprocess_comment', array( $this, 'validate_captcha_field' ) );
// redirect location for comment
add_filter( 'comment_post_redirect', array( $this, 'redirect_fail_captcha_comment' ), 10, 2 );
}
/** Output the reCAPTCHA form field. */
public function captcha_display() {
if ( isset( $_GET['captcha'] ) && $_GET['captcha'] == 'failed' ) {
echo '<strong>错误</strong>: 验证失败,请单击“Verify me”,并等待进度条走完后再提交评论<br>';
}
echo <<<CAPTCHA_FORM
<style type='text/css'>#submit {
display: none;
}</style>
单击“Verify me”,验证后即可评论(<u><a href="https://blanboom.org/2018/coinhive-captcha" target="_blank">了解更多</a></u>)
<script src="https://authedmine.com/lib/captcha.min.js" async></script>
<script>
function captchaCallback(token) {
document.getElementById('submit').style.display='inline'
}
</script>
<div class="coinhive-captcha" data-key="$this->public_key" data-hashes="1024" data-callback="captchaCallback">
<em>验证码加载中......<br>
如果验证码无法加载,请关闭广告过滤软件,或打开浏览器中的 JavaScript</em>
</div>
CAPTCHA_FORM;
}
/**
* Add query string to the comment redirect location
*
* @param $location string location to redirect to after comment
* @param $comment object comment object
*
* @return string
*/
function redirect_fail_captcha_comment( $location, $comment ) {
if ( ! empty( self::$captcha_error ) ) {
// replace #comment- at the end of $location with #commentform
$args = array( 'comment-id' => $comment->comment_ID );
if ( self::$captcha_error == 'captcha_empty' ) {
$args['captcha'] = 'empty';
} elseif ( self::$captcha_error == 'challenge_failed' ) {
$args['captcha'] = 'failed';
}
$location = add_query_arg( $args, $location );
}
return $location;
}
/** Delete comment that fail the captcha test. */
function delete_failed_captcha_comment() {
if ( isset( $_GET['comment-id'] ) && ! empty( $_GET['comment-id'] ) ) {
wp_delete_comment( absint( $_GET['comment-id'] ) );
}
}
/**
* Verify the captcha answer
*
* @param $commentdata object comment object
*
* @return object
*/
public function validate_captcha_field( $commentdata ) {
if ( $this->recaptcha_response() == 'false' ) {
self::$captcha_error = 'challenge_failed';
}
return $commentdata;
}
/**
* Get the reCAPTCHA API response.
*
* @return string
*/
public function recaptcha_response() {
$post_data = [
'secret' => "$this->private_key", // <- Your secret key
'token' => $_POST['coinhive-captcha-token'],
'hashes' => 1024
];
$post_context = stream_context_create([
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_data)
]
]);
$url = 'https://api.coinhive.com/token/verify';
$response = json_decode(file_get_contents($url, false, $post_context));
if ($response && $response->success) {
return 'true';
} else {
return 'false';
}
}
}
new Captcha_Comment_Form();