/**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = sprintf(__('Confirmation of your membership at %s', 'membership2'), self::COMM_VAR_BLOG_NAME);
     $this->message = self::get_default_message();
     $this->enabled = false;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = sprintf(__('Subscription renewed for %s', MS_TEXT_DOMAIN), self::COMM_VAR_BLOG_NAME);
     $this->message = self::get_default_message();
     $this->enabled = false;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = __('Your membership payment has failed', 'membership2');
     $this->message = self::get_default_message();
     $this->enabled = false;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = sprintf(__('Your %s membership receipt', MS_TEXT_DOMAIN), self::COMM_VAR_MS_NAME);
     $this->message = self::get_default_message();
     $this->enabled = false;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = __('Your billing details have been changed.', MS_TEXT_DOMAIN);
     $this->message = self::get_default_message();
     $this->enabled = false;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = sprintf(__('Reminder: your %s membership has ended', 'membership2'), self::COMM_VAR_MS_NAME);
     $this->message = self::get_default_message();
     $this->enabled = false;
     $this->period_enabled = true;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = __('Your credit card is about to expire', MS_TEXT_DOMAIN);
     $this->message = self::get_default_message();
     $this->enabled = false;
     $this->period_enabled = true;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * wpmu.dev Automessage plugin integration.
  *
  * @since  1.0.0
  *
  * @access public
  * @param array $hooks The existing hooks.
  * @return array The modified array of hooks.
  */
 public function automessage_custom_user_hooks($hooks)
 {
     $comm_types = MS_Model_Communication::get_communication_type_titles();
     foreach ($comm_types as $type => $desc) {
         $action = "ms_communications_process_{$type}";
         $hooks[$action] = array('action_nicename' => $desc);
     }
     return $hooks;
 }
 /**
  * Communication default communication.
  *
  * @since  1.0.0
  */
 public function reset_to_default()
 {
     parent::reset_to_default();
     $this->subject = __('Membership payment due soon', 'membership2');
     $this->message = self::get_default_message();
     $this->enabled = false;
     $this->period_enabled = true;
     do_action('ms_model_communication_reset_to_default_after', $this->type, $this);
 }
 /**
  * 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);
 }
 /**
  * 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();
 }
 /**
  * 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();
 }
Exemplo n.º 13
0
 /**
  * Returns a list of communication IDs for the specified membership.
  *
  * Possible values:
  *  null .. All communication IDs are returned.
  *  0 .. Global communication IDs are returned (defined in Settings page).
  *  <MembershipID> .. Communication IDs of that membership are returned.
  *
  * @since  1.0.1.0
  * @param  int $membership Indtifies a membership.
  * @return array List of communication IDs.
  */
 protected static function get_communication_ids($membership)
 {
     if (!isset(self::$Communication_IDs[0])) {
         self::$Communication_IDs = array(0 => array());
         $args = array('post_type' => self::get_post_type(), 'post_status' => 'any', 'fields' => 'ids', 'posts_per_page' => -1);
         MS_Factory::select_blog();
         $query = new WP_Query($args);
         $items = $query->posts;
         MS_Factory::revert_blog();
         foreach ($items as $id) {
             $comm = MS_Factory::load('MS_Model_Communication', $id);
             self::$Communication_IDs[$comm->membership_id][$comm->type] = $id;
         }
     }
     if ($membership instanceof MS_Model_Membership) {
         $key = $membership->id;
     } else {
         $key = $membership;
     }
     if (null === $key) {
         $result = self::$Communication_IDs;
     } elseif (isset(self::$Communication_IDs[$key])) {
         $result = self::$Communication_IDs[$key];
     } else {
         $result = array();
     }
     return $result;
 }
 /**
  * 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);
 }
 /**
  * Get post types that are part of this plugin.
  *
  * @since  1.0.0
  *
  * @return array The plugin core post types.
  */
 public static function get_ms_post_types()
 {
     $cpts = array(MS_Model_Membership::get_post_type(), MS_Model_Invoice::get_post_type(), MS_Model_Communication::get_post_type(), MS_Model_Relationship::get_post_type(), MS_Model_Event::get_post_type());
     return apply_filters('ms_rule_cptgroup_model_get_ms_post_types', $cpts);
 }
 /**
  * Completely whipe all Membership data from Database.
  *
  * Note: This function is not used currently...
  *
  * @since  1.0.0
  */
 private static function cleanup_db()
 {
     global $wpdb;
     $sql = array();
     $trash_ids = array();
     // Delete membership meta-data from users.
     $users = MS_Model_Member::get_members();
     foreach ($users as $user) {
         $user->delete_all_membership_usermeta();
         $user->save();
     }
     // Determine IDs of Membership Pages.
     $page_types = MS_Model_Pages::get_page_types();
     foreach ($page_types as $type => $name) {
         $page_id = MS_Model_Pages::get_setting($type);
         $trash_ids[] = $page_id;
     }
     /**
      * Delete all plugin settings.
      * Settings are saved by classes that extend MS_Model_option
      */
     foreach (MS_Model_Gateway::get_gateways() as $option) {
         $option->delete();
     }
     MS_Factory::load('MS_Model_Addon')->delete();
     MS_Factory::load('MS_Model_Pages')->delete();
     MS_Factory::load('MS_Model_Settings')->delete();
     /**
      * Delete transient data
      * Transient data is saved by classed that extend MS_Model_Transient
      */
     MS_Factory::load('MS_Model_Simulate')->delete();
     /**
      * Delete all plugin content.
      * Content is saved by classes that extend MS_Model_CustomPostType
      */
     $ms_posttypes = array(MS_Model_Communication::get_post_type(), MS_Model_Event::get_post_type(), MS_Model_Invoice::get_post_type(), MS_Model_Transactionlog::get_post_type(), MS_Model_Membership::get_post_type(), MS_Model_Relationship::get_post_type(), MS_Addon_Coupon_Model::get_post_type(), MS_Addon_Invitation_Model::get_post_type());
     foreach ($ms_posttypes as $type) {
         $sql[] = $wpdb->prepare("DELETE FROM {$wpdb->posts} WHERE post_type = %s;", $type);
     }
     // Remove orphaned post-metadata.
     $sql[] = "\n\t\tDELETE FROM {$wpdb->postmeta}\n\t\tWHERE NOT EXISTS (\n\t\t\tSELECT 1 FROM {$wpdb->posts} tmp WHERE tmp.ID = post_id\n\t\t);\n\t\t";
     // Clear all WP transient cache.
     $sql[] = "\n\t\tDELETE FROM {$wpdb->options}\n\t\tWHERE option_name LIKE '_transient_%';\n\t\t";
     foreach ($sql as $s) {
         $wpdb->query($s);
     }
     // Move Membership pages to trash.
     foreach ($trash_ids as $id) {
         wp_delete_post($id, true);
     }
     // Clear all data from WP Object cache.
     wp_cache_flush();
     // Redirect to the main page.
     wp_safe_redirect(MS_Controller_Plugin::get_admin_url());
     exit;
 }
 /**
  * Check membership status.
  *
  * Execute actions when time/period condition are met.
  * E.g. change membership status, add communication to queue, create invoices.
  *
  * This check is called via a cron job.
  *
  * @since  1.0.0
  * @internal  Used by Cron
  * @see MS_Model_Plugin::check_membership_status()
  */
 public function check_membership_status()
 {
     do_action('ms_model_relationship_check_membership_status_before', $this);
     /**
      * Use `define( 'MS_LOCK_SUBSCRIPTIONS', true );` in wp-config.php to prevent
      * Membership2 from sending *any* emails to users.
      * Also any currently enqueued message is removed from the queue
      *
      * @since  1.0.0
      */
     if (MS_Plugin::get_modifier('MS_LOCK_SUBSCRIPTIONS')) {
         return false;
     }
     $membership = $this->get_membership();
     $remaining_days = $this->get_remaining_period();
     $remaining_trial_days = $this->get_remaining_trial_period();
     $comms = MS_Model_Communication::get_communications($membership);
     $invoice_before_days = 5;
     //@todo create a setting to configure this period.
     $deactivate_expired_after_days = 30;
     //@todo create a setting to configure this period.
     $deactivate_pending_after_days = 30;
     //@todo create a setting to configure this period.
     $deactivate_trial_expired_after_days = 5;
     //@todo create a setting to configure this period.
     //@todo: Add a flag to subscriptions with sent communications. Then improve the conditions below to prevent multiple emails.
     do_action('ms_check_membership_status-' . $this->status, $this, $remaining_days, $remaining_trial_days);
     // Update the Subscription status.
     $next_status = $this->calculate_status(null);
     switch ($next_status) {
         case self::STATUS_TRIAL:
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL) && MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_AUTO_MSGS_PLUS)) {
                 // Send trial end communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_TRIAL_FINISHES];
                 if ($comm->enabled) {
                     $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                     //@todo: This will send out the reminder multiple times on the reminder-day (4 times or more often)
                     if ($days == $remaining_trial_days) {
                         $comm->add_to_queue($this->id);
                         MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_BEFORE_TRIAL_FINISHES, $this);
                     }
                 }
             }
             // Check for card expiration
             $gateway = $this->get_gateway();
             $gateway->check_card_expiration($this);
             break;
         case self::STATUS_TRIAL_EXPIRED:
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL)) {
                 // Mark the trial period as completed. $this->save() is below.
                 $this->trial_period_completed = true;
                 // Request payment to the gateway (for gateways that allows it).
                 $gateway = $this->get_gateway();
                 /*
                  * The subscription will be either automatically activated
                  * or set to pending.
                  *
                  * Important: Set trial_period_completed=true before calling
                  * request_payment()!
                  */
                 if ($gateway->request_payment($this)) {
                     $next_status = self::STATUS_ACTIVE;
                     $this->status = $next_status;
                     $this->config_period();
                     // Needed because of status change.
                 }
                 // Check for card expiration
                 $gateway->check_card_expiration($this);
                 // Deactivate expired memberships after a period of time.
                 if ($deactivate_trial_expired_after_days < -$remaining_trial_days) {
                     $this->deactivate_membership();
                 }
             }
             break;
         case self::STATUS_ACTIVE:
         case self::STATUS_EXPIRED:
         case self::STATUS_CANCELED:
             /*
              * Make sure the expire date has a correct value, in case the user
              * changed the payment_type of the parent membership after this
              * subscription was created.
              */
             if ($this->payment_type != $membership->payment_type) {
                 $this->payment_type = $membership->payment_type;
                 switch ($this->payment_type) {
                     case MS_Model_Membership::PAYMENT_TYPE_PERMANENT:
                         $this->expire_date = false;
                         break;
                     default:
                         // Either keep the current expire date (if valid) or
                         // calculate a new expire date, based on current date.
                         if (!$this->expire_date) {
                             $this->expire_date = $this->calc_expire_date(MS_Helper_Period::current_date());
                         }
                         break;
                 }
                 // Recalculate the days until the subscription expires.
                 $remaining_days = $this->get_remaining_period();
                 // Recalculate the new Subscription status.
                 $next_status = $this->calculate_status();
             }
             /*
              * Only "Recurring" memberships will ever try to automatically
              * renew the subscription. All other types will expire when the
              * end date is reached.
              */
             $auto_renew = $membership->payment_type == MS_Model_Membership::PAYMENT_TYPE_RECURRING;
             $deactivate = false;
             $invoice = null;
             if ($auto_renew && $membership->pay_cycle_repetitions > 0) {
                 /*
                  * The membership has a payment-repetition limit.
                  * When this limit is reached then we do not auto-renew the
                  * subscription but expire it.
                  */
                 $payments = $this->get_payments();
                 if (count($payments) >= $membership->pay_cycle_repetitions) {
                     $auto_renew = false;
                 }
             }
             if ($auto_renew) {
                 if ($remaining_days < $invoice_before_days) {
                     // Create a new invoice.
                     $invoice = $this->get_next_invoice();
                 } else {
                     $invoice = $this->get_current_invoice();
                 }
             } else {
                 $invoice = $this->get_current_invoice();
             }
             // Advanced communications Add-on.
             if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_AUTO_MSGS_PLUS)) {
                 // Before finishes communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_FINISHES];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 if ($days == $remaining_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_BEFORE_FINISHES, $this);
                 }
                 // After finishes communication.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_AFTER_FINISHES];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 if ($remaining_days < 0 && $days == abs($remaining_days)) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_AFTER_FINISHES, $this);
                 }
                 // Before payment due.
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_BEFORE_PAYMENT_DUE];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 $invoice_days = MS_Helper_Period::subtract_dates($invoice->due_date, MS_Helper_Period::current_date());
                 if (MS_Model_Invoice::STATUS_BILLED == $invoice->status && $days == $invoice_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_BEFORE_DUE, $this);
                 }
                 // After payment due event
                 $comm = $comms[MS_Model_Communication::COMM_TYPE_AFTER_PAYMENT_DUE];
                 $days = MS_Helper_Period::get_period_in_days($comm->period['period_unit'], $comm->period['period_type']);
                 $invoice_days = MS_Helper_Period::subtract_dates($invoice->due_date, MS_Helper_Period::current_date());
                 if (MS_Model_Invoice::STATUS_BILLED == $invoice->status && $days == $invoice_days) {
                     $comm->add_to_queue($this->id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_PAYMENT_AFTER_DUE, $this);
                 }
             }
             // -- End of advanced communications Add-on
             // Subscription ended. See if we can renew it.
             if ($remaining_days <= 0) {
                 if ($auto_renew) {
                     /*
                      * The membership can be renewed. Try to renew it
                      * automatically by requesting the next payment from the
                      * payment gateway (only works if gateway supports this)
                      */
                     $gateway = $this->get_gateway();
                     $gateway->check_card_expiration($this);
                     $gateway->request_payment($this);
                     // Check if the payment was successful.
                     $remaining_days = $this->get_remaining_period();
                 }
                 /*
                  * User did not renew the membership. Give him some time to
                  * react before restricting his access.
                  */
                 if ($deactivate_expired_after_days < -$remaining_days) {
                     $deactivate = true;
                 }
             }
             $next_status = $this->calculate_status(null);
             /*
              * When the subscription expires the first time then create a
              * new event that triggers the "Expired" email.
              */
             if (self::STATUS_EXPIRED == $next_status && $next_status != $this->status) {
                 MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_EXPIRED, $this);
             } elseif ($deactivate) {
                 $this->deactivate_membership();
                 $next_status = $this->status;
                 // Move membership to configured membership.
                 $membership = $this->get_membership();
                 $new_membership = MS_Factory::load('MS_Model_Membership', $membership->on_end_membership_id);
                 if ($new_membership->is_valid()) {
                     $member = MS_Factory::load('MS_Model_Member', $this->user_id);
                     $new_subscription = $member->add_membership($membership->on_end_membership_id, $this->gateway_id);
                     MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_MOVED, $new_subscription);
                     /*
                      * If the new membership is paid we want that the user
                      * confirms the payment in his account. So we set it
                      * to "Pending" first.
                      */
                     if (!$new_membership->is_free()) {
                         $new_subscription->status = self::STATUS_PENDING;
                     }
                 }
             }
             break;
         case self::STATUS_DEACTIVATED:
             /*
              * A subscription was finally deactivated.
              * Lets check if the member has any other active subscriptions,
              * or (if not) his account should be deactivated.
              *
              * First get a list of all subscriptions that do not have status
              * Pending / Deactivated.
              */
             $subscriptions = self::get_subscriptions(array('user_id' => $this->user_id));
             // Check if there is a subscription that keeps the user active.
             $deactivate = true;
             foreach ($subscriptions as $item) {
                 if ($item->id == $this->id) {
                     continue;
                 }
                 $deactivate = false;
             }
             if ($deactivate) {
                 $member = $this->get_member();
                 $member->is_member = false;
                 $member->save();
             }
             break;
         case self::STATUS_PENDING:
         default:
             // Do nothing.
             break;
     }
     // Save the new status.
     $this->status = $next_status;
     $this->save();
     // Save the changed email queue.
     foreach ($comms as $comm) {
         $comm->save();
     }
     do_action('ms_model_relationship_check_membership_status_after', $this);
 }
 /**
  * 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);
 }
 /**
  * Translate a single communication before it is saved to DB.
  *
  * @since  1.0.1.0
  *
  * @param  array $obj The serialized data collection.
  * @param  string $class The class name of the object.
  * @param  MS_Model_Communication $model The source object.
  * @return array The serialized data collection.
  */
 public function serialize_communication($data, $class, $model)
 {
     if ($this->current_lang == $this->default_lang) {
         return $data;
     }
     if (0 !== strpos($class, 'MS_Model_Communication')) {
         return $data;
     }
     // Save the translated values before resetting the model.
     $tr_subject = $data['subject'];
     $tr_message = $data['description'];
     // Reset the values to Default language.
     $data['subject'] = $model->reset_field('subject');
     $data['description'] = $model->reset_field('description');
     $data['message'] = $data['description'];
     // Store translations via WPML.
     if (function_exists('icl_add_string_translation')) {
         // 1. Subject.
         $string_name = 'ms-com-subject-' . $model->id;
         do_action('wpml_register_single_string', self::CONTEXT, $string_name, $data['subject']);
         $string_id = icl_get_string_id($data['subject'], self::CONTEXT, $string_name);
         if ($string_id) {
             icl_add_string_translation($string_id, $this->current_lang, $tr_subject, ICL_TM_COMPLETE);
         }
         // 1. Message Body.
         $string_name = 'ms-com-message-' . $model->id;
         do_action('wpml_register_single_string', self::CONTEXT, $string_name, $data['message']);
         $string_id = icl_get_string_id($data['message'], self::CONTEXT, $string_name);
         if ($string_id) {
             icl_add_string_translation($string_id, $this->current_lang, $tr_message, ICL_TM_COMPLETE);
         }
     }
     return $data;
 }
