From 85841b149ce9a51f83a8f3256171c06f800fd595 Mon Sep 17 00:00:00 2001 From: Niklas Becker Date: Mon, 24 Jul 2017 16:45:37 +0200 Subject: [PATCH 1/2] fixed reCaptcha not working when id is over 1000 and improved performance --- Classes/ViewHelpers/ReCaptchaViewHelper.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Classes/ViewHelpers/ReCaptchaViewHelper.php b/Classes/ViewHelpers/ReCaptchaViewHelper.php index dea9a56..409c2ff 100644 --- a/Classes/ViewHelpers/ReCaptchaViewHelper.php +++ b/Classes/ViewHelpers/ReCaptchaViewHelper.php @@ -94,17 +94,15 @@ public function render() } if (!$this->initialized) { - $key = $reCaptchaSettings['siteKey']; $this->initialized = true; $pageRenderer = $this->getPageRenderer(); $pageRenderer->addJsFooterInlineCode( 'recaptcha', ' var recaptchaCallback = function() { - for (var i = 1; i <= 1000; ++i) { - if (document.getElementById(\'g-recaptcha-\' + i)) { - grecaptcha.render(\'g-recaptcha-\' + i, {\'sitekey\' : \'' . $key . '\'}); - } + var recaptchas = document.getElementsByClassName("g-recaptcha"); + for(var i = 0; i < recaptchas.length; i++){ + grecaptcha.render(recaptchas.item(i), {"sitekey": "' . $reCaptchaSettings['siteKey'] . '"}); } }; /*]]>*/ From 23b1db64b99450341d184c127d260208a1e1f5d4 Mon Sep 17 00:00:00 2001 From: Niklas Becker Date: Thu, 27 Jul 2017 17:42:01 +0200 Subject: [PATCH 2/2] fix other scripts being included after this script not getting executed --- Classes/ViewHelpers/ReCaptchaViewHelper.php | 153 ++++++++++---------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/Classes/ViewHelpers/ReCaptchaViewHelper.php b/Classes/ViewHelpers/ReCaptchaViewHelper.php index 409c2ff..5beb678 100644 --- a/Classes/ViewHelpers/ReCaptchaViewHelper.php +++ b/Classes/ViewHelpers/ReCaptchaViewHelper.php @@ -28,93 +28,92 @@ * @package RH\RhRecaptcha\ViewHelpers * @author Richard Haeser */ -class ReCaptchaViewHelper extends AbstractViewHelper implements SingletonInterface -{ - /** - * @var bool - */ - protected $initialized = false; +class ReCaptchaViewHelper extends AbstractViewHelper implements SingletonInterface { + /** + * @var bool + */ + protected $initialized = FALSE; - /** - * @var bool - */ - protected $escapeOutput = false; + /** + * @var bool + */ + protected $escapeOutput = FALSE; - /** - * @var ConfigurationManagerInterface - */ - protected $configurationManager; + /** + * @var ConfigurationManagerInterface + */ + protected $configurationManager; - /** - * @param ConfigurationManagerInterface $configurationManager - * @return void - */ - public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) - { - $this->configurationManager = $configurationManager; - } + /** + * @param ConfigurationManagerInterface $configurationManager + * @return void + */ + public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) { + $this->configurationManager = $configurationManager; + } - /** - * Returns an instance of the page renderer - * - * @return PageRenderer - */ - public function getPageRenderer() - { - if (TYPO3_MODE === 'BE') { - $pageRenderer = $this->getDocInstance()->getPageRenderer(); - } else { - $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); - } + /** + * Returns an instance of the page renderer + * + * @return PageRenderer + */ + public function getPageRenderer() { + if (TYPO3_MODE === 'BE') { + $pageRenderer = $this->getDocInstance()->getPageRenderer(); + } else { + $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); + } - return $pageRenderer; - } + return $pageRenderer; + } - /** - * @return string - * @throws InvalidVariableException - */ - public function render() - { - $fullTs = $this->configurationManager->getConfiguration( - ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT - ); - $reCaptchaSettings = $fullTs['plugin.']['tx_powermail.']['settings.']['setup.']['reCAPTCHA.']; + /** + * @return string + * @throws InvalidVariableException + */ + public function render() { + $fullTs = $this->configurationManager->getConfiguration( + ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT + ); + $reCaptchaSettings = $fullTs['plugin.']['tx_powermail.']['settings.']['setup.']['reCAPTCHA.']; - if (isset($reCaptchaSettings) && - is_array($reCaptchaSettings) && - isset($reCaptchaSettings['siteKey']) && - $reCaptchaSettings['siteKey'] - ) { - $this->templateVariableContainer->add('siteKey', $reCaptchaSettings['siteKey']); - $content = $this->renderChildren(); - $this->templateVariableContainer->remove('siteKey'); - } else { - throw new InvalidVariableException('No siteKey provided in TypoScript constants', 1358349150); - } + if ( + isset($reCaptchaSettings) && + is_array($reCaptchaSettings) && + isset($reCaptchaSettings['siteKey']) && + $reCaptchaSettings['siteKey'] + ) { + $this->templateVariableContainer->add('siteKey', $reCaptchaSettings['siteKey']); + $content = $this->renderChildren(); + $this->templateVariableContainer->remove('siteKey'); + } else { + throw new InvalidVariableException('No siteKey provided in TypoScript constants', 1358349150); + } - if (!$this->initialized) { - $this->initialized = true; - $pageRenderer = $this->getPageRenderer(); - $pageRenderer->addJsFooterInlineCode( - 'recaptcha', - ' + if (!$this->initialized) { + $key = $reCaptchaSettings['siteKey']; + $this->initialized = TRUE; + $pageRenderer = $this->getPageRenderer(); + $pageRenderer->addJsFooterInlineCode( + 'recaptcha', ' var recaptchaCallback = function() { var recaptchas = document.getElementsByClassName("g-recaptcha"); - for(var i = 0; i < recaptchas.length; i++){ - grecaptcha.render(recaptchas.item(i), {"sitekey": "' . $reCaptchaSettings['siteKey'] . '"}); + for(var i = 0; i < recaptchas.length; i++) + { + grecaptcha.render(recaptchas.item(i), {"sitekey": "' . $reCaptchaSettings['siteKey'] . '"}); } }; - /*]]>*/ - -