Remove requirement to enter email address to unsubscribe
authorMatthew Wire <mjw@mjwconsult.co.uk>
Wed, 18 Aug 2021 18:01:05 +0000 (19:01 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Mon, 6 Sep 2021 15:12:35 +0000 (16:12 +0100)
CRM/Admin/Form/Preferences/Mailing.php
CRM/Mailing/Form/Unsubscribe.php
templates/CRM/Mailing/Form/Unsubscribe.tpl

index b2f4a975dc32a8317860a6e3b5a519604e1d1870..726a31dd4e208c44ff6603e72e1fd65ee5e00400 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 /**
- * This class generates form components for the maling component preferences.
+ * This class generates form components for the mailing component preferences.
  */
 class CRM_Admin_Form_Preferences_Mailing extends CRM_Admin_Form_Preferences {
 
index 72afaa5b999dfeb4fb1cecbb02487b55354236e5..cd804324aa25dc6a105bd24678e7ed1e10210115 100644 (file)
@@ -24,18 +24,32 @@ class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form {
    */
   public $submitOnce = TRUE;
 
-  public function preProcess() {
+  /**
+   * @var int
+   */
+  private $_job_id;
+
+  /**
+   * @var int
+   */
+  private $_queue_id;
 
-    $this->_type = 'unsubscribe';
+  /**
+   * @var string
+   */
+  private $_hash;
+
+  /**
+   * @var string
+   */
+  private $_email;
 
+  public function preProcess() {
     $this->_job_id = $job_id = CRM_Utils_Request::retrieve('jid', 'Integer', $this);
     $this->_queue_id = $queue_id = CRM_Utils_Request::retrieve('qid', 'Integer', $this);
     $this->_hash = $hash = CRM_Utils_Request::retrieve('h', 'String', $this);
 
-    if (!$job_id ||
-      !$queue_id ||
-      !$hash
-    ) {
+    if (!$job_id || !$queue_id || !$hash) {
       throw new CRM_Core_Exception(ts('Missing Parameters'));
     }
 
@@ -55,28 +69,22 @@ class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form {
     $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash, TRUE);
     $this->assign('groups', $groups);
     $groupExist = NULL;
-    foreach ($groups as $key => $value) {
+    foreach ($groups as $value) {
       if ($value) {
         $groupExist = TRUE;
       }
     }
     if (!$groupExist) {
-      $statusMsg = ts('%1 has been unsubscribed.',
-        [1 => $email]
-      );
+      $statusMsg = ts('%1 has been unsubscribed.', [1 => $email]);
       CRM_Core_Session::setStatus($statusMsg, '', 'error');
     }
     $this->assign('groupExist', $groupExist);
-
   }
 
   public function buildQuickForm() {
     CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
     $this->setTitle(ts('Unsubscribe Confirmation'));
 
-    $this->add('text', 'email_confirm', ts('Verify email address to unsubscribe:'));
-    $this->addRule('email_confirm', ts('Email address is required to unsubscribe.'), 'required');
-
     $buttons = [
       [
         'type' => 'next',
@@ -93,42 +101,19 @@ class CRM_Mailing_Form_Unsubscribe extends CRM_Core_Form {
   }
 
   public function postProcess() {
-    $values = $this->exportValues();
-
-    // check if EmailTyped matches Email address
-    $result = CRM_Utils_String::compareStr($this->_email, $values['email_confirm'], TRUE);
-    $job_id = $this->_job_id;
-    $queue_id = $this->_queue_id;
-    $hash = $this->_hash;
-
-    $confirmURL = CRM_Utils_System::url("civicrm/mailing/{$this->_type}", "reset=1&jid={$job_id}&qid={$queue_id}&h={$hash}&confirm=1");
+    $confirmURL = CRM_Utils_System::url("civicrm/mailing/unsubscribe", "reset=1&jid={$this->_job_id}&qid={$this->_queue_id}&h={$this->_hash}&confirm=1");
     $this->assign('confirmURL', $confirmURL);
-    $session = CRM_Core_Session::singleton();
-    $session->pushUserContext($confirmURL);
+    CRM_Core_Session::singleton()->pushUserContext($confirmURL);
 
-    if ($result == TRUE) {
-      // Email address verified
-      $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job_id, $queue_id, $hash);
+    // Email address verified
+    $groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($this->_job_id, $this->_queue_id, $this->_hash);
 
-      if (count($groups)) {
-        CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue_id, $groups, FALSE, $job_id);
-      }
-
-      $statusMsg = ts('%1 is unsubscribed.',
-        [1 => $values['email_confirm']]
-      );
-
-      CRM_Core_Session::setStatus($statusMsg, '', 'success');
+    if (count($groups)) {
+      CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($this->_queue_id, $groups, FALSE, $this->_job_id);
     }
-    elseif ($result == FALSE) {
-      // Email address not verified
-      $statusMsg = ts('%1 is not associated with this unsubscribe request.',
-        [1 => $values['email_confirm']]
-      );
 
-      CRM_Core_Session::setStatus($statusMsg, '', 'error');
-
-    }
+    $statusMsg = ts('%1 is unsubscribed.', [1 => CRM_Utils_String::maskEmail($this->_email)]);
+    CRM_Core_Session::setStatus($statusMsg, '', 'success');
   }
 
 }
index 6a1f97e792ae849ba2ad03fd3d7e44b265d75904..8cf750653016d43ff606161078d46e5ed65cfb0c 100644 (file)
     <div class="crm-block crm-form-block crm-miscellaneous-form-block">
       <p>{ts}You are requesting to unsubscribe this email address:{/ts}</p>
       <h3>{$email_masked}</h3>
-      <p>{ts}If this is not your email address, there is no need to do anything. You have <i><b>not</b></i> been added to any mailing lists. If this is your email address and you <i><b>wish to unsubscribe</b></i> please enter your email address below for verification purposes:{/ts}</p>
-      <table class="form-layout">
-        <tbody>
-          <tr>
-            <td class="label">{$form.email_confirm.label}</td>
-            <td class="content">{$form.email_confirm.html}</td>
-          </tr>
-        </tbody>
-      </table>
+      <p>
+        {ts}If this is not your email address, there is no need to do anything. You have <strong>not</strong> been added to any mailing lists.{/ts}
+        {ts}If this is your email address and you <strong>wish to unsubscribe</strong> please click the <strong>Unsubscribe</strong> button to confirm.{/ts}
+      </p>
       <div class="crm-submit-buttons">
         {include file="CRM/common/formButtons.tpl" location="bottom"}
       </div>