protected function ensure_subscribers() { if (isset($this->subscribers)) { return; } $this->subscribers = array(); // pull in the lib if (!class_exists('Mailchimp')) { require_once dirname(dirname(__FILE__)) . '/vendor/mailchimp/mailchimp/src/Mailchimp.php'; } // load subscribers from list $mailchimp = new Mailchimp($this->api_key); $start = 0; $run = true; while (true === $run) { $list_subscribers = $mailchimp->call('lists/members', array('id' => $this->list_id, 'opts' => array('start' => $start++, 'limit' => 100))); foreach ($list_subscribers['data'] as $list_subscriber) { $this->add_source_subscriber($list_subscriber); } // increment start if ($list_subscribers['total'] < 100 || ceil($list_subscribers['total'] / 100) < $start) { // continue! $run = false; } // kill off infinate loops } }
function subscribe_to_mailchimp($user_id) { $api_key = get_option('bbconnect_mailchimp_api_key'); $list_id = get_option('bbconnect_mailchimp_list_id'); $user = get_user_by('id', $user_id); $firstname = get_user_meta($user_id, 'first_name', true); $lastname = get_user_meta($user_id, 'last_name', true); $address1 = get_user_meta($user_id, 'bbconnect_address_one_1', true); $city = get_user_meta($user_id, 'bbconnect_address_city_1', true); $state = get_user_meta($user_id, 'bbconnect_address_state_1', true); $postal_code = get_user_meta($user_id, 'bbconnect_address_postal_code_1', true); $country = get_user_meta($user_id, 'bbconnect_address_country_1', true); $bbconnect_helper_country = bbconnect_helper_country(); $country = $bbconnect_helper_country[$country]; $email = $user->user_email; $mailchimp = new Mailchimp($api_key); $Mailchimp_Lists = new Mailchimp_Lists($mailchimp); try { $params = array('id' => $list_id, 'emails' => array(array('email' => $email))); $is_User_Registered = $mailchimp->call('lists/member-info', $params); if ($is_User_Registered['success_count'] == 0 || $is_User_Registered['success_count'] != 0 && $is_User_Registered['data'][0]['status'] != 'subscribed' && $is_User_Registered['data'][0]['status'] != 'unsubscribed') { $mc_email = array('email' => $email); $merge_vars = array('FNAME' => $firstname, 'LNAME' => $lastname, 'addr1' => $address1, 'city' => $city, 'state' => $state, 'zip' => $postal_code, 'country' => $country); $subscriber = $Mailchimp_Lists->subscribe($list_id, $mc_email, $merge_vars, '', false, false, false, false); if (empty($subscriber['leid'])) { wp_mail('*****@*****.**', 'CW Contact subscription to MailChimp failed 001', $email . ',' . $firstname . ' ' . $lastname . ' has failed to subscribe to mailchimp.'); } } } catch (\Mailchimp_Error $e) { wp_mail('*****@*****.**', 'CW Contact subscription to MailChimp failed 002', $email . ',' . $firstname . ' ' . $lastname . ' has failed to subscribe to mailchimp.'); return; } }
function registrar_email_en_mailchimp($email, $name, $last) { require_once 'mailchimp/Mailchimp.php'; $merge_vars = array('FNAME' => $name, 'LNAME' => $last); $MailChimp = new Mailchimp(''); $result = $MailChimp->call('lists/subscribe', array('id' => '', 'email' => array('email' => $email), 'merge_vars' => $merge_vars, 'double_optin' => false, 'update_existing' => true, 'replace_interests' => false, 'send_welcome' => false)); //print_r($result); }
public function sendUpdateProfileEmail() { $user_email = $_POST['user_email']; $list_id = $_POST['list_id']; $api = new Mailchimp(get_option('yikes-mc-api-key', '')); $explode_key = explode('-', get_option('yikes-mc-api-key', '')); $data_center = $explode_key[1]; $full_site_url = get_bloginfo('url'); try { // get the site URL // not sure this needs its own API call $account_details = $api->call('helper/account-details', array('apikey' => get_option('yikes-mc-api-key', ''))); // get the list details (default from name, default from email) $list_details = $api->call('lists/list', array('apikey' => get_option('yikes-mc-api-key', ''), 'filters' => array('list_id' => $list_id))); // get the subscribers info $subscriber_account_details = $api->call('lists/member-info', array('id' => $list_id, 'emails' => array(0 => array('email' => $user_email)))); $subscriber_id = $subscriber_account_details['data'][0]['id']; $explode_url = explode('.', $account_details['contact']['url']); $subject = 'MailChimp Profile Update'; $headers = 'From: ' . $list_details['data'][0]['default_from_name'] . ' <' . $list_details['data'][0]['default_from_email'] . '>' . "\r\n"; $headers .= 'Content-type: text/html'; $email_content = '<p>Greetings,</p> <p>A request has been made to update your MailChimp account profile information. To do so please use the following link: <a href="http://' . $explode_url[1] . '.' . $data_center . '.list-manage1.com/profile?u=' . $account_details['user_id'] . '&id=' . $list_id . '&e=' . $subscriber_id . '" title="Update MailChimp Profile">Update MailChimp Profile Info.</a>'; $email_content .= "<p>If you did not request this update, please disregard this email.</p>"; $email_content .= '<p> </p>'; $email_content .= '<p>This email was sent from : ' . $full_site_url . '</p>'; $email_content .= '<p> </p>'; $email_content .= '<p> </p>'; $email_content .= '<p style="font-size:13px;margin-top:5em;"><em>This email was generated by the <a href="http://www.wordpress.org/plugins/yikes-inc-easy-mailchimp-extender/" target="_blank">Easy Forms for MailChimp</a> plugin, created by <a href="http://www.yikesinc.com" target="_blank">YIKES Inc.</a></em></p>'; /* Confirm that the email was sent */ if (wp_mail($user_email, apply_filters('yikes-mailchimp-update-email-subject', $subject), apply_filters('yikes-mailchimp-update-email-content', $email_content), $headers)) { wp_send_json_success(array('response_text' => '<div class="yikes-easy-mc-success-message">' . sprintf(__('%s Update email successfully sent. Please check your inbox for the message.', 'yikes-inc-easy-mailchimp-extender'), '✔') . '</div>')); exit; } else { wp_send_json_error(array('response_text' => '<div class="yikes-easy-mc-error-message">' . sprintf(__('%s Email failed to send. Please contact the site administrator.', 'yikes-inc-easy-mailchimp-extender'), '✕') . '</div>')); exit; } // print_r($account_details); } catch (Exception $e) { $errorMessage = sprintf(__('Error sending update profile email. <strong>Error:</strong> %s. Please contact the site administrator.', 'yikes-inc-easy-mailchimp-extender'), $e->getMessage()); wp_send_json_error(array('response_text' => '<div class="yikes-easy-mc-error-message">✕ ' . $errorMessage . '</div>')); exit; } }
/** * Send a request to mailchimp servers * * @param $request string API handle to call (e.g. 'lists/list') * @param $args array Associative array of params to use in the request (default to empty array) * @param $force_update boolean Whether or not to update cached data with a fresh request (applied only for requests in $cachable_requests, default to false) * * @return mixed API response (as an associative array) * @since 1.0.0 */ public function do_request($request, $args = array(), $force_update = false) { if (is_null($this->mailchimp)) { return false; } $api_key = get_option('yith_wcmc_mailchimp_api_key'); $transient_name = 'yith_wcmc_' . md5($api_key); $data = get_transient($transient_name); if (in_array($request, $this->cachable_requests) && !$force_update && !empty($data) && isset($data[$request])) { return $data[$request]; } try { $result = $this->mailchimp->call($request, $args); if (in_array($request, $this->cachable_requests)) { $data[$request] = $result; set_transient($transient_name, $data, apply_filters('yith_wcmc_transient_expiration', DAY_IN_SECONDS)); } return $result; } catch (Exception $e) { return array('status' => false, 'code' => $e->getCode(), 'message' => $e->getMessage()); } }
public function is_valid_api_key($apikey) { if (empty($apikey)) { return null; } if (!class_exists('Mailchimp')) { require_once 'api/Mailchimp.php'; } $this->log_debug(__METHOD__ . "(): Validating login for API Info for key {$apikey}."); $api = new Mailchimp(trim($apikey), null); try { $lists = $api->call('lists/list', ''); } catch (Exception $e) { $this->log_error(__METHOD__ . '(): ' . $e->getCode() . ' - ' . $e->getMessage()); } if (empty($lists)) { $this->log_error(__METHOD__ . '(): Invalid API Key. Error ' . $e->getCode() . ' - ' . $e->getMessage()); return false; } else { $this->log_debug(__METHOD__ . '(): API Key is valid.'); return true; } }
/** * Set Mailchimp list transient and return the list * * @param $apikey string Mailchimp apikey * @param $post_id int Post id * * @return boolean|mixed array() * @since 1.0.0 * @author Antonio La Rocca <*****@*****.**> */ public function set_mailchimp_lists($apikey, $post_id) { if (isset($apikey) && strcmp($apikey, '') != 0) { // include libraries include_once $this->plugin_path . '/lib/vendor/mailchimp/Mailchimp.php'; // initialize mailchimp wrapper $mailchimp_wrapper = new Mailchimp($apikey, array('ssl_verifypeer' => false)); // fetch list $result = $mailchimp_wrapper->call('lists/list', array('apikey' => $apikey)); // generate result array $lists = array(); if (!empty($result) && isset($result['total']) && $result['total'] > 0) { foreach ($result['data'] as $list) { $lists[$list['id']] = $list['name']; } } // memorize result array in a transient set_transient('yit-mailchimp-newsletter-' . $post_id . '-list', $lists, WEEK_IN_SECONDS); return $lists; } else { return false; } }
<?php if (isset($_REQUEST['list-id'])) { $list_id = sanitize_key($_REQUEST['list-id']); // run our API call, to get list data.. $MailChimp = new Mailchimp(get_option('yikes-mc-api-key', '')); $api_key = get_option('yikes-mc-api-key', ''); // get this lists data $list_data = $MailChimp->call('lists/list', array('apikey' => $api_key, 'filters' => array('list_id' => $list_id))); // reset our data so we can easily use it $list_data = $list_data['data'][0]; // get the merge_variables $merge_variables = $MailChimp->call('lists/merge-vars', array('apikey' => $api_key, 'id' => array($list_id))); // re-store our data $merge_variables = $merge_variables['data'][0]['merge_vars']; // get the interest group data try { $interest_groupings = $MailChimp->call('lists/interest-groupings', array('apikey' => $api_key, 'id' => $list_id, 'counts' => true)); } catch (Exception $error) { $no_interest_groupings = $error->getMessage(); } $no_segments = __('No segments set up for this list.', 'yikes-inc-easy-mailchimp-extender'); // get the segment data try { $segments = $MailChimp->call('lists/segments', array('apikey' => $api_key, 'id' => $list_id, 'type' => 'saved')); } catch (Exception $segment_error) { $no_segments = $error->getMessage(); } // setup pagination variables $paged = isset($_REQUEST['paged']) ? $_REQUEST['paged'] : '0'; $limit = apply_filters('yikes_admin_list_subscriber_limit', '20');
$list_id = $_GET['mailchimp-list']; $email_id = (int) esc_attr($_GET['email-id']); /* * Confirm that our data is set * or abort... */ if (!isset($list_id) || !isset($email_id)) { wp_die("We've encountered an error. Please go back and try again", "yikes-inc-easy-mailchimp-extender"); exit; } // run our API call, to get list data.. $MailChimp = new Mailchimp(get_option('yikes-mc-api-key', '')); $api_key = get_option('yikes-mc-api-key', ''); // get this lists data try { $user_data = $MailChimp->call('lists/member-info', array('apikey' => $api_key, 'id' => $list_id, 'emails' => array(array('leid' => $email_id)))); } catch (Exception $e) { echo '<h4>Error</h4>'; echo __('We encountered an error:', 'yikes-inc-easy-mailchimp-extender') . ' ' . $e->getMessage() . ".\n"; return; } /* * Check for MailChimp returned errors */ if (isset($user_data) && $user_data['error_count'] >= 1) { echo '<h4>Error</h4>'; echo $user_data['errors'][0]['error'] . '.'; return; } if (isset($user_data['data'][0])) { // reset our data so we can easily use it
<?php /* Main template file which updates a list on a given field */ parse_str($p['form_data'], $formData); $list_id = $formData['mc-list-id']; $old_merge_tag_name = $formData['old-merge-tag']; $new_merge_tag = $formData['add-field-field-merge-tag']; $field_name = $formData['add-field-field-name']; $field_required = $formData['update-field-field-required']; $field_type = $p['field_type']; $field_default_value = $formData['add-field-default-value']; $api = new Mailchimp($this->optionVal['api-key']); if ($field_type == 'dropdown' || $field_type == 'radio') { try { $retval = $api->call('lists/merge-var-update', array('id' => $list_id, 'tag' => $old_merge_tag_name, 'options' => array('name' => $field_name, 'choices' => $formData['radio-dropdown-option'], 'req' => $field_required, 'tag' => $new_merge_tag))); return "done"; } catch (Exception $e) { // catch any errors returned from MailChimp $errorMessage = $e->getMessage(); echo $errorMessage; // write our error to the error log, // when advanced debug is enabled if ($this->optionVal['debug'] == 1) { $this->writeErrorToErrorLog($e); } die; } } else { if ($field_type == 'text') { try { $retval = $api->call('lists/merge-var-update', array('id' => $list_id, 'tag' => $old_merge_tag_name, 'options' => array('name' => $field_name, 'req' => $field_required, 'tag' => $new_merge_tag, 'default_value' => $field_default_value)));
/** * Subscribe Mailchimp user * * Add user to a mailchinmp list posted via AJAX-Request to wp_ajax_subscribe_mailchimp_user action * * @return void * @since 1.0.0 * @author Antonio La Rocca <*****@*****.**> */ public function subscribe_mailchimp_user() { $post_id = $_REQUEST['yit_mailchimp_newsletter_form_id']; $mail = $_REQUEST['yit_mailchimp_newsletter_form_email']; $apikey = ""; $list = ""; if (isset($post_id) && strcmp($post_id, '') != 0) { $apikey = YITH_Popup()->get_meta('_mailchimp-apikey', $post_id); $list = YITH_Popup()->get_meta('_mailchimp-list', $post_id); } if (isset($mail) && is_email($mail)) { if (isset($list) && strcmp($list, '-1') != 0 && isset($apikey) && strcmp($apikey, '') != 0 && check_ajax_referer('yit_mailchimp_newsletter_form_nonce', 'yit_mailchimp_newsletter_form_nonce', false)) { if (!class_exists('Mailchimp')) { include_once YITH_YPOP_INC . 'vendor/mailchimp/Mailchimp.php'; } $mailchimp_wrapper = new Mailchimp($apikey, array('ssl_verifypeer' => false)); $result = $mailchimp_wrapper->call('lists/batch-subscribe', array('apikey' => $apikey, 'id' => $list, 'batch' => array(array('email' => array('email' => $mail, 'email_type' => 'html'))), 'double_optin' => true)); if ($result['error_count'] != 0) { $message = '<span class="error">Something went wrong:'; $message .= '<ul>'; foreach ($result['errors'] as $error) { $code = $error['code']; $message .= '<li>'; if ($code <= 0) { $message .= __('Mailchimp server error', 'ypop'); } elseif ($code >= 100 && $code < 120) { $message .= __('Mailchimp user related error', 'ypop'); } elseif ($code >= 120 && $code < 200) { $message .= __('Mailchimp user related error (action)', 'ypop'); } elseif ($code >= 200 && $code < 210) { $message .= __('Mailchimp list related error', 'ypop'); } elseif ($code >= 210 && $code < 220) { $message .= __('Mailchimp list related error (basic action)', 'ypop'); } elseif ($code >= 220 && $code < 230) { $message .= __('Mailchimp list related error (import)', 'ypop'); } elseif ($code >= 230 && $code < 250) { $message .= __('Mailchimp list related error (email)', 'ypop'); } elseif ($code >= 250 && $code < 270) { $message .= __('Mailchimp list related error (merge)', 'ypop'); } elseif ($code >= 270 && $code < 300) { $message .= __('Mailchimp list related error (interest group)', 'ypop'); } else { $message .= __('Mailchimp general error', 'ypop'); } $message .= '</li>'; } $message .= '</ul></span>'; echo $message; } else { _e('<span class="success">Email successfully registered</span>', 'ypop'); } die; } else { _e('<span class="error">Ops! Something went wrong</span>', 'ypop'); die; } } else { _e('<span class="notice">Ops! You have to use a valid email address</span>', 'ypop'); die; } }
<?php /* Main Template for generating a new field to attach to a list */ $api = new Mailchimp($this->optionVal['api-key']); parse_str($p['form_data'], $formData); $list_id = $formData['mc-list-id']; $field_name = $formData['add-field-field-name']; $required = $formData['add-field-field-required']; $public = isset($formData['add-field-public']) ? $formData['add-field-public'] : ''; $merge_tag = $formData['add-field-field-merge-tag']; $field_type = $p['field_type']; $field_default_value = $formData['add-field-default-value']; if ($field_type == 'radio' || $field_type == 'dropdown') { try { $retval = $api->call('lists/merge-var-add', array('id' => $list_id, 'tag' => $merge_tag, 'name' => $field_name, 'options' => array('choices' => $formData['radio-dropdown-option'], 'field_type' => $field_type, 'req' => $required, 'public' => $public))); return "done"; } catch (Exception $e) { // catch any errors returned from MailChimp $errorMessage = $e->getMessage(); echo $errorMessage; // write our error to the error log, // when advanced debug is enabled if ($this->optionVal['debug'] == 1) { $this->writeErrorToErrorLog($e); } die; } } else { if ($field_type == 'date' || $field_type == 'birthday') { try { $retval = $api->call('lists/merge-var-add', array('id' => $list_id, 'tag' => $merge_tag, 'name' => $field_name, 'options' => array('field_type' => $field_type, 'req' => $required, 'public' => $public, 'dateformat' => $formData['add-field-dateformat'])));
public function mailchimp_submit() { $nonce = $_POST['mailchimpNonce']; if (!wp_verify_nonce($nonce, 'mailchimp-nonce')) { die(__('Security verification failed!', $this->plugin_slug)); } $return_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : esc_attr($_POST['request_url']); $form_id = esc_attr($_POST['form_id']); $ajax = isset($_POST['ajax']) ? $_POST['ajax'] : 0; $email = $_POST['EMAIL']; global $wpdb, $choobs_wp_mailchimp_forms_table; $form = $wpdb->get_row($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . $choobs_wp_mailchimp_forms_table . '` WHERE idform = %d AND form_status = %s', $form_id, 'active')); if (!$form) { if ($ajax == 0) { wp_redirect($return_url); exit; } _e('Could not complete the signup process. Please try again later.', $this->plugin_slug); exit; } unset($_POST['ajax']); unset($_POST['form_id']); unset($_POST['mailchimpNonce']); unset($_POST['request_url']); unset($_POST['_wp_http_referer']); unset($_POST['EMAIL']); $list_id = $form->form_list; $this->options = get_option('choobs_wp_mailchimp_info'); $mc = new Mailchimp(isset($this->options['choobs_wp_mailchimp_api_key']) ? $this->options['choobs_wp_mailchimp_api_key'] : ''); if (isset($this->options['choobs_wp_mailchimp_disable_ssl_verify']) && $this->options['choobs_wp_mailchimp_disable_ssl_verify'] == 'yes') { curl_setopt($mc->ch, CURLOPT_SSL_VERIFYPEER, false); } $params['id'] = $list_id; $params['email'] = array('email' => $email); $params['merge_vars'] = $_POST; $params['merge_vars']['new-email'] = $email; $params['double_optin'] = false; $params['update_existing'] = true; $params['send_welcome'] = true; try { $mc->call('lists/subscribe', $params); } catch (Exception $ex) { echo $ex->getMessage(); exit; } if ($ajax == 0) { wp_redirect($return_url); exit; } _e('Thank you for your interest. We will keep you updated with our latest offers and news.', $this->plugin_slug); exit; }
function ymc_subscription_add($cid, $comment = '') { $cid = (int) $cid; $yikes_api_key = $this->optionVal['api-key']; $exploded_api_key = explode('-', $yikes_api_key); $yikes_data_center = $exploded_api_key[1]; if (!is_object($comment)) { $comment = get_comment($cid); } if ($comment->comment_karma == 0) { $subscribe = get_comment_meta($cid, 'mailchimp_subscribe', true); if ($subscribe == 'on') { global $current_user; get_currentuserinfo(); $commenter_first_name = trim($current_user->user_firstname); $commenter_last_name = trim($current_user->user_lastname); if (isset($commenter_first_name) && $commenter_first_name != '') { $commenter_first_name = $commenter_first_name; // use the users first name set in the profile } else { $commenter_first_name = $comment->comment_author; // if no first name is set in the user profile, we will use the account name } if (isset($commenter_last_name) && $commenter_last_name != '') { $commenter_last_name = $commenter_last_name; // use the users last name set in the profile } else { $commenter_last_name = 'n/a'; // if the user has not set a last name in their profile, we set it to n/a } // store our API key // on the settings page, if they have chosen to display the checkbox $api = new Mailchimp($this->optionVal['api-key']); $apikey = $yikes_api_key; $listid = $this->optionVal['yks-mailchimp-optIn-default-list']; // Need to set up a default list to subscribe all users to $endpoint = 'https://api.mailchimp.com'; $optin = $this->optionVal['optin']; // setup our welcome email variable if (isset($this->optionVal['lists'][$listid]['fields'][$listid . '-email']['yks_mailchimp_send_welcome_' . $listid]) && $this->optionVal['lists'][$listid]['fields'][$listid . '-email']['yks_mailchimp_send_welcome_' . $listid] == '1') { $welcome = false; } else { $welcome = true; } // try adding subscriber, catch any error thrown try { $retval = $api->call('lists/subscribe', array('id' => $listid, 'email' => array('email' => $comment->comment_author_email), 'merge_vars' => array('FNAME' => $commenter_first_name, 'LNAME' => $commenter_last_name, 'NAME' => $commenter_first_name), 'double_optin' => $optin, 'send_welcome' => $welcome)); return "done"; } catch (Exception $e) { // catch any errors returned from MailChimp $error_message = $e->getMessage(); if (strpos($error_message, 'Click here to update your profile.') !== false) { $errorMessage = str_replace('Click here to update your profile.', '', $e->getMessage()); $errorMessage_explode = explode('to list', $errorMessage); echo $errorMessage_explode[0] . '.'; die; } if (strpos($error_message, 'Invalid') !== false) { $display_errorMessage = str_replace('Invalid MailChimp List ID:', "Oops! The Webmaster hasn't set up the default MailChimp list to subscribe you too. Please contact them and let them know of this error. In the meantime, un-check the subscription checkbox in the comment form when submitting comments.", $error_message); echo $display_errorMessage; die; } else { // str_replace('Invalid MailChimp List ID: .', 'The Webmaster hasn\t set up the default MailChimp list to subscribe you too. Please contact them and let them know of this error. In the meantime, un-check the subscription checkbox in the comment form when submitting comments.', $e->getMessage()); echo $errorMessage; die; } } } } }
public function get_mailing_list_choices() { $api_key = g1_get_theme_option('mailchimp', 'api_key', ''); $choices = array(); if (!empty($api_key)) { $api_version = G1_Mailchimp_Module()->get_api_version(); $debug_mode = true === G1_Mailchimp_Module()->get_debug_mode(); if ($api_version === '1.0') { if (!class_exists('MCAPI')) { require_once dirname(__FILE__) . '/lib/MCAPI.class.php'; } $mcapi = new MCAPI($api_key); $mc_lists = $mcapi->lists(); if (empty($mcapi->errorCode) && $mc_lists['total'] > 0) { foreach ($mc_lists['data'] as $mc_list) { $list_id = $mc_list['id']; $list_name = $mc_list['name']; $choices[$list_id] = $list_name; } } } if ($api_version === '2.0') { if (!class_exists('Mailchimp')) { require_once dirname(__FILE__) . '/lib/Mailchimp.php'; } $api = new Mailchimp($api_key, array('debug' => $debug_mode)); try { $res = $api->call('lists/list', array()); if (!empty($res['errors'])) { foreach ($res['errors'] as $error) { $choices[] = $error; } } else { if (abs($res['total']) > 0) { foreach ($res['data'] as $list) { $id = $list['id']; $name = $list['name']; $choices[$id] = $name; } } } } catch (Mailchimp_Error $e) { if ($debug_mode) { $choices[] = $e->getMessage() . ' (code: ' . $e->getCode() . ')'; } } } } else { $choices[] = __('To fetch the list you need to enter your MailChimp Api Key in the theme options panel.', 'g1_theme'); } if (empty($choices)) { $choices[] = __('Some errors occurred while fetching Mailchimp data.', 'g1_theme'); } return $choices; }
function _action_fw_ssd_mailchimp_widget() { $api_key = fw_ssd_get_option('mailchimp_api_key') ? fw_ssd_get_option('mailchimp_api_key') : false; $list_id = fw_ssd_get_option('mailchimp_list_id') ? fw_ssd_get_option('mailchimp_list_id') : false; if (!$api_key || !$list_id) { if (current_user_can('edit_theme_options')) { $error_msg = esc_html__('Mailchimp API Key or List ID are not set.', 'couponhut'); } else { $error_msg = esc_html__("Something went wrong. We couldn't sign you up.", 'couponhut'); } echo '<div class="response-error">' . $error_msg . '</div>'; } else { $email = strtolower($_POST['email']); $Mailchimp = new Mailchimp($api_key); $result = $Mailchimp->call('lists/subscribe', array('id' => $list_id, 'email' => array('email' => $email), 'double_optin' => false, 'update_existing' => false, 'replace_interests' => false, 'send_welcome' => true)); if (!empty($result->status) && 'error' == $result->status) { $error_msg = ''; if ('List_AlreadySubscribed' == $result->name) { $error_msg = esc_html__('Oops! This email address is already subscribed!', 'couponhut'); } elseif ('Email_NotExists' == $result->name) { $error_msg = esc_html__('Email address does not exist', 'couponhut'); } elseif ('List_DoesNotExist' == $result->name) { $error_msg = current_user_can('edit_theme_options') ? esc_html__('List does not exist, please choose a valid list.', 'couponhut') : esc_html__("Something went wrong. We couldn't sign you up.", 'couponhut'); } else { $error_msg = esc_html__("Something went wrong. We couldn't sign you up.", 'couponhut'); } // An error ocurred, return error message echo '<div class="alert alert-danger">' . $error_msg . '</div>'; } else { echo '<div class="alert alert-success">' . esc_html__('Success! You have signed up.', 'couponhut') . '</div>'; } } die; }
/** * Handle mailchimp lists loading * @since 1.2.3 */ public static function action_wp_ajax_prompt_mailchimp_get_lists() { if (empty($_POST['api_key'])) { wp_send_json_error(array('error' => __('An API Key is required', 'Postmatic'))); } if (!class_exists('Mailchimp')) { require_once dirname(dirname(__FILE__)) . '/vendor/mailchimp/mailchimp/src/Mailchimp.php'; } $api_key = sanitize_text_field($_POST['api_key']); $mailchimp = new Mailchimp($api_key); try { $mailchimp_lists = $mailchimp->call('lists/list', array('filters' => array('created_before' => date('Y-m-d H:i:s', strtotime('-14 days'))))); } catch (Exception $e) { wp_send_json_error(array('error' => $e->getMessage())); } if (empty($mailchimp_lists['data'])) { wp_send_json_error(array('error' => sprintf(__('We\'re sorry. None of your lists qualified. <a href="%s">Click here for more information</a>', 'Postmatic'), 'http://docs.gopostmatic.com/article/144-im-having-trouble-importing-my-mailchimp-lists'))); } $mailchimp_list_options = array(); foreach ($mailchimp_lists['data'] as $list) { $mailchimp_list_options[] = html('option', array('value' => $list['id']), $list['name'], ' (', $list['stats']['member_count'], ')'); } $local_list_options = array(); foreach (Prompt_Subscribing::get_signup_lists() as $index => $list) { $local_list_options[] = html('option', array('value' => $index), $list->subscription_object_label()); } $content = html('div', html('label for="import_list"', __('Choose a Mailchimp list to import from:', 'Postmatic'), ' ', html('select', array('name' => 'import_list', 'type' => 'select'), implode('', $mailchimp_list_options))), '<br/>', html('label id="signup_list_index_label" for="signup_list_index" style="display: none;"', __('Choose a Postmatic list to import to:', 'Postmatic'), ' ', html('select', array('name' => 'signup_list_index', 'type' => 'select'), implode('', $local_list_options)))); wp_send_json_success($content); }