/** * Returns text with {variables} replaced. * * @param string $text * @param array $list_ids Array of list id's * @return string $text with {variables} replaced. */ function mc4wp_replace_variables($text, $list_ids = array()) { // get current WPML language or general site language $language = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_locale(); // replace general vars $needles = array('{ip}', '{current_url}', '{date}', '{time}', '{language}'); $replacements = array($_SERVER['REMOTE_ADDR'], mc4wp_get_current_url(), date('m/d/Y'), date('H:i:s'), $language); $text = str_ireplace($needles, $replacements, $text); // subscriber count? only fetch these if the tag is actually used if (stristr($text, '{subscriber_count}') !== false) { $mailchimp = new MC4WP_MailChimp(); $subscriber_count = $mailchimp->get_subscriber_count($list_ids); $text = str_ireplace('{subscriber_count}', $subscriber_count, $text); } // replace {email} tag if (isset($_GET['mc4wp_email'])) { $email = esc_attr($_GET['mc4wp_email']); } elseif (isset($_COOKIE['mc4wp_email'])) { $email = esc_attr($_COOKIE['mc4wp_email']); } else { $email = ''; } $text = str_ireplace('{email}', $email, $text); // replace user variables $needles = array('{user_email}', '{user_firstname}', '{user_lastname}', '{user_name}', '{user_id}'); if (is_user_logged_in() && ($user = wp_get_current_user()) && $user instanceof WP_User) { // logged in user, replace vars by user vars $replacements = array($user->user_email, $user->user_firstname, $user->user_lastname, $user->display_name, $user->ID); $text = str_ireplace($needles, $replacements, $text); } else { // no logged in user, replace vars with empty string $text = str_ireplace($needles, '', $text); } return $text; }
/** * Returns text with {variables} replaced. * * @param string $string * @param array $additional_replacements * @param array Array of list ID's (needed if {subscriber_count} is set * * @return string $text The text with {variables} replaced. * replaced. */ public static function replace_variables($string, $additional_replacements = array(), $list_ids = array()) { // replace general vars $replacements = array('{ip}' => self::get_client_ip(), '{current_url}' => mc4wp_get_current_url(), '{current_path}' => !empty($_SERVER['REQUEST_URI']) ? esc_html($_SERVER['REQUEST_URI']) : '', '{date}' => date('m/d/Y'), '{time}' => date('H:i:s'), '{language}' => defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_locale(), '{email}' => self::get_known_email(), '{user_email}' => '', '{user_firstname}' => '', '{user_lastname}' => '', '{user_name}' => '', '{user_id}' => ''); // setup replacements for logged-in users if (is_user_logged_in() && ($user = wp_get_current_user()) && $user instanceof WP_User) { // logged in user, replace vars by user vars $replacements['{user_email}'] = $user->user_email; $replacements['{user_firstname}'] = $user->first_name; $replacements['{user_lastname}'] = $user->last_name; $replacements['{user_name}'] = $user->display_name; $replacements['{user_id}'] = $user->ID; } // merge with additional replacements $replacements = array_merge($replacements, $additional_replacements); // subscriber count? only fetch these if the tag is actually used if (stristr($string, '{subscriber_count}') !== false) { $mailchimp = new MC4WP_MailChimp(); $subscriber_count = $mailchimp->get_subscriber_count($list_ids); $replacements['{subscriber_count}'] = $subscriber_count; } // perform the replacement $string = str_ireplace(array_keys($replacements), array_values($replacements), $string); // replace dynamic variables if (stristr($string, '{data_') !== false) { $string = preg_replace_callback('/\\{data_([\\w-.]+)( default=\\"([^"]*)\\"){0,1}\\}/', array('MC4WP_Tools', 'replace_request_data_variables'), $string); } return $string; }
/** * Empty lists cache & fetch lists again. */ public function refresh_mailchimp_lists() { if (!$this->tools->is_user_authorized()) { wp_send_json(false); } $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->fetch_lists(); $success = !empty($lists); wp_send_json($success); }
/** * @return MC4WP_MailChimp_Subscriber[] */ public function map() { $mailchimp = new MC4WP_MailChimp(); $map = array(); foreach ($this->list_ids as $list_id) { $list = $mailchimp->get_list($list_id); if ($list instanceof MC4WP_MailChimp_List) { $map[$list_id] = $this->map_list($list); } } return $map; }
/** * @param array $list_ids * @return MC4WP_MailChimp_List[] */ protected function fetch_lists(array $list_ids) { $mailchimp = new MC4WP_MailChimp(); $lists = array(); foreach ($list_ids as $id) { $list = $mailchimp->get_list($id, true); if ($list instanceof MC4WP_MailChimp_List) { $lists[$id] = $list; } } return $lists; }
/** * 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(); require MC4WP_PLUGIN_DIR . 'includes/views/general-settings.php'; }
/** * Show checkbox settings page */ public function show_checkbox_settings() { $opts = mc4wp_get_options('checkbox'); $lists = $this->mailchimp->get_lists(); $checkbox_plugins = $this->get_checkbox_compatible_plugins(); $selected_checkbox_hooks = $this->get_selected_checkbox_hooks(); require MC4WP_PLUGIN_DIR . 'includes/views/pages/admin-checkbox-settings.php'; }
/** * @param $item * * @return string */ public function column_list($item) { $list_names = array(); $list_ids = explode(',', $item->list_ids); foreach ($list_ids as $list_id) { $list_names[] = $this->mailchimp->get_list_name($list_id); } return join(', ', $list_names); }
/** * @param $item * * @return string */ public function column_list($item) { $list_names = array(); $list_ids = array_map('trim', explode(',', $item->list_ids)); foreach ($list_ids as $list_id) { $list = $this->mailchimp->get_list($list_id); $list_names[] = sprintf('<a href="%s" target="_blank">' . $list->name . '</a>', $list->get_web_url()); } return join(', ', $list_names); }
/** * @return array|bool */ public function map_lists_fields() { $map = array(); // loop through selected lists foreach ($this->lists as $list_id) { $list = $this->mailchimp->get_list($list_id, false, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { continue; } // generate map for this given list $list_map = $this->map_data_to_list($list); if ($list_map === false) { return false; } $map[$list_id] = $list_map; } return $map; }
/** * @param $list_id * * @return array * @throws Exception */ public function map_list($list_id) { $list = $this->mailchimp->get_list($list_id, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { return array(); } $map = array(); // loop through list fields foreach ($list->merge_vars as $field) { $map[$field->tag] = $this->map_list_field($field); } // loop through list interest groupings if (!empty($list->groupings)) { $map['GROUPINGS'] = array_map(array($this, 'map_list_grouping'), $list->groupings); $map['GROUPINGS'] = array_filter($map['GROUPINGS']); } $map = array_filter($map); return $map; }
/** * @param string $slug * * @internal */ public function show_integration_settings_page($slug) { try { $integration = $this->integrations->get($slug); } catch (Exception $e) { echo sprintf('<h3>Integration not found.</h3><p>No integration with slug <strong>%s</strong> was found.</p>', $slug); return; } $opts = $integration->options; $lists = $this->mailchimp->get_lists(); require dirname(__FILE__) . '/views/integration-settings.php'; }
/** * @param $item * * @return string */ public function column_lists($item) { $form = MC4WP_Form::get($item->ID); $content = ''; if (!empty($form->settings['lists'])) { foreach ($form->settings['lists'] as $list_id) { $content .= $this->mailchimp->get_list_name($list_id) . '<br />'; } } else { return '<a style="color: red; text-decoration: underline;" href="' . get_edit_post_link($item->ID) . '">' . __('No MailChimp list(s) selected yet.', 'mailchimp-for-wp') . '</a>'; } return $content; }
/** * @param $list_id * * @return array * @throws Exception */ protected function map_list_fields($list_id) { $list = $this->mailchimp->get_list($list_id, true); // skip this list if it's unexisting if (!$list instanceof MC4WP_MailChimp_List) { return array(); } $map = array(); // loop through list fields foreach ($list->merge_vars as $field) { $map[$field->tag] = $this->map_list_field($field); } // loop through list interest groupings if (!empty($list->groupings)) { $map['GROUPINGS'] = array_map(array($this, 'map_list_grouping'), $list->groupings); $map['GROUPINGS'] = array_filter($map['GROUPINGS']); } // add global fields (fields belong to ALL lists automatically) $map = array_merge($map, $this->global_fields); // filter out empty values $map = array_filter($map); return $map; }
/** * @param MC4WP_Form $form * * @return string */ public function column_lists(MC4WP_Form $form) { $list_ids = $form->settings['lists']; if (empty($list_ids)) { $content = '<a href="' . mc4wp_get_edit_form_url($form->ID, 'settings') . '" style="color: red;">' . __('No lists selected.', 'mailchimp-for-wp') . '</a>'; return $content; } $names = array(); foreach ($list_ids as $list_id) { $list = $this->mailchimp->get_list($list_id); if ($list instanceof MC4WP_MailChimp_List) { $names[] = sprintf('<a href="https://admin.mailchimp.com/lists/members/?id=%s" target="_blank">', $list->web_id) . esc_html($list->name) . '</a>'; } } return join('<br />', $names); }
/** * 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'; }
/** * @return int */ protected function get_mailchimp_lists_count() { $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists(false, true); return count($lists); }
/** * Show the forms settings page */ public function show_form_settings() { $opts = mc4wp_get_options('form'); $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists(); // create array of missing form fields $missing_form_fields = array(); // check if form contains EMAIL field $search = preg_match('/<(input|textarea)(?=[^>]*name="EMAIL")[^>]*>/i', $opts['markup']); if (!$search) { $missing_form_fields[] = sprintf(__('An EMAIL field. Example: <code>%s</code>', 'mailchimp-for-wp'), '<input type="email" name="EMAIL" />'); } // check if form contains submit button $search = preg_match('/<(input|button)(?=[^>]*type="submit")[^>]*>/i', $opts['markup']); if (!$search) { $missing_form_fields[] = sprintf(__('A submit button. Example: <code>%s</code>', 'mailchimp-for-wp'), '<input type="submit" value="' . __('Sign Up', 'mailchimp-for-wp') . '" />'); } // loop through selected list ids if (isset($opts['lists']) && is_array($opts['lists'])) { foreach ($opts['lists'] as $list_id) { // get list object $list = $mailchimp->get_list($list_id); if (!is_object($list)) { continue; } // loop through merge vars of this list foreach ($list->merge_vars as $merge_var) { // if field is required, make sure it's in the form mark-up if (!$merge_var->req || $merge_var->tag === 'EMAIL') { continue; } // search for field tag in form mark-up using 'name="FIELD_NAME' without closing " because of array fields $search = stristr($opts['markup'], 'name="' . $merge_var->tag); if (false === $search) { $missing_form_fields[] = sprintf(__('A \'%s\' field', 'mailchimp-for-wp'), $merge_var->tag); } } } } require MC4WP_LITE_PLUGIN_DIR . 'includes/views/form-settings.php'; }
/** * Returns the number of subscribers on the selected lists (for the form context) * * @return int */ public function get_subscriber_count() { $mailchimp = new MC4WP_MailChimp(); return $mailchimp->get_subscriber_count($this->form->get_lists()); }
/** * Validates the posted fields against merge_vars of selected list(s) * * @param array $data * * @return array|boolean Array of data on success, false on error */ private function validate_merge_vars( array $data ) { $list_ids = $this->get_lists(); $mailchimp = new MC4WP_MailChimp(); foreach( $list_ids as $list_id ) { $list = $mailchimp->get_list( $list_id, false, true ); // make sure list was found if( ! is_object( $list ) ) { continue; } // loop through list fields foreach( $list->merge_vars as $merge_var ) { // skip email field, it's validated elsewhere if( $merge_var->tag === 'EMAIL' ) { continue; } $posted_value = ( isset( $data[ $merge_var->tag ] ) && '' !== $data[ $merge_var->tag ] ) ? $data[ $merge_var->tag ] : ''; // check if required field is given if( $merge_var->req && '' === $posted_value ) { $this->error_code = 'required_field_missing'; return false; } // format birthday fields in MM/DD format, required by MailChimp if( $merge_var->field_type === 'birthday' && $posted_value !== '' ) { $data[ $merge_var->tag ] = date( 'm/d', strtotime( $data[ $merge_var->tag ] ) ); } // format address fields if( $merge_var->field_type === 'address' && $posted_value !== '' ) { if( ! isset( $posted_value['addr1'] ) ) { // addr1, addr2, city, state, zip, country $address_pieces = explode( ',', $posted_value ); // try to fill it.... this is a long shot $data[ $merge_var->tag ] = array( 'addr1' => $address_pieces[0], 'city' => ( isset( $address_pieces[1] ) ) ? $address_pieces[1] : '', 'state' => ( isset( $address_pieces[2] ) ) ? $address_pieces[2] : '', 'zip' => ( isset( $address_pieces[3] ) ) ? $address_pieces[3] : '' ); } else { // form contains the necessary fields already: perfection $data[ $merge_var->tag ] = $posted_value; } } } } return $data; }
/** * Maps the received data to MailChimp lists * * @return array */ private function map_data($data) { $map = array(); $mapped_fields = array('EMAIL'); $unmapped_fields = array(); $mailchimp = new MC4WP_MailChimp(); // loop through selected lists foreach ($this->get_lists() as $list_id) { $list = $mailchimp->get_list($list_id, false, true); // skip this list if it's unexisting if (!is_object($list) || !isset($list->merge_vars)) { continue; } // start with empty list map $list_map = array(); // loop through other list fields foreach ($list->merge_vars as $field) { // skip EMAIL field if ($field->tag === 'EMAIL') { continue; } // check if field is required if ($field->req) { if (!isset($data[$field->tag]) || '' === $data[$field->tag]) { $this->error_code = 'required_field_missing'; return false; } } // if field is not set, continue. if (!isset($data[$field->tag])) { continue; } // grab field value from data $field_value = $data[$field->tag]; // format field value according to its type $field_value = $this->format_field_value($field_value, $field->field_type); // add field value to map $mapped_fields[] = $field->tag; $list_map[$field->tag] = $field_value; } // loop through list groupings if GROUPINGS data was sent if (isset($data['GROUPINGS']) && is_array($data['GROUPINGS']) && !empty($list->interest_groupings)) { $list_map['GROUPINGS'] = array(); foreach ($list->interest_groupings as $grouping) { // check if data for this group was sent if (isset($data['GROUPINGS'][$grouping->id])) { $group_data = $data['GROUPINGS'][$grouping->id]; } elseif (isset($data['GROUPINGS'][$grouping->name])) { $group_data = $data['GROUPINGS'][$grouping->name]; } else { // no data for this grouping was sent, just continue. continue; } // format new grouping $grouping = array('id' => $grouping->id, 'groups' => $group_data); // make sure groups is an array if (!is_array($grouping['groups'])) { $grouping['groups'] = sanitize_text_field($grouping['groups']); $grouping['groups'] = explode(',', $grouping['groups']); } $list_map['GROUPINGS'][] = $grouping; } // unset GROUPINGS if no grouping data was found for this list if (0 === count($list_map['GROUPINGS'])) { unset($list_map['GROUPINGS']); } } // add to total map $map[$list_id] = $list_map; } // map global fields $global_field_names = array('MC_LOCATION', 'MC_NOTES', 'MC_LANGUAGE'); foreach ($global_field_names as $field_name) { if (isset($data[$field_name])) { $this->global_fields[$field_name] = $data[$field_name]; } } // is there still unmapped data left? $total_fields_mapped = count($mapped_fields) + count($this->global_fields); if ($total_fields_mapped < count($data)) { foreach ($data as $field_key => $field_value) { if ($this->is_internal_var($field_key)) { continue; } if (!in_array($field_key, $mapped_fields)) { $unmapped_fields[$field_key] = $field_value; } } } // add built arrays to instance properties $this->unmapped_fields = $unmapped_fields; $this->lists_fields_map = $map; return true; }
/** * Returns the number of subscribers on the selected lists (for the form context) * * @return int */ public function get_subscriber_count() { $mailchimp = new MC4WP_MailChimp(); $count = $mailchimp->get_subscriber_count($this->integration->get_lists()); return number_format($count); }
/** * Shows the "Add Form" page * * @internal */ public function show_add_page() { $lists = $this->mailchimp->get_lists(); $number_of_lists = count($lists); require dirname(__FILE__) . '/views/add-form.php'; }
/** * Helper function to retrieve MailChimp lists through MailChimp for WordPress * * Will try v3.0+ first, then fallback to older versions. * * @return array */ protected function get_mailchimp_lists() { /** * @since 3.0 */ if (class_exists('MC4WP_MailChimp_Tools') && method_exists('MC4WP_MailChimp_Tools', 'get_lists')) { return MC4WP_MailChimp_Tools::get_lists(); } /** @deprecated MailChimp for WordPress v3.0 */ if (class_exists('MC4WP_MailChimp')) { $mailchimp = new \MC4WP_MailChimp(); return $mailchimp->get_lists(); } return array(); }
/** * Refreshes MailChimp lists. This can take a while if the connected MailChimp account has many lists. * * @return void */ function mc4wp_refresh_mailchimp_lists() { $mailchimp = new MC4WP_MailChimp(); $mailchimp->fetch_lists(); }
/** * Send an email with a subscription summary to a given email address */ private function send_email() { // bail if receiver is empty if ('' === $this->form_options['email_copy_receiver']) { return; } // email receiver $to = explode(',', str_replace(' ', '', $this->form_options['email_copy_receiver'])); $form = get_post($this->form_id); $form_name = $form->post_title; // email subject $subject = __('New MailChimp Sign-Up', 'mailchimp-for-wp') . ' - ' . get_bloginfo('name'); $mailchimp = new MC4WP_MailChimp(); // build email message ob_start(); ?> <h3>MailChimp for WordPress: <?php _e('New Sign-Up', 'mailchimp-for-wp'); ?> </h3> <p><?php printf(__('<strong>%s</strong> signed-up at %s on %s using the form "%s".', 'mailchimp-for-wp'), $this->data['EMAIL'], date('H:i'), date('d/m/Y'), $form_name); ?> </p> <table cellspacing="0" cellpadding="10" border="0" style="border: 1px solid #EEEEEE;"> <tbody> <?php foreach ($this->lists_fields_map as $list_id => $field_data) { ?> <tr> <td colspan="2"><h4 style="border-bottom: 1px solid #efefef; margin-bottom: 0; padding-bottom: 5px;"><?php echo __('List', 'mailchimp-for-wp') . ': ' . $mailchimp->get_list_name($list_id); ?> </h4></td> </tr> <tr> <td><strong><?php _e('Email address:', 'mailchimp-for-wp'); ?> </strong></td> <td><?php echo $this->data['EMAIL']; ?> </td> </tr> <?php foreach ($field_data as $field_tag => $field_value) { if ($field_tag === 'GROUPINGS' && is_array($field_value) && count($field_value) > 0) { foreach ($field_value as $grouping) { $groups = implode(', ', $grouping['groups']); ?> <tr> <td><strong><?php echo $mailchimp->get_list_grouping_name_by_id($list_id, $grouping['id']); ?> </strong></td> <td><?php echo esc_html($groups); ?> </td> </tr> <?php } } else { $field_name = $mailchimp->get_list_field_name_by_tag($list_id, $field_tag); // convert array values to comma-separated string value if (is_array($field_value)) { $field_value = implode(', ', $field_value); } ?> <tr> <td><strong><?php echo esc_html($field_name); ?> </strong></td> <td><?php echo esc_html($field_value); ?> </td> </tr> <?php } } ?> <?php } ?> <?php if (count($this->unmapped_fields) > 0) { ?> <tr> <td colspan="2"><h4 style="border-bottom: 1px solid #efefef; margin-bottom: 0; padding-bottom: 5px;"><?php _e('Other fields', 'mailchimp-for-wp'); ?> </h4></td> </tr> <?php foreach ($this->unmapped_fields as $field_tag => $field_value) { ?> <tr> <td><strong><?php echo esc_html(ucfirst(strtolower($field_tag))); ?> </strong></td> <td><?php echo esc_html($field_value); ?> </td> </tr> <?php } } ?> </tbody> </table> <?php if ($this->form_options['double_optin']) { ?> <p style="color:#666;"><?php printf(__('Note that you\'ve enabled double opt-in for the "%s" form. The user won\'t be added to the selected MailChimp lists until they confirm their email address.', 'mailchimp-for-wp'), $form_name); ?> </p> <?php } ?> <p style="color:#666;"><?php _e('This email was auto-sent by the MailChimp for WordPress plugin.', 'mailchimp-for-wp'); ?> </p> <?php $message = ob_get_contents(); ob_end_clean(); /** * @filter mc4wp_email_summary_receiver * @expects string|array String or array of emails * @param int $form_id The ID of the submitted form * @param string $email The email of the subscriber * @param array $lists_data Additional list fields, like FNAME etc (if any) * * Use to set email addresses to send the email summary to */ $receivers = apply_filters('mc4wp_email_summary_receiver', $to, $this->form_id, $this->data['EMAIL'], $this->lists_fields_map); /** * @filter mc4wp_email_summary_subject * @expects string|array String or array of emails * @param int $form_id The ID of the submitted form * @param string $email The email of the subscriber * @param array $lists_data Additional list fields, like FNAME etc (if any) * * Use to set subject of email summaries */ $subject = apply_filters('mc4wp_email_summary_subject', $subject, $this->form_id, $this->data['EMAIL'], $this->lists_fields_map); /** * @filter mc4wp_email_summary_message * @expects string|array String or array of emails * @param int $form_id The ID of the submitted form * @param string $email The email of the subscriber * @param array $lists_data Additional list fields, like FNAME etc (if any) * * Use to set or customize message of email summaries */ $message = apply_filters('mc4wp_email_summary_message', $message, $this->form_id, $this->data['EMAIL'], $this->lists_fields_map); // send email wp_mail($receivers, $subject, $message, 'Content-Type: text/html'); }
/** * Show the forms settings page */ public function show_form_settings() { $opts = mc4wp_get_options('form'); $mailchimp = new MC4WP_MailChimp(); $lists = $mailchimp->get_lists(); require MC4WP_LITE_PLUGIN_DIR . 'includes/views/form-settings.php'; }
/** * @param MC4WP_Form $form * @param MC4WP_Request $request */ public function process_unsubscribe_form(MC4WP_Form $form, MC4WP_Request $request = null) { $mailchimp = new MC4WP_MailChimp(); $log = $this->get_log(); $result = null; $data = $form->get_data(); // unsubscribe from each list foreach ($form->get_lists() as $list_id) { // TODO: Check if on list before proceeding with unsubscribe call? $result = $mailchimp->list_unsubscribe($list_id, $data['EMAIL']); } if (!$result) { $form->add_error('error'); $log->error(sprintf('Form %d > MailChimp API error: %s', $form->ID, $mailchimp->get_error_message())); // bail return; } // Success! Unsubscribed. $form->add_message('unsubscribed'); $log->info(sprintf("Form %d > Successfully unsubscribed %s", $form->ID, $data['EMAIL'])); /** * Fires right after a form was used to unsubscribe. * * @since 3.0 * * @param MC4WP_Form $form Instance of the submitted form. */ do_action('mc4wp_form_unsubscribed', $form); }
/** * Makes a subscription request * * @param array $data * @param int $related_object_id * * @return boolean */ protected function subscribe(array $data, $related_object_id = 0) { $integration = $this; $slug = $this->slug; $mailchimp = new MC4WP_MailChimp(); $log = $this->get_log(); $request = $this->get_request(); $list_ids = $this->get_lists(); /** @var MC4WP_MailChimp_Subscriber $subscriber */ $subscriber = null; $result = false; // validate lists if (empty($list_ids)) { $log->warning(sprintf('%s > No MailChimp lists were selected', $this->name)); return false; } /** * Filters data for integration requests. * * @param array $data */ $data = apply_filters('mc4wp_integration_data', $data); /** * Filters data for a specific integration request. * * The dynamic portion of the hook, `$slug`, refers to the integration slug. * * @param array $data * @param int $related_object_id */ $data = apply_filters("mc4wp_integration_{$slug}_data", $data, $related_object_id); /** * @ignore * @deprecated 4.0 */ $data = apply_filters('mc4wp_merge_vars', $data); /** * @deprecated 4.0 * @ignore */ $data = apply_filters('mc4wp_integration_merge_vars', $data, $integration); /** * @deprecated 4.0 * @ignore */ $data = apply_filters("mc4wp_integration_{$slug}_merge_vars", $data, $integration); $email_type = mc4wp_get_email_type(); $mapper = new MC4WP_List_Data_Mapper($data, $list_ids); /** @var MC4WP_MailChimp_Subscriber[] $map */ $map = $mapper->map(); foreach ($map as $list_id => $subscriber) { $subscriber->status = $this->options['double_optin'] ? 'pending' : 'subscribed'; $subscriber->email_type = $email_type; $subscriber->ip_signup = $request->get_client_ip(); /** @ignore (documented elsewhere) */ $subscriber = apply_filters('mc4wp_subscriber_data', $subscriber); /** * Filters subscriber data before it is sent to MailChimp. Only fires for integration requests. * * @param MC4WP_MailChimp_Subscriber $subscriber */ $subscriber = apply_filters('mc4wp_integration_subscriber_data', $subscriber); /** * Filters subscriber data before it is sent to MailChimp. Only fires for integration requests. * * The dynamic portion of the hook, `$slug`, refers to the integration slug. * * @param MC4WP_MailChimp_Subscriber $subscriber * @param int $related_object_id */ $subscriber = apply_filters("mc4wp_integration_{$slug}_subscriber_data", $subscriber, $related_object_id); $result = $mailchimp->list_subscribe($list_id, $subscriber->email_address, $subscriber->to_array(), $this->options['update_existing'], $this->options['replace_interests']); } // if result failed, show error message if (!$result) { // log error if ($mailchimp->get_error_code() == 214) { $log->warning(sprintf("%s > %s is already subscribed to the selected list(s)", $this->name, $subscriber->email_address)); } else { $log->error(sprintf('%s > MailChimp API Error: %s', $this->name, $mailchimp->get_error_message())); } // bail return false; } $log->info(sprintf('%s > Successfully subscribed %s', $this->name, $subscriber->email_address)); /** * Runs right after someone is subscribed using an integration * * @since 3.0 * * @param MC4WP_Integration $integration * @param string $email_address * @param array $merge_vars * @param MC4WP_MailChimp_Subscriber[] $subscriber_data * @param int $related_object_id */ do_action('mc4wp_integration_subscribed', $integration, $subscriber->email_address, $subscriber->merge_fields, $map, $related_object_id); return $result; }