function minimailchimp_subscribe()
{
    // Validate email
    $email = $_POST['email'];
    // Validate mailchimp configuration
    $apikey = base64_decode($_POST['mailchimp_api_key']);
    $list_id = $_POST['mailchimp_list_id'];
    //895f5f58bf'; // 'test-wp-theme-dev';
    // Get our MailChimp API class in scope
    if (!class_exists('mailchimpSF_MCAPI')) {
        require_once THEME_CUSTOM_LIBS_DIR . '/miniMCAPI.class.php';
    }
    // Initialize api
    $api = new mailchimpSF_MCAPI($apikey, false);
    // Subscribe email
    $response = $api->listSubscribe($list_id, $email);
    //$response is true or false
    // Handle response
    if ($response == true) {
        $result = array('result' => true, 'response_text' => '<strong>' . __('Subscribe success', 'theme_front') . '</strong> : ' . __('The confirmation mail will be sent to you soon.', 'theme_front'));
    } else {
        $result = array('result' => false, 'response_text' => '<strong>' . __('Subscribe failed', 'theme_front') . '</strong> : ' . __('Please check your email address or contact the admin.', 'theme_front'));
    }
    die(json_encode($result));
}
Esempio n. 2
0
/**
 * Attempts to signup a user, per the $_POST args.
 * 
 * This sets a global message, that is then used in the widget 
 * output to retrieve and display that message.
 *
 * @return bool
 */
