From ffcb7ca1f54f999f7263eaeaa9b223565e341145 Mon Sep 17 00:00:00 2001 From: Kartik Kathuria Date: Sat, 1 May 2021 18:28:37 +0530 Subject: [PATCH] edited comment and if/else so that it is easier to read --- ext/recaptcha/CRM/Utils/ReCAPTCHA.php | 44 ++++++++++++++------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/ext/recaptcha/CRM/Utils/ReCAPTCHA.php b/ext/recaptcha/CRM/Utils/ReCAPTCHA.php index 7c77951df4..d44d348740 100644 --- a/ext/recaptcha/CRM/Utils/ReCAPTCHA.php +++ b/ext/recaptcha/CRM/Utils/ReCAPTCHA.php @@ -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.') - ); - } } } -- 2.25.1