/**
  * Get Member's Stripe Customer Object.
  *
  * @since  1.0.0
  * @internal
  *
  * @param MS_Model_Member $member The member.
  */
 public function find_customer($member)
 {
     $customer_id = $member->get_gateway_profile(self::ID, 'customer_id');
     $customer = null;
     if (!empty($customer_id)) {
         $customer = M2_Stripe_Customer::retrieve($customer_id);
         // Seems like the customer was manually deleted on Stripe website.
         if (isset($customer->deleted) && $customer->deleted) {
             $customer = null;
             $member->set_gateway_profile(self::ID, 'customer_id', '');
         }
     }
     return apply_filters('ms_gateway_stripe_find_customer', $customer, $member, $this);
 }
    public function to_html()
    {
        $this->check_simulation();
        // Search for orphaned relationships and delete them.
        MS_Model_Member::clean_db();
        $listview = MS_Factory::create('MS_Helper_ListTable_Member');
        $listview->prepare_items();
        ob_start();
        ?>

		<div class="wrap ms-wrap ms-member-list">
			<?php 
        MS_Helper_Html::settings_header(array('title' => __('Members', MS_TEXT_DOMAIN), 'title_icon_class' => 'wpmui-fa wpmui-fa-users', 'desc' => __('Here you can manage the Memberships of existing Users.', MS_TEXT_DOMAIN)));
        // Display a filter to switch between individual memberships.
        $this->membership_filter();
        $listview->views();
        $listview->search_box();
        ?>
			<form method="post">
				<?php 
        $listview->display();
        ?>
			</form>
		</div>

		<?php 
        $html = ob_get_clean();
        return $html;
    }
 /**
  * Prepare html fields.
  *
  * @since  1.0.0
  *
  * @return array
  */
 function prepare_fields()
 {
     $invoice = $this->data['invoice'];
     $currency = MS_Plugin::instance()->settings->currency;
     $user_name = '';
     $transaction_link = '';
     $user_id = 0;
     $user_list = array();
     if ($invoice->id) {
         $member = $invoice->get_member();
         $user_id = $member->id;
         $user_name = $member->name;
         $transaction_link = sprintf('<a href="%s" target="_blank">%s</a>', MS_Controller_Plugin::get_admin_url('billing', array('show' => 'logs', 'invoice' => $invoice->id)), __('Show Transactions', 'membership2'));
     } else {
         $user_list = MS_Model_Member::get_usernames(null, MS_Model_Member::SEARCH_ALL_USERS);
     }
     $fields = array('link_transactions' => array('id' => 'link_transactions', 'title' => $transaction_link, 'type' => MS_Helper_Html::TYPE_HTML_TEXT, 'wrapper_class' => 'ms-transactions-link'), 'txt_user' => array('id' => 'txt_user', 'title' => __('Invoice for member', 'membership2'), 'type' => MS_Helper_Html::TYPE_HTML_TEXT, 'value' => sprintf('<a href="%s">%s</a>', MS_Controller_Plugin::get_admin_url('add-member', array('user_id' => $user_id)), $user_name)), 'txt_membership' => array('id' => 'txt_membership', 'title' => __('Payment for membership', 'membership2'), 'type' => MS_Helper_Html::TYPE_HTML_TEXT), 'txt_created' => array('id' => 'txt_created', 'title' => __('Invoice created on', 'membership2'), 'type' => MS_Helper_Html::TYPE_HTML_TEXT), 'txt_separator' => array('type' => MS_Helper_Html::TYPE_HTML_SEPARATOR), 'status' => array('id' => 'status', 'title' => __('Invoice status', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'field_options' => MS_Model_Invoice::get_status_types(true), 'value' => $invoice->status), 'user_id' => array('id' => 'user_id', 'title' => __('Invoice for member', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $invoice->user_id, 'field_options' => $user_list), 'membership_id' => array('id' => 'membership_id', 'title' => __('Payment for membership', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $invoice->membership_id, 'field_options' => $this->data['memberships']), 'amount' => array('id' => 'amount', 'title' => sprintf(__('Amount (%s)', 'membership2'), $currency), 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'value' => MS_Helper_Billing::format_price($invoice->amount), 'config' => array('step' => 'any', 'min' => 0)), 'discount' => array('id' => 'discount', 'title' => sprintf(__('Discount (%s)', 'membership2'), $currency), 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'value' => MS_Helper_Billing::format_price($invoice->discount), 'config' => array('step' => 'any', 'min' => 0)), 'due_date' => array('id' => 'due_date', 'title' => __('Due date', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_DATEPICKER, 'value' => $invoice->due_date), 'description' => array('id' => 'description', 'title' => __('Description', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_TEXT, 'class' => 'widefat', 'value' => $invoice->description), 'notes' => array('id' => 'notes', 'title' => __('Notes', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_TEXT_AREA, 'class' => 'widefat', 'value' => $invoice->get_notes_desc()), 'invoice_id' => array('id' => 'invoice_id', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $invoice->id), '_wpnonce' => array('id' => '_wpnonce', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce($this->data['action'])), 'action' => array('id' => 'action', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $this->data['action']), 'separator' => array('type' => MS_Helper_Html::TYPE_HTML_SEPARATOR), 'execute' => array('id' => 'execute', 'title' => __('Execute status change actions on Save (add/remove membership)', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_CHECKBOX, 'value' => true), 'cancel' => array('id' => 'cancel', 'type' => MS_Helper_Html::TYPE_HTML_LINK, 'title' => __('Cancel', 'membership2'), 'value' => __('Cancel', 'membership2'), 'url' => esc_url_raw(remove_query_arg(array('action', 'invoice_id'))), 'class' => 'wpmui-field-button button'), 'submit' => array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => __('Save Changes', 'membership2')));
     if ($invoice->id > 0) {
         $fields['user_id']['type'] = MS_Helper_Html::INPUT_TYPE_HIDDEN;
         $fields['membership_id']['type'] = MS_Helper_Html::INPUT_TYPE_HIDDEN;
         $fields['txt_membership']['value'] = $this->data['memberships'][$invoice->membership_id];
         $fields['txt_created']['value'] = MS_Helper_Period::format_date($invoice->invoice_date);
     } else {
         unset($fields['txt_user']);
         unset($fields['txt_membership']);
         unset($fields['txt_created']);
         unset($fields['txt_separator']);
     }
     return apply_filters('ms_view_billing_edit_prepare_fields', $fields, $this);
 }
Пример #4
0
 /**
  * Determines if the current user is permitted to even think about using
  * simulation. If not allowed, then most of this class will not be used.
  *
  * @since  1.0.0
  * @return bool
  */
 public static function can_simulate()
 {
     if (defined('DOING_CRON') && DOING_CRON) {
         // No simulation during cron jobs...
         return false;
     }
     if (MS_Model_Member::is_admin_user()) {
         return true;
     }
     return false;
 }
 /**
  * Prepare html fields.
  *
  * @since  1.0.1.0
  *
  * @return array
  */
 function prepare_fields()
 {
     $member = $this->data['member'];
     $userlist = MS_Model_Member::get_usernames(null, MS_Model_Member::SEARCH_ALL_USERS);
     $log = $this->data['log'];
     $data_action = MS_Controller_Billing::AJAX_ACTION_TRANSACTION_LINK_DATA;
     $update_action = MS_Controller_Billing::AJAX_ACTION_TRANSACTION_UPDATE;
     $fields = array();
     $member_id = 0;
     if ($member && $member->id) {
         $member_id = $member->id;
     }
     $fields['info'] = array('id' => array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => __('Transaction ID', 'membership2'), 'value' => $log->id), 'gateway' => array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => __('Payment Gateway', 'membership2'), 'value' => MS_Model_Gateway::get_name($log->gateway_id, true)), 'amount' => array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => __('Transaction Amount', 'membership2'), 'value' => MS_Helper_Billing::format_price($log->amount)), 'details' => array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => __('Transaction Details', 'membership2'), 'value' => $log->description), 'sep' => array('type' => MS_Helper_Html::TYPE_HTML_SEPARATOR, 'value' => 'vertical'));
     $fields['member'] = array('user_id' => array('id' => 'user_id', 'title' => __('1. Payment by user', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $member_id, 'field_options' => $userlist));
     $fields['subscription'] = array('subscription_id' => array('id' => 'subscription_id', 'title' => __('2. Payment for subscription', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => 0, 'field_options' => array()));
     $fields['invoice'] = array('invoice_id' => array('id' => 'invoice_id', 'title' => __('3. Link payment with invoice', 'membership2'), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => 0, 'field_options' => array(), 'after' => __('The selected Invoice will be marked as "paid"', 'membership2')));
     $fields['buttons'] = array('nonce_link_data' => array('id' => 'nonce_link_data', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce($data_action)), 'nonce_update' => array('id' => 'nonce_update', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce($update_action)), 'log_id' => array('id' => 'log_id', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $log->id), 'cancel' => array('id' => 'cancel', 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON, 'value' => __('Cancel', 'membership2'), 'class' => 'wpmui-field-button button close'), 'submit' => array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => __('Save Transaction', 'membership2')));
     return apply_filters('ms_view_billing_link_prepare_fields', $fields, $this);
 }
 /**
  * Save coupon application.
  *
  * Saving the application to keep track of the application in gateway return.
  * Using COUPON_REDEMPTION_TIME to expire coupon application.
  *
  * This is a non-static function, as it saves the current object!
  *
  * @since  1.0.0
  * @param MS_Model_Relationship $subscription The subscription to apply the coupon.
  */
 public function save_application($subscription)
 {
     // Don't save empty invitations.
     if (empty($this->code)) {
         return false;
     }
     $membership = $subscription->get_membership();
     $discount = $this->get_discount_value($subscription);
     $time = apply_filters('ms_addon_coupon_model_save_application_redemption_time', self::COUPON_REDEMPTION_TIME);
     // Grab the user account as we should be logged in by now.
     $user = MS_Model_Member::get_current_member();
     $key = self::get_transient_name($user->id, $membership->id);
     $transient = apply_filters('ms_addon_coupon_model_transient_value', array('id' => $this->id, 'user_id' => $user->id, 'membership_id' => $membership->id, 'discount' => $discount, 'message' => $this->coupon_message));
     MS_Factory::set_transient($key, $transient, $time);
     $this->save();
     do_action('ms_addon_coupon_model_save_application', $subscription, $this);
 }
 /**
  * Returns true only then, when the current user can edit menu items.
  *
  * Reasons why it might be denied:
  * - There are no menus where items can be added to.
  * - The user is no admin.
  *
  * @since  1.0.0
  * @return bool
  */
 public static function can_edit_menus()
 {
     $Can_Edit_Menus = null;
     if (null === $Can_Edit_Menus) {
         $Can_Edit_Menus = false;
         if (!MS_Plugin::is_network_wide()) {
             $menus = wp_get_nav_menus();
             if (MS_Model_Member::is_admin_user() && !empty($menus)) {
                 $Can_Edit_Menus = true;
             }
         }
         $Can_Edit_Menus = apply_filters('ms_model_pages_can_edit_menus', $Can_Edit_Menus);
     }
     return $Can_Edit_Menus;
 }
 /**
  * Create membership column.
  *
  * @since  1.0.0
  *
  * @param MS_Model_Member $member The member object.
  */
 public function column_membership($member)
 {
     if (MS_Model_Member::is_admin_user($member->id)) {
         $html = '<b>' . __('Admin User', MS_TEXT_DOMAIN) . '</b>';
     } else {
         $subscriptions = $member->get_membership_ids();
         $visitor = array('id' => 'ms-empty-' . $member->id, 'type' => MS_Helper_Html::TYPE_HTML_TEXT, 'value' => __('(Visitor)'), 'after' => 'Edit', 'class' => 'ms-empty-note');
         $list = array('id' => 'ms-memberships-' . $member->id, 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $subscriptions, 'field_options' => self::$memberships, 'multiple' => true, 'class' => 'ms-memberships', 'ajax_data' => array('action' => MS_Controller_Member::AJAX_ACTION_CHANGE_MEMBERSHIPS, 'member' => $member->id));
         $html = sprintf('<div class="no-auto-init">%1$s%2$s</div>', MS_Helper_Html::html_element($visitor, true), MS_Helper_Html::html_element($list, true));
     }
     return apply_filters('ms_helper_listtable_member_column_membership', $html, $member, $this);
 }
    public function to_html()
    {
        ob_start();
        ?>
		<div class="ms-account-wrapper">
			<?php 
        if (MS_Model_Member::is_logged_in()) {
            ?>
				<h2>
					<?php 
            _e('Invoice', MS_TEXT_DOMAIN);
            ?>
				</h2>
				<table>
					<thead>
						<tr>
							<th class="ms-col-invoice-no"><?php 
            _e('Invoice #', MS_TEXT_DOMAIN);
            ?>
</th>
							<th class="ms-col-invoice-status"><?php 
            _e('Status', MS_TEXT_DOMAIN);
            ?>
</th>
							<th class="ms-col-invoice-total"><?php 
            printf('%s (%s)', __('Total', MS_TEXT_DOMAIN), MS_Plugin::instance()->settings->currency);
            ?>
</th>
							<th class="ms-col-invoice-title"><?php 
            _e('Membership', MS_TEXT_DOMAIN);
            ?>
</th>
							<th class="ms-col-invoice-due"><?php 
            _e('Due date', MS_TEXT_DOMAIN);
            ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
            foreach ($this->data['invoices'] as $invoice) {
                $inv_membership = MS_Factory::load('MS_Model_Membership', $invoice->membership_id);
                $inv_classes = array('ms-invoice-' . $invoice->id, 'ms-subscription-' . $invoice->ms_relationship_id, 'ms-invoice-' . $invoice->status, 'ms-gateway-' . $invoice->gateway_id, 'ms-membership-' . $invoice->membership_id, 'ms-type-' . $inv_membership->type, 'ms-payment-' . $inv_membership->payment_type);
                ?>
						<tr class="<?php 
                echo esc_attr(implode(' ', $inv_classes));
                ?>
">
							<td class="ms-col-invoice-no"><?php 
                printf('<a href="%s">%s</a>', get_permalink($invoice->id), $invoice->get_invoice_number());
                ?>
</td>
							<td class="ms-col-invoice-status"><?php 
                echo esc_html($invoice->status_text());
                ?>
</td>
							<td class="ms-col-invoice-total"><?php 
                echo esc_html(MS_Helper_Billing::format_price($invoice->total));
                ?>
</td>
							<td class="ms-col-invoice-title"><?php 
                echo esc_html($inv_membership->name);
                ?>
</td>
							<td class="ms-col-invoice-due"><?php 
                echo esc_html(MS_Helper_Period::format_date($invoice->due_date, __('F j', MS_TEXT_DOMAIN)));
                ?>
</td>
						</tr>
					<?php 
            }
            ?>
					</tbody>
				</table>
			<?php 
        } else {
            ?>
				<?php 
            $redirect = esc_url_raw(add_query_arg(array()));
            $title = __('Your account', MS_TEXT_DOMAIN);
            echo do_shortcode("[ms-membership-login redirect='{$redirect}' title='{$title}']");
            ?>
			<?php 
        }
        ?>
		</div>
		<?php 
        $html = ob_get_clean();
        $html = apply_filters('ms_compact_code', $html);
        return $html;
    }
 /**
  * Tries to auto-detect the currently displayed membership-ID.
  *
  * Use this function by calling the filter `ms_detect_membership_id`
  *
  * Detection logic:
  * 1. If a valid preferred value was specified then this value is used.
  * 2. Examine REQUEST data and look for membership/subscription/invoice.
  * 3. Check currently logged in user and use the top-priority subscription.
  *
  * @since  1.0.1.0
  * @param  int $preferred The preferred ID is only used if it is a valid ID.
  * @param  bool $no_member_check If set to true the member subscriptions are
  *         not checked, which means only REQUEST data is examined.
  * @param  bool $ignore_system If set to true, then the return value will
  *         never be a system-membership-ID (no auto-assigned membership).
  * @return int A valid Membership ID or 0 if all tests fail.
  */
 public function autodetect_membership($preferred = 0, $no_member_check = false, $ignore_system = false)
 {
     $membership_id = 0;
     // Check 1: If the preferred value is correct use it.
     if ($preferred) {
         $membership = MS_Factory::load('MS_Model_Membership', $preferred);
         // Only use the membership_id if it's valid and not filtered by ignore_system.
         if ($membership->is_valid() && $membership->id == $preferred) {
             if (!$ignore_system || !$membership->is_system()) {
                 $membership_id = $membership->id;
             }
         }
     }
     // Check 2: Examine the REQUEST parameters to find a valid ID.
     if (!$membership_id) {
         if (!$membership_id) {
             if (isset($_REQUEST['membership_id'])) {
                 $membership_id = $_REQUEST['membership_id'];
             } elseif (isset($_REQUEST['subscription_id'])) {
                 $sub_id = $_REQUEST['subscription_id'];
                 $subscription = MS_Factory::load('MS_Model_Relationship', $sub_id);
                 $membership_id = $subscription->membership_id;
             } elseif (isset($_REQUEST['ms_relationship_id'])) {
                 $sub_id = $_REQUEST['ms_relationship_id'];
                 $subscription = MS_Factory::load('MS_Model_Relationship', $sub_id);
                 $membership_id = $subscription->membership_id;
             } elseif (isset($_REQUEST['invoice_id'])) {
                 $inv_id = $_REQUEST['invoice_id'];
                 $invoice = MS_Factory::load('MS_Model_Invoice', $inv_id);
                 $membership_id = $invoice->membership_id;
             }
             $membership_id = intval($membership_id);
         }
         // Reset the membership_id if it's invalid or filtered by ignore_system.
         if ($membership_id) {
             $membership = MS_Factory::load('MS_Model_Membership', $membership_id);
             if (!$membership->is_valid()) {
                 $membership_id = 0;
             } elseif ($membership->id != $membership_id) {
                 $membership_id = 0;
             } elseif ($ignore_system && $membership->is_system) {
                 $membership_id = 0;
             }
         }
     }
     // Check 3: Check subscriptions of the current user.
     if (!$no_member_check && !$membership_id && is_user_logged_in()) {
         $member = MS_Model_Member::get_current_member();
         $subscription = $member->get_subscription('priority');
         if ($subscription) {
             $membership_id = $subscription->membership_id;
         }
         // Reset the membership_id if it's invalid or filtered by ignore_system.
         if ($membership_id) {
             $membership = MS_Factory::load('MS_Model_Membership', $membership_id);
             if (!$membership->is_valid()) {
                 $membership_id = 0;
             } elseif ($membership->id != $membership_id) {
                 $membership_id = 0;
             } elseif ($ignore_system && $membership->is_system) {
                 $membership_id = 0;
             }
         }
     }
     return apply_filters('ms_controller_membership_autodetect_membership', $membership_id, $preferred, $no_member_check);
 }
 /**
  * Auto setup settings.
  *
  * Fires after a membership setup is completed.
  * This hook is executed every time a new membership is created.
  *
  * Related Action Hooks:
  * - ms_controller_membership_setup_completed
  *
  * @since  1.0.0
  *
  * @param MS_Model_Membership $membership
  */
 public function auto_setup_settings($membership)
 {
     $settings = $this->get_model();
     // Create special pages.
     MS_Model_Pages::create_missing_pages();
     $pg_prot_cont = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT);
     $pg_acco = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_ACCOUNT);
     $pg_regi = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_REGISTER);
     $pg_regi_comp = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_REG_COMPLETE);
     $pg_memb = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_MEMBERSHIPS);
     // Publish special pages.
     // Tip: Only pages must be published that are added to the menu.
     wp_publish_post($pg_acco->ID);
     if (!$membership->private) {
         wp_publish_post($pg_memb->ID);
         wp_publish_post($pg_regi->ID);
     }
     // Create new WordPress menu-items.
     MS_Model_Pages::create_menu(MS_Model_Pages::MS_PAGE_ACCOUNT);
     if (!$membership->private) {
         MS_Model_Pages::create_menu(MS_Model_Pages::MS_PAGE_MEMBERSHIPS);
         MS_Model_Pages::create_menu(MS_Model_Pages::MS_PAGE_REGISTER);
     }
     // Enable Membership2.
     $settings->plugin_enabled = true;
     $settings->save();
     // Enable the "Allow user registration" setting of WordPress
     MS_Model_Member::allow_registration();
 }
    public function to_html()
    {
        $fields = $this->prepare_fields();
        $subscription = $this->data['ms_relationship'];
        $invoice = $subscription->get_current_invoice();
        $member = MS_Model_Member::get_current_member();
        $gateway = $this->data['gateway'];
        // Stripe is using Ajax, so the URL is empty.
        $action_url = apply_filters('ms_gateway_stripe_view_button_form_action_url', '');
        $row_class = 'gateway_' . $gateway->id;
        if (!$gateway->is_live_mode()) {
            $row_class .= ' sandbox-mode';
        }
        ob_start();
        ?>
		<form action="<?php 
        echo esc_url($action_url);
        ?>
" method="post">
			<?php 
        foreach ($fields as $field) {
            MS_Helper_Html::html_element($field);
        }
        ?>
			<script
				src="https://checkout.stripe.com/checkout.js" class="stripe-button"
				data-key="<?php 
        echo esc_attr($gateway->get_publishable_key());
        ?>
"
				data-amount="<?php 
        echo esc_attr(ceil(abs($invoice->total * 100)));
        //amount in cents
        ?>
"
				data-name="<?php 
        echo esc_attr(bloginfo('name'));
        ?>
"
				data-description="<?php 
        echo esc_attr(strip_tags($invoice->short_description));
        ?>
"
				data-currency="<?php 
        echo esc_attr($invoice->currency);
        ?>
"
				data-panel-label="<?php 
        echo esc_attr($gateway->pay_button_url);
        ?>
"
				data-label="<?php 
        echo esc_attr($gateway->pay_button_url);
        ?>
"
				data-email="<?php 
        echo esc_attr($member->email);
        ?>
"
				>
			</script>
		</form>
		<?php 
        $payment_form = apply_filters('ms_gateway_form', ob_get_clean(), $gateway, $invoice, $this);
        ob_start();
        ?>
		<tr class="<?php 
        echo esc_attr($row_class);
        ?>
">
			<td class="ms-buy-now-column" colspan="2">
				<?php 
        echo $payment_form;
        ?>
			</td>
		</tr>
		<?php 
        $html = ob_get_clean();
        $html = apply_filters('ms_gateway_button-' . $gateway->id, $html, $this);
        return $html;
    }
 /**
  * Checks if the current user can access the specified attachment.
  *
  * @since  1.0.0
  * @param  int $attachment_id
  * @return bool
  */
 public function can_access_file($attachment_id)
 {
     $access = false;
     if (MS_Model_Member::is_normal_admin()) {
         return true;
     }
     if (!MS_Model_Addon::is_enabled(MS_Addon_Mediafiles::ID)) {
         /*
          * Default protection mode:
          * Protect Attachments based on the parent post.
          */
         $parent_id = get_post_field('post_parent', $attachment_id);
         if (!$parent_id) {
             $access = true;
         } else {
             $member = MS_Model_Member::get_current_member();
             foreach ($member->subscriptions as $subscription) {
                 $membership = $subscription->get_membership();
                 $access = $membership->has_access_to_post($parent_id);
                 if ($access) {
                     break;
                 }
             }
         }
     } else {
         /*
          * Advanced protection mode (via Add-on):
          * Each Attachment can be protected individually.
          */
         $member = MS_Model_Member::get_current_member();
         foreach ($member->subscriptions as $subscription) {
             $rule = $subscription->get_membership()->get_rule(MS_Rule_Media::RULE_ID);
             $access = $rule->has_access($attachment_id);
             if ($access) {
                 break;
             }
         }
     }
     return apply_filters('ms_rule_media_can_access_file', $access, $attachment_id);
 }
    public function to_html()
    {
        ob_start();
        ?>
		<div class="ms-account-wrapper">
			<?php 
        if (MS_Model_Member::is_logged_in()) {
            ?>
				<h2>
					<?php 
            _e('Activity', MS_TEXT_DOMAIN);
            ?>
				</h2>
				<table>
					<thead>
						<tr>
							<th class="ms-col-activity-date"><?php 
            _e('Date', MS_TEXT_DOMAIN);
            ?>
</th>
							<th class="ms-col-activity-title"><?php 
            _e('Activity', MS_TEXT_DOMAIN);
            ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
            foreach ($this->data['events'] as $event) {
                $ev_classes = array('ms-activity-topic-' . $event->topic, 'ms-activity-type-' . $event->type, 'ms-membership-' . $event->membership_id);
                ?>
						<tr class="<?php 
                echo esc_attr(implode(' ', $ev_classes));
                ?>
">
							<td class="ms-col-activity-date"><?php 
                echo esc_html(MS_Helper_Period::format_date($event->post_modified, __('F j (H:i)', MS_TEXT_DOMAIN)));
                ?>
</td>
							<td class="ms-col-activity-title"><?php 
                echo esc_html($event->description);
                ?>
</td>
						</tr>
					<?php 
            }
            ?>
				</tbody>
			</table>
			<?php 
        } else {
            $redirect = esc_url_raw(add_query_arg(array()));
            $title = __('Your account', MS_TEXT_DOMAIN);
            echo do_shortcode("[ms-membership-login redirect='{$redirect}' title='{$title}']");
        }
        ?>
		</div>
		<?php 
        $html = ob_get_clean();
        $html = apply_filters('ms_compact_code', $html);
        return $html;
    }
 /**
  * Returns HTML partial that contains the logout form
  *
  * @since  1.0.0
  *
  * @return string
  */
 private function logout_form()
 {
     if (!MS_Model_Member::is_logged_in()) {
         return '';
     }
     $member = MS_Model_Member::get_current_member();
     extract($this->data);
     if (empty($redirect_logout)) {
         $redirect_logout = home_url();
     }
     $yourname = sprintf(__('You are logged in as %s.', MS_TEXT_DOMAIN), ucfirst($member->username));
     $html = sprintf('%1$s <a class="login_button" href="%2$s">%3$s</a>', $yourname, wp_logout_url($redirect_logout), __('Logout', MS_TEXT_DOMAIN));
     if (!empty($holder)) {
         $html = sprintf('<%1$s class="%2$s">%3$s</%1$s>', esc_attr($holder), esc_attr($holderclass), $html);
     }
     return $html;
 }
 /**
  * Checks if the current user can access the specified attachment.
  *
  * @since  1.0.0
  * @param  int $attachment_id
  * @return bool
  */
 public function can_access_file($attachment_id)
 {
     $access = false;
     if (MS_Model_Member::is_normal_admin()) {
         return true;
     }
     /*
      * Default protection mode:
      * Protect Attachments based on the parent post.
      */
     $parent_id = get_post_field('post_parent', $attachment_id);
     if (!$parent_id) {
         $access = true;
     } else {
         $member = MS_Model_Member::get_current_member();
         foreach ($member->subscriptions as $subscription) {
             $membership = $subscription->get_membership();
             $access = $membership->has_access_to_post($parent_id);
             if ($access) {
                 break;
             }
         }
     }
     return apply_filters('ms_rule_media_can_access_file', $access, $attachment_id);
 }
 /**
  * Customize the Admin Toolbar.
  *
  * Related Action Hooks:
  * - wp_before_admin_bar_render
  *
  * @since  1.0.0
  */
 public function customize_toolbar()
 {
     if (MS_Model_Member::is_admin_user() && MS_Plugin::is_enabled() && !is_network_admin() && MS_Model_Simulate::can_simulate()) {
         if ($this->simulate->is_simulating()) {
             $this->add_detail_nodes();
         } else {
             $this->add_test_membership_node();
         }
     } else {
         if (!MS_Plugin::is_enabled()) {
             $this->add_unprotected_node();
         }
     }
 }
 /**
  * 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;
 }
Пример #19
0
 /**
  * Redirect user to account page.
  *
  * Only redirect when no previous redirect_to is set or when going to /wp-admin/.
  *
  * @since  1.0.0
  *
  * @param string $redirect_to URL to redirect to.
  * @param string $request URL the user is coming from.
  * @param object $user Logged user's data.
  * @return string The redirect url.
  */
 public function login_redirect($redirect_to, $request, $user)
 {
     if (!empty($user->ID) && !MS_Model_Member::is_admin_user($user->ID) && (empty($redirect_to) || admin_url() == $redirect_to)) {
         $redirect_to = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT);
     }
     return apply_filters('ms_controller_frontend_login_redirect', $redirect_to, $request, $user, $this);
 }
 /**
  * Verify access to post.
  *
  * Verify membership rules hierarchy for specific post or CPT.
  *
  * @since  1.0.0
  * @api
  *
  * @param int $post_id ID of specific post
  * @return boolean True if has access to current page. Default is false.
  */
 public function has_access_to_post($post_id)
 {
     $has_access = null;
     if (MS_Model_Member::is_normal_admin()) {
         return true;
     }
     if (!empty($post_id)) {
         $post = get_post($post_id);
         if ('attachment' === $post->post_type) {
             $post_id = get_post_field('post_parent', $post_id);
         }
     }
     // If 'has access' is found in the hierarchy, it does have access.
     $rules = $this->get_rules_hierarchy();
     foreach ($rules as $rule) {
         $rule->prepare_rule($subscription);
         // url groups have final decision
         if (MS_Rule_Url::RULE_ID == $rule->rule_type && $rule->has_rule_for_post($post_id)) {
             $has_access = $rule->has_access($post_id);
             break;
         } else {
             $rule_access = $rule->has_access($post_id);
             if (null !== $rule_access) {
                 $has_access = $rule_access;
             }
         }
         if ($has_access) {
             break;
         }
     }
     if (null === $has_access) {
         // The post is not denied by any rule, so allow access.
         $has_access = true;
     }
     return apply_filters('ms_model_membership_has_access_to_post', $has_access, $this);
 }
