Support multiple CAPTCHA instances on the same page

This commit is contained in:
Kijin Sung 2016-06-08 00:04:32 +09:00
parent 5012bc194b
commit fe63f24617
5 changed files with 19 additions and 6 deletions

View file

@ -9,7 +9,7 @@ if ($addon_info->use_signup === 'Y' && preg_match('/^(?:disp|proc)Member(?:SignU
{
$enable_captcha = true;
}
if ($addon_info->use_recovery === 'Y' && preg_match('/^(?:disp|proc)Member(?:FindAccount|ResendAuthMail)/i', Context::get('act')))
elseif ($addon_info->use_recovery === 'Y' && preg_match('/^(?:disp|proc)Member(?:FindAccount|ResendAuthMail)/i', Context::get('act')))
{
$enable_captcha = true;
}

View file

@ -5,6 +5,7 @@ class reCAPTCHA
protected static $verify = 'https://www.google.com/recaptcha/api/siteverify';
protected static $config = null;
protected static $script_added = false;
protected static $sequence = 1;
public static function init($config)
{
@ -49,12 +50,13 @@ class reCAPTCHA
if (!self::$script_added)
{
Context::addHtmlFooter('<script src="https://www.google.com/recaptcha/api.js" async defer></script>');
Context::loadFile(array('./addons/recaptcha/recaptcha.js', 'body'));
Context::addHtmlFooter('<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=reCaptchaCallback" async defer></script>');
self::$script_added = true;
}
$html = '<div class="g-recaptcha" data-sitekey="%s" data-theme="%s" data-size="%s"></div>';
$html = sprintf($html, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal');
$html = '<div id="recaptcha-instance-%d" class="g-recaptcha" data-sitekey="%s" data-theme="%s" data-size="%s"></div>';
$html = sprintf($html, self::$sequence++, escape(self::$config->site_key), self::$config->theme ?: 'light', self::$config->size ?: 'normal');
return $html;
}
}

View file

@ -0,0 +1,11 @@
function reCaptchaCallback() {
$(".g-recaptcha").each(function() {
var instance = $(this);
grecaptcha.render(instance.attr("id"), {
sitekey: instance.data("sitekey"),
size: instance.data("size"),
theme: instance.data("theme")
});
});
}