function test_remove_empty_merge_feilds()
 {
     $merge = new StdClass();
     $merge->fname = 'test';
     $merge->test = ' ';
     $merge->hello = null;
     $submit = mailchimpSF_merge_remove_empty($merge);
     $this->assertTrue($merge->fname === $submit->fname);
     $this->assertTrue(empty($submit->test));
     $this->assertTrue(empty($submit->hello));
 }
Example #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());
    $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
    $merge = mailchimpSF_merge_submit($mv);
    //Catch errors and fail early.
    if (is_wp_error($merge)) {
        $msg = "<strong class='mc_error_msg'>" . $merge->get_error_message() . "</strong>";
        mailchimpSF_global_msg($msg);
        return false;
    }
    // Head back to the beginning of the merge vars array
    reset($mv);
    // Ensure we have an array
    $igs = !is_array($igs) ? array() : $igs;
    $igs = mailchimpSF_groups_submit($igs);
    // Clear out empty merge vars
    $merge = mailchimpSF_merge_remove_empty($merge);
    if (isset($_POST['email_type']) && in_array($_POST['email_type'], array('text', 'html', 'mobile'))) {
        $email_type = $_POST['email_type'];
    } else {
        $email_type = 'html';
    }
    $api = mailchimpSF_get_api();
    if (!$api) {
        $url = mailchimpSF_signup_form_url();
        $error = '<strong class="mc_error_msg">' . __('We encountered a problem adding ' . $email . ' to the list. Please <a href="' . $url . '">sign up here.</a>') . '</strong>';
        mailchimpSF_global_msg($error);
        return false;
    }
    $url = 'lists/' . $listId . '/members/' . md5(strtolower($email));
    $status = mailchimpSF_check_status($url);
    // If update existing is turned off and the subscriber exists, error out.
    if (get_option('mc_update_existing') == false && $status === 'subscribed') {
        $msg = 'This email address is already subscribed to the list.';
        $error = new WP_Error('mailchimp-update-existing', $msg);
        mailchimpSF_global_msg('<strong class="mc_error_msg">' . $msg . '</strong>');
        return false;
    }
    $body = mailchimpSF_subscribe_body($merge, $igs, $email_type, $email, $status, get_option('mc_double_optin'));
    $retval = $api->post($url, $body, 'PUT');
    // If we have errors, then show them
    if (is_wp_error($retval)) {
        $msg = "<strong class='mc_error_msg'>" . $retval->get_error_message() . "</strong>";
        mailchimpSF_global_msg($msg);
        return false;
    }
    if ($retval['status'] == 'subscribed') {
        $esc = __("Success, you've been signed up.", 'mailchimp_i18n');
        $msg = "<strong class='mc_success_msg'>{$esc}</strong>";
    } else {
        $esc = __("Success, you've been signed up! Please look for our confirmation email.", 'mailchimp_i18n');
        $msg = "<strong class='mc_success_msg'>{$esc}</strong>";
    }
    // Set our global message
    mailchimpSF_global_msg($msg);
    return true;
}