PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.5.4
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.5.4
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / fields / class-field-recaptcha.php

class-field-recaptcha.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.5.4, at includes/fields/class-field-recaptcha.php

232 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Text Field Class
5 */
6 class WeForms_Form_Field_reCaptcha extends WeForms_Field_Contract {
7
8 public function __construct() {
9 $this->name = __( 'reCaptcha', 'weforms' );
10 $this->input_type = 'recaptcha';
11 $this->icon = 'qrcode';
12 }
13
14 /**
15 * Render the text field
16 *
17 * @param array $field_settings
18 * @param int $form_id
19 *
20 * @return void
21 */
22 public function render( $field_settings, $form_id ) {
23 $settings = weforms_get_settings( 'recaptcha' );
24 $is_invisible = false;
25 $public_key = isset( $settings->key ) ? $settings->key : '';
26 $type = isset( $settings->type ) ? $settings->type : '';
27 $theme = isset( $field_settings['recaptcha_theme'] ) ? $field_settings['recaptcha_theme'] : 'light';
28 /** Recaptcha V3 start */
29 if( 'v3' == $type ) { ?>
30 <li <?php $this->print_list_attributes( $field_settings ); ?> >
31 <?php if ( ! $public_key ) {
32 esc_html_e( 'reCaptcha API key is missing.', 'weforms');
33 } else { ?>
34 <li <?php $this->print_list_attributes( $field_settings ); ?> >
35 <div class="wpuf-fields <?php echo ' wpuf_'. esc_attr( $field_settings['name'] ).'_'. esc_attr( $form_id ); ?>">
36 <input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">
37 <input type="hidden" name="g-action" id="g-action">
38 <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $public_key );?>"> </script>
39 <script>
40 grecaptcha.ready(function() {
41 grecaptcha.execute('<?php echo esc_attr( $public_key );?>', {action: 'captcha_validation'}).then( function( token ) {
42 document.getElementById('g-recaptcha-response').value = token;
43 document.getElementById('g-action').value = 'captcha_validation';
44
45 });
46 setInterval(function () {
47 grecaptcha.execute('<?php echo esc_attr( $public_key );?>', {action: 'captcha_validation'}).then( function( token ) {
48 document.getElementById('g-recaptcha-response').value = token;
49 document.getElementById('g-action').value = 'captcha_validation';
50 });
51 }, 60000);
52 });
53 </script>
54 </div>
55 </li>
56 <?php }
57 return ;
58 }
59 /** Recaptcha V3 end */
60
61 if ( isset( $field_settings['recaptcha_type'] ) ) {
62 $is_invisible = $field_settings['recaptcha_type'] == 'invisible_recaptcha' ? true : false;
63 }
64
65 $invisible_css = $is_invisible ? ' style="margin: 0; padding: 0" ' : '';
66
67 ?> <li <?php $this->print_list_attributes( $field_settings ); echo esc_attr( $invisible_css ); ?>>
68
69 <?php
70
71 if ( !$is_invisible ) {
72 $this->print_label( $field_settings );
73 }
74
75 if ( ! $public_key ) {
76 esc_html_e( 'reCaptcha API key is missing.', 'weforms');
77
78 } else {
79
80 ?>
81
82 <div class="wpuf-fields <?php echo ' wpuf_'. esc_attr( $field_settings['name'] ).'_'. esc_attr( $form_id ); ?>">
83 <script>
84 function weformsRecaptchaCallback(token) {
85 jQuery('[name="g-recaptcha-response"]').val(token);
86 jQuery('.weforms_submit_btn').attr('disabled',false).show();
87 jQuery('.weforms_submit_btn_recaptcha').hide();
88 }
89
90 jQuery(document).ready( function($) {
91 $('.weforms_submit_btn').attr('disabled',true);
92 });
93 </script>
94
95 <input type="hidden" name="g-recaptcha-response">
96 <?php
97
98 if ( $is_invisible ) { ?>
99
100 <script src="https://www.google.com/recaptcha/api.js?onload=weFormsreCaptchaLoaded&render=explicit&hl=en" async defer></script>
101
102 <script>
103
104 jQuery(document).ready(function($) {
105 var btn = $('.weforms_submit_btn');
106 var gc_btn = btn.clone().removeClass().addClass('weforms_submit_btn_recaptcha').attr('disabled',false);
107 btn.after(gc_btn);
108 btn.hide();
109
110 $(document).on('click','.weforms_submit_btn_recaptcha',function(e){
111 e.preventDefault();
112 e.stopPropagation();
113 grecaptcha.execute();
114 })
115 });
116
117 var weFormsreCaptchaLoaded = function() {
118
119 grecaptcha.render('recaptcha', {
120 'size' : 'invisible',
121 'sitekey' : '<?php echo esc_attr( $public_key ); ?>',
122 'callback' : weformsRecaptchaCallback
123 });
124
125 grecaptcha.execute();
126 };
127 </script>
128
129 <div id='recaptcha' class="g-recaptcha" data-theme="<?php echo esc_attr( $theme ); ?>" data-sitekey="<?php echo esc_attr( $public_key ); ?>" data-callback="weformsRecaptchaCallback" data-size="invisible"></div>
130
131 <?php } else { ?>
132
133 <script src="https://www.google.com/recaptcha/api.js"></script>
134 <div id='recaptcha' data-theme="<?php echo esc_attr( $theme ); ?>" class="g-recaptcha" data-sitekey="<?php echo esc_attr ( $public_key ); ?>" data-callback="weformsRecaptchaCallback"></div>
135 <?php } ?>
136
137 </div>
138
139 <?php
140 } ?>
141
142 </li>
143 <?php
144 }
145
146 /**
147 * Custom validator
148 *
149 * @return array
150 */
151 public function get_validator() {
152 return [
153 'callback' => 'has_recaptcha_api_keys',
154 'button_class' => 'button-faded',
155 'msg_title' => __( 'Site key and Secret key', 'weforms' ),
156 'msg' => sprintf(
157 __( 'You need to set Site key and Secret key in <a href="%s" target="_blank">Settings</a> in order to use "Recaptcha" field. <a href="%s" target="_blank">Click here to get the these key</a>.', 'weforms' ),
158 admin_url( 'admin.php?page=weforms#/settings' ),
159 'https://www.google.com/recaptcha/'
160 ),
161 ];
162 }
163
164 /**
165 * Get field options setting
166 *
167 * @return array
168 */
169 public function get_options_settings() {
170 $settings = [
171 [
172 'name' => 'label',
173 'title' => __( 'Title', 'weforms' ),
174 'type' => 'text',
175 'section' => 'basic',
176 'priority' => 10,
177 'help_text' => __( 'Title of the section', 'weforms' ),
178 ],
179 [
180 'name' => 'recaptcha_type',
181 'title' => 'reCaptcha type',
182 'type' => 'radio',
183 'options' => [
184 'enable_no_captcha' => __( 'Enable noCaptcha', 'weforms' ),
185 'invisible_recaptcha' => __( 'Enable Invisible reCaptcha', 'weforms' ),
186 ],
187 'default' => 'enable_no_captcha',
188 'section' => 'basic',
189 'priority' => 11,
190 'help_text' => __( 'Select reCaptcha type', 'weforms' ),
191 'show_if' => 'is_recaptcha_v2'
192 ],
193 [
194 'name' => 'recaptcha_theme',
195 'title' => 'reCaptcha Theme',
196 'type' => 'radio',
197 'options' => [
198 'light' => __( 'Light', 'weforms' ),
199 'dark' => __( 'Dark', 'weforms' ),
200 ],
201 'default' => 'light',
202 'section' => 'advanced',
203 'priority' => 12,
204 'help_text' => __( 'Select reCaptcha Theme', 'weforms' ),
205 'show_if' => 'is_recaptcha_v2'
206 ],
207 ];
208
209 return $settings;
210 }
211
212 /**
213 * Get the field props
214 *
215 * @return array
216 */
217 public function get_field_props() {
218 $props = [
219 'template' => $this->get_type(),
220 'label' => '',
221 'recaptcha_type' => 'enable_no_captcha',
222 'is_meta' => 'yes',
223 'id' => 0,
224 'is_new' => true,
225 'wpuf_cond' => null,
226 'recaptcha_theme' => 'light',
227 ];
228
229 return $props;
230 }
231 }
232