/**
  * 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);
 }
 /**
  * Handle saving of Communication settings.
  *
  * @since  1.0.0
  *
  * @param mixed[] $fields The data to process.
  */
 public function save_communication($type, $fields)
 {
     $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED;
     if (!$this->is_admin_user()) {
         return $msg;
     }
     $membership_id = null;
     if (isset($_POST['membership_id'])) {
         $membership_id = intval($_POST['membership_id']);
     }
     $comm = MS_Model_Communication::get_communication($type, $membership_id, true);
     if (!empty($fields)) {
         lib3()->array->equip($fields, 'enabled', 'subject', 'email_body', 'period_unit', 'period_type', 'cc_enabled', 'cc_email');
         $comm->enabled = lib3()->is_true($fields['enabled']);
         $comm->subject = $fields['subject'];
         $comm->message = $fields['email_body'];
         $comm->period = array('period_unit' => $fields['period_unit'], 'period_type' => $fields['period_type']);
         $comm->cc_enabled = !empty($fields['cc_enabled']);
         $comm->cc_email = $fields['cc_email'];
         $comm->save();
         $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED;
     }
     return apply_filters('ms_controller_communication_save', $msg, $type, $fields, $this);
 }
 /**
  * Display Membership Edit page.
  *
  * @since  1.0.0
  */
 public function page_edit()
 {
     $membership = $this->load_membership();
     $data = array();
     $data['tabs'] = $this->get_edit_tabs();
     $data['settings'] = MS_Plugin::instance()->settings;
     $data['membership'] = $membership;
     switch ($this->get_active_edit_tab()) {
         case self::TAB_EMAILS:
             $default_type = MS_Model_Communication::COMM_TYPE_REGISTRATION;
             if (!empty($_REQUEST['membership_id'])) {
                 $membership_id = intval($_REQUEST['membership_id']);
                 $comm_types = array_keys(MS_Model_Communication::get_communication_type_titles($membership_id));
                 $default_type = reset($comm_types);
             }
             $temp_type = isset($_GET['comm_type']) ? $_GET['comm_type'] : '';
             if (MS_Model_Communication::is_valid_communication_type($temp_type)) {
                 $type = $temp_type;
             } else {
                 $type = $default_type;
             }
             $comm = MS_Model_Communication::get_communication($type, $membership, true);
             $data['comm'] = $comm;
             break;
     }
     $view = MS_Factory::create('MS_View_Membership_Edit');
     $view->data = apply_filters('ms_view_membership_edit_data', $data, $this);
     $view->render();
 }
 /**
  * Check for card expiration date.
  *
  * Save event for card expire soon.
  *
  * @since  1.0.0
  *
  * @access protected
  * @param MS_Model_Relationship $subscription The membership relationship.
  */
 public function check_card_expiration($subscription)
 {
     do_action('ms_gateway_check_card_expiration_before', $this);
     $member = MS_Factory::load('MS_Model_Member', $subscription->user_id);
     $card_exp = $member->get_gateway_profile($this->id, 'card_exp');
     if (!empty($card_exp)) {
         $comm = MS_Model_Communication::get_communication(MS_Model_Communication::COMM_TYPE_CREDIT_CARD_EXPIRE);
         $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
         $card_expire_days = MS_Helper_Period::subtract_dates($card_exp, MS_Helper_Period::current_date(), DAY_IN_SECONDS, true);
         if ($card_expire_days < 0 || $days == $card_expire_days) {
             MS_Model_Event::save_event(MS_Model_Event::TYPE_CREDIT_CARD_EXPIRE, $subscription);
         }
     }
     do_action('ms_gateway_check_card_expiration_after', $this, $subscription);
 }
 /**
  * Callback function from 'Membership' navigation.
  *
  * Menu Item: Membership > Settings
  *
  * @since  1.0.0
  */
 public function admin_page()
 {
     $hook = 'ms_controller_settings-' . $this->active_tab;
     do_action($hook);
     $view = MS_Factory::create('MS_View_Settings_Edit');
     $view = apply_filters($hook . '_view', $view);
     $data = array();
     $data['tabs'] = $this->get_tabs();
     $data['settings'] = $this->get_model();
     $data['message'] = self::_message();
     if (isset($data['message']['error'])) {
         lib3()->ui->admin_message($data['message']['error'], 'err');
     }
     switch ($this->get_active_tab()) {
         case self::TAB_EMAILS:
             $type = MS_Model_Communication::COMM_TYPE_REGISTRATION;
             $temp_type = isset($_GET['comm_type']) ? $_GET['comm_type'] : '';
             if (MS_Model_Communication::is_valid_communication_type($temp_type)) {
                 $type = $temp_type;
             }
             $comm = MS_Model_Communication::get_communication($type);
             $data['comm'] = $comm;
             break;
     }
     $data = array_merge($data, $view->data);
     $view->data = apply_filters($hook . '_data', $data);
     $view->model = $this->get_model();
     $view->render();
 }