active-campaign-handler.php
4 years ago
forms-captcha.php
4 years ago
getresponse-handler.php
4 years ago
integration-base.php
4 years ago
mailchimp-handler.php
4 years ago
forms-captcha.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Integrations; |
| 4 | |
| 5 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 6 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 7 | use Jet_Form_Builder\Plugin; |
| 8 | |
| 9 | /** |
| 10 | * Captcha manager class |
| 11 | */ |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Define Forms_Captcha class |
| 20 | */ |
| 21 | class Forms_Captcha { |
| 22 | |
| 23 | const CAPTCHA_ACTION_PREFIX = 'jet_form_builder_captcha__'; |
| 24 | |
| 25 | public static $script_rendered = false; |
| 26 | |
| 27 | private $field_key = '_captcha_token'; |
| 28 | private $api = 'https://www.google.com/recaptcha/api/siteverify'; |
| 29 | private $defaults = array( |
| 30 | 'enabled' => false, |
| 31 | 'key' => '', |
| 32 | 'secret' => '', |
| 33 | ); |
| 34 | |
| 35 | private function api_front_url( $key ): string { |
| 36 | return esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) ); |
| 37 | } |
| 38 | |
| 39 | public function verify( $form_id = null, $is_ajax = false ) { |
| 40 | |
| 41 | $captcha = $this->get_data( $form_id ); |
| 42 | |
| 43 | if ( empty( $captcha['enabled'] ) ) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | $request = $this->sanitize_token_from_request(); |
| 48 | |
| 49 | if ( empty( $request[ $this->field_key ] ) ) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | $token = sanitize_text_field( $request[ $this->field_key ] ); |
| 54 | $response = wp_remote_post( |
| 55 | $this->api, |
| 56 | array( |
| 57 | 'body' => array( |
| 58 | 'secret' => $captcha['secret'], |
| 59 | 'response' => $token, |
| 60 | ), |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | $body = wp_remote_retrieve_body( $response ); |
| 65 | $body = json_decode( $body, true ); |
| 66 | |
| 67 | if ( ! $body || empty( $body['success'] ) ) { |
| 68 | return false; |
| 69 | } elseif ( ! empty( $body['action'] ) && ( self::CAPTCHA_ACTION_PREFIX . $form_id ) === $body['action'] ) { |
| 70 | |
| 71 | return $body['success']; |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | private function sanitize_token_from_request(): array { |
| 77 | $response = array(); |
| 78 | $request = jet_form_builder()->form_handler->request_handler->get_request(); |
| 79 | |
| 80 | if ( isset( $request[ $this->field_key ] ) ) { |
| 81 | $response[ $this->field_key ] = sanitize_text_field( $request[ $this->field_key ] ); |
| 82 | } |
| 83 | |
| 84 | return $response; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns captcha settings for passed form ID |
| 89 | * |
| 90 | * @param [type] $post_id [description] |
| 91 | * |
| 92 | * @return [type] [description] |
| 93 | */ |
| 94 | public function get_data( $form_id = null ) { |
| 95 | |
| 96 | if ( ! $form_id ) { |
| 97 | $form_id = get_the_ID(); |
| 98 | } |
| 99 | |
| 100 | $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id ); |
| 101 | |
| 102 | if ( ! $captcha || ! is_array( $captcha ) ) { |
| 103 | return $this->defaults; |
| 104 | } elseif ( isset( $captcha['use_global'] ) && $captcha['use_global'] ) { |
| 105 | return Tab_Handler_Manager::instance()->options( |
| 106 | 'captcha-tab', |
| 107 | array( 'enabled' => $captcha['enabled'] ) |
| 108 | ); |
| 109 | |
| 110 | } else { |
| 111 | return wp_parse_args( $captcha, $this->defaults ); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | public function render( $form_id ) { |
| 117 | |
| 118 | $captcha = $this->get_data( $form_id ); |
| 119 | |
| 120 | if ( empty( $captcha['enabled'] ) || empty( $captcha['key'] ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $key = esc_attr( $captcha['key'] ); |
| 125 | |
| 126 | wp_enqueue_script( |
| 127 | 'jet-form-builder-recaptcha', |
| 128 | $this->api_front_url( $key ), |
| 129 | array(), |
| 130 | jet_form_builder()->get_version(), |
| 131 | true |
| 132 | ); |
| 133 | |
| 134 | wp_enqueue_script( |
| 135 | 'jet-form-builder-recaptcha-handler', |
| 136 | jet_form_builder()->plugin_url( 'assets/js/re-captcha-v3.js' ), |
| 137 | array( 'jquery' ), |
| 138 | jet_form_builder()->get_version(), |
| 139 | true |
| 140 | ); |
| 141 | |
| 142 | $action_prefix = self::CAPTCHA_ACTION_PREFIX; |
| 143 | |
| 144 | wp_add_inline_script( |
| 145 | 'jet-form-builder-recaptcha-handler', |
| 146 | " |
| 147 | window.JetFormBuilderReCaptchaConfig = window.JetFormBuilderReCaptchaConfig || {}; |
| 148 | window.JetFormBuilderReCaptchaConfig[ $form_id ] = { key: '$key', action_prefix: '$action_prefix' }; |
| 149 | ", |
| 150 | 'before' |
| 151 | ); |
| 152 | |
| 153 | ?> |
| 154 | <input type="hidden" class="captcha-token" name="<?php echo esc_attr( $this->field_key ); ?>" value=""> |
| 155 | <?php |
| 156 | } |
| 157 | |
| 158 | } |
| 159 |