LLMS_Turnstile::validate( mixed $valid )
Validate the Turnstile captcha response.
Parameters Parameters
- $valid
-
(mixed) (Required) The current validation status.
Return Return
(mixed) True if validation fails, otherwise the original $valid value.
Source Source
File: includes/spam/class-llms-turnstile.php
public function validate( $valid ) {
if ( ! $this->is_enabled() ) {
return $valid;
}
// If $valid is already a truthy, return early since something else already encountered a validation issue.
if ( $valid ) {
return $valid;
}
// If we don't have a response to test, return an error and stop registration.
$captcha = llms_filter_input_sanitize_string( INPUT_POST, 'cf-turnstile-response' );
if ( ! $captcha ) {
if ( apply_filters( 'llms_enable_recaptcha_logs', false ) ) {
error_log( 'LifterLMS form blocked due to missing captcha' );
}
// Customize the error message displayed when a registration is blocked.
llms_add_notice( __( 'Blocked.', 'lifterlms' ), 'error' );
return true;
}
// Ok, try to validate the captcha.
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) && filter_var( $_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP ) ) {
// Use the CloudFlare IP if it exists.
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$data = array(
'secret' => $this->secret_key,
'response' => $captcha,
'remoteip' => $ip,
);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" .
"User-Agent: PHP Script\r\n",
'method' => 'POST',
'content' => http_build_query( $data ),
),
);
$stream = stream_context_create( $options );
$result = file_get_contents( $url_path, false, $stream );
$response = $result;
$response_keys = json_decode( $response, true );
if ( intval( $response_keys['success'] ) !== 1 ) {
if ( apply_filters( 'llms_enable_spam_logs', false ) ) {
error_log( 'LLMS_Turnstile verification failed: ' . print_r( $response, true ) );
}
llms_add_notice( __( 'Verification failed. Please try again.', 'lifterlms' ), 'error' );
return true;
}
// We're okay to proceed.
return $valid;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 9.0.0 | Introduced. |