/** * @return bool */ public function process() { $api = mc4wp_get_api(); do_action('mc4wp_before_subscribe', $this->data['EMAIL'], $this->data, 0); $result = false; $email_type = $this->get_email_type(); // loop through selected lists foreach ($this->list_fields_map as $list_id => $list_field_data) { // allow plugins to alter merge vars for each individual list $list_merge_vars = $this->get_list_merge_vars($list_id, $list_field_data); // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $this->data['EMAIL'], $list_merge_vars, $email_type, $this->form->settings['double_optin'], $this->form->settings['update_existing'], $this->form->settings['replace_interests'], $this->form->settings['send_welcome']); do_action('mc4wp_subscribe', $this->data['EMAIL'], $list_id, $list_merge_vars, $result, 'form', 'form', 0); } do_action('mc4wp_after_subscribe', $this->data['EMAIL'], $this->data, 0, $result); // did we succeed in subscribing with the parsed data? if (!$result) { $this->message_type = $api->get_error_code() === 214 ? 'already_subscribed' : 'error'; $this->mailchimp_error = $api->get_error_message(); } else { $this->message_type = 'subscribed'; // store user email in a cookie MC4WP_Tools::remember_email($this->data['EMAIL']); } $this->success = $result; return $result; }
/** * @return bool */ public function process() { $api = mc4wp_get_api(); do_action('mc4wp_before_subscribe', $this->user_data['EMAIL'], $this->user_data, 0); $result = false; $email_type = $this->get_email_type(); // loop through selected lists foreach ($this->map->list_fields as $list_id => $list_field_data) { // allow plugins to alter merge vars for each individual list $list_merge_vars = $this->get_list_merge_vars($list_id, $list_field_data); // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $this->user_data['EMAIL'], $list_merge_vars, $email_type, $this->form->settings['double_optin'], $this->form->settings['update_existing'], $this->form->settings['replace_interests'], $this->form->settings['send_welcome']); do_action('mc4wp_subscribe', $this->user_data['EMAIL'], $list_id, $list_merge_vars, $result, 'form', 'form', $this->form->ID); } do_action('mc4wp_after_subscribe', $this->user_data['EMAIL'], $this->user_data, 0, $result); // did we succeed in subscribing with the parsed data? if (!$result) { $this->message_type = $api->get_error_code() === 214 ? 'already_subscribed' : 'error'; $this->mailchimp_error = $api->get_error_message(); } else { $this->message_type = 'subscribed'; // store user email in a cookie $this->set_email_cookie($this->user_data['EMAIL']); // send an email copy if that is desired // todo: move this to hook callback if ($this->form->settings['send_email_copy']) { $email = new MC4WP_Email_Notification($this->form->settings['email_copy_receiver'], $this->form, $this); $email->send(); } } $this->success = $result; return $result; }
/** * Returns number of subscribers on given lists. * * @param array $list_ids of list id's. * @return int Sum of subscribers for given lists. */ function mc4wp_get_subscriber_count($list_ids) { $list_counts = get_transient('mc4wp_list_counts'); if (false === $list_counts) { // make api call $api = mc4wp_get_api(); $lists = $api->get_lists(); $list_counts = array(); if ($lists) { foreach ($lists as $list) { $list_counts["{$list->id}"] = $list->stats->member_count; } $transient_lifetime = apply_filters('mc4wp_lists_count_cache_time', 1200); // 20 mins by default set_transient('mc4wp_list_counts', $list_counts, $transient_lifetime); set_transient('mc4wp_list_counts_fallback', $list_counts, 3600 * 24); // 1 day } else { // use fallback transient $list_counts = get_transient('mc4wp_list_counts_fallback'); if (!$list_counts) { return 0; } } } // start calculating subscribers count for all list combined $count = 0; foreach ($list_ids as $id) { $count += isset($list_counts[$id]) ? $list_counts[$id] : 0; } return apply_filters('mc4wp_subscriber_count', $count); }
function CF7_pre_send($cf7) { if (isset($_POST['is_subcribe']) && count($_POST['is_subcribe']) > 0) { $option = mc4wp_get_options('form'); $api = mc4wp_get_api(); foreach ($option['lists'] as $v) { $api->subscribe($v, $cf7->mail['recipient']); } } }
/** * @return bool */ public function process() { $api = mc4wp_get_api(); $result = false; foreach ($this->get_lists() as $list_id) { $result = $api->unsubscribe($list_id, $this->user_data['EMAIL']); } if (!$result) { $this->mailchimp_error = $api->get_error_message(); $this->message_type = in_array($api->get_error_code(), array(215, 232)) ? 'not_subscribed' : 'error'; } else { $this->message_type = 'unsubscribed'; } $this->success = $result; return $result; }
/** * Makes a subscription request * * @param string $email * @param array $merge_vars * @param int $related_object_ID * @return string|boolean */ protected function subscribe($email, array $merge_vars = array(), $type = '', $related_object_ID = 0) { $type = '' !== $type ? $type : $this->type; $api = mc4wp_get_api(); $opts = $this->get_options(); $lists = $this->get_lists(); if (empty($lists)) { // show helpful error message to admins, but only if not using ajax if ($this->show_error_messages()) { wp_die(' <h3>MailChimp for WP - Error</h3> <p>Please select a list to subscribe to in the <a href="' . admin_url('admin.php?page=mailchimp-for-wp-checkbox-settings') . '">checkbox settings</a>.</p> <p style="font-style:italic; font-size:12px;">This message is only visible to administrators for debugging purposes.</p> ', 'Error - MailChimp for WP', array('back_link' => true)); } return 'no_lists_selected'; } // maybe guess first and last name if (isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) { $strpos = strpos($merge_vars['NAME'], ' '); if ($strpos !== false) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } // set ip address if (!isset($merge_vars['OPTIN_IP']) && isset($_SERVER['REMOTE_ADDR'])) { $merge_vars['OPTIN_IP'] = sanitize_text_field($_SERVER['REMOTE_ADDR']); } $result = false; /** * @filter `mc4wp_merge_vars` * @expects array * @param array $merge_vars * @param string $type * * Use this to filter the final merge vars before the request is sent to MailChimp */ $merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, $type); /** * @filter `mc4wp_merge_vars` * @expects string * @param string $email_type * * Use this to change the email type this users should receive */ $email_type = apply_filters('mc4wp_email_type', 'html'); /** * @action `mc4wp_before_subscribe` * @param string $email * @param array $merge_vars * * Runs before the request is sent to MailChimp */ do_action('mc4wp_before_subscribe', $email, $merge_vars); foreach ($lists as $list_id) { $result = $api->subscribe($list_id, $email, $merge_vars, $email_type, $opts['double_optin'], $opts['update_existing'], true, $opts['send_welcome']); do_action('mc4wp_subscribe', $email, $list_id, $merge_vars, $result, 'checkbox', $type, $related_object_ID); } /** * @action `mc4wp_after_subscribe` * @param string $email * @param array $merge_vars * @param boolean $result * * Runs after the request is sent to MailChimp */ do_action('mc4wp_after_subscribe', $email, $merge_vars, $result); // if result failed, show error message (only to admins for non-AJAX) if ($result !== true && $api->has_error() && $this->show_error_messages()) { wp_die('<h3>MailChimp for WP - Error</h3> <p>The MailChimp server returned the following error message as a response to our sign-up request:</p> <pre>' . $api->get_error_message() . "</pre>\n\t\t\t\t\t<p>This is the data that was sent to MailChimp: </p>\n\t\t\t\t\t<strong>Email</strong>\n\t\t\t\t\t<pre>{$email}</pre>\n\t\t\t\t\t<strong>Merge variables</strong>\n\t\t\t\t\t<pre>" . print_r($merge_vars, true) . '</pre> <p><small>This message is only visible to administrators for debugging purposes.</small></p> ', 'Error - MailChimp for WP', array('back_link' => true)); } return $result; }
/** * Makes a subscription request * * @param string $email * @param array $merge_vars * @param string $type * @param int $related_object_id * @return string|boolean */ protected function subscribe($email, array $merge_vars = array(), $type = '', $related_object_id = 0) { $type = '' !== $type ? $type : $this->type; $api = mc4wp_get_api(); $opts = $this->get_options(); $lists = $this->get_lists(); if (empty($lists)) { // show helpful error message to admins, but only if not using ajax if ($this->show_error_messages()) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . sprintf(__('Please select a list to subscribe to in the <a href="%s">checkbox settings</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=mailchimp-for-wp-checkbox-settings')) . '</p>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } return 'no_lists_selected'; } $merge_vars = MC4WP_Tools::guess_merge_vars($merge_vars); // set ip address if (!isset($merge_vars['OPTIN_IP'])) { $merge_vars['OPTIN_IP'] = MC4WP_Tools::get_client_ip(); } $result = false; /** * @filter `mc4wp_merge_vars` * @expects array * @param array $merge_vars * @param string $type * * Use this to filter the final merge vars before the request is sent to MailChimp */ $merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, $type); /** * @filter `mc4wp_merge_vars` * @expects string * @param string $email_type * * Use this to change the email type this users should receive */ $email_type = apply_filters('mc4wp_email_type', 'html'); /** * @action `mc4wp_before_subscribe` * @param string $email * @param array $merge_vars * * Runs before the request is sent to MailChimp */ do_action('mc4wp_before_subscribe', $email, $merge_vars); foreach ($lists as $list_id) { $result = $api->subscribe($list_id, $email, $merge_vars, $email_type, $opts['double_optin'], $opts['update_existing'], true, $opts['send_welcome']); do_action('mc4wp_subscribe', $email, $list_id, $merge_vars, $result, 'checkbox', $type, $related_object_id); } /** * @action `mc4wp_after_subscribe` * @param string $email * @param array $merge_vars * @param boolean $result * * Runs after the request is sent to MailChimp */ do_action('mc4wp_after_subscribe', $email, $merge_vars, $result); // if result failed, show error message (only to admins for non-AJAX) if ($result !== true && $api->has_error()) { // log error error_log(sprintf('MailChimp for WordPres (%s): %s', date('Y-m-d H:i:s'), $this->type, $api->get_error_message())); if ($this->show_error_messages()) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . __('The MailChimp server returned the following error message as a response to our sign-up request:', 'mailchimp-for-wp') . '</p>' . '<pre>' . $api->get_error_message() . '</pre>' . '<p>' . __('This is the data that was sent to MailChimp:', 'mailchimp-for-wp') . '</p>' . '<strong>' . __('Email address:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html($email) . '</pre>' . '<strong>' . __('Merge variables:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html(print_r($merge_vars, true)) . '</pre>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } } return $result; }
/** * Subscribes the given email and additional list fields * * @param array $lists_data * @return bool */ private function subscribe($lists_data) { $api = mc4wp_get_api(); do_action('mc4wp_before_subscribe', $this->data['EMAIL'], $this->data, 0); $result = false; $email_type = $this->get_email_type(); // loop through selected lists foreach ($lists_data as $list_id => $list_field_data) { // allow plugins to alter merge vars for each individual list $list_merge_vars = $this->get_list_merge_vars($list_id, $list_field_data); // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $this->data['EMAIL'], $list_merge_vars, $email_type, $this->form_options['double_optin'], $this->form_options['update_existing'], $this->form_options['replace_interests'], $this->form_options['send_welcome']); do_action('mc4wp_subscribe', $this->data['EMAIL'], $list_id, $list_merge_vars, $result, 'form', 'form', 0); } do_action('mc4wp_after_subscribe', $this->data['EMAIL'], $this->data, 0, $result); if ($result !== true) { // subscribe request failed, store error. $this->success = false; $this->error_code = $result; $this->mailchimp_error = $api->get_error_message(); return false; } // subscription succeeded // store user email in a cookie $this->set_email_cookie($this->data['EMAIL']); // Store success result $this->success = true; return true; }
/** * Get MailChimp lists * Try cache first, then try API, then try fallback cache. */ private function get_mailchimp_lists() { $cached_lists = get_transient('mc4wp_mailchimp_lists'); $refresh_cache = isset($_POST['renew-cached-data']); if ($refresh_cache || !$cached_lists || empty($cached_lists)) { // make api request for lists $api = mc4wp_get_api(); $lists = array(); $lists_data = $api->get_lists(); if ($lists_data) { $lists = array(); foreach ($lists_data as $list) { $lists["{$list->id}"] = (object) array('id' => $list->id, 'name' => $list->name, 'subscriber_count' => $list->stats->member_count, 'merge_vars' => array(), 'interest_groupings' => array()); // get interest groupings $groupings_data = $api->get_list_groupings($list->id); if ($groupings_data) { $lists["{$list->id}"]->interest_groupings = array_map(array($this, 'strip_unnecessary_grouping_properties'), $groupings_data); } } // get merge vars for all lists at once $merge_vars_data = $api->get_lists_with_merge_vars(array_keys($lists)); if ($merge_vars_data) { foreach ($merge_vars_data as $list) { // add merge vars to list $lists["{$list->id}"]->merge_vars = array_map(array($this, 'strip_unnecessary_merge_vars_properties'), $list->merge_vars); } } // cache renewal triggered manually? if (isset($_POST['renew-cached-data'])) { if ($lists) { add_settings_error("mc4wp", "cache-renewed", 'Renewed MailChimp cache.', 'updated'); } else { add_settings_error("mc4wp", "cache-renew-failed", 'Failed to renew MailChimp cache - please try again later.'); } } // store lists in transients set_transient('mc4wp_mailchimp_lists', $lists, 24 * 3600); // 1 day set_transient('mc4wp_mailchimp_lists_fallback', $lists, 1209600); // 2 weeks return $lists; } else { // api request failed, get fallback data (with longer lifetime) $cached_lists = get_transient('mc4wp_mailchimp_lists_fallback'); if (!$cached_lists) { return array(); } } } return $cached_lists; }
/** * Process the request * * @return bool */ public function process() { $api = mc4wp_get_api(); if ($this->type === 'subscribe') { $success = $api->subscribe($this->list_id, $this->email, $this->merge_vars, $this->config['email_type'], $this->config['double_optin'], $this->config['update_existing'], $this->config['replace_interests'], $this->config['send_welcome']); } else { $success = $api->unsubscribe($this->list_id, $this->email, $this->config['send_goodbye'], $this->config['send_notification'], $this->config['delete_member']); } if ($success) { // store user email in a cookie // todo: decouple this MC4WP_Tools::remember_email($this->email); } // convert API response to our own response object $response = new MC4WP_API_Response($this->type, $success, $api->get_last_response()); $this->response = $response; /** * @api * @action 'mc4wp_request_processed' * * @param Request * @param Response */ do_action('mc4wp_request_processed', $this, $response); return $response; }
/** * Makes a subscription request * * @param string $email * @param array $merge_vars * @param string $type * @param null $related_object_id * * @return bool */ protected function subscribe($email, array $merge_vars = array(), $type = '', $related_object_id = null) { $type = '' !== $type ? $type : $this->type; $api = mc4wp_get_api(); $opts = $this->get_options(); $lists = $this->get_lists(); if (empty($lists)) { if ($this->show_error_messages()) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . sprintf(__('Please select a list to subscribe to in the <a href="%s">checkbox settings</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=mailchimp-for-wp-checkbox-settings')) . '</p>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } return 'no_lists_selected'; } // maybe guess first and last name if (isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) { $strpos = strpos($merge_vars['NAME'], ' '); if ($strpos !== false) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } // set ip address if (!isset($merge_vars['OPTIN_IP']) && isset($_SERVER['REMOTE_ADDR'])) { $merge_vars['OPTIN_IP'] = sanitize_text_field($_SERVER['REMOTE_ADDR']); } $result = false; /** * @filter `mc4wp_merge_vars` * @expects array * @param array $merge_vars * @param string $type * * Use this to filter the final merge vars before the request is sent to MailChimp */ $merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, $type); /** * @filter `mc4wp_merge_vars` * @expects string * @param string $email_type * * Use this to change the email type this users should receive */ $email_type = apply_filters('mc4wp_email_type', 'html'); /** * @action `mc4wp_before_subscribe` * @param string $email * @param array $merge_vars * * Runs before the request is sent to MailChimp */ do_action('mc4wp_before_subscribe', $email, $merge_vars); foreach ($lists as $list_id) { $result = $api->subscribe($list_id, $email, $merge_vars, $email_type, $opts['double_optin'], false, true, $opts['send_welcome']); do_action('mc4wp_subscribe', $email, $list_id, $merge_vars, $result, 'checkbox', $type, $related_object_id); } /** * @action `mc4wp_after_subscribe` * @param string $email * @param array $merge_vars * @param boolean $result * * Runs after the request is sent to MailChimp */ do_action('mc4wp_after_subscribe', $email, $merge_vars, $result); // check if result succeeded, show debug message to administrators (only in NON-AJAX requests) if ($result !== true && $api->has_error() && $this->show_error_messages()) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . __('The MailChimp server returned the following error message as a response to our sign-up request:', 'mailchimp-for-wp') . '</p>' . '<pre>' . $api->get_error_message() . '</pre>' . '<p>' . __('This is the data that was sent to MailChimp:', 'mailchimp-for-wp') . '</p>' . '<strong>' . __('Email address:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html($email) . '</pre>' . '<strong>' . __('Merge variables:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html(print_r($merge_vars, true)) . '</pre>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } return $result; }
/** * Returns the HTML for success or error messages * * @return string */ public function get_response_html() { // get all form messages $messages = $this->get_form_messages(); // retrieve correct message $type = $this->is_successful() ? 'success' : $this->get_error_code(); $message = isset($messages[$type]) ? $messages[$type] : $messages['error']; /** * @filter mc4wp_form_error_message * @deprecated 2.0.5 * @use mc4wp_form_messages * * Used to alter the error message, don't use. Use `mc4wp_form_messages` instead. */ $message['text'] = apply_filters('mc4wp_form_error_message', $message['text'], $this->get_error_code()); $html = '<div class="mc4wp-alert mc4wp-' . $message['type'] . '">' . $message['text'] . '</div>'; // show additional MailChimp API errors to administrators if (false === $this->is_successful() && current_user_can('manage_options')) { // show MailChimp error message (if any) to administrators $api = mc4wp_get_api(); if ($api->has_error()) { $html .= '<div class="mc4wp-alert mc4wp-error"><strong>' . __('MailChimp Error:', 'mailchimp-for-wp') . '</strong><br />' . $api->get_error_message() . '</div>'; } } return $html; }
/** * Show the API settings page */ public function show_api_settings() { $opts = mc4wp_get_options('general'); $connected = mc4wp_get_api()->is_connected(); // cache renewal triggered manually? $force_cache_refresh = isset($_POST['mc4wp-renew-cache']) && $_POST['mc4wp-renew-cache'] == 1; $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists($force_cache_refresh); if ($force_cache_refresh) { if (false === empty($lists)) { add_settings_error("mc4wp", "mc4wp-cache-success", __('Renewed MailChimp cache.', 'mailchimp-for-wp'), 'updated'); } else { add_settings_error("mc4wp", "mc4wp-cache-error", __('Failed to renew MailChimp cache - please try again later.', 'mailchimp-for-wp')); } } require MC4WP_LITE_PLUGIN_DIR . 'includes/views/api-settings.php'; }
/** * Show general settings page */ public function show_general_settings() { $opts = mc4wp_get_options('general'); $connected = mc4wp_get_api()->is_connected(); if (!$connected) { add_settings_error('mc4wp', 'invalid-api-key', sprintf(__('Please make sure the plugin is connected to MailChimp. <a href="%s">Provide a valid API key.</a>', 'mailchimp-for-wp'), admin_url('?page=mailchimp-for-wp')), 'updated'); } // cache renewal triggered manually? $force_cache_refresh = isset($_POST['mc4wp-renew-cache']) && $_POST['mc4wp-renew-cache'] == 1; $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists($force_cache_refresh); if ($force_cache_refresh) { if (false === empty($lists)) { add_settings_error('mc4wp', 'mc4wp-cache-success', __('Renewed MailChimp cache.', 'mailchimp-for-wp'), 'updated'); } else { add_settings_error('mc4wp', 'mc4wp-cache-error', __('Failed to renew MailChimp cache - please try again later.', 'mailchimp-for-wp')); } } require MC4WP_PLUGIN_DIR . 'includes/views/pages/admin-general-settings.php'; }
/** * Returns the HTML for success or error messages * * @return string */ private function get_form_message_html( $form_id = 0 ) { // don't show message if form wasn't submitted if( ! is_object( $this->form_request ) ) { return ''; } // get all form messages $messages = $this->get_form_messages( $form_id ); // retrieve correct message $type = ( $this->form_request->is_successful() ) ? 'success' : $this->form_request->get_error_code(); $message = ( isset( $messages[ $type ] ) ) ? $messages[ $type ] : $messages['error']; /** * @filter mc4wp_form_error_message * @deprecated 2.0.5 * @use mc4wp_form_messages * * Used to alter the error message, don't use. Use `mc4wp_form_messages` instead. */ $message['text'] = apply_filters('mc4wp_form_error_message', $message['text'], $this->form_request->get_error_code() ); $html = '<div class="mc4wp-alert mc4wp-'. $message['type'].'">' . $message['text'] . '</div>'; // show additional MailChimp API errors to administrators if( false === $this->form_request->is_successful() && current_user_can( 'manage_options' ) ) { // show MailChimp error message (if any) to administrators $api = mc4wp_get_api(); if( $api->has_error() ) { $html .= '<div class="mc4wp-alert mc4wp-error"><strong>Admin notice:</strong> '. $api->get_error_message() . '</div>'; } } return $html; }
/** * Update the subscriber uid with the new user data * * @param int $user_id * @return bool */ public function update_subscriber($user_id) { // get user $user = $this->get_user($user_id); if (!$user) { return false; } // get subscriber uid $subscriber_uid = $this->get_user_subscriber_uid($user); if (!$subscriber_uid) { return $this->subscribe_user($user_id); } // check email address if ('' === $user->user_email || !is_email($user->user_email)) { $this->error = 'Invalid email.'; return false; } // check if user should be synced if (!$this->should_sync_user($user)) { return false; } $merge_vars = $this->extract_merge_vars_from_user($user); $merge_vars['new-email'] = $user->user_email; // update subscriber in mailchimp $api = mc4wp_get_api(); $success = $api->update_subscriber($this->list_id, array('leid' => $subscriber_uid), $merge_vars, $this->settings['email_type'], $this->settings['replace_interests']); // TODO: Remove check for `get_error_code`, available since MailChimp for WP Lite 2.2.8 and MailChimp for WP Pro 2.6.3. Update dependency check in that case. if (!$success) { // subscriber leid did not match anything in the list, remove it and re-subscribe. if (!method_exists($api, 'get_error_code') || $api->get_error_code() === 232) { delete_user_meta($user_id, $this->meta_key); return $this->subscribe_user($user_id); } $this->error = $api->get_error_message(); $this->log->write_line(sprintf('Could not update user %d. MailChimp returned the following error: %s', $user_id, $this->error)); } return $success; }
/** * Subscribes the given email and additional list fields * * - Guesses FNAME and LNAME, if not set but NAME is. * - Adds OPTIN_IP field * - Validates merge_vars according to selected list(s) requirements * - Checks if a list was selected or given in form * * @param string $email * @param array $merge_vars * * @return bool */ private function subscribe( $email, $merge_vars = array() ) { // Try to guess FNAME and LNAME if they are not given, but NAME is if( isset( $merge_vars['NAME'] ) && !isset( $merge_vars['FNAME'] ) && ! isset( $merge_vars['LNAME'] ) ) { $strpos = strpos($merge_vars['NAME'], ' '); if( $strpos !== false ) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } // set ip address if( ! isset( $merge_vars['OPTIN_IP'] ) && isset( $_SERVER['REMOTE_ADDR'] ) ) { $merge_vars['OPTIN_IP'] = $_SERVER['REMOTE_ADDR']; } $api = mc4wp_get_api(); // get lists to subscribe to $lists = $this->get_lists(); if ( empty( $lists ) ) { $this->error_code = 'no_lists_selected'; return false; } // validate fields according to mailchimp list field types $merge_vars = $this->validate_merge_vars( $merge_vars ); if( false === $merge_vars ) { return false; } do_action( 'mc4wp_before_subscribe', $email, $merge_vars, 0 ); $result = false; $email_type = $this->get_email_type(); foreach ( $lists as $list_id ) { // allow plugins to alter merge vars for each individual list $list_merge_vars = apply_filters( 'mc4wp_merge_vars', $merge_vars, 0, $list_id ); // send a subscribe request to MailChimp for each list $result = $api->subscribe( $list_id, $email, $list_merge_vars, $email_type, $this->form_options['double_optin'] ); } do_action( 'mc4wp_after_subscribe', $email, $merge_vars, 0, $result ); if ( $result !== true ) { // subscribe request failed, store error. $this->success = false; $this->error_code = $result; return false; } // store user email in a cookie $this->set_email_cookie( $email ); // Store success result $this->success = true; return true; }
/** * Show the API settings page */ public function show_api_settings() { $opts = mc4wp_get_options('general'); $connected = mc4wp_get_api()->is_connected(); // cache renewal triggered manually? $force_cache_refresh = isset($_POST['mc4wp-renew-cache']) && $_POST['mc4wp-renew-cache'] == 1; $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists($force_cache_refresh); if ($lists && count($lists) === 100) { add_settings_error('mc4wp', 'mc4wp-lists-at-limit', __('The plugin can only fetch a maximum of 100 lists from MailChimp, only your first 100 lists are shown.', 'mailchimp-for-wp')); } if ($force_cache_refresh) { if (false === empty($lists)) { add_settings_error('mc4wp', 'mc4wp-cache-success', __('Renewed MailChimp cache.', 'mailchimp-for-wp'), 'updated'); } else { add_settings_error('mc4wp', 'mc4wp-cache-error', __('Failed to renew MailChimp cache - please try again later.', 'mailchimp-for-wp')); } } require MC4WP_LITE_PLUGIN_DIR . 'includes/views/api-settings.php'; }
/** * Returns number of subscribers on given lists. * * @param array $list_ids of list id's. * @return int Sum of subscribers for given lists. */ public function get_subscriber_count($list_ids) { // don't count when $list_ids is empty or not an array if (!is_array($list_ids) || count($list_ids) === 0) { return 0; } $list_counts = get_transient('mc4wp_list_counts'); if (false === $list_counts) { // make api call $api = mc4wp_get_api(); $lists = $api->get_lists(); $list_counts = array(); if (is_array($lists)) { foreach ($lists as $list) { $list_counts["{$list->id}"] = $list->stats->member_count; } /** * @filter `mc4wp_lists_count_cache_time` * @expects int * * Sets the amount of time the subscriber count for lists should be stored */ $transient_lifetime = apply_filters('mc4wp_lists_count_cache_time', 1200); // 20 mins by default set_transient('mc4wp_list_counts', $list_counts, $transient_lifetime); set_transient('mc4wp_list_counts_fallback', $list_counts, 86400); // 1 day } else { // use fallback transient $list_counts = get_transient('mc4wp_list_counts_fallback'); if (false === $list_counts) { return 0; } } } // start calculating subscribers count for all list combined $count = 0; foreach ($list_ids as $id) { $count += isset($list_counts[$id]) ? $list_counts[$id] : 0; } return apply_filters('mc4wp_subscriber_count', $count); }
function checkout_subscribers() { if (isset($_POST['_mc4wp_subscribe_registration_form']) && $_POST['_mc4wp_subscribe_registration_form'] == 1) { if (is_user_logged_in()) { $user = wp_get_current_user(); update_user_meta($user->ID, 'user_meta_mailchimp', 1); $firstName = $user->first_name; $lastName = $user->last_name; $email = $user->user_email; $api = mc4wp_get_api(); $lists = get_list_mailchimp(); $merge_vars = array('FNAME' => $firstName, 'LNAME' => $lastName); foreach ($lists as $list) { if (!$api->list_has_subscriber($list, $email)) { $api->subscribe($list, $email, $merge_vars); } } } } }
/** * Act on posted data * * @var array $data */ public function subscribe(array $data) { $email = null; $merge_vars = array(); foreach ($data as $name => $value) { // uppercase all variables $name = trim(strtoupper($name)); $value = is_scalar($value) ? trim($value) : $value; if ($name === 'EMAIL' && is_email($value)) { // set the email address $email = $value; } else { if ($name === 'GROUPINGS') { $groupings = $value; // malformed if (!is_array($groupings)) { continue; } // setup groupings array $merge_vars['GROUPINGS'] = array(); foreach ($groupings as $grouping_id_or_name => $groups) { $grouping = array(); if (is_numeric($grouping_id_or_name)) { $grouping['id'] = $grouping_id_or_name; } else { $grouping['name'] = $grouping_id_or_name; } // comma separated list should become an array if (!is_array($groups)) { $groups = explode(',', $groups); } $grouping['groups'] = array_map('stripslashes', $groups); // add grouping to array $merge_vars['GROUPINGS'][] = $grouping; } if (empty($merge_vars['GROUPINGS'])) { unset($merge_vars['GROUPINGS']); } } else { if ($name === 'BIRTHDAY') { // format birthdays in the DD/MM format required by MailChimp $merge_vars['BIRTHDAY'] = date('m/d', strtotime($value)); } else { if ($name === 'ADDRESS') { if (!isset($value['addr1'])) { // addr1, addr2, city, state, zip, country $addr_pieces = explode(',', $value); // try to fill it.... this is a long shot $merge_vars['ADDRESS'] = array('addr1' => $addr_pieces[0], 'city' => isset($addr_pieces[1]) ? $addr_pieces[1] : '', 'state' => isset($addr_pieces[2]) ? $addr_pieces[2] : '', 'zip' => isset($addr_pieces[3]) ? $addr_pieces[3] : ''); } else { // form contains the necessary fields already: perfection $merge_vars['ADDRESS'] = $value; } } else { // just add to merge vars array $merge_vars[$name] = $value; } } } } } // check if an email address has been found if (!$email) { $this->error = 'invalid_email'; return false; } // Try to guess FNAME and LNAME if they are not given, but NAME is if (isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) { $strpos = strpos($merge_vars['NAME'], ' '); if ($strpos !== false) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } $api = mc4wp_get_api(); $opts = mc4wp_get_options('form'); $lists = $this->get_lists(); if (empty($lists)) { $this->error = 'no_lists_selected'; return false; } do_action('mc4wp_before_subscribe', $email, $merge_vars, 0); $result = false; $email_type = $this->get_email_type(); foreach ($lists as $list_id) { // allow plugins to alter merge vars for each individual list $list_merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, 0, $list_id); // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $email, $list_merge_vars, $email_type, $opts['double_optin']); } do_action('mc4wp_after_subscribe', $email, $merge_vars, 0, $result); if ($result !== true) { // subscribe request failed, store error. $this->success = false; $this->error = $result; return false; } // store user email in a cookie $this->set_email_cookie($email); // Store success result $this->success = true; return true; }
private function subscribe($email, array $merge_vars = array(), $signup_type = 'comment', $comment_ID = null) { $api = mc4wp_get_api(); $opts = mc4wp_get_options('checkbox'); if (!$opts['lists'] || empty($opts['lists'])) { if ((!defined("DOING_AJAX") || !DOING_AJAX) && current_user_can('manage_options')) { wp_die(' <h3>MailChimp for WP - Error</h3> <p>Please select a list to subscribe to in the <a href="' . admin_url('admin.php?page=mc4wp-pro-checkbox-settings') . '">checkbox settings</a>.</p> <p style="font-style:italic; font-size:12px;">This message is only visible to administrators for debugging purposes.</p> ', "Error - MailChimp for WP", array('back_link' => true)); } return 'no_lists_selected'; } // maybe guess first and last name if (isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) { $strpos = strpos($merge_vars['NAME'], ' '); if ($strpos) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } $result = false; $merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, $signup_type); $email_type = apply_filters('mc4wp_email_type', 'html'); $lists = apply_filters('mc4wp_lists', $opts['lists'], $merge_vars); foreach ($lists as $list_ID) { $result = $api->subscribe($list_ID, $email, $merge_vars, $email_type, $opts['double_optin'], false, true, $opts['send_welcome']); } if ($result === true) { $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; do_action('mc4wp_subscribe_checkbox', $email, $opts['lists'], $signup_type, $merge_vars, $comment_ID, $from_url); } // check if result succeeded, show debug message to administrators (only in NON-AJAX requests) if ($result !== true && $api->has_error() && current_user_can('manage_options') && (!defined("DOING_AJAX") || !DOING_AJAX)) { wp_die("<h3>MailChimp for WP - Error</h3>\r\n\t\t\t\t\t<p>The MailChimp server returned the following error message as a response to our sign-up request:</p>\r\n\t\t\t\t\t<pre>" . $api->get_error_message() . "</pre>\r\n\t\t\t\t\t<p>This is the data that was sent to MailChimp: </p>\r\n\t\t\t\t\t<strong>Email</strong>\r\n\t\t\t\t\t<pre>{$email}</pre>\r\n\t\t\t\t\t<strong>Merge variables</strong>\r\n\t\t\t\t\t<pre>" . print_r($merge_vars, true) . "</pre>\r\n\t\t\t\t\t<p style=\"font-style:italic; font-size:12px; \">This message is only visible to administrators for debugging purposes.</p>\r\n\t\t\t\t\t", "Error - MailChimp for WP", array('back_link' => true)); } return $result; }
/** * Makes a subscription request * * @param string $email * @param array $merge_vars * @param string $signup_type * @param int $comment_ID * @return boolean */ protected function subscribe($email, array $merge_vars = array(), $signup_type = 'comment', $comment_id = null) { $api = mc4wp_get_api(); $opts = mc4wp_get_options('checkbox'); $lists = $this->get_lists(); if (empty($lists)) { if ((!defined('DOING_AJAX') || !DOING_AJAX) && current_user_can('manage_options')) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . sprintf(__('Please select a list to subscribe to in the <a href="%s">checkbox settings</a>.', 'mailchimp-for-wp'), admin_url('admin.php?page=mc4wp-lite-checkbox-settings')) . '</p>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } return 'no_lists_selected'; } // maybe guess first and last name if (isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) { $strpos = strpos($merge_vars['NAME'], ' '); if ($strpos !== false) { $merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos); $merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos); } else { $merge_vars['FNAME'] = $merge_vars['NAME']; } } // set ip address if (!isset($merge_vars['OPTIN_IP']) && isset($_SERVER['REMOTE_ADDR'])) { $merge_vars['OPTIN_IP'] = $_SERVER['REMOTE_ADDR']; } $result = false; $merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, $signup_type); $email_type = apply_filters('mc4wp_email_type', 'html'); do_action('mc4wp_before_subscribe', $email, $merge_vars); foreach ($lists as $list_id) { $result = $api->subscribe($list_id, $email, $merge_vars, $email_type, $opts['double_optin'], false, true); } do_action('mc4wp_after_subscribe', $email, $merge_vars, $result); if ($result === true) { $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; do_action('mc4wp_subscribe_checkbox', $email, $lists, $signup_type, $merge_vars, $comment_id, $from_url); } // check if result succeeded, show debug message to administrators (only in NON-AJAX requests) if ($result !== true && $api->has_error() && current_user_can('manage_options') && (!defined('DOING_AJAX') || !DOING_AJAX) && (!isset($_POST['_wpcf7_is_ajax_call']) || $_POST['_wpcf7_is_ajax_call'] != 1)) { wp_die('<h3>' . __('MailChimp for WordPress - Error', 'mailchimp-for-wp') . '</h3>' . '<p>' . __('The MailChimp server returned the following error message as a response to our sign-up request:', 'mailchimp-for-wp') . '</p>' . '<pre>' . $api->get_error_message() . '</pre>' . '<p>' . __('This is the data that was sent to MailChimp:', 'mailchimp-for-wp') . '</p>' . '<strong>' . __('Email address:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html($email) . '</pre>' . '<strong>' . __('Merge variables:', 'mailchimp-for-wp') . '</strong>' . '<pre>' . esc_html(print_r($merge_vars, true)) . '</pre>' . '<p style="font-style:italic; font-size:12px;">' . __('This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp') . '</p>', __('MailChimp for WordPress - Error', 'mailchimp-for-wp'), array('back_link' => true)); } return $result; }
/** * Subscribes the given email and additional list fields * * @param array $lists_data * @return bool */ private function subscribe($lists_data) { $api = mc4wp_get_api(); do_action('mc4wp_before_subscribe', $this->data['EMAIL'], $this->data, 0); $result = false; $email_type = $this->get_email_type(); foreach ($lists_data as $list_id => $list_field_data) { // allow plugins to alter merge vars for each individual list $list_merge_vars = $this->get_list_merge_vars($list_field_data); $list_merge_vars = apply_filters('mc4wp_merge_vars', $list_merge_vars, 0, $list_id); // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $this->data['EMAIL'], $list_merge_vars, $email_type, $this->form_options['double_optin'], $this->form_options['update_existing'], $this->form_options['replace_interests'], $this->form_options['send_welcome']); } do_action('mc4wp_after_subscribe', $this->data['EMAIL'], $this->data, 0, $result); if ($result !== true) { // subscribe request failed, store error. $this->success = false; $this->error_code = $result; $this->mailchimp_error = $api->get_error_message(); return false; } // subscription succeeded // store user email in a cookie $this->set_email_cookie($this->data['EMAIL']); /** * @deprecated Don't use, will be removed in v2.0 * TODO: remove this */ $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; do_action('mc4wp_subscribe_form', $this->data['EMAIL'], array_keys($lists_data), 0, $this->data, $from_url); // Store success result $this->success = true; return true; }