Пример #21
0
 /**
  * Main entry point: Handles the export action.
  *
  * This task will exit the current request as the result will be a download
  * and no HTML page that is displayed.
  *
  * @since  1.0.0
  */
 public function process()
 {
     $data = (object) array();
     $data->source_key = self::KEY;
     $data->source = 'Membership2';
     $data->plugin_version = MS_PLUGIN_VERSION;
     $data->export_time = date('Y-m-d H:i');
     $data->notes = array(__('Exported data:', MS_TEXT_DOMAIN), __('- Memberships (without protection rules)', MS_TEXT_DOMAIN), __('- Members (including Stripe/Authorize payment settings)', MS_TEXT_DOMAIN), __('- Subscriptions (link between Members and Memberships)', MS_TEXT_DOMAIN), __('- Invoices', MS_TEXT_DOMAIN));
     $data->memberships = array();
     // Export the base membership (i.e. the Membership2 settings)
     $membership = MS_Model_Membership::get_base();
     $data->memberships[] = $this->export_membership($membership->id);
     // Export all memberships.
     $memberships = MS_Model_Membership::get_memberships(array('post_parent' => 0));
     foreach ($memberships as $membership) {
         $data->memberships[] = $this->export_membership($membership->id);
     }
     // Export the members.
     $members = MS_Model_Member::get_members();
     $data->members = array();
     foreach ($members as $member) {
         if (!$member->is_member) {
             continue;
         }
         $data->members[] = $this->export_member($member->id);
     }
     // Export plugin settings.
     $obj = array();
     $data->settings = $this->export_settings();
     // Export Coupons.
     $data->coupons = array();
     lib2()->net->file_download(json_encode($data), 'membership2-export.json');
 }
 /**
  * Returns HTML partial that contains the logout form
  *
  * @since  1.0.0
  *
  * @return string
  */
 private function logout_form()
 {
     if (!MS_Model_Member::is_logged_in()) {
         return '';
     }
     $member = MS_Model_Member::get_current_member();
     extract($this->data);
     if (empty($redirect_logout)) {
         $redirect_logout = MS_Helper_Utility::home_url('/');
     }
     $yourname = sprintf(__('You are logged in as %s.', 'membership2'), ucfirst($member->name));
     $yourname = apply_filters('ms_shortcode_logout_message', $yourname, $member);
     $logout_text = apply_filters('ms_shortcode_logout_link_text', __('Logout', 'membership2'), $member);
     $redirect_logout = apply_filters('ms_shortcode_logout_redirect', $redirect_logout, $member);
     $html = sprintf('%1$s <a class="login_button" href="%2$s">%3$s</a>', $yourname, wp_logout_url($redirect_logout), $logout_text);
     if (!empty($holder)) {
         $html = sprintf('<%1$s class="%2$s">%3$s</%1$s>', esc_attr($holder), esc_attr($holderclass), $html);
     }
     return $html;
 }
 /**
  * Determines the users country based on his IP address.
  *
  * @since  1.0.0
  * @internal
  *
  * @param string $mode [declared|vat|card|auto] Either the country from user
  *        settings or the auto-detected country.
  */
 protected static function fetch_country($mode = 'declared')
 {
     $member = MS_Model_Member::get_current_member();
     $country = false;
     $store_it = false;
     $non_auto_countries = array('declared', 'vat', 'card');
     if (!in_array($mode, $non_auto_countries)) {
         $mode = 'auto';
     }
     $auto_detect = 'auto' == $mode;
     $key = 'tax_' . $mode . '_country';
     // If no country is stored use the API to determine it.
     if ($auto_detect) {
         try {
             $ip_info = lib3()->net->current_ip();
             $data = (object) (array) self::taxamo()->locateGivenIP($ip_info->ip);
             $country = (object) array('code' => $data->country_code);
             // Store result in Session, not in DB.
             $store_it = true;
             $member = null;
         } catch (Exception $ex) {
             MS_Helper_Debug::log('Taxamo error: ' . $ex->getMessage());
         }
     } else {
         // Try to get the stored country from user-meta or session (for guest)
         $country = self::get_tax_profile_value($member, $key);
     }
     // API did not return a valid resonse, use a dummy value.
     if (!$country) {
         $country = (object) array('code' => '');
     }
     // Store result in user-deta or session.
     if ($store_it && self::set_tax_profile_value($member, $key, $country)) {
         $member->save();
     }
     $country_names = self::get_country_codes('name');
     if ($country->code && isset($country_names[$country->code])) {
         $country->name = $country_names[$country->code];
     } else {
         $country->name = $country_names['XX'];
     }
     return $country;
 }
 /**
  * Create new WP user.
  *
  * @since  1.0.0
  * @internal
  * @throws Exception
  */
 private function create_new_user()
 {
     // Check if the WordPress settings allow user registration.
     if (!MS_Model_Member::can_register()) {
         throw new Exception(__('Registration is currently not allowed.', 'membership2'), 1);
         return;
     }
     if (is_user_logged_in()) {
         throw new Exception(__('You cannot register a new account, because you are already logged in.', 'membership2'), 1);
         return;
     }
     $validation_errors = new WP_Error();
     $required = array('username' => __('Username', 'membership2'), 'email' => __('Email address', 'membership2'), 'password' => __('Password', 'membership2'), 'password2' => __('Password confirmation', 'membership2'));
     /**
      * Filter the required field list to customize the fields that are
      * mandatory.
      *
      * @since 1.0.1.0
      * @var   array
      */
     $required = apply_filters('ms_model_member_create_user_required_fields', $required);
     foreach ($required as $field => $message) {
         if (empty($this->{$field}) && empty($_POST[$field])) {
             $validation_errors->add($field, sprintf(__('Please ensure that the <span class="ms-bold">%s</span> information is completed.', 'membership2'), $message));
         }
     }
     if ($this->password != $this->password2) {
         $validation_errors->add('passmatch', __('Please ensure the passwords match.', 'membership2'));
     }
     if (!validate_username($this->username)) {
         $validation_errors->add('usernamenotvalid', __('The username is not valid, sorry.', 'membership2'));
     }
     if (username_exists($this->username)) {
         $validation_errors->add('usernameexists', __('That username is already taken, sorry.', 'membership2'));
     }
     if (!is_email($this->email)) {
         $validation_errors->add('emailnotvalid', __('The email address is not valid, sorry.', 'membership2'));
     }
     if (email_exists($this->email)) {
         $validation_errors->add('emailexists', __('That email address is already taken, sorry.', 'membership2'));
     }
     // Check the multisite Email-Domain limitation for new registrations.
     if (is_multisite()) {
         $illegal_names = get_site_option('illegal_names');
         $limited_domains = get_site_option('limited_email_domains');
         $banned_domains = get_site_option('banned_email_domains');
         $email_domain = substr(strrchr($this->email, '@'), 1);
         if ($illegal_names && is_array($illegal_names)) {
             if (in_array($this->username, $illegal_names)) {
                 $validation_errors->add('illegalname', __('The username is not valid, sorry.', 'membership2'));
             }
         }
         if ($limited_domains && is_array($limited_domains)) {
             if (!in_array($email_domain, $limited_domains)) {
                 $validation_errors->add('emaildomain', __('That email domain is not allowed for registration, sorry.', 'membership2'));
             }
         }
         if ($banned_domains && is_array($banned_domains)) {
             if (in_array($email_domain, $banned_domains)) {
                 $validation_errors->add('emaildomain', __('That email domain is not allowed for registration, sorry.', 'membership2'));
             }
         }
     }
     $validation_errors = apply_filters('ms_model_membership_create_new_user_validation_errors', $validation_errors);
     // Compatibility with WangGuard
     $_POST['user_email'] = $this->email;
     $user_data = array('user_name' => $this->username, 'orig_username' => $this->username, 'user_email' => $this->email, 'errors' => $validation_errors);
     $user_data = apply_filters('wpmu_validate_user_signup', $user_data);
     if (is_wp_error($user_data)) {
         /*
          * Some plugins incorrectly return a WP_Error object as result of
          * the wpmu_validate_user_signup filter.
          */
         $validation_errors = $user_data;
     } else {
         $validation_errors = $user_data['errors'];
     }
     $errors = $validation_errors->get_error_messages();
     if (!empty($errors)) {
         throw new Exception(implode('<br/>', $errors));
     } else {
         if (!$this->password) {
             /**
              * For some reason the user did not provide a password in the
              * registration form. We help out here by creating a password
              * for the little bugger and send him a password-reset email.
              *
              * So: Generate a STRONG password for the new user.
              *
              * Important: This password should be sent to the user via the
              * Email template "User Account Created"
              */
             $this->password = wp_generate_password(24);
             $this->password2 = $this->password;
         }
         $user_id = wp_create_user($this->username, $this->password, $this->email);
         if (is_wp_error($user_id)) {
             $validation_errors->add('userid', $user_id->get_error_message());
             throw new Exception(implode('<br/>', $validation_errors->get_error_messages()));
         }
         $this->id = $user_id;
     }
     do_action('ms_model_member_create_new_user', $this);
 }
