} else {
     // account added successfully.
     // check account status
     $account_status = $ac->api("account/status?account={$account_name}");
     //dbg($account_status);
     while ($account_status->status == "creating") {
         echo "<p style=\"background-color: yellow;\">Account still being created... (waiting 15 seconds before checking again)</p>";
         sleep(15);
         $account_status = $ac->api("account/status?account={$account_name}");
     }
     if ($account_status->status != "active") {
         $alert = "Account Status is " . $account_status->status;
     } else {
         $_SESSION["account_admin_password"] = $account->password;
         // obtain this account's API URL and Key
         $ac2 = new ActiveCampaign("https://{$account_name}.activehosted.com", null, "admin", $account->password);
         $user_me = $ac2->api("user/me");
         // save account-specific API connection info to session
         $_SESSION["account_api_url"] = $user_me->apiurl;
         $_SESSION["account_api_key"] = $user_me->apikey;
         // create new user in this account.
         $user = array("username" => "user1", "password" => "test", "password_r" => "test", "email" => "*****@*****.**", "first_name" => "User", "last_name" => "One", "group[3]" => "3");
         $user_add = $ac2->api("user/add", $user);
         // create "New List" webhook?
         /*
         $webhook = array(
         	"name" => "New List hook",
         	"url" => "http://requestb.in/hajsgd",
         	"action[list_add]" => "list_add",
         	"init[public]" => "public",
         	"init[admin]" => "admin",
<?php

require_once "includes/ActiveCampaign.class.php";
$ac = new ActiveCampaign("API_URL", "API_KEY");
/*
 * TEST API CREDENTIALS.
 */
if (!(int) $ac->credentials_test()) {
    echo "<p>Access denied: Invalid credentials (URL and/or API key).</p>";
    exit;
}
echo "<p>Credentials valid! Proceeding...</p>";
/*
 * VIEW ACCOUNT DETAILS.
 */
$account = $ac->api("account/view");
echo "<pre>";
print_r($account);
echo "</pre>";
/*
 * ADD NEW LIST.
 */
$list = array("name" => "List 3", "sender_name" => "My Company", "sender_addr1" => "123 S. Street", "sender_city" => "Chicago", "sender_zip" => "60601", "sender_country" => "USA");
$list_add = $ac->api("list/add", $list);
if (!(int) $list_add->success) {
    // request failed
    echo "<p>Adding list failed. Error returned: " . $list_add->error . "</p>";
    exit;
}
// successful request
$list_id = (int) $list_add->id;
 public function ar_post_content($entry, $form)
 {
     $settings = get_option("gf_activecampaign_settings");
     $ac = new ActiveCampaign($settings["url"], $settings["token"]);
     $ar_first_name = '';
     $ar_last_name = '';
     foreach ($form['fields'] as $key => $value) {
         if ($value['inputName'] == 'ar_list_id') {
             $id = $value['id'];
             $list_id = $entry[$id];
         } else {
             $list_id = $settings["gf_activecampaign_list_id"];
         }
         if ($value['inputName'] == 'ar_first_name') {
             $id = $value['id'];
             $ar_first_name = $entry[$id];
         }
         if ($value['inputName'] == 'ar_last_name') {
             $id = $value['id'];
             $ar_last_name = $entry[$id];
         }
         if ($value['inputName'] == 'ar_email') {
             $id = $value['id'];
             $ar_email = $entry[$id];
         }
         if (empty($ar_first_name) || empty($ar_last_name)) {
             if (!empty($value['inputs'])) {
                 foreach ($value['inputs'] as $key => $field) {
                     if ($field['name'] == 'ar_first_name') {
                         $id = $field['id'];
                         $ar_first_name = $entry["{$id}"];
                     }
                     if ($field['name'] == 'ar_last_name') {
                         $id = $field['id'];
                         $ar_last_name = $entry["{$id}"];
                     }
                 }
             }
         }
     }
     $contact = array("email" => $ar_email, "first_name" => $ar_first_name, "last_name" => $ar_last_name, "p[{$list_id}]" => $list_id, "status[{$list_id}]" => 1);
     if ($ac) {
         $message = (int) $ac->credentials_test();
         if ($message == true) {
             $contact_sync = $ac->api("contact/sync", $contact);
         }
     }
 }
Description: AC Split Test
Version: 1.0
Author: Henry Reith
Author URI: http://henryreith.co/
License: GPLv2 or later
*/
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $api_url = "";
    // << ---- Add your Active Campaign API URL should go in here
    $api_key = "";
    // << ---- Add your Active Campaign API Key should go in here
    define("ACTIVECAMPAIGN_URL", $api_url);
    define("ACTIVECAMPAIGN_API_KEY", $api_key);
    require_once "includes/ActiveCampaign.class.php";
    // << ---- path to the included Active Campaign API file: ActiveCampaign.class.php
    $ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY);
    $webhook_data = $_POST;
    // basic validation check
    // if (!isset($webhook_data["contact"]["id"])) exit(); I'm not validating the data but you could if you wanted to
    // Just get the contact ID & email detail
    $contact_id = $webhook_data["contact"]["id"];
    $contact_email = $webhook_data["contact"]["email"];
    // Decide if id is Odd or even and do
    if ($contact_id % 2) {
        // If odd add this tag
        $contact = array("email" => $contact_email, 'tags' => 'HR-Split-A');
        $contact_sync = $ac->api("contact/sync", $contact);
        // sync with AC
    } else {
        // If even add this tag
        $contact = array("email" => $contact_email, 'tags' => 'HR-Split-B');
 /**
  * Get all the Active Campaign lists
  *
  * @access public
  * @return void
  */
 function nnr_new_int_get_activecampaign_lists_v1()
 {
     do_action('nnr_news_int_before_get_activecampaign_lists_v1');
     require_once dirname(dirname(__FILE__)) . '/services/activecampaign/ActiveCampaign.class.php';
     $options = '';
     if (isset($_POST['app_url']) && $_POST['app_url'] != '' && isset($_POST['api_key']) && $_POST['api_key'] != '') {
         $ac = new ActiveCampaign($_POST['app_url'], $_POST['api_key']);
         if (!(int) $ac->credentials_test()) {
             echo "";
             exit;
         }
         $lists = $ac->api("list/list?ids=all");
         foreach ($lists as $list) {
             if (!isset($list->id) || empty($list->id)) {
                 continue;
             }
             if ($_POST['list'] == $list->id) {
                 $options .= '<option value="' . $list->id . '" selected="selected">' . $list->name . '</option>';
             } else {
                 $options .= '<option value="' . $list->id . '">' . $list->name . '</option>';
             }
         }
     }
     do_action('nnr_news_int_before_get_activecampaign_lists_v1');
     echo apply_filters('nnr_news_int_get_activecampaign_lists_v1', $options);
     die;
 }
 /**
  * Record an impression made by a optin fire
  * This function is called via PHP.
  *
  * @access public
  * @static
  * @return void
  */
 function nnr_new_int_add_email_v1()
 {
     do_action('nnr_news_int_before_submission_add_email_v1');
     // No First Name
     if (!isset($_POST['first_name'])) {
         $_POST['first_name'] = '';
     }
     // No Last Name
     if (!isset($_POST['last_name'])) {
         $_POST['last_name'] = '';
     }
     // Could not find Data ID
     if (!isset($_POST['data_id']) || $_POST['data_id'] == '') {
         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('Could not find Data ID.', $_POST['text_domain'])));
         die;
     }
     // Get all newsletter data for this data instance
     $data_manager = new NNR_Data_Manager_v1($_POST['table_name']);
     $data_instance = $data_manager->get_data_from_id($_POST['data_id']);
     $success_action = isset($data_instance['args']['newsletter']['success_action']) ? stripcslashes($data_instance['args']['newsletter']['success_action']) : 'message';
     $success_mesage = isset($data_instance['args']['newsletter']['success_message']) ? stripcslashes($data_instance['args']['newsletter']['success_message']) : __('Welcome to the community!', $_POST['text_domain']);
     $success_url = isset($data_instance['args']['newsletter']['success_url']) ? stripcslashes($data_instance['args']['newsletter']['success_url']) : '';
     // No Email
     if (!isset($_POST['email']) || $_POST['email'] == '') {
         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('No Email address provided.', $_POST['text_domain'])));
         die;
     }
     // Invalid Email Address
     if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('Invalid Email provided.', $_POST['text_domain'])));
         die;
     }
     // WordPress
     if ($_POST['type'] == 'wordpress') {
         $newsletter_db = new NNR_Newsletter_Integrations_Submission_v1($_POST['news_table_name']);
         $result = $newsletter_db->add_data(array('data_id' => $_POST['data_id'], 'email' => $_POST['email'], 'first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name']));
         if ($result) {
             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
             die;
         } else {
             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'message' => __('We already have your email!', $_POST['text_domain'])));
             die;
         }
     } else {
         if ($_POST['type'] == 'mailchimp') {
             require_once dirname(dirname(__FILE__)) . '/services/mailchimp/MailChimp.php';
             if (!isset($data_instance['args']['newsletter']['mailchimp']['api_key']) || $data_instance['args']['newsletter']['mailchimp']['api_key'] == '') {
                 echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('MailChimp account is not setup properly.', $_POST['text_domain'])));
                 die;
             }
             if (!isset($data_instance['args']['newsletter']['mailchimp']['list']) || $data_instance['args']['newsletter']['mailchimp']['list'] == '') {
                 echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('MailChimp: No list specified.', $_POST['text_domain'])));
                 die;
             }
             if (!isset($data_instance['args']['newsletter']['mailchimp']['optin'])) {
                 $data_instance['args']['newsletter']['mailchimp']['optin'] = true;
             }
             $MailChimp = new NNR_New_Int_MailChimp($data_instance['args']['newsletter']['mailchimp']['api_key']);
             $result = $MailChimp->call('lists/subscribe', array('id' => $data_instance['args']['newsletter']['mailchimp']['list'], 'email' => array('email' => $_POST['email']), 'merge_vars' => array('FNAME' => $_POST['first_name'], 'LNAME' => $_POST['last_name']), 'double_optin' => $data_instance['args']['newsletter']['mailchimp']['optin'], 'update_existing' => false, 'replace_interests' => false, 'send_welcome' => true));
             if ($result) {
                 if (isset($result['email'])) {
                     echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                     die;
                 } else {
                     if (isset($result['status']) && $result['status'] == 'error') {
                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $result['error']));
                         die;
                     }
                 }
             } else {
                 echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('Unable to subscribe.', $_POST['text_domain'])));
                 die;
             }
         } else {
             if ($_POST['type'] == 'aweber') {
                 require_once dirname(dirname(__FILE__)) . '/services/aweber/aweber_api.php';
                 $aweber = new AWeberAPI($data_instance['args']['newsletter']['aweber']['consumer_key'], $data_instance['args']['newsletter']['aweber']['consumer_secret']);
                 try {
                     $account = $aweber->getAccount($data_instance['args']['newsletter']['aweber']['access_key'], $data_instance['args']['newsletter']['aweber']['access_secret']);
                     $list = $account->loadFromUrl('/accounts/' . $account->id . '/lists/' . $data_instance['args']['newsletter']['aweber']['list_id']);
                     $subscriber = array('email' => $_POST['email'], 'name' => $_POST['first_name'] . ' ' . $_POST['last_name'], 'ip' => $_SERVER['REMOTE_ADDR']);
                     $newSubscriber = $list->subscribers->create($subscriber);
                     echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                     die;
                 } catch (AWeberAPIException $exc) {
                     echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $exc->message));
                     die;
                 }
             } else {
                 if ($_POST['type'] == 'getresponse') {
                     require_once dirname(dirname(__FILE__)) . '/services/getresponse/jsonRPCClient.php';
                     $api = new jsonRPCClient('http://api2.getresponse.com');
                     try {
                         $api->add_contact($data_instance['args']['newsletter']['getresponse']['api_key'], array('campaign' => $data_instance['args']['newsletter']['getresponse']['campaign'], 'name' => $_POST['first_name'] . ' ' . $_POST['last_name'], 'email' => $_POST['email']));
                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                         die;
                     } catch (RuntimeException $exc) {
                         $msg = $exc->getMessage();
                         $msg = substr($msg, 0, strpos($msg, ";"));
                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $msg));
                         die;
                     }
                 } else {
                     if ($_POST['type'] == 'campaignmonitor') {
                         require_once dirname(dirname(__FILE__)) . '/services/campaignmonitor/csrest_subscribers.php';
                         $wrap = new CS_REST_Subscribers($data_instance['args']['newsletter']['campaignmonitor']['list'], $data_instance['args']['newsletter']['campaignmonitor']['api_key']);
                         // Check if subscriber is already subscribed
                         $result = $wrap->get($_POST['email']);
                         if ($result->was_successful()) {
                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('You are already subscribed to this list.', $_POST['text_domain'])));
                             die;
                         }
                         $result = $wrap->add(array('EmailAddress' => $_POST['email'], 'Name' => $_POST['first_name'] . ' ' . $_POST['last_name'], 'Resubscribe' => true));
                         if ($result->was_successful()) {
                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                             die;
                         } else {
                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $result->response->Message));
                             die;
                         }
                     } else {
                         if ($_POST['type'] == 'madmimi') {
                             require_once dirname(dirname(__FILE__)) . '/services/madmimi/MadMimi.class.php';
                             $mailer = new MadMimi($data_instance['args']['newsletter']['madmimi']['username'], $data_instance['args']['newsletter']['madmimi']['api_key']);
                             try {
                                 // Check if user is already in list
                                 $result = $mailer->Memberships($_POST['email']);
                                 $lists = new SimpleXMLElement($result);
                                 if ($lists->list) {
                                     foreach ($lists->list as $l) {
                                         if ($l->attributes()->{'name'}->{0} == $data_instance['args']['newsletter']['madmimi']['list']) {
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'message' => __('You are already subscribed to this list.', $_POST['text_domain'])));
                                             die;
                                         }
                                     }
                                 }
                                 $result = $mailer->AddMembership($data_instance['args']['newsletter']['madmimi']['list'], $_POST['email'], array('first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name']));
                                 echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                                 die;
                             } catch (RuntimeException $exc) {
                                 echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $msg));
                                 die;
                             }
                         } else {
                             if ($_POST['type'] == 'infusionsoft') {
                                 require_once dirname(dirname(__FILE__)) . '/services/infusionsoft/isdk.php';
                                 try {
                                     $infusion_app = new iSDK();
                                     $infusion_app->cfgCon($data_instance['args']['newsletter']['infusionsoft']['app_id'], $data_instance['args']['newsletter']['infusionsoft']['api_key'], 'throw');
                                 } catch (iSDKException $e) {
                                     echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $e->getMessage()));
                                     die;
                                 }
                                 if (empty($error_message)) {
                                     $contact_data = $infusion_app->dsQuery('Contact', 1, 0, array('Email' => $_POST['email']), array('Id', 'Groups'));
                                     // Check if contact already exists
                                     if (0 < count($contact_data)) {
                                         if (false === strpos($contact_data[0]['Groups'], $data_instance['args']['newsletter']['infusionsoft']['list'])) {
                                             $infusion_app->grpAssign($contact_data[0]['Id'], $data_instance['args']['newsletter']['infusionsoft']['list']);
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                                             die;
                                         } else {
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'message' => __('You are already subscribed to this list.', $_POST['text_domain'])));
                                             die;
                                         }
                                     } else {
                                         $new_contact_id = $infusion_app->dsAdd('Contact', array('FirstName' => $_POST['first_name'], 'LastName' => $_POST['last_name'], 'Email' => $_POST['email']));
                                         $infusion_app->grpAssign($new_contact_id, $data_instance['args']['newsletter']['infusionsoft']['list']);
                                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                                         die;
                                     }
                                 }
                             } else {
                                 if ($_POST['type'] == 'mymail') {
                                     // Check if plugin is activated
                                     if (!function_exists('mymail')) {
                                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('MyMail is not activated.', $_POST['text_domain'])));
                                         die;
                                     }
                                     // Add subscriber
                                     $subscriber_id = mymail('subscribers')->add(array('email' => $_POST['email'], 'firstname' => $_POST['first_name'], 'lastname' => $_POST['last_name']), false);
                                     // Add to List
                                     if (!is_wp_error($subscriber_id)) {
                                         mymail('subscribers')->assign_lists($subscriber_id, array($data_instance['args']['newsletter']['mymail']['list']));
                                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                                         die;
                                     } else {
                                         echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'message' => __('You are already subscribed to this list.', $_POST['text_domain'])));
                                         die;
                                     }
                                 } else {
                                     if ($_POST['type'] == 'activecampaign') {
                                         require_once dirname(dirname(__FILE__)) . '/services/activecampaign/ActiveCampaign.class.php';
                                         $ac = new ActiveCampaign($data_instance['args']['newsletter']['activecampaign']['app_url'], $data_instance['args']['newsletter']['activecampaign']['api_key']);
                                         if (!(int) $ac->credentials_test()) {
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('Unable to to connect to Active Campaign.', $_POST['text_domain'])));
                                             die;
                                         }
                                         // Add subscriber
                                         $contact_sync = $ac->api("contact/add", array("email" => $_POST['email'], "first_name" => $_POST['first_name'], "last_name" => $_POST['last_name'], "p[" . $data_instance['args']['newsletter']['activecampaign']['list'] . "]" => $data_instance['args']['newsletter']['activecampaign']['list'], "status[" . $data_instance['args']['newsletter']['activecampaign']['list'] . "]" => 1));
                                         if ((int) $contact_sync->success) {
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'check', 'success_action' => $success_action, 'url' => $success_url, 'message' => $success_mesage, 'conversion' => apply_filters('nnr_news_int_submission_success_v1', array('data_id' => $_POST['data_id'], 'table_name' => $_POST['stats_table_name']))));
                                             die;
                                         } else {
                                             echo json_encode(array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => $contact_sync->error));
                                             die;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     do_action('nnr_news_int_after_submission_add_email_v1');
     echo json_encode(apply_filters('nnr_news_int_submission_add_email_v1', array('id' => $_POST['data_id'], 'status' => 'warning', 'message' => __('Unable to subscribe user. Newsletter not setup properly.', $_POST['text_domain']))));
     die;
     // this is required to terminate immediately and return a proper response
 }