示例#1
0
/**
 * Sets the API Key to whatever value was passed to this func
 *
 * @return array of vars
 **/
function mailchimpSF_set_api_key($api_key = '')
{
    $delete_setup = false;
    $api = new mailchimpSF_MCAPI($api_key);
    $api->ping();
    if (empty($api->errorCode)) {
        $msg = "<p class='success_msg'>" . esc_html(__("Success! We were able to verify your API Key! Let's continue, shall we?", 'mailchimp_i18n')) . "</p>";
        update_option('mc_apikey', $api_key);
        $req = $api->getAccountDetails();
        update_option('mc_username', $req['username']);
        update_option('mc_user_id', $req['user_id']);
        $cur_list_id = get_option('mc_list_id');
        if (!empty($cur_list_id)) {
            //we *could* support paging, but few users have that many lists (and shouldn't)
            $lists = $api->lists(array(), 0, 100);
            $lists = $lists['data'];
            //but don't delete if the list still exists...
            $delete_setup = true;
            foreach ($lists as $list) {
                if ($list['id'] == $cur_list_id) {
                    $list_id = isset($_POST['mc_list_id']) ? $_POST['mc_list_id'] : '';
                    $delete_setup = false;
                }
            }
        }
    } else {
        $msg = "<p class='error_msg'>" . esc_html(__('Uh-oh, we were unable to verify your API Key. Please check them and try again!', 'mailchimp_i18n')) . "<br/>";
        $msg .= __('The server said:', 'mailchimp_i18n') . "<em>" . esc_html($api->errorMessage) . "</em></p>";
        $username = get_option('mc_username');
        if (empty($username)) {
            $delete_setup = true;
        }
    }
    // Set a global message
    mailchimpSF_global_msg($msg);
    // If we need to delete our setup, do it
    if ($delete_setup) {
        mailchimpSF_delete_setup();
    }
}
示例#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 = $html_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':
                        $msg = __("That email address is already subscribed to the list", 'mailchimp_i18n') . '.';
                        $account = $api->getAccountDetails(array("modules", "orders", "rewards-credits", "rewards-inspections", "rewards-referrals", "rewards-applied"));
                        if (!$api->errorCode) {
                            list($dummy, $dc) = explode('-', get_option('mc_apikey'));
                            $uid = $account['user_id'];
                            $username = $account['username'];
                            $eid = base64_encode($email);
                            $msg .= ' ' . sprintf(__('<a href="%s">Click here to update your profile.</a>', 'mailchimp_i18n'), "http://{$username}.{$dc}.list-manage.com/subscribe/send-email?u={$uid}&id={$listId}&e={$eid}");
                        }
                        $errs[] = $msg;
                        $html_errs[] = count($errs) - 1;
                        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_index => $error) {
            if (!in_array($error_index, $html_errs)) {
                $error = esc_html($error);
            }
            $msg .= '&raquo; ' . $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;
}