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

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