<commit_before><?php

/**
 * Description of MailActions
 *
 * @author jeremy
 */
class MailActions extends Actions
{
  public static function executeListSubscribe()
  {
    $nextUrl = isset($_POST['returnUrl']) && $_POST['returnUrl'] ? $_POST['returnUrl'] : '/join-list';

    if (!Request::isPost())
    {
      return Controller::redirect($nextUrl);
    }

    Session::set(Session::KEY_LIST_SUB_SIGNATURE, $_POST['listSig'] ?? true);

    $email = $_POST['email'];
    if (!$email|| !filter_var($email, FILTER_VALIDATE_EMAIL))
    {
      Session::set(Session::KEY_LIST_SUB_ERROR, $email ? __('Please provide a valid email address.') : __('Please provide an email address.'));
    }
    elseif (!$_POST['listId'])
    {
      Session::set(Session::KEY_LIST_SUB_ERROR, __('List not provided.'));
    }
    else
    {
      $mcListId = $_POST['listId'];
      $mergeFields = isset($_POST['mergeFields']) ? unserialize($_POST['mergeFields']) : [];
      $errorOrSuccess = static::subscribeToMailchimp($email, $mcListId, $mergeFields);

      if ($errorOrSuccess === true)
      {
        Session::set(Session::KEY_MAILCHIMP_LIST_IDS, array_merge(Session::get(Session::KEY_MAILCHIMP_LIST_IDS, []), [$mcListId]));
        Session::set(Session::KEY_LIST_SUB_SUCCESS, true);
        Session::set(Session::KEY_LIST_SUB_FB_EVENT, $_POST['fbEvent'] ?? null);
      }
      else
      {
        Session::set(Session::KEY_LIST_SUB_ERROR, $errorOrSuccess);
      }
    }

    return Controller::redirect($nextUrl);
  }

  public static function subscribeToMailchimp($email, $listId, array $mergeFields = [])
  {
    $mcApi   = new Mailchimp();
    $success = $mcApi->listSubscribe($listId, $email, $mergeFields, 'html', false);
    if (!$success)
    {
      throw new MailchimpSubscribeException($mcApi->errorMessage ?: __('Something went wrong adding you to the list.'));
    }
    return true;
  }

  public static function prepareJoinListPartial(array $vars)
  {
    $vars['listSig'] = md5(serialize($vars));
    $vars += ['btnClass' => 'btn-primary', 'returnUrl' => Request::getRelativeUri()];

    if (Session::get(Session::KEY_LIST_SUB_SIGNATURE) == $vars['listSig'])
    {
      $vars['error'] = Session::get(Session::KEY_LIST_SUB_ERROR);<commit_msg>fix mailing list subscribe<commit_after><?php

/**
 * Description of MailActions
 *
 * @author jeremy
 */
class MailActions extends Actions
{
  public static function executeListSubscribe()
  {
    $nextUrl = isset($_POST['returnUrl']) && $_POST['returnUrl'] ? $_POST['returnUrl'] : '/join-list';

    if (!Request::isPost())
    {
      return Controller::redirect($nextUrl);
    }

    Session::set(Session::KEY_LIST_SUB_SIGNATURE, $_POST['listSig'] ?? true);

    $email = $_POST['email'];
    if (!$email|| !filter_var($email, FILTER_VALIDATE_EMAIL))
    {
      Session::set(Session::KEY_LIST_SUB_ERROR, $email ? __('Please provide a valid email address.') : __('Please provide an email address.'));
    }
    elseif (!$_POST['listId'])
    {
      Session::set(Session::KEY_LIST_SUB_ERROR, __('List not provided.'));
    }
    else
    {
      $mcListId = $_POST['listId'];
      $mergeFields = isset($_POST['mergeFields']) ? unserialize($_POST['mergeFields']) : [];
      try
      {
        static::subscribeToMailchimp($email, $mcListId, $mergeFields);
        Session::set(Session::KEY_MAILCHIMP_LIST_IDS, array_merge(Session::get(Session::KEY_MAILCHIMP_LIST_IDS, []), [$mcListId]));
        Session::set(Session::KEY_LIST_SUB_SUCCESS, true);
        Session::set(Session::KEY_LIST_SUB_FB_EVENT, $_POST['fbEvent'] ?? null);
      }
      catch (MailchimpSubscribeException $e)
      {
        Session::set(Session::KEY_LIST_SUB_SUCCESS, false);
        Session::set(Session::KEY_LIST_SUB_ERROR, $e->getMessage());
      }
    }

    return Controller::redirect($nextUrl);
  }

  public static function subscribeToMailchimp($email, $listId, array $mergeFields = [])
  {
    $mcApi   = new Mailchimp();
    $success = $mcApi->listSubscribe($listId, $email, $mergeFields, 'html', false);
    if (!$success)
    {
      throw new MailchimpSubscribeException($mcApi->errorMessage ?: __('Something went wrong adding you to the list.'));
    }
    return true;
  }

  public static function prepareJoinListPartial(array $vars)
  {
    $vars['listSig'] = md5(serialize($vars));
    $vars += ['btnClass' => 'btn-primary', 'returnUrl' => Request::getRelativeUri()];

    if (Session::get(Session::KEY_LIST_SUB_SIGNATURE) == $vars['listSig'])
    {
      $vars['error'] = Session::get(Session::KEY_LIST_SUB_ERROR);
{'fraction_non_alphanumeric': 0.16684937513703135, 'fraction_numerical': 0.0004385003288752467, 'mean_word_length': 4.108622620380739, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7645422', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1431, 'n_words': 253}