diff --git a/recaptcha.module b/recaptcha.module index e0e31e6..6c34789 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -72,7 +72,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) { @@ -105,6 +105,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'), @@ -125,12 +126,29 @@ 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'); + + // 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. @@ -190,5 +208,12 @@ function recaptcha_captcha_validation($solution, $response, $element, $form_stat function template_preprocess_recaptcha_widget_noscript(&$variables) { $variables['sitekey'] = check_plain($variables['widget']['sitekey']); $variables['language'] = check_plain($variables['widget']['language']); - $variables['url'] = check_url(url('https://www.google.com/recaptcha/api/fallback', array('query' => array('k' => $variables['widget']['sitekey'], 'hl' => $variables['widget']['language']), 'absolute' => TRUE))); + $variables['url'] = check_url( + url('https://www.google.com/recaptcha/api/fallback', array( + 'query' => array( + 'k' => $variables['widget']['sitekey'], + 'hl' => $variables['widget']['language'], + ), + 'absolute' => TRUE, + ))); } diff --git a/recaptcha.test b/recaptcha.test index 48b409b..8f92fd5 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 . '