| 1 |
<?php |
| 2 |
/** |
| 3 |
* reCAPTCHA v2 Validation |
| 4 |
* |
| 5 |
* @package Contact Forms |
| 6 |
*/ |
| 7 |
|
| 8 |
// phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Server-side CAPTCHA validation requires POST data |
| 9 |
|
| 10 |
class AccuaForm_Validation_Captcha2b extends Validation { |
| 11 |
protected $message = 'Error: The reCAPTCHA response provided was incorrect. Please retry.'; |
| 12 |
protected $privateKey; |
| 13 |
|
| 14 |
public function isValid( $value ) { |
| 15 |
if ( ! isset( $_POST['g-recaptcha-response'] ) ) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
$response = stripslashes_deep( $_POST['g-recaptcha-response'] ); |
| 20 |
|
| 21 |
if ( '' === $response ) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
$verify_url = 'https://www.google.com/recaptcha/api/siteverify'; |
| 26 |
|
| 27 |
$verify_data = array( |
| 28 |
'secret' => $this->privateKey, |
| 29 |
'response' => $response, |
| 30 |
'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', |
| 31 |
); |
| 32 |
|
| 33 |
$response_obj = wp_remote_post( |
| 34 |
$verify_url, |
| 35 |
array( |
| 36 |
'body' => $verify_data, |
| 37 |
'timeout' => 20, |
| 38 |
) |
| 39 |
); |
| 40 |
|
| 41 |
if ( is_wp_error( $response_obj ) ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
$body = wp_remote_retrieve_body( $response_obj ); |
| 46 |
$result = json_decode( $body, true ); |
| 47 |
|
| 48 |
if ( ! empty( $result['success'] ) ) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
} |
| 55 |
|