Exemplo n.º 20
0
 /**
  * Register plugin custom post types.
  *
  * @since  1.0.0
  */
 public function register_custom_post_types()
 {
     do_action('ms_plugin_register_custom_post_types_before', $this);
     $cpts = apply_filters('ms_plugin_register_custom_post_types', array(MS_Model_Membership::get_post_type() => MS_Model_Membership::get_register_post_type_args(), MS_Model_Relationship::get_post_type() => MS_Model_Relationship::get_register_post_type_args(), MS_Model_Invoice::get_post_type() => MS_Model_Invoice::get_register_post_type_args(), MS_Model_Communication::get_post_type() => MS_Model_Communication::get_register_post_type_args(), MS_Model_Event::get_post_type() => MS_Model_Event::get_register_post_type_args()));
     foreach ($cpts as $cpt => $args) {
         MS_Helper_Utility::register_post_type($cpt, $args);
     }
 }
 /**
  * Display a footer below the Settings box.
  * The footer will show information on the next scheduled cron jobs and also
  * allow the user to run these jobs instantly.
  *
  * @since  1.0.0
  * @param  string $tab_name Name of the currently open settings-tab.
  */
 protected function render_settings_footer($tab_name)
 {
     if ('general' != $tab_name) {
         return;
     }
     $status_stamp = wp_next_scheduled('ms_cron_check_membership_status') - time();
     $email_stamp = wp_next_scheduled('ms_cron_process_communications') - time();
     if ($status_stamp > 0) {
         $status_delay = sprintf(__('in %s hrs %s min', 'membership2'), floor(($status_stamp - 1) / 3600), date('i', $status_stamp));
     } else {
         $status_delay = __('(now...)', 'membership2');
     }
     if ($email_stamp > 0) {
         $email_delay = sprintf(__('in %s hrs %s min', 'membership2'), floor(($email_stamp - 1) / 3600), date('i', $email_stamp));
     } else {
         $email_delay = __('(now...)', 'membership2');
     }
     $status_url = esc_url_raw(add_query_arg(array('run_cron' => 'ms_cron_check_membership_status')));
     $email_url = esc_url_raw(add_query_arg(array('run_cron' => 'ms_cron_process_communications')));
     $lbl_run = __('Run now!', 'membership2');
     echo '<div class="cf ms-settings-footer"><div class="ms-tab-container">&nbsp;</div>';
     echo '<div>';
     if (MS_Plugin::get_modifier('MS_LOCK_SUBSCRIPTIONS')) {
         _e('Membership Status Checks are disabled.', 'membership2');
         echo ' ';
     } else {
         printf(__('Check Membership Status changes %s.') . ' ', '<a href="' . $status_url . '" title="' . $lbl_run . '">' . $status_delay . '</a>');
     }
     if (MS_Plugin::get_modifier('MS_STOP_EMAILS')) {
         _e('Sending Email Responses is disabled.', 'membership2');
     } else {
         $count = MS_Model_Communication::get_queue_count();
         if (!$count) {
             $msg = __('No pending Email Responses found', 'membership2');
         } elseif (1 == $count) {
             $msg = __('Send 1 pending Email Response %1$s', 'membership2');
         } else {
             $msg = __('Send %2$s pending Email Responses %1$s', 'membership2');
         }
         printf($msg, '<a href="' . $email_url . '"title="' . $lbl_run . '">' . $email_delay . '</a>', $count);
     }
     echo '</div></div>';
 }