/** * Process a subscribe form. * * @param MC4WP_Form $form */ public function process_subscribe_form(MC4WP_Form $form) { $api = $this->get_api(); $result = false; $email = $form->data['EMAIL']; $email_type = $form->get_email_type(); $merge_vars = $form->data; /** * Filters merge vars which are sent to MailChimp, only fires for form requests. * * @param array $merge_vars * @param MC4WP_Form $form */ $merge_vars = (array) apply_filters('mc4wp_form_merge_vars', $merge_vars, $form); // create a map of all lists with list-specific merge vars $map = new MC4WP_Field_Map($merge_vars, $form->get_lists()); // loop through lists foreach ($map->list_fields as $list_id => $merge_vars) { // send a subscribe request to MailChimp for each list $result = $api->subscribe($list_id, $email, $merge_vars, $email_type, $form->settings['double_optin'], $form->settings['update_existing'], $form->settings['replace_interests'], $form->settings['send_welcome']); } // do stuff on failure if (!$result) { if ($api->get_error_code() == 214) { // handle "already_subscribed" as a soft-error $form->errors[] = 'already_subscribed'; $this->get_log()->warning(sprintf("Form %d > %s is already subscribed to the selected list(s)", $form->ID, mc4wp_obfuscate_string($form->data['EMAIL']))); } else { // log error $this->get_log()->error(sprintf('Form %d > MailChimp API error: %s', $form->ID, $api->get_error_message())); // add error code to form object $form->errors[] = 'error'; } // bail return; } $this->get_log()->info(sprintf("Form %d > Successfully subscribed %s", $form->ID, $form->data['EMAIL'])); /** * Fires right after a form was used to subscribe. * * @since 3.0 * * @param MC4WP_Form $form Instance of the submitted form * @param string $email * @param array $merge_vars * @param array $pretty_data */ do_action('mc4wp_form_subscribed', $form, $email, $merge_vars, $map->pretty_data); }
/** * Show the API Settings page */ public function show_generals_setting_page() { $opts = mc4wp_get_options(); $connected = !empty($opts['api_key']); if ($connected) { try { $connected = $this->get_api()->is_connected(); } catch (MC4WP_API_Connection_Exception $e) { $message = sprintf("<strong>%s</strong><br /> %s", __("Error connecting to MailChimp:", 'mailchimp-for-wp'), $e); $message .= '<br /><br />' . sprintf('<a href="%s">' . __('Here\'s some info on solving common connectivity issues.', 'mailchimp-for-wp') . '</a>', 'https://mc4wp.com/kb/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice'); $this->messages->flash($message, 'error'); $connected = false; } catch (MC4WP_API_Exception $e) { $this->messages->flash(sprintf("<strong>%s</strong><br /> %s", __("MailChimp returned the following error:", 'mailchimp-for-wp'), $e), 'error'); $connected = false; } } $lists = $this->mailchimp->get_cached_lists(); $obfuscated_api_key = mc4wp_obfuscate_string($opts['api_key']); require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php'; }
/** * Show the API Settings page */ public function show_generals_setting_page() { $opts = mc4wp_get_options(); $connected = mc4wp('api')->is_connected(); $lists = $this->mailchimp->get_lists(); $obfuscated_api_key = mc4wp_obfuscate_string($opts['api_key']); require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php'; }
/** * 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(), $related_object_id = 0) { $integration = $this; $slug = $this->slug; /** * @var MC4WP_API $api */ $api = mc4wp('api'); $lists = $this->get_lists(); $result = false; // validate lists if (empty($lists)) { $this->get_log()->warning(sprintf('%s > No MailChimp lists were selected', $this->name)); return false; } /** * Filters the final merge variables before the request is sent to MailChimp, for all integrations. * * @param array $merge_vars * @param MC4WP_Integration $integration */ $merge_vars = (array) apply_filters('mc4wp_integration_merge_vars', $merge_vars, $integration); /** * Filters the final merge variables before the request is sent to MailChimp, for a specific integration. * * The dynamic portion of the hook, `$slug`, refers to the integration slug. * * @param array $merge_vars * @param MC4WP_Integration $integration */ $merge_vars = (array) apply_filters('mc4wp_integration_' . $slug . '_merge_vars', $merge_vars, $integration); $email_type = mc4wp_get_email_type(); // create field map $map = new MC4WP_Field_Map($merge_vars, $lists); foreach ($map->list_fields as $list_id => $list_field_data) { $result = $api->subscribe($list_id, $email, $list_field_data, $email_type, $this->options['double_optin'], $this->options['update_existing'], $this->options['replace_interests'], $this->options['send_welcome']); } // if result failed, show error message if (!$result) { // log error if ($api->get_error_code() === 214) { $this->get_log()->warning(sprintf("%s > %s is already subscribed to the selected list(s)", $this->name, mc4wp_obfuscate_string($email))); } else { $this->get_log()->error(sprintf('%s > MailChimp API Error: %s', $this->name, $api->get_error_message())); } // bail return false; } $this->get_log()->info(sprintf('%s > Successfully subscribed %s', $this->name, $email)); /** * Runs right after someone is subscribed using an integration * * @since 3.0 * * @param MC4WP_Integration $integration * @param string $email * @param array $merge_vars * @param int $related_object_id */ do_action('mc4wp_integration_subscribed', $integration, $email, $merge_vars, $related_object_id); return $result; }