FWSMC_subform_action_callback
The MailChimp Subscription Plus FWSMC subform action callback function.
Description
FWSMC_subform_action_callback();
Usage
if ( !function_exists( 'FWSMC_subform_action_callback' ) ) { require_once ABSPATH . PLUGINDIR . 'mailchimp-subscription-plus/mailchimp-subscription-plus.php'; } // NOTICE! Understand what this does before running. $result = FWSMC_subform_action_callback();
Defined (1)
The function is defined in the following location(s).
- /mailchimp-subscription-plus.php
- function FWSMC_subform_action_callback() {
- $error = '';
- $status = error;
- if (empty($_POST['name']) || empty($_POST['email'])) {
- $error = __( 'Both fields are required to enter.', 'fws-mailchimp-subscribe' );
- } else {
- if (!wp_verify_nonce($_POST['_fwsmc_subnonce'], 'fwsmc_subform')) {
- $error = __( 'Verification error, try again.', 'fws-mailchimp-subscribe' );
- } else {
- $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
- $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
- $extranamevalue = filter_var($_POST['extramergefield'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
- $ak = get_option('fwsmc-apiKey');
- $mc = new \Drewm\MailChimp($ak);
- $fnamevar = get_option('fwsmc-firstNameMergField');
- $mvars = array('optin_ip'=>$_SERVER['REMOTE_ADDR'], $fnamevar => $name);
- if ($extranamevar = get_option('fwsmc-extraMergeField')) {
- $mvars[$extranamevar] = $extranamevalue;
- }
- $result = $mc->call('lists/subscribe', array(
- 'id' => get_option('fwsmc-listID'),
- 'email' => array('email'=>$email),
- 'merge_vars' => $mvars,
- 'double_optin' => true,
- 'update_existing' => false,
- 'replace_interests' => false,
- 'send_welcome' => false
- ));
- //var_dump($result);
- if (!empty($result['euid'])) {
- $error = __( 'Thanks, please check your emailbox and confirm your subscription.', 'fws-mailchimp-subscribe' );
- $status = 'success';
- } else {
- if (isset($result['status'])) {
- switch ($result['code']) {
- case 214:
- $error = __( 'You\'re already subscribed to this list.', 'fws-mailchimp-subscribe' );
- break;
- // check the MailChimp API for more options
- default:
- $error = __( 'An unknownerroroccurred.', 'fws-mailchimp-subscribe' );
- break;
- }
- }
- }
- }
- }
- $resp = array('status' => $status, 'errmessage' => $error);
- header( "Content-Type: application/json" );
- echo json_encode($resp);
- die();
- }