/**
  * Adds global fields like OPTIN_IP, MC_LANGUAGE, OPTIN_DATE, etc to the list of user-submitted field data.
  *
  * @param string $list_id
  * @param array $list_field_data
  * @return array
  */
 protected function get_list_merge_vars($list_id, $list_field_data)
 {
     $merge_vars = array();
     // add OPTIN_IP, we do this here as the user shouldn't be allowed to set this
     $merge_vars['OPTIN_IP'] = MC4WP_Tools::get_client_ip();
     // make sure MC_LANGUAGE matches the requested format. Useful when getting the language from WPML etc.
     if (isset($this->map->global_fields['MC_LANGUAGE'])) {
         $merge_vars['MC_LANGUAGE'] = strtolower(substr($this->map->global_fields['MC_LANGUAGE'], 0, 2));
     }
     $merge_vars = array_merge($merge_vars, $list_field_data);
     /**
      * @filter `mc4wp_merge_vars`
      * @expects array
      * @param int $form_id
      * @param string $list_id
      *
      * Can be used to filter the merge variables sent to a given list
      */
     $merge_vars = (array) apply_filters('mc4wp_merge_vars', $merge_vars, 0, $list_id);
     return $merge_vars;
 }
 /**
  * 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;
 }
 /**
  * Prepare the requests this form creates
  *
  * @return bool
  */
 public function prepare()
 {
     $lists = $this->get_lists();
     $merge_vars = $this->parse_merge_vars();
     $email = $this->data['EMAIL'];
     $config = array('email_type' => $this->get_email_type(), 'ip' => MC4WP_Tools::get_client_ip());
     $extra = array('related_object_id' => $this->form->ID, 'referer' => $_SERVER['HTTP_REFERER'], 'type' => 'form');
     // create a request object for each list
     foreach ($lists as $list_id) {
         $request = MC4WP_API_Request::create($this->config['action'], $list_id, $email, $merge_vars, $config, $extra);
         $this->requests[] = $request;
     }
     return true;
 }
    /**
     * Build email message
     *
     * @return string
     */
    public function build_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->request->user_data['EMAIL'], date(get_option('time_format')), date(get_option('date_format')), $this->form->name);
        ?>
</p>
		<table cellspacing="0" cellpadding="10" border="0" style="border: 1px solid #EEEEEE;">
			<tbody>
			<?php 
        foreach ($this->request->map->list_fields 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') . ': ' . $this->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->request->user_data['EMAIL'];
            ?>
</td>
				</tr>
				<?php 
            foreach ($field_data as $field_tag => $field_value) {
                if ($field_tag === 'GROUPINGS' && is_array($field_value)) {
                    foreach ($field_value as $grouping) {
                        $groups = implode(', ', $grouping['groups']);
                        ?>
							<tr>
								<td><strong><?php 
                        echo $this->mailchimp->get_list_grouping_name($list_id, $grouping['id']);
                        ?>
:</strong></td>
								<td><?php 
                        echo esc_html($groups);
                        ?>
</td>
							</tr>
						<?php 
                    }
                } else {
                    $field_name = $this->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->request->map->custom_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->request->map->custom_fields as $field_tag => $field_value) {
                // 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_tag);
                ?>
:</strong></td>
						<td><?php 
                echo esc_html($field_value);
                ?>
</td>
					</tr>
				<?php 
            }
            ?>
			<?php 
        }
        ?>

			</tbody>
		</table>
		<br />

		<p><?php 
        printf(__('User subscribed from %s from IP %s.', 'mailchimp-for-wp'), esc_html($this->request->http_referer), MC4WP_Tools::get_client_ip());
        ?>
</p>

		<?php 
        if ($this->form->settings['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'), $this->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 
        return ob_get_clean();
    }
    /**
     * Send an email with a subscription summary to a given email address
     */
    protected function send_email()
    {
        // bail if receiver is empty
        if ('' === $this->form->settings['email_copy_receiver']) {
            return;
        }
        // email receiver
        $to = explode(',', str_replace(' ', '', $this->form->settings['email_copy_receiver']));
        // email subject
        $subject = __('New MailChimp Sign-Up', 'mailchimp-for-wp') . ' - ' . get_bloginfo('name');
        $mailchimp = new MC4WP_MailChimp();
        $referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $_SERVER['REQUEST_URI'];
        // 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->user_data['EMAIL'], date('H:i'), date('d/m/Y'), $this->form->name);
        ?>
</p>
		<table cellspacing="0" cellpadding="10" border="0" style="border: 1px solid #EEEEEE;">
			<tbody>
			<?php 
        foreach ($this->list_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->user_data['EMAIL'];
            ?>
</td>
				</tr>
				<?php 
            foreach ($field_data as $field_tag => $field_value) {
                if ($field_tag === 'GROUPINGS' && is_array($field_value)) {
                    foreach ($field_value as $grouping) {
                        $groups = implode(', ', $grouping['groups']);
                        ?>
							<tr>
								<td><strong><?php 
                        echo $mailchimp->get_list_grouping_name($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) {
                // 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_tag);
                ?>
</strong></td>
						<td><?php 
                echo esc_html($field_value);
                ?>
</td>
					</tr>
					<?php 
            }
            ?>
			<?php 
        }
        ?>

			</tbody>
		</table>

		<p><?php 
        printf(__('User subscribed from %s from IP %s.', 'mailchimp-for-wp'), esc_html($referer), MC4WP_Tools::get_client_ip());
        ?>
</p>

		<?php 
        if ($this->form->settings['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'), $this->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->user_data['EMAIL'], $this->list_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->user_data['EMAIL'], $this->list_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->user_data['EMAIL'], $this->list_fields_map);
        // send email
        wp_mail($receivers, $subject, $message, 'Content-Type: text/html');
    }