function sailthru_user_login($user_login, $user)
{
    if (get_option('sailthru_setup_complete')) {
        $sailthru = get_option('sailthru_setup_options');
        $api_key = $sailthru['sailthru_api_key'];
        $api_secret = $sailthru['sailthru_api_secret'];
        //$client = new Sailthru_Client( $api_key, $api_secret );
        $client = new WP_Sailthru_Client($api_key, $api_secret);
        $id = $user->user_email;
        $options = array('login' => array('user_agent' => $_SERVER['HTTP_USER_AGENT'], 'key' => 'email', 'ip' => $_SERVER['SERVER_ADDR'], 'site' => $_SERVER['HTTP_HOST']), 'fields' => array('keys' => 1));
        try {
            if ($client) {
                $st = $client->saveUser($id, $options);
            }
        } catch (Sailthru_Client_Exception $e) {
            //silently fail
            return;
        }
    }
}
 function add_subscriber()
 {
     if (!wp_verify_nonce($_POST['sailthru_nonce'], "add_subscriber_nonce")) {
         $result['error'] = true;
         $result['message'] = "No naughty business please";
     }
     $email = trim($_POST['email']);
     if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($email)) {
         $result['error'] = true;
         $result['message'] = "Please enter a valid email address.";
     } else {
         $email = filter_var($email, FILTER_VALIDATE_EMAIL);
     }
     if (isset($_POST['first_name']) && !empty($_POST['first_name'])) {
         $first_name = filter_var(trim($_POST['first_name']), FILTER_SANITIZE_STRING);
     } else {
         $first_name = '';
     }
     if (isset($_POST['last_name']) && !empty($_POST['last_name'])) {
         $last_name = filter_var(trim($_POST['last_name']), FILTER_SANITIZE_STRING);
     } else {
         $last_name = '';
     }
     if ($first_name || $last_name) {
         $options = array('vars' => array('first_name' => $first_name, 'last_name' => $last_name));
     }
     $subscribe_to_lists = array();
     if (!empty($_POST['sailthru_email_list'])) {
         //add the custom fields info to the api call! This is where the magic happens
         $customfields = get_option('sailthru_forms_options');
         $key = get_option('sailthru_forms_key');
         for ($i = 0; $i < $key; $i++) {
             $field_key = $i + 1;
             if (!empty($customfields[$field_key]['sailthru_customfield_name'])) {
                 $name_stripped = preg_replace("/[^\\da-z]/i", '_', $customfields[$field_key]['sailthru_customfield_name']);
                 if (!empty($_POST['custom_' . $name_stripped])) {
                     $vars[$name_stripped] = filter_var(trim($_POST['custom_' . $name_stripped]), FILTER_SANITIZE_STRING);
                 }
             }
         }
         //end for loop
         if (empty($vars)) {
             $vars = '';
         }
         $options = array('vars' => $vars);
         $subscribe_to_lists = array();
         if (!empty($_POST['sailthru_email_list'])) {
             $lists = explode(',', $_POST['sailthru_email_list']);
             foreach ($lists as $key => $list) {
                 $subscribe_to_lists[$list] = 1;
             }
             $options['lists'] = $subscribe_to_lists;
         } else {
             $options['lists'] = array('Sailthru Subscribe Widget' => 1);
             // subscriber is an orphan
         }
         $options['vars']['source'] = get_bloginfo('url');
         $result['data'] = array('email' => $email, 'options' => $options);
         if (empty($result['error'])) {
             $sailthru = get_option('sailthru_setup_options');
             $api_key = $sailthru['sailthru_api_key'];
             $api_secret = $sailthru['sailthru_api_secret'];
             //$client = new Sailthru_Client( $api_key, $api_secret );
             $client = new WP_Sailthru_Client($api_key, $api_secret);
             try {
                 if ($client) {
                     $res = $client->saveUser($email, $options);
                 }
             } catch (Sailthru_Client_Exception $e) {
                 //silently fail
                 return;
             }
             if ($res['ok'] != true) {
                 $result['error'] = true;
                 $result['message'] = "There was an error subscribing you. Please try again later.";
             }
             $result['result'] = $res;
         }
         // did this request come from an ajax call?
         if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
             $result = json_encode($result);
             echo $result;
             exit;
         } else {
             echo $result['message'];
             exit;
         }
     }
     // end add_subscriber()
 }