/**
  * Prepare the fields that are displayed in the form.
  *
  * @since  1.0.1.0
  * @return array
  */
 protected function get_fields()
 {
     $comm = $this->data['comm'];
     $membership = false;
     $membership_id = 0;
     if (isset($this->data['membership'])) {
         $membership = $this->data['membership'];
         if ($membership instanceof MS_Model_Membership) {
             $membership_id = $membership->id;
         } else {
             $membership = false;
         }
     }
     lib2()->array->equip($comm, 'type', 'enabled', 'period', 'subject', 'description', 'cc_enabled', 'cc_email');
     $action = MS_Controller_Communication::AJAX_ACTION_UPDATE_COMM;
     $nonce = wp_create_nonce($action);
     $comm_titles = MS_Model_Communication::get_communication_type_titles($membership);
     $key_active = __('Send Email', MS_TEXT_DOMAIN);
     $key_inactive = __('No Email', MS_TEXT_DOMAIN);
     $key_skip = __('Use default template', MS_TEXT_DOMAIN);
     $titles = array($key_active => array(), $key_inactive => array(), $key_skip => array());
     foreach ($comm_titles as $type => $title) {
         $tmp_comm = MS_Model_Communication::get_communication($type, $membership, true);
         if ($membership && !$tmp_comm->override) {
             $titles[$key_skip][$type] = $title;
         } elseif ($tmp_comm->enabled) {
             $titles[$key_active][$type] = $title;
         } else {
             $titles[$key_inactive][$type] = $title;
         }
     }
     $fields = array('comm_type' => array('id' => 'comm_type', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $comm->type, 'field_options' => $titles), 'switch_comm_type' => array('id' => 'switch_comm_type', 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON, 'value' => __('Load Template', MS_TEXT_DOMAIN)), 'override' => array('id' => 'override', 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER, 'value' => $comm->override, 'before' => __('Use default template', MS_TEXT_DOMAIN), 'after' => __('Define custom template', MS_TEXT_DOMAIN), 'wrapper_class' => 'ms-block ms-tcenter', 'class' => 'override-slider', 'ajax_data' => array('type' => $comm->type, 'field' => 'override', 'action' => $action, '_wpnonce' => $nonce, 'membership_id' => $membership_id)), 'membership_id' => array('id' => 'membership_id', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership_id), 'type' => array('id' => 'type', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $comm->type), 'enabled' => array('id' => 'enabled', 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER, 'value' => $comm->enabled, 'class' => 'state-slider', 'before' => '&nbsp;<i class="wpmui-fa wpmui-fa-ban"></i>', 'after' => '<i class="wpmui-fa wpmui-fa-envelope"></i>&nbsp;', 'ajax_data' => array('type' => $comm->type, 'field' => 'enabled', 'action' => $action, '_wpnonce' => $nonce, 'membership_id' => $membership_id)), 'period_unit' => array('id' => 'period_unit', 'type' => MS_Helper_Html::INPUT_TYPE_TEXT, 'title' => __('Period after/before', MS_TEXT_DOMAIN), 'value' => $comm->period['period_unit']), 'period_type' => array('id' => 'period_type', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $comm->period['period_type'], 'field_options' => MS_Helper_Period::get_period_types('plural')), 'subject' => array('id' => 'subject', 'type' => MS_Helper_Html::INPUT_TYPE_TEXT, 'title' => apply_filters('ms_translation_flag', __('Message Subject', MS_TEXT_DOMAIN), 'communication-subject'), 'value' => $comm->subject, 'class' => 'ms-comm-subject widefat'), 'email_body' => array('id' => 'email_body', 'type' => MS_Helper_Html::INPUT_TYPE_WP_EDITOR, 'title' => apply_filters('ms_translation_flag', '', 'communication-body'), 'value' => $comm->description, 'field_options' => array('media_buttons' => false, 'editor_class' => 'wpmui-ajax-update')), 'cc_enabled' => array('id' => 'cc_enabled', 'type' => MS_Helper_Html::INPUT_TYPE_CHECKBOX, 'title' => __('Send copy to Administrator', MS_TEXT_DOMAIN), 'value' => $comm->cc_enabled, 'class' => 'ms-inline-block'), 'cc_email' => array('id' => 'cc_email', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $comm->cc_email, 'field_options' => MS_Model_Member::get_admin_user_emails()), 'save_email' => array('id' => 'save_email', 'value' => __('Save Changes', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT), 'action' => array('id' => 'action', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'save_comm'), 'nonce' => array('id' => '_wpnonce', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce('save_comm')), 'load_action' => array('id' => 'load_action', 'name' => 'action', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'load_action'), 'load_nonce' => array('id' => '_wpnonce1', 'name' => '_wpnonce', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce('load_action')));
     if (!$membership instanceof MS_Model_Membership) {
         unset($fields['override']);
         unset($fields['membership_id']);
     }
     return apply_filters('ms_view_settings_prepare_email_fields', $fields);
 }
 /**
  * Send email message.
  *
  * Delete history of sent messages after max is reached.
  *
  * @since  1.0.0
  *
  * @param mixed $reference A reference to identify the member/subscription.
  * @return bool True if successfully sent email.
  */
 public function send_message($reference)
 {
     $user_id = 0;
     $subscription = null;
     $member = null;
     if ($reference instanceof MS_Model_Relationship) {
         $user_id = $reference->user_id;
         $subscription = $reference;
         $member = $subscription->get_member();
     } elseif ($reference instanceof MS_Model_Member) {
         $user_id = $reference->id;
         $member = $reference;
         $subscription = null;
     }
     /**
      * Documented in process_queue()
      *
      * @since  1.0.1.0
      */
     if (MS_Plugin::get_modifier('MS_STOP_EMAILS')) {
         do_action('lib2_debug_log', sprintf('Following Email was not sent: "%s" to user "%s".', $this->type, $user_id));
         return false;
     }
     do_action('ms_model_communication_send_message_before', $member, $subscription, $this);
     $sent = false;
     if ($this->enabled) {
         if (!is_email($member->email)) {
             do_action('lib2_debug_log', sprintf('Invalid user email. User_id: %1$s, email: %2$s', $user_id, $member->email));
             return false;
         }
         $comm_vars = $this->get_comm_vars($subscription, $member);
         // Replace the email variables.
         $message = str_replace(array_keys($comm_vars), array_values($comm_vars), stripslashes($this->message));
         $subject = str_replace(array_keys($comm_vars), array_values($comm_vars), stripslashes($this->subject));
         $html_message = wpautop($message);
         $text_message = strip_tags(preg_replace('/\\<a .*?href="(.*?)".*?\\>.*?\\<\\/a\\>/is', '$0 [$1]', $message));
         $subject = strip_tags(preg_replace('/\\<a .*?href="(.*?)".*?\\>.*?\\<\\/a\\>/is', '$0 [$1]', $subject));
         $message = $text_message;
         if ('text/html' == $this->get_mail_content_type()) {
             $this->add_filter('wp_mail_content_type', 'get_mail_content_type');
             $message = $html_message;
         }
         $recipients = array($member->email);
         if ($this->cc_enabled) {
             $recipients[] = $this->cc_email;
         }
         $admin_emails = MS_Model_Member::get_admin_user_emails();
         $headers = '';
         if (!empty($admin_emails[0])) {
             $headers = array(sprintf('From: %s <%s> ', get_option('blogname'), $admin_emails[0]));
         }
         $recipients = apply_filters('ms_model_communication_send_message_recipients', $recipients, $this, $subscription);
         $html_message = apply_filters('ms_model_communication_send_message_html_message', $html_message, $this, $subscription);
         $text_message = apply_filters('ms_model_communication_send_message_text_message', $text_message, $this, $subscription);
         $subject = apply_filters('ms_model_communication_send_message_subject', $subject, $this, $subscription);
         $headers = apply_filters('ms_model_communication_send_message_headers', $headers, $this, $subscription);
         /*
          * Send the mail.
          * wp_mail will not throw an error, so no error-suppression/handling
          * is required here. On error the function response is FALSE.
          */
         $sent = wp_mail($recipients, $subject, $message, $headers);
         // Log the outgoing email.
         do_action('lib2_debug_log', sprintf('Sent email [%s] to <%s>: %s', $this->type, implode('>, <', $recipients), $sent ? 'OK' : 'ERR'));
         /*
         			// -- Debugging code --
         			MS_Helper_Debug::log(
         				sprintf(
         					"Variables:\n%s",
         					print_r( $comm_vars, true )
         				)
         			);
         			//*/
         if ('text/html' == $this->get_mail_content_type()) {
             $this->remove_filter('wp_mail_content_type', 'get_mail_content_type');
         }
     }
     do_action('ms_model_communication_send_message', $member, $subscription, $this);
     return $sent;
 }