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