Skip to content

feat: Adding Cloudflare Turnstile support #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3039,8 +3039,8 @@ function checkVaptchaAction($user)
function turnstile_init() {
$site_key = iro_opt('turnstile_site_key');
echo '<div class="cf-turnstile" data-sitekey="' . esc_attr($site_key) . '" data-theme="' . (iro_opt('turnstile_theme') ?: 'light') . '"></div>';
echo '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>';
echo '<style>.cf-turnstile {margin: 10px 0 20px;}</style>';
echo '<script class="cf-turnstile" src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>';
echo '<style>.cf-turnstile {margin: 0 0 0 -15px;}</style>';
}
add_action('login_form', 'turnstile_init');
add_action('register_form', 'turnstile_init');
Expand All @@ -3053,7 +3053,7 @@ function verify_turnstile($user, $username = '', $password = '') {
}

if (empty($_POST['cf-turnstile-response'])) {
return new WP_Error('invalid_turnstile', __('<strong>错误</strong>: 请完成人机验证', 'sakurairo'));
return new WP_Error('invalid_turnstile', '<strong>错误</strong>: 请完成人机验证', 'sakurairo');
}

$secret_key = iro_opt('turnstile_secret_key');
Expand All @@ -3069,13 +3069,13 @@ function verify_turnstile($user, $username = '', $password = '') {
]);

if (is_wp_error($response)) {
return new WP_Error('turnstile_error', __('<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo'));
return new WP_Error('turnstile_error', '<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo');
}

$body = json_decode(wp_remote_retrieve_body($response), true);

if (!$body['success']) {
return new WP_Error('invalid_turnstile', __('<strong>错误</strong>: 人机验证失败', 'sakurairo'));
return new WP_Error('invalid_turnstile','<strong>错误</strong>: 人机验证失败', 'sakurairo');
}

return $user;
Expand All @@ -3084,7 +3084,7 @@ function verify_turnstile($user, $username = '', $password = '') {

function turnstile_lostpassword_check($errors) {
if (empty($_POST['cf-turnstile-response'])) {
$errors->add('invalid_turnstile', __('<strong>错误</strong>: 请完成人机验证', 'sakurairo'));
$errors->add('invalid_turnstile', '<strong>错误</strong>: 请完成人机验证', 'sakurairo');
return $errors;
}

Expand All @@ -3101,14 +3101,14 @@ function turnstile_lostpassword_check($errors) {
]);

if (is_wp_error($response)) {
$errors->add('turnstile_error', __('<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo'));
$errors->add('turnstile_error', '<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo');
return $errors;
}

$body = json_decode(wp_remote_retrieve_body($response), true);

if (!$body['success']) {
$errors->add('invalid_turnstile', __('<strong>错误</strong>: 人机验证失败', 'sakurairo'));
$errors->add('invalid_turnstile', '<strong>错误</strong>: 人机验证失败', 'sakurairo');
}

return $errors;
Expand All @@ -3117,7 +3117,7 @@ function turnstile_lostpassword_check($errors) {

function turnstile_registration_check($errors, $sanitized_user_login, $user_email) {
if (empty($_POST['cf-turnstile-response'])) {
$errors->add('invalid_turnstile', __('<strong>错误</strong>: 请完成人机验证', 'sakurairo'));
$errors->add('invalid_turnstile', '<strong>错误</strong>: 请完成人机验证', 'sakurairo');
return $errors;
}

Expand All @@ -3134,14 +3134,14 @@ function turnstile_registration_check($errors, $sanitized_user_login, $user_emai
]);

if (is_wp_error($response)) {
$errors->add('turnstile_error', __('<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo'));
$errors->add('turnstile_error', '<strong>错误</strong>: 无法验证人机验证,请稍后再试', 'sakurairo');
return $errors;
}

$body = json_decode(wp_remote_retrieve_body($response), true);

if (!$body['success']) {
$errors->add('invalid_turnstile', __('<strong>错误</strong>: 人机验证失败', 'sakurairo'));
$errors->add('invalid_turnstile', '<strong>错误</strong>: 人机验证失败', 'sakurairo');
}

return $errors;
Expand Down
Binary file modified opt/languages/ja.mo
Binary file not shown.
Loading