コード例 #1
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'])) {
            // This filters out all 'falsey' elements
            $opt_val = array_filter($opt_val);
            // If they weren't all empty
            if ($opt_val) {
                $opt_val = implode('-', $opt_val);
                if (strlen($opt_val) < 12) {
                    $opt_val = '';
                }
            } else {
                $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':
                            if ($mv_tag_keys[$merge_key]['phoneformat'] == 'US') {
                                $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 = mailchimpSF_get_api();
            if (!$api) {
                return;
            }
            $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) {
                            $dc = get_option('mc_sopresto_dc');
                            $uid = $account['user_id'];
                            $username = preg_replace('/\\s+/', '-', $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;
}
コード例 #2
0
                <input type="submit" name="Submit" value="<?php 
    esc_attr_e('Logout', 'mailchimp_i18n');
    ?>
" class="button" />
                <?php 
    wp_nonce_field('mc_logout', '_mcsf_nonce_action');
    ?>
            </form>
        </td>
    </tr>
</table>
<?php 
}
// End Logout form
//Just get out if nothing else matters...
$api = mailchimpSF_get_api();
if (!$api) {
    return;
}
if ($api) {
    ?>
    <h3 class="mc-h2"><?php 
    esc_html_e('Your Lists', 'mailchimp_i18n');
    ?>
</h3>

<div>

    <p class="mc-p"><?php 
    esc_html_e('Please select the MailChimp list you’d like to connect to your form.', 'mailchimp_i18n');
    ?>
コード例 #3
0
function mailchimpSF_check_status($endpoint)
{
    $endpoint .= '?fields=status';
    $api = mailchimpSF_get_api();
    $subscriber = $api->get($endpoint, null);
    if (is_wp_error($subscriber)) {
        return false;
    }
    return $subscriber['status'];
}
コード例 #4
0
/**
 * Displays a MailChimp Signup Form
 **/
function mailchimpSF_signup_form($args = array())
{
    extract($args);
    $mv = get_option('mc_merge_vars');
    $igs = get_option('mc_interest_groups');
    // See if we have valid Merge Vars
    if (!is_array($mv)) {
        echo $before_widget;
        ?>
		<div class="mc_error_msg">
			<?php 
        echo __('Sorry, there was a problem loading your MailChimp details. Please navigate to <strong>Settings</strong> and click <strong>MailChimp Setup</strong> to try again.', 'mailchimp_i18n');
        ?>
		</div>
		<?php 
        echo $after_widget;
        return;
    }
    if (!empty($before_widget)) {
        echo $before_widget;
    }
    $header = get_option('mc_header_content');
    // See if we have custom header content
    if (!empty($header)) {
        // See if we need to wrap the header content in our own div
        if (strlen($header) == strlen(strip_tags($header))) {
            echo !empty($before_title) ? $before_title : '<div class="mc_custom_border_hdr">';
            echo $header;
            // don't escape $header b/c it may have HTML allowed
            echo !empty($after_title) ? $after_title : '</div><!-- /mc_custom_border_hdr -->';
        } else {
            echo $header;
            // don't escape $header b/c it may have HTML allowed
        }
    }
    $sub_heading = trim(get_option('mc_subheader_content'));
    if (get_option('mc_nuke_all_styles') != true) {
        ?>
	<style>
		.widget_mailchimpsf_widget .widget-title {
		line-height: 1.4em;
		margin-bottom: 0.75em;
	}
	#mc_subheader {
		line-height: 1.25em;
		margin-bottom: 18px;
	}
	.mc_merge_var {
		margin-bottom: 1.0em;
	}
	.mc_var_label,
	.mc_interest_label {
		display: block;
		margin-bottom: 0.5em;
	}
	.mc_input {
		-moz-box-sizing: border-box;
		-webkit-box-sizing: border-box;
		box-sizing: border-box;
		width: 100%;
	}
	.mc_input.mc_phone {
		width: auto;
	}
	select.mc_select {
		margin-top: 0.5em;
		width: 100%;
	}
	.mc_address_label {
		margin-top: 1.0em;
		margin-bottom: 0.5em;
		display: block;
	}
	.mc_address_label ~ select {
		width: 100%;		
	}
	.mc_list li {
		list-style: none;
		background: none !important;
	}
	.mc_interests_header {
		margin-top: 1.0em;
		margin-bottom: 0.5em;
	}
	.mc_interest label,
	.mc_interest input {
		margin-bottom: 0.4em;
	}
	#mc_signup_submit {
		margin-top: 1.5em;
		width: 80%;
	}
	#mc_unsub_link a {
		font-size: 0.75em;
	}
	#mc_unsub_link {
		margin-top: 1.0em;
	}
	.mc_header_address,
	.mc_email_format {
		display: block;
		font-weight: bold;
		margin-top: 1.0em;
		margin-bottom: 0.5em;
	}
	.mc_email_options {
		margin-top: 0.5em;
	}
	.mc_email_type {
		padding-left: 4px;
	}
	</style>
	<?php 
    }
    ?>

