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