PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.5
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.5
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.4.5, at includes/fields/class-field-recaptcha.php

226 lines 8.6 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 });
47 </script>
48 </div>
49 </li>
50 <?php }
51 return ;
52 }
53 /** Recaptcha V3 end */
54
55 if ( isset( $field_settings['recaptcha_type'] ) ) {
56 $is_invisible = $field_settings['recaptcha_type'] == 'invisible_recaptcha' ? true : false;
57 }
58
59 $invisible_css = $is_invisible ? ' style="margin: 0; padding: 0" ' : '';
60
61 ?> <li <?php $this->print_list_attributes( $field_settings ); echo esc_attr( $invisible_css ); ?>>
62
63 <?php
64
65 if ( !$is_invisible ) {
66 $this->print_label( $field_settings );
67 }
68
69 if ( ! $public_key ) {
70 esc_html_e( 'reCaptcha API key is missing.', 'weforms');
71
72 } else {
73
74 ?>
75
76 <div class="wpuf-fields <?php echo ' wpuf_'. esc_attr( $field_settings['name'] ).'_'. esc_attr( $form_id ); ?>">
77 <script>
78 function weformsRecaptchaCallback(token) {
79 jQuery('[name="g-recaptcha-response"]').val(token);
80 jQuery('.weforms_submit_btn').attr('disabled',false).show();
81 jQuery('.weforms_submit_btn_recaptcha').hide();
82 }
83
84 jQuery(document).ready( function($) {
85 $('.weforms_submit_btn').attr('disabled',true);
86 });
87 </script>
88
89 <input type="hidden" name="g-recaptcha-response">
90 <?php
91
92 if ( $is_invisible ) { ?>
93
94 <script src="https://www.google.com/recaptcha/api.js?onload=weFormsreCaptchaLoaded&render=explicit&hl=en" async defer></script>
95
96 <script>
97
98 jQuery(document).ready(function($) {
99 var btn = $('.weforms_submit_btn');
100 var gc_btn = btn.clone().removeClass().addClass('weforms_submit_btn_recaptcha').attr('disabled',false);
101 btn.after(gc_btn);
102 btn.hide();
103
104 $(document).on('click','.weforms_submit_btn_recaptcha',function(e){
105 e.preventDefault();
106 e.stopPropagation();
107 grecaptcha.execute();
108 })
109 });
110
111 var weFormsreCaptchaLoaded = function() {
112
113 grecaptcha.render('recaptcha', {
114 'size' : 'invisible',
115 'sitekey' : '<?php echo esc_attr( $public_key ); ?>',
116 'callback' : weformsRecaptchaCallback
117 });
118
119 grecaptcha.execute();
120 };
121 </script>
122
123 <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>
124
125 <?php } else { ?>
126
127 <script src="https://www.google.com/recaptcha/api.js"></script>
128 <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>
129 <?php } ?>
130
131 </div>
132
133 <?php
134 } ?>
135
136 </li>
137 <?php
138 }
139
140 /**
141 * Custom validator
142 *
143 * @return array
144 */
145 public function get_validator() {
146 return [
147 'callback' => 'has_recaptcha_api_keys',
148 'button_class' => 'button-faded',
149 'msg_title' => __( 'Site key and Secret key', 'weforms' ),
150 'msg' => sprintf(
151 __( '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' ),
152 admin_url( 'admin.php?page=weforms#/settings' ),
153 'https://www.google.com/recaptcha/'
154 ),
155 ];
156 }
157
158 /**
159 * Get field options setting
160 *
161 * @return array
162 */
163 public function get_options_settings() {
164 $settings = [
165 [
166 'name' => 'label',
167 'title' => __( 'Title', 'weforms' ),
168 'type' => 'text',
169 'section' => 'basic',
170 'priority' => 10,
171 'help_text' => __( 'Title of the section', 'weforms' ),
172 ],
173 [
174 'name' => 'recaptcha_type',
175 'title' => 'reCaptcha type',
176 'type' => 'radio',
177 'options' => [
178 'enable_no_captcha' => __( 'Enable noCaptcha', 'weforms' ),
179 'invisible_recaptcha' => __( 'Enable Invisible reCaptcha', 'weforms' ),
180 ],
181 'default' => 'enable_no_captcha',
182 'section' => 'basic',
183 'priority' => 11,
184 'help_text' => __( 'Select reCaptcha type', 'weforms' ),
185 'show_if' => 'is_recaptcha_v2'
186 ],
187 [
188 'name' => 'recaptcha_theme',
189 'title' => 'reCaptcha Theme',
190 'type' => 'radio',
191 'options' => [
192 'light' => __( 'Light', 'weforms' ),
193 'dark' => __( 'Dark', 'weforms' ),
194 ],
195 'default' => 'light',
196 'section' => 'advanced',
197 'priority' => 12,
198 'help_text' => __( 'Select reCaptcha Theme', 'weforms' ),
199 'show_if' => 'is_recaptcha_v2'
200 ],
201 ];
202
203 return $settings;
204 }
205
206 /**
207 * Get the field props
208 *
209 * @return array
210 */
211 public function get_field_props() {
212 $props = [
213 'template' => $this->get_type(),
214 'label' => '',
215 'recaptcha_type' => 'enable_no_captcha',
216 'is_meta' => 'yes',
217 'id' => 0,
218 'is_new' => true,
219 'wpuf_cond' => null,
220 'recaptcha_theme' => 'light',
221 ];
222
223 return $props;
224 }
225 }
226