Add helper functions for reCAPTCHA extension
authorMatthew Wire <mjw@mjwconsult.co.uk>
Sun, 16 May 2021 19:07:21 +0000 (20:07 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Fri, 9 Jul 2021 17:14:01 +0000 (18:14 +0100)
CRM/Campaign/Form/Petition/Signature.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Form.php
CRM/Event/Form/Registration/AdditionalParticipant.php
CRM/Event/Form/Registration/Register.php
CRM/PCP/Form/PCPAccount.php
CRM/Profile/Form.php

index f60d55bc0e06a227454a143ed1a50dd4e2eef856..ae55710cd499cfe3f678a53909571db02ce80c0d 100644 (file)
@@ -152,6 +152,27 @@ class CRM_Campaign_Form_Petition_Signature extends CRM_Core_Form {
     return $session->get('userID');
   }
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    $ufGroupIDs = [];
+    if (!empty($this->_contactProfileId)) {
+      $ufGroupIDs[] = $this->_contactProfileId;
+    }
+    if (!empty($this->_activityProfileId)) {
+      $ufGroupIDs[] = $this->_activityProfileId;
+    }
+    return $ufGroupIDs;
+  }
+
   public function preProcess() {
     $this->bao = new CRM_Campaign_BAO_Petition();
     $this->_mode = self::MODE_CREATE;
index 6f1450dfe6bfe4805b3696a75638e9c29e03e029..818fde0b2a2c892190a610e996902d90e68847d7 100644 (file)
@@ -41,6 +41,35 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
   protected $_paymentProcessorID;
   protected $_snippet;
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    $ufGroupIDs = [];
+    if (!empty($this->_values['custom_pre_id'])) {
+      $ufGroupIDs[] = $this->_values['custom_pre_id'];
+    }
+    if (!empty($this->_values['custom_post_id'])) {
+      // custom_post_id can be an array (because we can have multiple for events).
+      // It is handled as array for contribution page as well though they don't support multiple profiles.
+      if (!is_array($this->_values['custom_post_id'])) {
+        $ufGroupIDs[] = $this->_values['custom_post_id'];
+      }
+      else {
+        $ufGroupIDs = array_merge($ufGroupIDs, $this->_values['custom_post_id']);
+      }
+    }
+
+    return $ufGroupIDs;
+  }
+
   /**
    * Set variables up before form is built.
    */
index 58e15df2afd756adfa737fdda66314c8ed165aa8..5ecbb1b0937eedb2a5359b9920fdb136402818da 100644 (file)
@@ -820,13 +820,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
   }
 
-  /**
-   * Check if ReCAPTCHA has to be added on Contribution form forcefully.
-   */
-  protected function hasToAddForcefully() {
-    return CRM_Utils_ReCAPTCHA::hasToAddForcefully();
-  }
-
   /**
    * Add onbehalf/honoree profile fields and native module fields.
    *
index ca3be72142f916ea096e7906e97936bcebd8de21..32f9ec17fcc585626c8e4283da59d17952fa7614 100644 (file)
@@ -2751,4 +2751,18 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     return $this->exportedValues[$fieldName] ?? NULL;
   }
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    return [];
+  }
+
 }
index 1d369f93ba57ac6d18b3dda4478083f162c149a8..e5614e018b289395357d40d155276bf87da32f52 100644 (file)
@@ -27,6 +27,26 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
    */
   public $additionalParticipantId = NULL;
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    $ufGroupIDs = [];
+    foreach (['pre', 'post'] as $keys) {
+      if (isset($this->_values['additional_custom_' . $keys . '_id'])) {
+        $ufGroupIDs[] = $this->_values['additional_custom_' . $keys . '_id'];
+      }
+    }
+    return $ufGroupIDs;
+  }
+
   /**
    * Set variables up before form is built.
    *
index d47d6afd4c23549c07dd6f418da9baac79021537..163badbf5e2b98d346f080e7a93a227dc9cede21 100644 (file)
@@ -128,6 +128,34 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     return $contactID;
   }
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    $ufGroupIDs = [];
+    if (!empty($this->_values['custom_pre_id'])) {
+      $ufGroupIDs[] = $this->_values['custom_pre_id'];
+    }
+    if (!empty($this->_values['custom_post_id'])) {
+      // custom_post_id can be an array (because we can have multiple for events).
+      // It is handled as array for contribution page as well though they don't support multiple profiles.
+      if (!is_array($this->_values['custom_post_id'])) {
+        $ufGroupIDs[] = $this->_values['custom_post_id'];
+      }
+      else {
+        $ufGroupIDs = array_merge($ufGroupIDs, $this->_values['custom_post_id']);
+      }
+    }
+    return $ufGroupIDs;
+  }
+
   /**
    * Set variables up before form is built.
    *
index 5be2e61e829acd370573af55e1d46d208bf0529d..9925bdbf70f095ae79ddbc6ab9a460dab2df367b 100644 (file)
@@ -35,6 +35,24 @@ class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
    */
   public $_single;
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    $ufGroupIDs = [];
+    if (!empty($this->_pageId)) {
+      $ufGroupIDs[] = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
+    }
+    return $ufGroupIDs;
+  }
+
   public function preProcess() {
     $session = CRM_Core_Session::singleton();
     $config = CRM_Core_Config::singleton();
@@ -117,21 +135,21 @@ class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
    * @return void
    */
   public function buildQuickForm() {
-    $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
-    if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) {
+    $ufGroupID = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
+    if (CRM_PCP_BAO_PCP::checkEmailProfile($ufGroupID)) {
       $this->assign('profileDisplay', TRUE);
     }
     $fields = NULL;
     if ($this->_contactID) {
-      if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) {
-        $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
+      if (CRM_Core_BAO_UFGroup::filterUFGroups($ufGroupID, $this->_contactID)) {
+        $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD);
       }
       $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
     }
     else {
-      CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE);
+      CRM_Core_BAO_CMSUser::buildForm($this, $ufGroupID, TRUE);
 
-      $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
+      $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD);
     }
 
     if ($fields) {
index af6642111c689844ff4b05d0982295738654ccab..e8e58cb7f74fb7c99ee754af2a6189a84a7aa316 100644 (file)
@@ -270,6 +270,34 @@ class CRM_Profile_Form extends CRM_Core_Form {
     return 'Profile';
   }
 
+  /**
+   * Get the active UFGroups (profiles) on this form
+   * Many forms load one or more UFGroups (profiles).
+   * This provides a standard function to retrieve the IDs of those profiles from the form
+   * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
+   *
+   * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
+   *
+   * @return array
+   */
+  public function getUFGroupIDs() {
+    if (empty($this->_profileIds)) {
+      $dao = new CRM_Core_DAO_UFGroup();
+      $dao->id = $this->_gid;
+      $this->_profileIds = (array) $dao;
+    }
+    return $this->_profileIds;
+  }
+
+  /**
+   * Are we using the profile in create mode?
+   *
+   * @return bool
+   */
+  public function getIsCreateMode() {
+    return ($this->_mode == self::MODE_CREATE);
+  }
+
   /**
    * Pre processing work done here.
    *