Пример #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();
    }
}