Admin
2 years ago
Assets
2 years ago
Pages
3 years ago
PostTypes
2 years ago
Shortcodes
2 years ago
Sitemap
2 years ago
Templates
2 years ago
Users
3 years ago
ActionsService.php
3 years ago
CompatibilityService.php
2 years ago
HealthService.php
2 years ago
LineItemStateService.php
2 years ago
PluginService.php
3 years ago
PluginServiceProvider.php
2 years ago
RecaptchaValidationService.php
2 years ago
StateService.php
2 years ago
ThemeService.php
2 years ago
ThemeServiceProvider.php
3 years ago
TranslationsServiceProvider.php
3 years ago
RecaptchaValidationService.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress; |
| 4 | |
| 5 | /** |
| 6 | * Recaptcha Validation Service. |
| 7 | */ |
| 8 | class RecaptchaValidationService { |
| 9 | /** |
| 10 | * Is recaptcha Enabled? |
| 11 | * |
| 12 | * @return boolean |
| 13 | */ |
| 14 | public function isEnabled() { |
| 15 | return (bool) get_option( 'surecart_recaptcha_enabled', false ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Get reCaptcha min score. |
| 20 | * |
| 21 | * @return string |
| 22 | */ |
| 23 | public function getMinScore() { |
| 24 | return apply_filters( 'surecart_recaptcha_min_score', 0.5 ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get reCaptcha secret key. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function getSecretKey() { |
| 33 | return get_option( 'surecart_recaptcha_secret_key', '' ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get reCaptcha secret key. |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public function getSiteKey() { |
| 42 | return get_option( 'surecart_recaptcha_site_key', '' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get reCaptcha response data. |
| 47 | * |
| 48 | * @param string $grecaptcha recaptcha token. |
| 49 | * @return object |
| 50 | */ |
| 51 | public function makeRequest( $grecaptcha ) { |
| 52 | $recaptcha_verify = wp_remote_post( |
| 53 | 'https://www.google.com/recaptcha/api/siteverify', |
| 54 | [ |
| 55 | 'method' => 'POST', |
| 56 | 'body' => [ |
| 57 | 'secret' => $this->getSecretKey(), |
| 58 | 'response' => $grecaptcha, |
| 59 | ], |
| 60 | ] |
| 61 | ); |
| 62 | return json_decode( wp_remote_retrieve_body( $recaptcha_verify ) ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check is validate token. |
| 67 | * |
| 68 | * @param object $verify_data recaptcha token. |
| 69 | * @return bool |
| 70 | */ |
| 71 | public function isTokenValid( $verify_data ) { |
| 72 | if ( empty( $verify_data->success ) ) { |
| 73 | return false; |
| 74 | } |
| 75 | return (bool) $verify_data->success; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Check is validate score. |
| 80 | * |
| 81 | * @param object $verify_data recaptcha token. |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function isValidScore( $verify_data ) { |
| 85 | return $verify_data->score && $verify_data->score >= $this->getMinScore(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Validate the recaptcha. |
| 90 | * |
| 91 | * @param string $grecaptcha_token The recaptcha token |
| 92 | * |
| 93 | * @return true|\WP_Error |
| 94 | */ |
| 95 | public function validate( $grecaptcha_token ) { |
| 96 | $response = $this->makeRequest( $grecaptcha_token ); |
| 97 | |
| 98 | // recaptcha failed. |
| 99 | if ( ! $this->isTokenValid( $response ) ) { |
| 100 | return new \WP_Error( 'invalid_recaptcha', __( 'Invalid recaptcha check. Please try again.', 'surecart' ) ); |
| 101 | } |
| 102 | |
| 103 | // score is not valid. |
| 104 | if ( apply_filters( 'surecart_recaptcha_needed_validation_score', false ) && ! $this->isValidScore( $response ) ) { |
| 105 | return new \WP_Error( 'invalid_recaptcha_score', __( 'Invalid recaptcha score. Please try again.', 'surecart' ) ); |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 |