diff --git a/js/invisible-recaptcha.js b/js/invisible-recaptcha.js new file mode 100644 index 0000000..a6ddc02 --- /dev/null +++ b/js/invisible-recaptcha.js @@ -0,0 +1,28 @@ + +var submittedFormId = ''; + +function onInvisibleSubmit(token) { + document.getElementById(submittedFormId).submit(); +} + +(function ($) { + Drupal.behaviors.invisibleRecaptcha = { + attach: function (context) { + $('form', context).each(function () { + var $form = $(this); + + if ($form.find('.g-recaptcha[data-size="invisible"]').length) { + $form.find('input[type="submit"]').click(function (e) { + e.preventDefault(); + validateInvisibleCaptcha($form); + }); + } + }); + + function validateInvisibleCaptcha($form) { + submittedFormId = $form.attr('id'); + grecaptcha.execute(); + } + } + }; +})(jQuery); diff --git a/recaptcha.admin.inc b/recaptcha.admin.inc index bd2dc23..9e78116 100644 --- a/recaptcha.admin.inc +++ b/recaptcha.admin.inc @@ -62,10 +62,27 @@ function recaptcha_admin_settings() { '#options' => array( '' => t('Normal (default)'), 'compact' => t('Compact'), + 'invisible' => t('Invisible'), ), '#title' => t('Size'), '#type' => 'select', ); + $form['recaptcha_widget_settings']['recaptcha_badge'] = array( + '#default_value' => variable_get('recaptcha_badge', ''), + '#description' => t('Reposition the reCAPTCHA badge. "Inline" allows you to control the CSS.'), + '#options' => array( + 'bottomright ' => t('Bottom Right (default)'), + 'bottomleft ' => t('Bottom Left'), + 'inline' => t('Inline'), + ), + '#title' => t('Badge'), + '#type' => 'select', + '#states' => array( + 'visible' => array( + ':input[name="recaptcha_size"]' => array('value' => 'invisible'), + ), + ), + ); $form['recaptcha_widget_settings']['recaptcha_tabindex'] = array( '#type' => 'textfield', '#title' => t('Tabindex'), diff --git a/recaptcha.info b/recaptcha.info index 46e5082..2474645 100644 --- a/recaptcha.info +++ b/recaptcha.info @@ -15,3 +15,4 @@ files[] = recaptcha-php/src/ReCaptcha/RequestMethod/Post.php files[] = recaptcha-php/src/ReCaptcha/RequestMethod/Socket.php files[] = recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php files[] = src/ReCaptcha/RequestMethod/Drupal7Post.php +files[] = js/invisible-recaptcha.js diff --git a/recaptcha.install b/recaptcha.install index bd2cc6e..0c032eb 100644 --- a/recaptcha.install +++ b/recaptcha.install @@ -16,6 +16,7 @@ function recaptcha_uninstall() { variable_del('recaptcha_size'); variable_del('recaptcha_tabindex'); variable_del('recaptcha_noscript'); + variable_del('recaptcha_badge'); } /** diff --git a/recaptcha.module b/recaptcha.module index 1e65aab..9c3f01e 100644 --- a/recaptcha.module +++ b/recaptcha.module @@ -106,8 +106,17 @@ function recaptcha_captcha($op, $captcha_type = '') { 'data-theme' => variable_get('recaptcha_theme', 'light'), 'data-type' => variable_get('recaptcha_type', 'image'), 'data-size' => variable_get('recaptcha_size', ''), + 'data-badge' => variable_get('recaptcha_badge', ''), 'data-tabindex' => variable_get('recaptcha_tabindex', 0), ); + + if ('invisible' === $attributes['data-size']) { + $attributes['data-callback'] = 'onInvisibleSubmit'; + $captcha['form']['#attached']['js'] = array( + drupal_get_path('module', 'recaptcha') . '/js/invisible-recaptcha.js', + ); + } + // Filter out empty tabindex/size. $attributes = array_filter($attributes);