Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-20767

Unsubscribe action does not trigger hook_post

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 4.7.20
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Versioning Impact:
      Patch (backwards-compatible bug fixes)
    • Documentation Required?:
      None
    • Funding Source:
      Contributed Code
    • Verified?:
      No

      Description

      The _post hook is designed to be used to pick up on database writes, but as we know it isn't triggered in all circumstances. This leads to inconsistent behavior.

      For example, if setting 'is_opt_out' via the API or front-end forms, hook_post is triggered.

      However, if a user unsubscribes from a mailing, then the code:

            $contact = new CRM_Contact_BAO_Contact();
            $contact->id = $q->contact_id;
            $contact->is_opt_out = TRUE;
            $contact->save();
      

      is used (see https://github.com/civicrm/civicrm-core/blob/3359d0b7eb0719ed022f58ebe4e64de18b55e191/CRM/Mailing/Event/BAO/Unsubscribe.php#L88-L91)

      This does not trigger hook_post; but does trigger hook_postSave, which is called from Core_DAO.

      So let's update this code to use the API?

            civicrm_api3('contact', 'create', array(
              'id' => $q->contact_id,
              'is_opt_out' => 1
            ));
      

      As a follow up, the same pattern of using CRM_Contact_BAO_Contact() to quickly 'save' an update is infrequently used elsewhere:

      \CRM_Contact_BAO_Individual::format
      \CRM_Contact_Form_Task_AlterPreferences::postProcess

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              john John K.
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: