Skip to content

Commit

Permalink
[TASK] Allow multiple recaptcha' on the same page
Browse files Browse the repository at this point in the history
Made the javascipt function unique per form using the form's id.
  • Loading branch information
keijsers committed Aug 15, 2018
1 parent ed02bd6 commit f11c8c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions app/code/community/Studioforty9/Recaptcha/Block/Explicit.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Studioforty9_Recaptcha_Block_Explicit extends Mage_Core_Block_Template
'ca_ES' => 'ca',
'zh_CN' => 'zh-CN',
'zh_HK|zh_TW' => 'zh-TW',
'hr_HR' => 'hr',
'hr_HR' => 'hr',
'cs_CZ' => 'cs',
'da_DK' => 'da',
'nl_NL' => 'nl',
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getSize()
*
* @return string
*/
public function getRecaptchaScript()
public function getRecaptchaScript($id)
{
if (! $this->_getHelper()->isEnabled()) {
return '';
Expand All @@ -148,7 +148,7 @@ public function getRecaptchaScript()
}

$query = array(
'onload' => 'onloadCallback',
'onload' => 'onloadCallback'.$id,
'render' => 'explicit',
'hl' => $lang
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
*/
$id = $this->getRecaptchaId();
?>
<?php if ($this->isAllowed($this->getAction()->getFullActionName())): ?>
<?php if ($this->isAllowed($this->getAction()->getFullActionName())): ?>

<div class="recaptcha" style="overflow:hidden;position:relative;margin-bottom:10px;">
<input type="checkbox" id="cb-<?php echo $id ?>" name="cb-<?php echo $id ?>" value="" class="required-entry" style="visibility:hidden; position:absolute; left:-1000000px" />
<div id="el-<?php echo $id ?>"></div>
<script type="text/javascript">
var onloadCallback = function() {
grecaptcha.render('el-<?php echo $id ?>', {
'sitekey': "<?php echo $this->getSiteKey(); ?>",
'theme': "<?php echo $this->getTheme(); ?>",
'type': "<?php echo $this->getType(); ?>",
'size': "<?php echo $this->getSize(); ?>",
'callback': function(response) {
if (response.length > 0) {
$('cb-<?php echo $id ?>').writeAttribute('value', 'checked');
$('cb-<?php echo $id ?>').checked = true;
}
}
});
};
</script>
<?php echo $this->getRecaptchaScript(); ?>
</div>
<div class="recaptcha" style="overflow:hidden;position:relative;margin-bottom:10px;">
<input type="checkbox" id="cb-<?php echo $id ?>" name="cb-<?php echo $id ?>" value="" class="required-entry" style="visibility:hidden; position:absolute; left:-1000000px" />
<div id="el-<?php echo $id ?>"></div>
<?php echo $this->getRecaptchaScript($id); ?>
</div>
<?php endif; ?>

<script type="text/javascript">
function onloadCallback<?php echo $id ?>(){
ReCaptchaRender = grecaptcha.render('el-<?php echo $id ?>', {
'sitekey': "<?php echo $this->getSiteKey(); ?>",
'theme': "<?php echo $this->getTheme(); ?>",
'type': "<?php echo $this->getType(); ?>",
'size': "<?php echo $this->getSize(); ?>",
'callback': function (response) {
if (response.length > 0) {
$('cb-<?php echo $id ?>').writeAttribute('value', 'checked');
$('cb-<?php echo $id ?>').checked = true;
}
}
});
};
</script>

0 comments on commit f11c8c3

Please sign in to comment.