PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / integrations / forms-captcha.php
jetformbuilder / includes / integrations Last commit date
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