You are here

function campaignmonitor_user_updatex in Campaign Monitor 8

Implements hook_user_update().

Makes sure that user e-mail addresses are synchronized with Campaign Monitor when user accounts are updated, if this option has been enabledin the administration interface.

File

modules/campaignmonitor_user/campaignmonitor_user.module, line 110
Adds a tab to the profile page that enables users to select which newsletters they want to subscribe to.

Code

function campaignmonitor_user_updatex(&$edit, $account, $category) {

  // In $edit['mail'] we have the entered e-mail address, and in
  // $account->original->mail the original one before editing the account. If
  // they are equal, we do nothing.
  if (isset($edit['mail']) && $edit['mail'] != $account->original->mail && $category == 'account') {

    // Get Campaign Monitor settings and check if the e-mail address should be
    // synchronized.
    $settings = variable_get('campaignmonitor_general', []);
    if (isset($settings['synchronize']) && $settings['synchronize']) {
      $cm = CampaignMonitor::getConnector();
      $lists_info = $cm
        ->getLists();
      $lists_to_update = [];
      foreach ($lists_info as $list_id => $list) {

        // Check if the list is selected to be shown.
        $list_options = variable_get('campaignmonitor_list_' . $list_id, []);
        if (campaignmonitor_is_list_enabled($list_id) && isset($list_options['display']['user']) && $list_options['display']['user']) {

          // Check if the user is subscribed to the current list, but not yet
          // subscribed with the new e-mail.
          if ($cm
            ->isSubscribed($list_id, $account->original->mail) && !$cm
            ->isSubscribed($list_id, $edit['mail'])) {
            if ($cm
              ->updateSubscriberEmail($list_id, $account->original->mail, $edit['mail'])) {
              drupal_set_message(t('Your e-mail adress has been updated for the "@list" list.', [
                '@list' => $list['name'],
              ]), 'status');
            }
            else {
              drupal_set_message(t('Your e-mail adress has not been updated for the "@list" list.', [
                '@list' => $list['name'],
              ]), 'error');
            }
          }
        }
      }
    }
  }
}