Skip to content

Commit 2a9ed5c

Browse files
authored
Create lifterlms-cloudflare-turnstile.php
1 parent ff1cbde commit 2a9ed5c

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* LifterLMS Turnstile Support for Checkout and Open Registration
4+
5+
* Plugin Name: LifterLMS Turnstile Support
6+
* Plugin URI: https://lifterlms.com/
7+
* Description: Adds Cloudflare Turnstile support to LifterLMS Checkout and Open Registration forms.
8+
* Version: 1.0
9+
* Author: LifterLMS
10+
* Author URI: https://lifterlms.com/
11+
* Text Domain: lifterlms-turnstile
12+
* License: GPLv3
13+
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
14+
* Requires at least: 5.9
15+
* Tested up to: 6.7
16+
* Requires PHP: 7.4
17+
*/
18+
19+
// Change here or put these into your wp-config.php file.
20+
// Keys are obtained when creating a new Widget in Cloudflare Turnstile.
21+
if ( ! defined( 'LLMS_TURNSTILE_SECRET_KEY' ) ) {
22+
define( 'LLMS_TURNSTILE_SECRET_KEY', 'secret-key' );
23+
}
24+
if ( ! defined( 'LLMS_TURNSTILE_SITE_KEY' ) ) {
25+
define( 'LLMS_TURNSTILE_SITE_KEY', 'site-key' );
26+
}
27+
28+
function llms_add_turnstile_script() {
29+
wp_enqueue_script('cloudflare-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js');
30+
}
31+
add_action( 'wp_head', 'llms_add_turnstile_script' );
32+
33+
function llms_add_turnstile_check() { ?>
34+
<div class="cf-turnstile" data-sitekey="<?php echo esc_attr( LLMS_TURNSTILE_SITE_KEY ); ?>"></div>
35+
<?php
36+
}
37+
add_action( 'llms_checkout_footer_before', 'llms_add_turnstile_check' );
38+
add_action( 'lifterlms_after_registration_fields', 'llms_add_turnstile_check' );
39+
40+
function llms_validate_turnstile( $valid ) {
41+
// If $valid is already a truthy, return early since something else already encountered a validation issue.
42+
if ( $valid ) {
43+
return $valid;
44+
}
45+
46+
// If we don't have a response to test, return an error and stop registration.
47+
$captcha = llms_filter_input_sanitize_string( INPUT_POST, 'cf-turnstile-response' );
48+
if ( ! $captcha ) {
49+
error_log( "checkout blocked due to missing captcha" );
50+
// Customize the error message displayed when a registration is blocked.
51+
llms_add_notice( __( 'Blocked.', 'my-text-domain' ), 'error' );
52+
return true;
53+
}
54+
55+
// Ok, try to validate the captcha.
56+
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) && filter_var( $_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP ) ) {
57+
// Use the CloudFlare IP if it exists.
58+
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
59+
} else {
60+
$ip = $_SERVER['REMOTE_ADDR'];
61+
}
62+
$url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
63+
$data = array( 'secret' => LLMS_TURNSTILE_SECRET_KEY, 'response' => $captcha, 'remoteip' => $ip );
64+
$options = array(
65+
'http' => array(
66+
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
67+
"User-Agent: PHP Script\r\n",
68+
'method' => 'POST',
69+
'content' => http_build_query( $data )
70+
)
71+
);
72+
$stream = stream_context_create( $options );
73+
$result = file_get_contents( $url_path, false, $stream );
74+
$response = $result;
75+
$response_keys = json_decode( $response, true );
76+
77+
if ( intval( $response_keys["success"] ) !== 1 ) {
78+
// Not valid. Block them.
79+
// Customize the error message displayed when a registration is blocked.
80+
llms_add_notice( __( 'Blocked.', 'my-text-domain' ), 'error' );
81+
return true;
82+
}
83+
84+
// We're okay to proceed.
85+
return $valid;
86+
}
87+
add_filter( 'llms_before_checkout_validation', 'llms_validate_turnstile' );
88+
add_filter( 'llms_before_registration_validation', 'llms_validate_turnstile' );

0 commit comments

Comments
 (0)