diff --git a/mailchimp.module b/mailchimp.module index 6b4cc52..ddb11f3 100644 --- a/mailchimp.module +++ b/mailchimp.module @@ -334,6 +334,32 @@ function mailchimp_get_memberinfo($list_id, $email, $reset = FALSE) { return $memberinfo; } + +/** + * Get the marketing permissions for a subscribed member + * + * Simple wrapper around mailchimp_get_memberinfo(). + * + * @param string $list_id + * Unique string identifier for the list on your MailChimp account. + * @param string $email + * Email address to check for on the identified MailChimp List. + * @param bool $reset + * Set to TRUE to ignore the cache. (Used heavily in testing functions.) + * + * @return array + * An array of marketing permissions, or an empty array if not subscribed + */ +function mailchimp_get_marketing_permissions($list_id, $email, $reset = FALSE) { + $memberinfo = mailchimp_get_memberinfo($list_id, $email, $reset); + if (isset($memberinfo->status) && $memberinfo->status == 'subscribed' && isset($memberinfo->marketing_permissions)) { + return $memberinfo->marketing_permissions; + } + else + return []; +} + + /** * Check if the given email is subscribed to the given list. * @@ -364,7 +390,7 @@ function mailchimp_is_subscribed($list_id, $email, $reset = FALSE) { * * @see Mailchimp_Lists::subscribe() */ -function mailchimp_subscribe($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $format = 'html') { +function mailchimp_subscribe($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $gdpr_consent = FALSE, $format = 'html') { $config = \Drupal::config('mailchimp.settings'); if ($config->get('cron')) { @@ -374,13 +400,14 @@ function mailchimp_subscribe($list_id, $email, $merge_vars = NULL, $interests = 'merge_vars' => $merge_vars, 'interests' => $interests, 'double_optin' => $double_optin, + 'gdpr_consent' => $gdpr_consent, 'format' => $format, ); return mailchimp_addto_queue('mailchimp_subscribe_process', $args); } - return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, $double_optin, $format); + return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, $double_optin, $format, $gdpr_consent); } /** @@ -388,7 +415,7 @@ function mailchimp_subscribe($list_id, $email, $merge_vars = NULL, $interests = * * @see Mailchimp_Lists::subscribe() */ -function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $format = 'html') { +function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $format = 'html', $gdpr_consent = FALSE) { $config = \Drupal::config('mailchimp.settings'); $result = FALSE; @@ -424,6 +451,23 @@ function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $inte $parameters['merge_fields'] = (object) $merge_vars; } + // Has GDPR consent been given? + if ($gdpr_consent) { + // if the member is already subscribed get the marketing permission id(s) + // for the list and enable them + $marketing_permissions = mailchimp_get_marketing_permissions($list_id, $email); + $was_subscribed = FALSE; + if ($marketing_permissions) { + foreach ($marketing_permissions as $marketing_permission) { + $parameters['marketing_permissions'][] = [ + 'marketing_permission_id' => $marketing_permission->marketing_permission_id, + 'enabled' => TRUE + ]; + } + $was_subscribed = TRUE; + } + } + // Add member to list. $result = $mc_lists->addOrUpdateMember($list_id, $email, $parameters); @@ -437,6 +481,26 @@ function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $inte 'email' => $email, 'list' => $list_id, )); + + // For newly subscribed members set GDPR consent if it's been given + if (!$was_subscribed && $gdpr_consent) { + // if the member is already subscribed get the marketing permission id(s) + // for the list and enable them + foreach ($result->marketing_permissions as $marketing_permission) { + $parameters['marketing_permissions'][] = [ + 'marketing_permission_id' => $marketing_permission->marketing_permission_id, + 'enabled' => TRUE + ]; + // Update the member + $result = $mc_lists->addOrUpdateMember($list_id, $email, $parameters); + if (!isset($result->id)) { + watchdog('mailchimp', 'A problem occurred setting marketing permissions for @email on list @list.', array( + '@email' => $email, + '@list' => $list_id, + ), WATCHDOG_WARNING); + } + } + } } else { if (!$config->get('test_mode')) { @@ -453,7 +517,7 @@ function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $inte 'email' => $email, 'list' => $list_id, )); - return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, TRUE, $format); + return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, TRUE, $format, $gdpr_consent); } \Drupal::logger('mailchimp')->error('An error occurred subscribing {email} to list {list}. "{message}"', array( @@ -495,7 +559,7 @@ function mailchimp_addto_queue($function, $args) { * * @see Mailchimp_Lists::updateMember() */ -function mailchimp_update_member($list_id, $email, $merge_vars, $interests = array(), $format = 'html', $double_optin = FALSE) { +function mailchimp_update_member($list_id, $email, $merge_vars, $interests = array(), $format = 'html', $double_optin = FALSE, $gdpr_consent) { $config = \Drupal::config('mailchimp.settings'); if ($config->get('cron')) { @@ -506,12 +570,13 @@ function mailchimp_update_member($list_id, $email, $merge_vars, $interests = arr 'interests' => $interests, 'format' => $format, 'double_optin' => $double_optin, + 'gdpr_consent' => $gdpr_consent, ); return mailchimp_addto_queue('mailchimp_update_member_process', $args); } - return mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, $double_optin); + return mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, $double_optin, $gdpr_consent); } /** @@ -519,7 +584,7 @@ function mailchimp_update_member($list_id, $email, $merge_vars, $interests = arr * * @see Mailchimp_Lists::updateMember() */ -function mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, $double_optin = FALSE) { +function mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, $double_optin = FALSE, $gdpr_consent = FALSE) { $result = FALSE; try { @@ -550,6 +615,23 @@ function mailchimp_update_member_process($list_id, $email, $merge_vars, $interes $parameters['merge_fields'] = (object) $merge_vars; } + // Has GDPR consent been given? + if ($gdpr_consent) { + // if the member is already subscribed get the marketing permission id(s) + // for the list and enable them + $marketing_permissions = mailchimp_get_marketing_permissions($list_id, $email); + $was_subscribed = FALSE; + if ($marketing_permissions) { + foreach ($marketing_permissions as $marketing_permission) { + $parameters['marketing_permissions'][] = [ + 'marketing_permission_id' => $marketing_permission->marketing_permission_id, + 'enabled' => TRUE + ]; + } + $was_subscribed = TRUE; + } + } + // Update member. $result = $mcapi->updateMember($list_id, $email, $parameters); @@ -576,7 +658,7 @@ function mailchimp_update_member_process($list_id, $email, $merge_vars, $interes 'list' => $list_id, )); - return mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, TRUE); + return mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, TRUE, $gdpr_consent); } \Drupal::logger('mailchimp')->error('An error occurred updating {email} on list {list}. "{message}"', array( diff --git a/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php b/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php index 182cdd7..0272c07 100644 --- a/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php +++ b/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php @@ -227,6 +227,42 @@ class MailchimpSignupForm extends EntityForm { ), ); + $form['subscription_settings']['gdpr_consent'] = array( + '#type' => 'checkbox', + '#title' => t('Add a GDPR consent checkbox'), + '#description' => t('Add a GDPR consent checkbox to the signup form that syncs with the Mailchimp marketing permission field.'), + '#default_value' => isset($signup->settings['gdpr_consent']) ? $signup->settings['gdpr_consent'] : FALSE, + ); + + $form['subscription_settings']['gdpr_checkbox_label'] = array( + '#type' => 'textarea', + '#title' => t('Consent checkbox label'), + '#description' => t('The label to display on the GDPR consent checkbox, for example "I agree to the privacy policy". This should coincide with what you have in Mailchimp!'), + '#default_value' => isset($signup->settings['gdpr_checkbox_label']) ? $signup->settings['gdpr_checkbox_label'] : NULL, + '#states' => array( + // Hide unless needed. + 'visible' => array( + ':input[name="gdpr_consent"]' => array('checked' => TRUE), + ), + 'required' => array( + ':input[name="gdpr_consent"]' => array('checked' => TRUE), + ), + ), + ); + + $form['subscription_settings']['gdpr_consent_required'] = array( + '#type' => 'checkbox', + '#title' => t('GDPR consent required'), + '#description' => t('Make the GDPR consent checkbox a required field.'), + '#default_value' => isset($signup->settings['gdpr_consent_required']) ? $signup->settings['gdpr_consent_required'] : FALSE, + '#states' => array( + // Hide unless needed. + 'visible' => array( + ':input[name="gdpr_consent"]' => array('checked' => TRUE), + ), + ), + ); + return $form; } @@ -265,6 +301,9 @@ class MailchimpSignupForm extends EntityForm { $signup->settings['doublein'] = $form_state->getValue('doublein'); $signup->settings['include_interest_groups'] = $form_state->getValue('include_interest_groups'); $signup->settings['safe_interest_groups'] = $form_state->getValue('safe_interest_groups'); + $signup->settings['gdpr_consent'] = $form_state->getValue('gdpr_consent'); + $signup->settings['gdpr_checkbox_label'] = $form_state->getValue('gdpr_checkbox_label'); + $signup->settings['gdpr_consent_required'] = $form_state->getValue('gdpr_consent_required'); // Clear path value if mode doesn't include signup page. if (!isset($mode[MAILCHIMP_SIGNUP_PAGE])) { diff --git a/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php b/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php index f034fa5..881d619 100644 --- a/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php +++ b/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php @@ -101,6 +101,16 @@ class MailchimpSignupPageForm extends FormBase { ); $form['mailchimp_lists'][$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($list); } + + // Include the GDPR consent checkbox if necessary + if ($this->signup->settings['gdpr_consent']) { + $form['mailchimp_lists'][$wrapper_key]['gdpr_consent'] = array( + '#type' => 'checkbox', + '#default_value' => isset($this->signup->settings['gdpr_checkbox_label']) ? $this->signup->settings['gdpr_checkbox_label'] : NULL, + '#title' => $this->signup->settings['gdpr_checkbox_label'], + '#required' => isset($this->signup->settings['gdpr_consent_required']) ? $this->signup->settings['gdpr_consent_required'] : FALSE, + ); + } } } else { @@ -109,6 +119,16 @@ class MailchimpSignupPageForm extends FormBase { $form['mailchimp_lists']['#weight'] = 9; $form['mailchimp_lists']['interest_groups'] = mailchimp_interest_groups_form_elements($list); } + // Include the GDPR consent checkbox if necessary + if ($this->signup->settings['gdpr_consent']) { + $form['mailchimp_lists']['#weight'] = 9; + $form['mailchimp_lists']['gdpr_consent'] = array( + '#type' => 'checkbox', + '#default_value' => isset($this->signup->settings['gdpr_checkbox_label']) ? $this->signup->settings['gdpr_checkbox_label'] : NULL, + '#title' => $this->signup->settings['gdpr_checkbox_label'], + '#required' => isset($this->signup->settings['gdpr_consent_required']) ? $this->signup->settings['gdpr_consent_required'] : FALSE, + ); + } } $mergevars_wrapper_id = isset($list->id) ? $list->id : ''; @@ -190,6 +210,7 @@ class MailchimpSignupPageForm extends FormBase { $subscribe_lists[0] = array( 'subscribe' => reset($this->signup->mc_lists), 'interest_groups' => isset($mailchimp_lists['interest_groups']) ? $mailchimp_lists['interest_groups'] : NULL, + 'gdpr_consent' => isset($mailchimp_lists['gdpr_consent']) ? $mailchimp_lists['gdpr_checkbox_label'] : NULL, ); } else { @@ -220,7 +241,8 @@ class MailchimpSignupPageForm extends FormBase { $interests[] = $current_interests; } } - $result = mailchimp_subscribe($list_id, $email, $mergevars, $interests, $this->signup->settings['doublein']); + + $result = mailchimp_subscribe($list_id, $email, $mergevars, $interests, $this->signup->settings['doublein'], $this->signup->settings['gdpr_consent']); if (empty($result)) { drupal_set_message(t('There was a problem with your newsletter signup to %list.', array(