diff --git a/recaptcha.js b/recaptcha.js new file mode 100644 index 0000000..d44a0ad --- /dev/null +++ b/recaptcha.js @@ -0,0 +1,27 @@ +(function ($, window, document) { + + Drupal.behaviors.recaptcha = { + attach: function (context, settings) { + $('.g-recaptcha', context).each(function () { + if (typeof grecaptcha == 'undefined') { + return; + } + if ($(this).hasClass('drupal-recaptcha-processed')) { + grecaptcha.reset(); + } + else { + grecaptcha.render(this, $(this).data()); + $(this).addClass('drupal-recaptcha-processed'); + } + + }); + } + } + + window.drupalRecaptchaOnload = function () { + $('.g-recaptcha').each(function () { + grecaptcha.render(this, $(this).data()); + $(this).addClass('drupal-recaptcha-processed'); + }); + } +})(jQuery, window, document); \ No newline at end of file diff --git a/recaptcha.module b/recaptcha.module index 1e65aab..b44520a 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -68,7 +68,7 @@ function recaptcha_permission() { /** * Implements hook_captcha(). */ -function recaptcha_captcha($op, $captcha_type = '') { +function recaptcha_captcha($op, $captcha_type = '', $captcha_sid = '') { global $language; switch ($op) { @@ -101,6 +101,7 @@ function recaptcha_captcha($op, $captcha_type = '') { } $attributes = array( + 'id'=>'g-recaptcha'.$captcha_sid, 'class' => 'g-recaptcha', 'data-sitekey' => $recaptcha_site_key, 'data-theme' => variable_get('recaptcha_theme', 'light'), @@ -114,6 +115,11 @@ function recaptcha_captcha($op, $captcha_type = '') { $captcha['form']['recaptcha_widget'] = array( '#markup' => '', '#suffix' => $noscript, + '#attached' => array( + 'js' => array( + drupal_get_path('module', 'recaptcha') . '/recaptcha.js', + ), + ), ); // @todo: #1664602: D7 does not yet support "async" in drupal_add_js(). @@ -122,14 +128,30 @@ function recaptcha_captcha($op, $captcha_type = '') { '#tag' => 'script', '#value' => '', '#attributes' => array( - 'src' => url('https://www.google.com/recaptcha/api.js', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), + 'src' => url('https://www.google.com/recaptcha/api.js?onload=ReCaptchaCallbackForMultiple&render=explicit', array('query' => array('hl' => $language->language), 'absolute' => TRUE)), 'async' => 'async', 'defer' => 'defer', ), ); drupal_add_html_head($data, 'recaptcha_api'); - } - else { + + //to make it display multiple + $current_head = drupal_add_html_head(); + $callback_script = (isset($current_head['recaptcha_render_callback_script'])) ? $current_head['recaptcha_render_callback_script'] : NULL; + if (!isset($callback_script)){ + $callback_script = array( + '#type' => 'markup', + '#markup' => '', + ); + } + $callback_script = str_replace('var ReCaptchaCallbackForMultiple = function(){', + 'var ReCaptchaCallbackForMultiple = function(){'. + 'grecaptcha.render(\'g-recaptcha'.($captcha_sid).'\', {\'sitekey\' : \''.$recaptcha_site_key.'\'});', $callback_script); + drupal_add_html_head($callback_script, 'recaptcha_render_callback_script'); + } else { // Fallback to Math captcha as reCAPTCHA is not configured. $captcha = captcha_captcha('generate', 'Math'); } diff --git a/recaptcha.test b/recaptcha.test index 4afedf7..f262a76 100644 --- a/recaptcha.test +++ b/recaptcha.test @@ -127,13 +127,17 @@ class ReCaptchaBasicTest extends DrupalWebTestCase { // Check if there is a reCAPTCHA on the login form. $this->drupalGet('user'); - $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); - $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); + $captcha_sid = $this->getCaptchaSid(); + $grecaptcha = '
'; + $this->assertRaw($grecaptcha, '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.' . $grecaptcha); + $this->assertRaw('', '[testReCaptchaOnLoginForm]: reCAPTCHA is shown on form.'); $this->assertNoRaw($grecaptcha . '