Hi,
Could you please add a check to see if WooCommerce is active in /advanced-google-recaptcha/ibs/functions.php within the captcha_fields function?
My website doesn’t use WooCommerce, so right now this useless code is being added:
jQuery("form.woocommerce-checkout").on("submit", function(){
setTimeout(function(){
wpcaptcha_captcha();
},100);
});I have rewritten this functions as follows (tested and working):
static function captcha_fields()
{
$options = WPCaptcha_Setup::get_options();
if ($options['captcha'] == 'recaptchav2') {
echo '<div class="g-recaptcha" style="transform: scale(0.9); -webkit-transform: scale(0.9); transform-origin: 0 0; -webkit-transform-origin: 0 0;" data-sitekey="' . esc_html($options['captcha_site_key']) . '"></div>';if ( class_exists( 'woocommerce' ) ) {
echo '
<script>
jQuery("form.woocommerce-checkout").on("submit", function(){
setTimeout(function(){
grecaptcha.reset();
},100);
});
</script>';
}
} else if ($options['captcha'] == 'recaptchav3') {
echo '<input type="hidden" name="g-recaptcha-response" class="agr-recaptcha-response" value="" />';
echo '<script>
function wpcaptcha_captcha(){
grecaptcha.execute("' . esc_html($options['captcha_site_key']) . '", {action: "submit"}).then(function(token) {
var captchas = document.querySelectorAll(".agr-recaptcha-response");
captchas.forEach(function(captcha) {
captcha.value = token;
});
});
}';
if ( class_exists( 'woocommerce' ) ) {
echo '
jQuery("form.woocommerce-checkout").on("submit", function(){
setTimeout(function(){
wpcaptcha_captcha();
},100);
});';
}
echo '</script>';
} else if ($options['captcha'] == 'builtin') {
echo '<p><label for="wpcaptcha_captcha">Are you human? Please solve: ';
$captcha_id = rand(1000,9999);
echo '<img class="wpcaptcha-captcha-img" style="vertical-align: text-top;" src="' . esc_url(WPCAPTCHA_PLUGIN_URL) . 'libs/captcha.php?wpcaptcha-generate-image=true&color=" . esc_attr(urlencode("#FFFFFF')) . '&noise=1&id=' . intval($captcha_id) . '" alt="Captcha" />';
echo '<input class="input" type="text" size="3" name="wpcaptcha_captcha[' . intval($captcha_id) . ']" id="wpcaptcha_captcha" />';
echo '</label></p><br />';
}
}
Would appreciate if this could be updated to not load the WooCommerce JQuery for people that don’t use WooCommerce. Helps with the ‘unnecessary JavaScript’ warning that Google PageSpeed checks for when optimizing.
Any questions or more info needed? Feel free to ask. 🙂
