edited comment and if/else so that it is easier to read
authorKartik Kathuria <kathuriakartik0@gmail.com>
Sat, 1 May 2021 12:58:37 +0000 (18:28 +0530)
committerKartik Kathuria <kathuriakartik0@gmail.com>
Sat, 1 May 2021 12:58:37 +0000 (18:28 +0530)
ext/recaptcha/CRM/Utils/ReCAPTCHA.php

index 7c77951df4c57cd20c660716895c924060b83b4e..d44d348740ef09cf493273eb9ebeaf68f1c6b198 100644 (file)
@@ -63,32 +63,34 @@ class CRM_Utils_ReCAPTCHA {
   public static function add(&$form) {
     $error = NULL;
 
-    // If statement has been altered so that we do not need to check !$this->get_template_vars("isCaptcha") in CRM/Contribute/Form/Contribution/Main.php
-    if (!function_exists('recaptcha_get_html')) {
-      require_once E::path('lib/recaptcha/recaptchalib.php');
+    // If we already added reCAPTCHA then don't add it again.
+    // The `recaptcha_get_html` function only exists once recaptchalib.php has been included via this function.
+    if (function_exists('recaptcha_get_html')) {
+      return;
+    }
+    require_once E::path('lib/recaptcha/recaptchalib.php');
 
-      // Load the Recaptcha api.js over HTTPS
-      $useHTTPS = TRUE;
+    // Load the Recaptcha api.js over HTTPS
+    $useHTTPS = TRUE;
 
-      $html = recaptcha_get_html(\Civi::settings()->get('recaptchaPublicKey'), $error, $useHTTPS);
+    $html = recaptcha_get_html(\Civi::settings()->get('recaptchaPublicKey'), $error, $useHTTPS);
 
-      $form->assign('recaptchaHTML', $html);
-      $form->assign('recaptchaOptions', \Civi::settings()->get('recaptchaOptions'));
-      $form->add(
-        'text',
+    $form->assign('recaptchaHTML', $html);
+    $form->assign('recaptchaOptions', \Civi::settings()->get('recaptchaOptions'));
+    $form->add(
+      'text',
+      'g-recaptcha-response',
+      'reCaptcha',
+      NULL,
+      TRUE
+    );
+    $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
+    $form->addRule('g-recaptcha-response', E::ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
+    if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
+      $form->setElementError(
         'g-recaptcha-response',
-        'reCaptcha',
-        NULL,
-        TRUE
+        E::ts('Please go back and complete the CAPTCHA at the bottom of this form.')
       );
-      $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
-      $form->addRule('g-recaptcha-response', E::ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
-      if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
-        $form->setElementError(
-          'g-recaptcha-response',
-          E::ts('Please go back and complete the CAPTCHA at the bottom of this form.')
-        );
-      }
     }
   }