From 268ff0e8c0611d2c0a88b5535f4eaf8b175f473a Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Wed, 12 Jun 2019 18:09:19 +0100 Subject: [PATCH] Deduplicate recaptcha handling --- CRM/Campaign/Form/Petition/Signature.php | 4 +--- CRM/Contribute/Form/ContributionBase.php | 12 +++--------- CRM/Event/Form/Registration.php | 4 +--- CRM/Mailing/Form/Subscribe.php | 5 +---- CRM/Profile/Form.php | 4 +--- CRM/Utils/ReCAPTCHA.php | 13 +++++++++++++ 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/CRM/Campaign/Form/Petition/Signature.php b/CRM/Campaign/Form/Petition/Signature.php index f39166cebf..29e398ef18 100644 --- a/CRM/Campaign/Form/Petition/Signature.php +++ b/CRM/Campaign/Form/Petition/Signature.php @@ -605,9 +605,7 @@ class CRM_Campaign_Form_Petition_Signature extends CRM_Core_Form { } if ($addCaptcha && !$viewOnly) { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - $captcha->add($this); - $this->assign("isCaptcha", TRUE); + CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); } } } diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 18fcdc93be..2e95581344 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -749,11 +749,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { * Enable ReCAPTCHA on Contribution form */ protected function enableCaptchaOnForm() { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - if ($captcha->hasSettingsAvailable()) { - $captcha->add($this); - $this->assign('isCaptcha', TRUE); - } + CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); } public function assignPaymentFields() { @@ -813,8 +809,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { */ protected function displayCaptchaWarning() { if (CRM_Core_Permission::check("administer CiviCRM")) { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - if (!$captcha->hasSettingsAvailable()) { + if (!CRM_Utils_ReCAPTCHA::hasSettingsAvailable()) { $this->assign('displayCaptchaWarning', TRUE); } } @@ -824,8 +819,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { * Check if ReCAPTCHA has to be added on Contribution form forcefully. */ protected function hasToAddForcefully() { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - return $captcha->hasToAddForcefully(); + return CRM_Utils_ReCAPTCHA::hasToAddForcefully(); } /** diff --git a/CRM/Event/Form/Registration.php b/CRM/Event/Form/Registration.php index af19e44236..070125c337 100644 --- a/CRM/Event/Form/Registration.php +++ b/CRM/Event/Form/Registration.php @@ -618,9 +618,7 @@ class CRM_Event_Form_Registration extends CRM_Core_Form { } if ($addCaptcha && !$viewOnly) { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - $captcha->add($this); - $this->assign('isCaptcha', TRUE); + CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); } } } diff --git a/CRM/Mailing/Form/Subscribe.php b/CRM/Mailing/Form/Subscribe.php index 6a8feae66b..56e34f1e26 100644 --- a/CRM/Mailing/Form/Subscribe.php +++ b/CRM/Mailing/Form/Subscribe.php @@ -147,10 +147,7 @@ ORDER BY title"; } if ($addCaptcha) { - // add captcha - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - $captcha->add($this); - $this->assign('isCaptcha', TRUE); + CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); } $this->addButtons([ diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php index f5d605dfb3..9052831d2b 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -891,10 +891,8 @@ class CRM_Profile_Form extends CRM_Core_Form { //finally add captcha to form. if ($this->_isAddCaptcha) { - $captcha = CRM_Utils_ReCAPTCHA::singleton(); - $captcha->add($this); + CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); } - $this->assign("isCaptcha", $this->_isAddCaptcha); if ($this->_mode != self::MODE_SEARCH) { if (isset($addToGroupId)) { diff --git a/CRM/Utils/ReCAPTCHA.php b/CRM/Utils/ReCAPTCHA.php index 3198043014..fab82510ce 100644 --- a/CRM/Utils/ReCAPTCHA.php +++ b/CRM/Utils/ReCAPTCHA.php @@ -120,4 +120,17 @@ class CRM_Utils_ReCAPTCHA { } } + /** + * Enable ReCAPTCHA on Contribution form + * + * @param CRM_Core_Form $form + */ + public static function enableCaptchaOnForm(&$form) { + $captcha = CRM_Utils_ReCAPTCHA::singleton(); + if ($captcha->hasSettingsAvailable()) { + $captcha->add($form); + $form->assign('isCaptcha', TRUE); + } + } + } -- 2.25.1