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 old mode 100755 new mode 100644 index ea97613..ae5d91b --- 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..ffbfcfb 100755 --- a/config/install/captcha.settings.yml +++ b/config/install/captcha.settings.yml @@ -1,3 +1,4 @@ +enabled_default: 0 default_challenge: 'captcha/Math' description: 'This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.' administration_mode: false diff --git a/config/schema/captcha.settings.yml b/config/schema/captcha.settings.yml index 3cdcddb..b91cb03 100755 --- a/config/schema/captcha.settings.yml +++ b/config/schema/captcha.settings.yml @@ -5,6 +5,9 @@ captcha.settings: type: config_object label: 'External Links Settings' mapping: + enabled_default: + type: integer + label: 'Add captcha to all forms.' default_challenge: type: string label: 'The default challenge for captcha.' diff --git a/src/Form/CaptchaSettingsForm.php b/src/Form/CaptchaSettingsForm.php old mode 100755 new mode 100644 index 877123c..84b92b1 --- 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'));