app/Plugin/NZRecaptcha42/Event/TemplateEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZRecaptcha42\Event;
  3. use Plugin\NZRecaptcha42\Service\RecaptchaService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Eccube\Event\TemplateEvent;
  6. class TemplateEventSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var RecaptchaService
  10.      */
  11.     private $recaptchaService;
  12.     public function __construct(RecaptchaService $recaptchaService)
  13.     {
  14.         $this->recaptchaService $recaptchaService;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             'Entry/index.twig' => 'onEntryIndex',
  20.             'Mypage/login.twig' => 'onMypageLogin',
  21.             'Contact/index.twig' => 'onContactIndex',
  22.         ];
  23.     }
  24.     public function onEntryIndex(TemplateEvent $event)
  25.     {
  26.         if (!$this->recaptchaService->isEnabledForEntry()) {
  27.             return;
  28.         }
  29.         $this->addRecaptchaScript($event'entry''eccube_entry');
  30.     }
  31.     public function onMypageLogin(TemplateEvent $event)
  32.     {
  33.         if (!$this->recaptchaService->isEnabledForLogin()) {
  34.             return;
  35.         }
  36.         $this->addRecaptchaScript($event'login''login_mypage');
  37.     }
  38.     public function onContactIndex(TemplateEvent $event)
  39.     {
  40.         if (!$this->recaptchaService->isEnabledForContact()) {
  41.             return;
  42.         }
  43.         $this->addRecaptchaScript($event'contact''contact');
  44.     }
  45.     private function addRecaptchaScript(TemplateEvent $eventstring $actionstring $formName)
  46.     {
  47.         $siteKey $this->recaptchaService->getSiteKey();
  48.         if (!$siteKey) {
  49.             return;
  50.         }
  51.         $script = <<<HTML
  52. <script src="https://www.google.com/recaptcha/api.js?render={$siteKey}"></script>
  53. <script>
  54. (function() {
  55.     'use strict';
  56.     function addRecaptchaToken() {
  57.         var form = document.querySelector('form[name="{$formName}"]');
  58.         if (!form) form = document.getElementById('{$formName}');
  59.         if (!form) return;
  60.         var input = document.createElement('input');
  61.         input.type = 'hidden';
  62.         input.name = 'g-recaptcha-response';
  63.         form.appendChild(input);
  64.         form.addEventListener('submit', function(e) {
  65.             if (input.value) return true;
  66.             e.preventDefault();
  67.             grecaptcha.ready(function() {
  68.                 grecaptcha.execute('{$siteKey}', {action: '{$action}'}).then(function(token) {
  69.                     input.value = token;
  70.                     form.submit();
  71.                 });
  72.             });
  73.         });
  74.     }
  75.     if (document.readyState === 'loading') {
  76.         document.addEventListener('DOMContentLoaded', addRecaptchaToken);
  77.     } else {
  78.         addRecaptchaToken();
  79.     }
  80. })();
  81. </script>
  82. HTML;
  83.         // 第2引数をfalseにしてtemplate_from_stringを使用
  84.         $event->addSnippet($scriptfalse);
  85.     }
  86. }