/** * This function verifies Sailthru is working by making an API Call to Sailthru * */ function sailthru_verify_setup() { $sailthru = get_option('sailthru_setup_options'); $api_key = $sailthru['sailthru_api_key']; $api_secret = $sailthru['sailthru_api_secret']; $template = isset($sailthru['sailthru_setup_email_template']) ? $sailthru['sailthru_setup_email_template'] : ''; $res = array(); $tpl_errors = array(); if ($template == '') { $res['error'] = true; $res['errormessage'] = 'select a template'; } else { // now check to see if we can make an API call //$client = new Sailthru_Client( $api_key, $api_secret ); $client = new WP_Sailthru_Client($api_key, $api_secret); $res = $client->getTemplates(); if (!isset($res['error'])) { // we can make a call, now check the template is configured try { $tpl = $client->getTemplate($template); $tpl_errors = sailthru_verify_template($tpl); } catch (Exception $e) { $tpl_errors = array('Request to Sailthru API failed: ' . $e->getMessage()); } if (count($tpl_errors) > 0) { // add errors to the error message $res['error'] = true; $res['errormessage'] = 'template not configured'; } else { $res['error'] = false; } } else { $res['error'] = true; $res['errormessage'] = 'not configured'; } } return $res; }
function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) { // we'll be going through Sailthru so we'll handle text/html emails there already // replace the <> in the reset password message link to allow the link to display. // in HTML emails $message = preg_replace('#<(https?://[^*]+)>#', '$1', $message); extract(apply_filters('wp_mail', compact($to, $subject, $message, $headers = '', $attachments = array()))); // recipients $recipients = is_array($to) ? implode(',', $to) : $to; // as the client library accepts these... $vars = array('subject' => $subject, 'body' => $message); // template $sailthru_configs = get_option('sailthru_setup_options'); $template = $sailthru_configs['sailthru_setup_email_template']; // SEND $sailthru = get_option('sailthru_setup_options'); $api_key = $sailthru['sailthru_api_key']; $api_secret = $sailthru['sailthru_api_secret']; $client = new WP_Sailthru_Client($api_key, $api_secret); $r = $client->send($template, $recipients, $vars, array()); return true; }
<div id="icon-sailthru" class="icon32"></div> <h2><?php _e('Sailthru Subscribe', 'sailthru-for-wordpress'); ?> </h2> <?php $sailthru = get_option('sailthru_setup_options'); $customfields = get_option('sailthru_forms_options'); if (!is_array($sailthru)) { echo '<p>Please return to the <a href="' . esc_url(menu_page_url('sailthru_configuration_menu', false)) . '">Sailthru Settings screen</a> and set up your API key and secret before setting up this widget.</p>'; return; } $api_key = $sailthru['sailthru_api_key']; $api_secret = $sailthru['sailthru_api_secret']; $client = new WP_Sailthru_Client($api_key, $api_secret); try { if ($client) { $res = $client->getLists(); } } catch (Sailthru_Client_Exception $e) { //silently fail return; } $lists = $res['lists']; ?> <div id="<?php echo $this->get_field_id('title'); ?> _div" style="display: block;"> <p>
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() }