<div id="mc_signup">
	<form method="post" action="#mc_signup" id="mc_signup_form">
		<input type="hidden" id="mc_submit_type" name="mc_submit_type" value="html" />
		<input type="hidden" name="mcsf_action" value="mc_submit_signup_form" />
		<?php 
    wp_nonce_field('mc_submit_signup_form', '_mc_submit_signup_form_nonce', false);
    ?>
		
	<?php 
    if ($sub_heading) {
        ?>
		<div id="mc_subheader">
			<?php 
        echo $sub_heading;
        ?>
		</div><!-- /mc_subheader -->
		<?php 
    }
    ?>
	
	<div class="mc_form_inside">
		
		<div class="updated" id="mc_message">
			<?php 
    echo mailchimpSF_global_msg();
    ?>
		</div><!-- /mc_message -->

		<?php 
    //don't show the "required" stuff if there's only 1 field to display.
    $num_fields = 0;
    foreach ((array) $mv as $var) {
        $opt = 'mc_mv_' . $var['tag'];
        if ($var['required'] || get_option($opt) == 'on') {
            $num_fields++;
        }
    }
    if (is_array($mv)) {
        // head on back to the beginning of the array
        reset($mv);
    }
    // Loop over our vars, and output the ones that are set to display
    foreach ($mv as $var) {
        if (!$var['public']) {
            echo '<div style="display:none;">' . mailchimp_form_field($var, $num_fields) . '</div>';
        } else {
            echo mailchimp_form_field($var, $num_fields);
        }
    }
    // Show an explanation of the * if there's more than one field
    if ($num_fields > 1) {
        ?>
			<div id="mc-indicates-required">
				* = <?php 
        esc_html_e('required field', 'mailchimp_i18n');
        ?>
			</div><!-- /mc-indicates-required -->
			<?php 
    }
    // Show our Interest groups fields if we have them, and they're set to on
    if (is_array($igs) && !empty($igs)) {
        foreach ($igs as $ig) {
            if (is_array($ig) && isset($ig['id'])) {
                if ($igs && get_option('mc_show_interest_groups_' . $ig['id']) == 'on') {
                    if ($ig['type'] != 'hidden') {
                        ?>
				
							<div class="mc_interests_header">
								<?php 
                        echo esc_html($ig['title']);
                        ?>
							</div><!-- /mc_interests_header -->
							<div class="mc_interest">
						<?php 
                    } else {
                        ?>
							<div class="mc_interest" style="display: none;">
						<?php 
                    }
                    ?>
			

					<?php 
                    mailchimp_interest_group_field($ig);
                    ?>
				
					</div><!-- /mc_interest -->
			
					<?php 
                }
            }
        }
    }
    if (get_option('mc_email_type_option')) {
        ?>
		<div class="mergeRow">
			<label class="mc_email_format"><?php 
        _e('Preferred Format', 'mailchimp_i18n');
        ?>
</label>
		    <div class="field-group groups mc_email_options">
		        <ul class="mc_list">
			        <li><input type="radio" name="email_type" id="email_type_html" value="html" checked="checked"><label for="email_type_html" class="mc_email_type"><?php 
        _e('HTML', 'mailchimp_i18n');
        ?>
</label></li>
			        <li><input type="radio" name="email_type" id="email_type_text" value="text"><label for="email_type_text" class="mc_email_type"><?php 
        _e('Text', 'mailchimp_i18n');
        ?>
</label></li>
		        </ul>
			</div>
		</div>	

		<?php 
    }
    $submit_text = get_option('mc_submit_text');
    ?>

		<div class="mc_signup_submit">
			<input type="submit" name="mc_signup_submit" id="mc_signup_submit" value="<?php 
    echo esc_attr($submit_text);
    ?>
" class="button" />
		</div><!-- /mc_signup_submit -->
	
	
		<?php 
    $user = get_option('mc_user');
    if ($user && get_option('mc_use_unsub_link') == 'on') {
        $api = mailchimpSF_get_api();
        $host = 'http://' . $api->datacenter . '.list-manage.com';
        ?>
			<div id="mc_unsub_link" align="center">
				<a href="<?php 
        echo esc_url($host . '/unsubscribe/?u=' . $user['account_id'] . '&amp;id=' . get_option('mc_list_id'));
        ?>
" target="_blank"><?php 
        esc_html_e('unsubscribe from list', 'mailchimp_i18n');
        ?>
</a>
			</div><!-- /mc_unsub_link -->
			<?php 
    }
    if ($user && get_option('mc_rewards') == 'on') {
        ?>
			<br/>
			<div id="mc_display_rewards" align="center">
				<?php 
        esc_html_e('powered by', 'mailchimp_i18n');
        ?>
 <a href="<?php 
        echo esc_url('http://www.mailchimp.com/affiliates/?aid=' . $user['account_id'] . '&amp;afl=1');
        ?>
">MailChimp</a>!
			</div><!-- /mc_display_rewards -->
			<?php 
    }
    ?>
		
	</div><!-- /mc_form_inside -->
	</form><!-- /mc_signup_form -->
</div><!-- /mc_signup_container -->
	<?php 
    if (!empty($after_widget)) {
        echo $after_widget;
    }
}