function mailchimpSF_signup_submit()
{
    $mv = get_option('mc_merge_vars', array());
    $mv_tag_keys = array();
    $igs = get_option('mc_interest_groups', array());
    $success = true;
    $listId = get_option('mc_list_id');
    $email = isset($_POST['mc_mv_EMAIL']) ? strip_tags(stripslashes($_POST['mc_mv_EMAIL'])) : '';
    $merge = $errs = array();
    // Set up some vars
    // Loop through our Merge Vars, and if they're empty, but required, then print an error, and mark as failed
    foreach ($mv as $var) {
        $opt = 'mc_mv_' . $var['tag'];
        $opt_val = isset($_POST[$opt]) ? $_POST[$opt] : '';
        if (is_array($opt_val) && isset($opt_val['area'])) {
            $opt_val = implode('-', $opt_val);
        } else {
            if (is_array($opt_val) && $var['field_type'] == 'address') {
                if ($var['req'] == 'Y') {
                    if (empty($opt_val['addr1']) || empty($opt_val['city'])) {
                        $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
                        $success = false;
                    }
                }
                $merge[$var['tag']] = $opt_val;
                continue;
            } else {
                if (is_array($opt_val)) {
                    $opt_val = implode($opt_val);
                }
            }
        }
        if ($var['req'] == 'Y' && trim($opt_val) == '') {
            $success = false;
            $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($var['name']));
        } else {
            if ($var['tag'] != 'EMAIL') {
                $merge[$var['tag']] = $opt_val;
            }
        }
        // We also want to create an array where the keys are the tags for easier validation later
        $mv_tag_keys[$var['tag']] = $var;
    }
    // Head back to the beginning of the merge vars array
    reset($mv);
    // Ensure we have an array
    $igs = !is_array($igs) ? array() : $igs;
    foreach ($igs as $ig) {
        $groups = '';
        if (get_option('mc_show_interest_groups_' . $ig['id']) == 'on') {
            $groupings = array();
            switch ($ig['form_field']) {
                case 'select':
                case 'dropdown':
                case 'radio':
                    if (isset($_POST['group'][$ig['id']])) {
                        $groupings = array('id' => $ig['id'], 'groups' => str_replace(',', '\\,', stripslashes($_POST['group'][$ig['id']])));
                    }
                    break;
                case 'checkboxes':
                case 'checkbox':
                    if (isset($_POST['group'][$ig['id']])) {
                        foreach ($_POST['group'][$ig['id']] as $i => $value) {
                            // Escape
                            $groups .= str_replace(',', '\\,', stripslashes($value)) . ',';
                        }
                        $groupings = array('id' => $ig['id'], 'groups' => $groups);
                    }
                    break;
                default:
                    // Nothing
                    break;
            }
            if (!isset($merge['GROUPINGS']) || !is_array($merge['GROUPINGS'])) {
                $merge['GROUPINGS'] = array();
            }
            if (!empty($groupings)) {
                $merge['GROUPINGS'][] = $groupings;
            }
        }
    }
    // If we're good
    if ($success) {
        // Clear out empty merge vars
        foreach ($merge as $k => $v) {
            if (is_array($v) && empty($v)) {
                unset($merge[$k]);
            } else {
                if (!is_array($v) && trim($v) === '') {
                    unset($merge[$k]);
                }
            }
        }
        // If we have an empty $merge, then assign empty string.
        if (count($merge) == 0 || $merge == '') {
            $merge = '';
        }
        if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
            $email_type = $_POST['email_type'];
        } else {
            $email_type = 'html';
        }
        // Custom validation based on type
        if (is_array($merge) && !empty($merge)) {
            foreach ($merge as $merge_key => $merge_value) {
                if ($merge_key !== 'GROUPINGS') {
                    switch ($mv_tag_keys[$merge_key]['field_type']) {
                        case 'phone':
                            $phone = $merge_value;
                            if (!empty($phone)) {
                                if (!preg_match('/[0-9]{0,3}-[0-9]{0,3}-[0-9]{0,4}/', $phone)) {
                                    $errs[] = sprintf(__("%s must consist of only numbers", 'mailchimp_i18n'), esc_html($mv_tag_keys[$merge_key]['name']));
                                    $success = false;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        if ($success) {
            $api = new mailchimpSF_MCAPI(get_option('mc_apikey'));
            $retval = $api->listSubscribe($listId, $email, $merge, $email_type);
            if (!$retval) {
                switch ($api->errorCode) {
                    case '105':
                        $errs[] = __("Please try again later", 'mailchimp_i18n') . '.';
                        break;
                    case '214':
                        $errs[] = __("That email address is already subscribed to the list", 'mailchimp_i18n') . '.';
                        break;
                    case '250':
                        list($field, $rest) = explode(' ', $api->errorMessage, 2);
                        $errs[] = sprintf(__("You must fill in %s.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
                        break;
                    case '254':
                        list($i1, $i2, $i3, $field, $rest) = explode(' ', $api->errorMessage, 5);
                        $errs[] = sprintf(__("%s has invalid content.", 'mailchimp_i18n'), esc_html($mv_tag_keys[$field]['name']));
                        break;
                    case '270':
                        $errs[] = __("An invalid Interest Group was selected", 'mailchimp_i18n') . '.';
                        break;
                    case '502':
                        $errs[] = __("That email address is invalid", 'mailchimp_i18n') . '.';
                        break;
                    default:
                        $errs[] = $api->errorCode . ":" . $api->errorMessage;
                        break;
                }
                $success = false;
            }
        }
    }
    // If we have errors, then show them
    if (count($errs) > 0) {
        $msg = '<span class="mc_error_msg">';
        foreach ($errs as $error) {
            $msg .= '&raquo; ' . esc_html($error) . '<br />';
        }
        $msg .= '</span>';
    } else {
        $msg = "<strong class='mc_success_msg'>" . esc_html(__("Success, you've been signed up! Please look for our confirmation email!", 'mailchimp_i18n')) . "</strong>";
    }
    // Set our global message
    mailchimpSF_global_msg($msg);
    return $success;
}
Esempio n. 3
0
 function calendarSubscription($email, $name)
 {
     global $wpdb;
     require_once dirname(__FILE__) . '/../mailchimp/miniMCAPI.class.php';
     if (stripslashes($email) != '') {
         $exists = $wpdb->get_row("SELECT COUNT(*) as counter FROM " . $this->table_subscribers_calendar . " WHERE calendar = " . $this->id_calendar . " AND email = '" . $email . "'");
         if ($exists->counter == 0) {
             $wpdb->insert($this->table_subscribers_calendar, array('name' => $name, 'email' => $email, 'calendar' => $this->id_calendar, 'subscription_date' => current_time('mysql')), array('%s', '%s', '%d', '%s'));
         }
         if ($this->calendar_obj->mailchimp_api != "" && $this->calendar_obj->subscribe_active) {
             $mailchimp_class = new mailchimpSF_MCAPI($this->calendar_obj->mailchimp_api);
             $retval = $mailchimp_class->listSubscribe($this->calendar_obj->mailchimp_list, $email);
             if (!$mailchimp_class->errorCode) {
                 die('ok');
             } else {
                 die('0');
             }
         }
     }
 }