ProtectedRoute.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Routes\Captcha; |
| 4 | |
| 5 | /** |
| 6 | * Note: Functionality forked from `give_email_access_login()`. |
| 7 | * |
| 8 | * @since 2.10.2 |
| 9 | */ |
| 10 | trait ProtectedRoute { |
| 11 | |
| 12 | /** |
| 13 | * @since 2.10.2 |
| 14 | * |
| 15 | * @return bool |
| 16 | */ |
| 17 | public function isCaptchaEnabled() { |
| 18 | $recaptcha_key = give_get_option( 'recaptcha_key' ); |
| 19 | $recaptcha_secret = give_get_option( 'recaptcha_secret' ); |
| 20 | $recaptcha_enabled = ( give_is_setting_enabled( give_get_option( 'enable_recaptcha' ) ) ) && ! empty( $recaptcha_key ) && ! empty( $recaptcha_secret ) ? true : false; |
| 21 | return $recaptcha_enabled; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @since 2.10.2 |
| 26 | * |
| 27 | * @param string $value |
| 28 | * @param WP_REST_Request $request |
| 29 | * @param string $param The name of the parameter. |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function validateRecaptcha( $value, $request, $param ) { |
| 34 | |
| 35 | if ( ! $this->isCaptchaEnabled() ) { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | if ( ! $value ) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | $request = wp_remote_post( |
| 44 | 'https://www.google.com/recaptcha/api/siteverify', |
| 45 | [ |
| 46 | 'body' => [ |
| 47 | 'secret' => give_get_option( 'recaptcha_secret' ), |
| 48 | 'response' => $value, |
| 49 | 'remoteip' => $request->get_param( 'give_ip' ), |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | if ( is_wp_error( $request ) ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if ( 200 !== wp_remote_retrieve_response_code( $request ) ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | $response = json_decode( $request['body'], true ); |
| 63 | |
| 64 | return ! ! $response['success']; |
| 65 | } |
| 66 | } |
| 67 |