PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.1
JetFormBuilder — Dynamic Blocks Form Builder v3.0.1
3.6.4.1 3.6.4 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
163 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 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 'threshold' => 0.5,
34 );
35
36 public function __construct() {
37 add_filter( 'jet-form-builder/request-handler/request', array( $this, 'handle_request' ) );
38 }
39
40 /**
41 * @param $request
42 *
43 * @return mixed
44 * @throws Request_Exception
45 */
46 public function handle_request( $request ) {
47 $this->verify( $request );
48
49 return $request;
50 }
51
52 private function api_front_url( $key ): string {
53 return esc_url_raw( sprintf( 'https://www.google.com/recaptcha/api.js?render=%s', $key ) );
54 }
55
56 /**
57 * @param $request
58 *
59 * @throws Request_Exception
60 */
61 protected function verify( $request ) {
62 $form_id = jet_fb_handler()->get_form_id();
63 $captcha = $this->get_data( $form_id );
64
65 if ( empty( $captcha['enabled'] ) ) {
66 return;
67 }
68
69 if ( empty( $request[ $this->field_key ] ) ) {
70 return;
71 }
72
73 $token = sanitize_text_field( $request[ $this->field_key ] );
74 $response = wp_remote_post(
75 $this->api,
76 array(
77 'body' => array(
78 'secret' => $captcha['secret'],
79 'response' => $token,
80 ),
81 )
82 );
83
84 $body = wp_remote_retrieve_body( $response );
85 $body = json_decode( $body, true );
86
87 $action = $body['action'] ?? '';
88 $score = $body['score'] ?? 0;
89
90 if ( ( self::PREFIX . $form_id ) === $action && $score > $captcha['threshold'] ) {
91 return;
92 }
93
94 throw new Request_Exception( 'captcha_failed', $body, $response );
95 }
96
97 /**
98 * Returns captcha settings for passed form ID
99 *
100 * @param [type] $post_id [description]
101 *
102 * @return [type] [description]
103 */
104 public function get_data( $form_id = null ) {
105 $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id );
106
107 if ( ! $captcha || ! is_array( $captcha ) ) {
108 return $this->defaults;
109 } elseif ( isset( $captcha['use_global'] ) && $captcha['use_global'] ) {
110 return Tab_Handler_Manager::instance()->options(
111 'captcha-tab',
112 array( 'enabled' => $captcha['enabled'] )
113 );
114
115 } else {
116 return wp_parse_args( $captcha, $this->defaults );
117 }
118 }
119
120 public function render( $form_id ) {
121
122 $captcha = $this->get_data( $form_id );
123
124 if ( empty( $captcha['enabled'] ) || empty( $captcha['key'] ) ) {
125 return;
126 }
127
128 $key = esc_attr( $captcha['key'] );
129
130 wp_enqueue_script(
131 'jet-form-builder-recaptcha',
132 $this->api_front_url( $key ),
133 array(),
134 jet_form_builder()->get_version(),
135 true
136 );
137
138 wp_enqueue_script(
139 'jet-form-builder-recaptcha-handler',
140 jet_form_builder()->plugin_url( 'assets/js/re-captcha-v3.js' ),
141 array( 'jquery' ),
142 jet_form_builder()->get_version(),
143 true
144 );
145
146 $action_prefix = self::PREFIX;
147
148 wp_add_inline_script(
149 'jet-form-builder-recaptcha-handler',
150 "
151 window.JetFormBuilderReCaptchaConfig = window.JetFormBuilderReCaptchaConfig || {};
152 window.JetFormBuilderReCaptchaConfig[ $form_id ] = { key: '$key', action_prefix: '$action_prefix' };
153 ",
154 'before'
155 );
156
157 ?>
158 <input type="hidden" class="captcha-token" name="<?php echo esc_attr( $this->field_key ); ?>" value="">
159 <?php
160 }
161
162 }
163