<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ /** @var $block Magento\ReCaptchaUi\Block\ReCaptcha */ $config = $block->getCaptchaUiConfig(); $renderingOptions = $config['rendering'] ?? []; $isInvisible = !empty($config['invisible']); ?> <div class="admin__field <?= /* @noEscape */ $isInvisible ? 'field-invisible-recaptcha' : 'field-recaptcha' ?>"><div id="admin-recaptcha" class="admin-recaptcha-content<?= /* @noEscape */ !empty($renderingOptions['size']) ? ' size-' . $renderingOptions['size'] : '' ?>"></div></div><script>
    require([
        'jquery'
    ], function (
        $
    ) {
        const element = document.createElement('script');
        const scriptTag = document.getElementsByTagName('script')[0];

        element.async = true;
        element.src = 'https://www.google.com/recaptcha/api.js'
            + '?onload=globalOnRecaptchaOnLoadCallback&render=explicit';

        let isRecaptchaLoaded = false;
        let token = '';
        let maxRetryAttempts = 5;
        let attempts = 0;
        let widgetId = 0;
        <?php if ($isInvisible): ?>
        $('#login-form').submit(function (event) {
            if (!token) {
                event.preventDefault(event);
                event.stopImmediatePropagation();
                event.stopPropagation();

                let attemptRecaptcha = () => {
                    attempts++;
                    if (attempts > maxRetryAttempts){
                        console.error("Could not fetch invisible ReCaptcha token. Please refresh the page.");
                        return;
                    }
                    if (!isRecaptchaLoaded) {

                        setTimeout(() => {
                            attemptRecaptcha()
                        }, 1000);
                        return;
                    }
                    grecaptcha.execute(widgetId)
                        .then( () => {
                            event.preventDefault(event);
                            event.stopImmediatePropagation();
                            event.stopPropagation();
                        }, (reason) => { })
                        .catch(err => { console.error(err); });
                }
                attemptRecaptcha();
            }
        });
        <?php endif; ?> window.globalOnRecaptchaOnLoadCallback = function () {

            widgetId = grecaptcha.render('admin-recaptcha', {
                <?php foreach ($renderingOptions as $key => $value): ?> '<?= $block->escapeJs($key) ?>': '<?= $block->escapeJs($value) ?>',
                <?php endforeach; ?> 'callback': function (_token) {
                    <?php if ($isInvisible): ?>
                    token = _token;
                    $('#login-form').unbind('submit');
                    $('#login-form').submit();
                    <?php endif; ?> }
            });
            isRecaptchaLoaded = true;
        }
        scriptTag.parentNode.insertBefore(element, scriptTag);
    });</script>