require_once($_SERVER['DOCUMENT_ROOT'] . '/logger.php');
$logger = new logger($_SERVER['DOCUMENT_ROOT'].'/logs/checkout.html');
$logger->ConsoleMsg('start log');
function bh_wc_checkout_verify()
{
global $logger;
$logger->ConsoleMsg('bh_wc_checkout_verify()');
}
add_action('woocommerce_checkout_process', 'bh_wc_checkout_verify');
require_once($_SERVER['DOCUMENT_ROOT'] . '/logger.php');
$logger = new logger($_SERVER['DOCUMENT_ROOT'].'/logs/checkout.html');
$logger->ConsoleMsg('start log');
$options = get_option('bh_admin_options');
$bh_captcha_count = 0;
$site_key = $options['site_key'];
function bh_wc_checkout_verify($data,$errors)
{
global $logger;
$logger->ConsoleMsg('bh_wc_checkout_verify()');
}
function form_field()
{
global $bh_captcha_count;
$bh_captcha_count++;
echo '<div id="recaptcha' . $bh_captcha_count . '" class="bh-recaptcha"></div>';
}
function bh_enqueueScripts()
{
global $site_key;
$inlineScript = "
var renderInvisibleReCaptcha = function() {
for (var i = 0; i < document.forms.length; ++i) {
var form = document.forms[i];
var holder = form.querySelector('.bh-recaptcha');
if (null === holder) continue;
holder.innerHTML = '';
(function(frm){
var cf7SubmitElm = frm.querySelector('.wpcf7-submit');
var holderId = grecaptcha.render(holder,{
'sitekey': '{$site_key}', 'size': 'invisible', 'badge' : 'bottomleft',
'callback' : function (recaptchaToken) {
if((null !== cf7SubmitElm) && (typeof jQuery != 'undefined')){jQuery(frm).submit();grecaptcha.reset(holderId);return;}
frm.submit();
},
'expired-callback' : function(){grecaptcha.reset(holderId);}
});
if(null !== cf7SubmitElm && (typeof jQuery != 'undefined') ){
jQuery(cf7SubmitElm).off('click').on('click', function(clickEvt){
clickEvt.preventDefault();
grecaptcha.execute(holderId);
});
}
else
{
frm.onsubmit = function (evt){evt.preventDefault();grecaptcha.execute(holderId);};
}
})(form);
}
};
";
$language = trim(pll_current_language());
$lang = ($language) ? "?hl=$language" : "";
$googleApiUrl = 'https://www.google.com/recaptcha/api.js?onload=renderInvisibleReCaptcha&render=explicit';
empty($lang) ?: $googleApiUrl .= "&hl=$lang";
wp_enqueue_script('invisible-recaptcha', $googleApiUrl, array(), null, true);
wp_add_inline_script('invisible-recaptcha', $inlineScript, 'before');
add_filter('script_loader_tag', function ($tag, $handle) {
if ('invisible-recaptcha' !== $handle)
return $tag;
return str_replace(' src', ' async defer src', $tag);
}, 99, 2);
}
add_action('woocommerce_after_checkout_validation', 'bh_wc_checkout_verify', 10, 2);
add_action('woocommerce_checkout_after_order_review', 'form_field');
add_action( 'wp_enqueue_scripts', 'bh_enqueueScripts' );