Skip to content

Commit

Permalink
Merge pull request #19 from pixelant/multiple_recaptcha
Browse files Browse the repository at this point in the history
[BUGFIX] render multiple recaptchas on one page
  • Loading branch information
anjeylink authored Oct 15, 2018
2 parents 5a6e7e1 + e15ce42 commit 02c4e26
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 71 deletions.
30 changes: 11 additions & 19 deletions Classes/Utility/ConfigurationUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class ConfigurationUtility
Expand All @@ -51,27 +53,17 @@ class ConfigurationUtility
public static function getConfiguration()
{
if (self::$configuration === null) {
$configuration = self::getTSFE()->tmpl->setup;
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$configurationManager = $objectManager->get(ConfigurationManagerInterface::class);

$settings = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'pxaformenhancement'
);

if (isset($configuration['plugin.']['tx_pxaformenhancement.']['settings.'])
&& is_array($configuration['plugin.']['tx_pxaformenhancement.']['settings.'])
) {
self::$configuration = GeneralUtility::removeDotsFromTS(
$configuration['plugin.']['tx_pxaformenhancement.']['settings.']
);
} else {
self::$configuration = [];
}
self::$configuration = is_array($settings) ? $settings : [];
}

return self::$configuration;
}

/**
* @return TypoScriptFrontendController
*/
public static function getTSFE()
{
return $GLOBALS['TSFE'];
}
}
92 changes: 92 additions & 0 deletions Classes/ViewHelpers/GetSettingsViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);

namespace Pixelant\PxaFormEnhancement\ViewHelpers;

use Pixelant\PxaFormEnhancement\Utility\ConfigurationUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/***************************************************************
*
* Copyright notice
*
* (c) 2016 Andriy Oprysko <[email protected]>, Pixelant
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
* Class GetSettingsViewHelper
* @package Pixelant\PxaFormEnhancement\ViewHelpers
*/
class GetSettingsViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var boolean
*/
protected $escapeChildren = false;

/**
* @var boolean
*/
protected $escapeOutput = false;

/**
* Register arguments
*/
public function initializeArguments()
{
$this->registerArgument('as', 'string', 'Render settings as variable');
}

/**
* Get settings of pxa_form_enhancement
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$as = $arguments['as'];
$settings = ConfigurationUtility::getConfiguration();

if (!empty($as)) {
$variableProvider = $renderingContext->getVariableProvider();
if ($variableProvider->exists($as)) {
$variableProvider->remove($as);
}

$variableProvider->add($as, $settings);
$content = $renderChildrenClosure();
$variableProvider->remove($as);

return $content;
}

return $settings;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);
namespace Pixelant\PxaFormEnhancement\ViewHelpers;

use Pixelant\PxaFormEnhancement\Utility\ConfigurationUtility;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/***************************************************************
*
Expand All @@ -32,48 +34,42 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class InitRecaptchaViewHelper extends AbstractViewHelper

/**
* Class InitRecaptchaViewHelper
* @package Pixelant\PxaFormEnhancement\ViewHelpers
*/
class IncludeRecaptchaJsViewHelper extends AbstractViewHelper
{
/**
* recaptcha url
*/
const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api.js?onload=onloadCallbackRecaptcha&render=explicit';
use CompileWithRenderStatic;

/**
* Initialize
*
* @return void
* recaptcha url
*/
public function initializeArguments()
{
$this->registerArgument('recaptchaIdentifier', 'string', 'Unique identifier for recaptcha element', true);
}
protected static $recaptchaUrl = 'https://www.google.com/recaptcha/api.js';

/**
* Add JS for recaptcha
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public function render()
{
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$configuration = ConfigurationUtility::getConfiguration();

if ($configuration['siteKey'] && $configuration['siteSecret']) {
/** @var PageRenderer $pageRenderer */
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);

$pageRenderer->addJsFooterInlineCode(
'pxa_form_enhancement',
sprintf(
$this->getJsInitTemplate(),
$this->arguments['recaptchaIdentifier'],
$configuration['siteKey']
)
);

$reCaptchaLanguage = '&hl=' . ($configuration['language'] ?: $pageRenderer->getLanguage());
$reCaptchaLanguage = '?hl=' . ($configuration['language'] ?: $pageRenderer->getLanguage());
$pageRenderer->addJsFooterFile(
self::RECAPTCHA_URL . $reCaptchaLanguage,
self::$recaptchaUrl . $reCaptchaLanguage,
'text/javascript',
false,
false,
Expand All @@ -82,28 +78,8 @@ public function render()
'|',
true
);

$message = '';
} else {
$message = LocalizationUtility::translate('fe.error.credentials_not_set', 'pxa_form_enhancement');
return LocalizationUtility::translate('fe.error.credentials_not_set', 'PxaFormEnhancement');
}

return $message;
}

/**
* Js template
*
* @return string
*/
protected function getJsInitTemplate()
{
return '
var onloadCallbackRecaptcha = function() {
grecaptcha.render(
\'#%s\',
{\'sitekey\': \'%s\'}
);
};';
}
}
8 changes: 6 additions & 2 deletions Resources/Private/Frontend/Partials/Recaptcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
id="{element.uniqueIdentifier}"
value="{f:translate(key: 'fe.recaptcha.passCheck', extensionName: 'PxaFormEnhancement')}"
/>
<div class="{element.properties.elementClassAttribute}" id="#{element.uniqueIdentifier}"></div>
<pxa:getSettings as="settings">
<div class="g-recaptcha {element.properties.elementClassAttribute}"
id="#{element.uniqueIdentifier}"
data-sitekey="{settings.siteKey}"></div>
</pxa:getSettings>
</f:render>

<pxa:initRecaptcha recaptchaIdentifier="{element.uniqueIdentifier}"/>
<pxa:includeRecaptchaJs />
</formvh:renderRenderable>
</html>
4 changes: 2 additions & 2 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 1,
'version' => '3.2.1',
'version' => '3.2.2',
'constraints' => [
'depends' => [
'typo3' => '8.7.0-9.9.99',
'typo3' => '8.7.0-9.5.99',
],
'conflicts' => [
],
Expand Down

0 comments on commit 02c4e26

Please sign in to comment.