| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cloudflare Turnstile Validation for Contact Forms |
| 4 |
* |
| 5 |
* @package Contact Forms |
| 6 |
* @subpackage Validation |
| 7 |
* @since 1.9.14 |
| 8 |
*/ |
| 9 |
|
| 10 |
// phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Server-side CAPTCHA validation requires POST data |
| 11 |
|
| 12 |
class AccuaForm_Validation_Turnstile extends Validation { |
| 13 |
protected $message = 'Please verify you are not a robot.'; |
| 14 |
protected $secretKey; |
| 15 |
|
| 16 |
public function isValid( $value ) { |
| 17 |
// Check if user is whitelisted (from Turnstile plugin). |
| 18 |
if ( function_exists( 'cfturnstile_whitelisted' ) && cfturnstile_whitelisted() ) { |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
// Check if widget is disabled via filter. |
| 23 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Hook from Simple Cloudflare Turnstile plugin |
| 24 |
if ( function_exists( 'apply_filters' ) && apply_filters( 'cfturnstile_widget_disable', false ) ) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
// Check for Turnstile response. |
| 29 |
if ( ! isset( $_POST['cf-turnstile-response'] ) || empty( $_POST['cf-turnstile-response'] ) ) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
$response = stripslashes_deep( $_POST['cf-turnstile-response'] ); |
| 34 |
|
| 35 |
if ( '' === $response ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
// Use Turnstile plugin's verification function if available. |
| 40 |
if ( function_exists( 'cfturnstile_check' ) ) { |
| 41 |
$check = cfturnstile_check( $response ); |
| 42 |
|
| 43 |
if ( $check && isset( $check['success'] ) && true === $check['success'] ) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
// Update error message if custom message is available. |
| 48 |
if ( function_exists( 'cfturnstile_failed_message' ) ) { |
| 49 |
$custom_message = cfturnstile_failed_message(); |
| 50 |
if ( ! empty( $custom_message ) ) { |
| 51 |
$this->message = $custom_message; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
// Fallback: Direct verification if Turnstile plugin is not available. |
| 59 |
if ( empty( $this->secretKey ) ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$verify_url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'; |
| 64 |
|
| 65 |
$verify_data = array( |
| 66 |
'secret' => $this->secretKey, |
| 67 |
'response' => $response, |
| 68 |
'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', |
| 69 |
); |
| 70 |
|
| 71 |
$response_obj = wp_remote_post( |
| 72 |
$verify_url, |
| 73 |
array( |
| 74 |
'body' => $verify_data, |
| 75 |
'timeout' => 20, |
| 76 |
) |
| 77 |
); |
| 78 |
|
| 79 |
if ( is_wp_error( $response_obj ) ) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
$body = wp_remote_retrieve_body( $response_obj ); |
| 84 |
$result = json_decode( $body, true ); |
| 85 |
|
| 86 |
if ( ! empty( $result['success'] ) ) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
} |
| 93 |
|