Пример #25
0
 /**
  * Verify access to the current content.
  *
  * @since  1.0.0
  *
  * @param string $id The content id to verify access.
  * @param bool $admin_has_access Default true: Admin will always have access,
  *             no matter how protection is set up. False will ignore the
  *             admin status and check protection rules normaly.
  *
  * @return boolean TRUE if has access, FALSE otherwise.
  */
 public function has_access($id, $admin_has_access = true)
 {
     if ($admin_has_access && MS_Model_Member::is_normal_admin()) {
         return true;
     }
     /*
      * $access will be one of these:
      *   - TRUE .. Access explicitly granted
      *   - FALSE .. Access explicitly denied
      *   - NULL .. Access implicitly allowed (i.e. "not-denied")
      */
     $access = $this->get_rule_value($id);
     if ($this->is_base_rule) {
         /*
          * Base rule ..
          *   - The meaning of TRUE/FALSE is inverted
          *   - NULL is always "allowed"
          */
         $access = !$access;
     } else {
         // Apply dripped-content rules if neccessary.
         if ($access && $this->has_dripped_rules($id)) {
             if (!empty($this->_subscription_id)) {
                 $subscription = MS_Factory::load('MS_Model_Relationship', $this->_subscription_id);
                 $start_date = $subscription->start_date;
             } else {
                 $start_date = null;
             }
             $avail_date = $this->get_dripped_avail_date($id, $start_date);
             $now = MS_Helper_Period::current_date();
             $access = strtotime($now) >= strtotime($avail_date);
         }
         if (MS_Model_Rule::RULE_VALUE_UNDEFINED === $access) {
             // NULL .. "not-denied" is translated to "allowed"
             $access = true;
         }
     }
     // At this point $access can either be TRUE or FALSE, not NULL!
     $access = (bool) $access;
     return apply_filters('ms_rule_has_access', $access, $id, $this->rule_type, $this);
 }
 /**
  * Get a simple array of capabilties (e.g. for display in select lists)
  *
  * @since  1.0.0
  * @global array $menu
  *
  * @return array {
  *      @type string $id The id.
  *      @type string $name The name.
  * }
  */
 public function get_capabilities($args = null)
 {
     if (null === $this->_content_array) {
         $this->_content_array = array();
         $member = MS_Model_Member::get_current_member();
         $capslist = $member->wp_user->allcaps;
         $ignored_caps = array('level_10' => 1, 'level_9' => 1, 'level_8' => 1, 'level_7' => 1, 'level_6' => 1, 'level_5' => 1, 'level_4' => 1, 'level_3' => 1, 'level_2' => 1, 'level_1' => 1, 'level_0' => 1, 'administrator' => 1);
         $capslist = array_diff_assoc($capslist, $ignored_caps);
         $capslist = array_keys($capslist);
         /**
          * Exclude certain capabilities for security reasons.
          *
          * @since  1.0.0
          * @var array
          */
         $exclude = apply_filters('ms_rule_membercaps_model_exclude', array(MS_Plugin::instance()->controller->capability, 'edit_plugins', 'delete_plugins', 'edit_files', 'edit_users', 'delete_users', 'remove_users', 'promote_users', 'list_users'));
         $capslist = array_diff($capslist, $exclude);
         $this->_content_array = array_combine($capslist, $capslist);
         // Make sure the rule_value only contains valid items.
         $rule_value = array_intersect_key($this->rule_value, $this->_content_array);
         $this->rule_value = lib2()->array->get($rule_value);
         // If not visitor membership, just show Membership2
         if (!$this->get_membership()->is_base()) {
             $this->_content_array = array_intersect_key($this->_content_array, $this->rule_value);
         }
         $this->_content_array = apply_filters('ms_rule_membercaps_model_get_content_array', $this->_content_array, $this);
     }
     $contents = $this->_content_array;
     // Search the shortcode-tag...
     if (!empty($args['s'])) {
         foreach ($contents as $key => $name) {
             if (false === stripos($name, $args['s'])) {
                 unset($contents[$key]);
             }
         }
     }
     $filter = self::get_exclude_include($args);
     if (is_array($filter->include)) {
         $contents = array_intersect($contents, $filter->include);
     } elseif (is_array($filter->exclude)) {
         $contents = array_diff($contents, $filter->exclude);
     }
     // Pagination
     if (!empty($args['posts_per_page'])) {
         $total = $args['posts_per_page'];
         $offset = !empty($args['offset']) ? $args['offset'] : 0;
         $contents = array_slice($contents, $offset, $total);
     }
     return $contents;
 }
 /**
  * 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);
 }
 /**
  * This list has no views.
  *
  * @since  1.0.2.0
  *
  * @return array
  */
 public function get_views()
 {
     $views = array();
     $args = array();
     $count = 0;
     $views['label'] = array('label' => __('Subscription Status:', 'membership2'));
     if (empty($_REQUEST['membership_id'])) {
         // All users
         $url = esc_url_raw(add_query_arg('status', 'all'));
         $views['all'] = array('url' => $url, 'label' => __('All users', 'membership2'));
     } else {
         $args['membership_id'] = $_REQUEST['membership_id'];
     }
     // Active, Trial, Cancelled
     $url = esc_url_raw(remove_query_arg('status'));
     $args['subscription_status'] = MS_Model_Relationship::STATUS_ACTIVE;
     $count = MS_Model_Member::get_members_count($args);
     $views['active'] = array('url' => $url, 'label' => __('Active subscription', 'membership2'), 'count' => $count);
     // Cancelled
     $url = esc_url_raw(add_query_arg('status', MS_Model_Relationship::STATUS_CANCELED));
     $args['subscription_status'] = MS_Model_Relationship::STATUS_CANCELED;
     $count = MS_Model_Member::get_members_count($args);
     $views['cancelled'] = array('url' => $url, 'label' => __('Cancelled', 'membership2'), 'count' => $count);
     // Trial
     if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL)) {
         $url = esc_url_raw(add_query_arg('status', MS_Model_Relationship::STATUS_TRIAL));
         $args['subscription_status'] = MS_Model_Relationship::STATUS_TRIAL;
         $count = MS_Model_Member::get_members_count($args);
         $views['trial'] = array('url' => $url, 'label' => __('Trial', 'membership2'), 'count' => $count);
     }
     // Expired, Trial-Expired
     $url = esc_url_raw(add_query_arg('status', 'expired'));
     $args['subscription_status'] = 'expired';
     $count = MS_Model_Member::get_members_count($args);
     $views['expired'] = array('url' => $url, 'label' => __('Expired', 'membership2'), 'count' => $count);
     return $views;
 }
 /**
  * Verify if current user can perform management actions.
  *
  * @since  1.0.0
  *
  * @return boolean True if can, false otherwise.
  */
 public function is_admin_user()
 {
     $is_admin_user = MS_Model_Member::is_admin_user();
     return apply_filters('ms_controller_is_admin_user', $is_admin_user);
 }
 /**
  * Returns a URL to trigger the specified membership action.
  *
  * The URL can be used in a link or a form with only a submit button.
  *
  * @since  1.0.0
  * @param  string $action
  * @return string The URL.
  */
 protected function get_action_url($membership, $action, $step)
 {
     if (empty($this->data['member'])) {
         $member = MS_Model_Member::get_current_member();
     } else {
         $member = $this->data['member'];
     }
     if (is_numeric($membership)) {
         $membership = MS_Factory::load('MS_Model_Membership', $membership);
     }
     $membership->_move_from = $member->cancel_ids_on_subscription($membership->id);
     $fields = $this->prepare_fields($membership->id, $action, $step, $membership->_move_from);
     if (is_user_logged_in()) {
         $current = MS_Model_Pages::MS_PAGE_MEMBERSHIPS;
     } else {
         $current = MS_Model_Pages::MS_PAGE_REGISTER;
     }
     $url = MS_Model_Pages::get_page_url($current);
     if ($action == MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP) {
         // Only add the membership_id to the URL.
         $url = esc_url_raw(add_query_arg('membership_id', $membership->id, $url));
     } else {
         $url = esc_url_raw(add_query_arg('_wpnonce', wp_create_nonce($action), $url));
         foreach ($fields as $field) {
             $url = esc_url_raw(add_query_arg($field['id'], $field['value'], $url));
         }
     }
     return apply_filters('ms_view_shortcode_membershipsignup_action_url', $url, $action, $membership, $this);
 }