PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.1
JetFormBuilder — Dynamic Blocks Form Builder v1.3.1
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
190 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\Plugin;
7
8 /**
9 * Captcha manager class
10 */
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Define Forms_Captcha class
19 */
20 class Forms_Captcha {
21
22 public static $script_rendered = false;
23
24 private $field_key = '_captcha_token';
25 private $api = 'https://www.google.com/recaptcha/api/siteverify';
26 private $defaults = array(
27 'enabled' => false,
28 'key' => '',
29 'secret' => '',
30 );
31
32
33 public function verify( $form_id = null, $is_ajax = false ) {
34
35 $captcha = $this->get_data( $form_id );
36
37 if ( empty( $captcha['enabled'] ) ) {
38 return true;
39 }
40
41 $request = $this->sanitize_token_from_request( $is_ajax );
42
43 if ( empty( $request[ $this->field_key ] ) ) {
44 return false;
45 }
46
47 $token = esc_attr( $request[ $this->field_key ] );
48 $response = wp_remote_post( $this->api, array(
49 'body' => array(
50 'secret' => $captcha['secret'],
51 'response' => $token,
52 ),
53 ) );
54
55 $body = wp_remote_retrieve_body( $response );
56 $body = json_decode( $body, true );
57
58 if ( ! $body || empty( $body['success'] ) ) {
59 return false;
60 } else {
61 return $body['success'];
62 }
63
64 }
65
66 private function sanitize_token_from_request( $is_ajax ) {
67 $response = array();
68
69 if ( ! $is_ajax && isset( $_POST[ $this->field_key ] ) ) {
70 $response[ $this->field_key ] = sanitize_text_field( $_POST[ $this->field_key ] );
71
72 } elseif ( isset( $_REQUEST['values'] ) ) {
73 foreach ( $_REQUEST['values'] as $field ) {
74 if ( $field['name'] === $this->field_key ) {
75
76 $response[ $field['name'] ] = esc_attr( $field['value'] );
77 }
78 }
79 }
80
81 return $response;
82 }
83
84 /**
85 * Returns captcha settings for passed form ID
86 *
87 * @param [type] $post_id [description]
88 *
89 * @return [type] [description]
90 */
91 public function get_data( $form_id = null ) {
92
93 if ( ! $form_id ) {
94 $form_id = get_the_ID();
95 }
96
97 $captcha = Plugin::instance()->post_type->get_recaptcha( $form_id );
98
99 if ( ! $captcha || ! is_array( $captcha ) ) {
100 return $this->defaults;
101 } elseif ( isset( $captcha['use_global'] ) && $captcha['use_global'] ) {
102 return Tab_Handler_Manager::instance()->options(
103 'captcha-tab',
104 array( 'enabled' => $captcha['enabled'] )
105 );
106
107 } else {
108 return wp_parse_args( $captcha, $this->defaults );
109 }
110
111 }
112
113 public function render( $form_id ) {
114
115 $captcha = $this->get_data( $form_id );
116
117 if ( empty( $captcha['enabled'] ) ) {
118 return;
119 }
120
121 $key = esc_attr( $captcha['key'] );
122
123 if ( ! $key ) {
124 return;
125 }
126
127 if ( ! self::$script_rendered ) {
128 self::$script_rendered = true;
129 printf( '<script id="jet-form-builder-recaptcha-js" src="https://www.google.com/recaptcha/api.js?render=%s"></script>', $key );
130 }
131
132 ?>
133 <input type="hidden" class="captcha-token" name="<?php echo $this->field_key; ?>" value="">
134 <script>
135
136 if ( ! window.JetFormBuilderCaptcha ) {
137 window.JetFormBuilderCaptcha = function( formID ) {
138 var $script = document.querySelector( 'script#jet-form-builder-recaptcha-js' ),
139 $cpField = jQuery( 'form[data-form-id="' + formID + '"]' ).find( '.captcha-token' );
140
141 function setFormToken() {
142 if ( window.JetFormBuilderToken ) {
143 $cpField.val( window.JetFormBuilderToken );
144 } else if ( window.grecaptcha ) {
145 window.grecaptcha.ready( function() {
146 grecaptcha.execute(
147 '<?php echo $key; ?>',
148 {
149 action: 'submit_form',
150 },
151 ).then( function( token ) {
152 $cpField.val( token );
153 window.JetFormBuilderToken = token;
154 } );
155 } );
156 }
157 }
158
159 if ( ! $script ) {
160
161 $script = document.createElement( 'script' );
162
163 $script.id = 'jet-form-builder-recaptcha-js';
164 $script.src = 'https://www.google.com/recaptcha/api.js?render=<?php echo $key; ?>';
165
166 $cpField.parentNode.insertBefore( $script, $cpField );
167
168 $script.onload = function() {
169 setFormToken();
170 };
171
172 } else {
173 setFormToken();
174 }
175 }
176 }
177
178 window.JetFormBuilderCaptcha( <?php echo $form_id; ?> );
179
180 jQuery( window ).on( 'jet-popup/show-event/after-show', function() {
181
182 window.JetFormBuilderCaptcha( <?php echo $form_id; ?> );
183
184 } );
185 </script>
186 <?php
187
188 }
189
190 }