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