diff --git a/captcha.inc b/captcha.inc index 9c223ed..45150c1 100755 --- a/captcha.inc +++ b/captcha.inc @@ -38,6 +38,8 @@ function captcha_set_form_id_setting($form_id, $captcha_type) { $captcha_point->enable(); $captcha_point->save(); + + return $captcha_point; } /** diff --git a/captcha.module b/captcha.module index ea97613..ae5d91b 100755 --- a/captcha.module +++ b/captcha.module @@ -162,6 +162,11 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_ ->getStorage('captcha_point') ->load($form_id); + if(!$captcha_point && $config->get('enabled_default')){ + $captcha_point = captcha_set_form_id_setting($form_id, $config->get('default_challenge')); + $captcha_point->enable(); + } + if ($captcha_point && $captcha_point->status()) { // Build CAPTCHA form element. $captcha_element = [ diff --git a/config/install/captcha.settings.yml b/config/install/captcha.settings.yml index ac797b0..a2419cf 100755 --- a/config/install/captcha.settings.yml +++ b/config/install/captcha.settings.yml @@ -1,4 +1,5 @@ default_challenge: 'captcha/Math' +enabled_default: 1 description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.' administration_mode: false allow_on_admin_pages: false diff --git a/src/Form/CaptchaSettingsForm.php b/src/Form/CaptchaSettingsForm.php index 877123c..84b92b1 100755 --- a/src/Form/CaptchaSettingsForm.php +++ b/src/Form/CaptchaSettingsForm.php @@ -92,6 +92,13 @@ class CaptchaSettingsForm extends ConfigFormBase { '#default_value' => $config->get('default_challenge'), ]; + //Option for enabling CAPTCHA for all forms. + $form['form_protection']['enabled_default'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Default challenge on non-listed forms.'), + '#description' => $this->t('Enable CAPTCHA for every form not listed in "CAPTCHA points"'), + '#default_value' => $config->get('enabled_default'), + ]; // Field for the CAPTCHA administration mode. $form['form_protection']['administration_mode'] = [ '#type' => 'checkbox', @@ -213,6 +220,7 @@ class CaptchaSettingsForm extends ConfigFormBase { $config->set('administration_mode', $form_state->getValue('administration_mode')); $config->set('allow_on_admin_pages', $form_state->getValue('allow_on_admin_pages')); $config->set('default_challenge', $form_state->getValue('default_challenge')); + $config->set('enabled_default', $form_state->getValue('enabled_default')); // CAPTCHA description stuff. $config->set('add_captcha_description', $form_state->getValue('add_captcha_description'));