/**
  * Returns the default admin menu items for Membership2.
  * Helper function used by add_menu_pages
  *
  * @since  1.0.0
  * @return array
  */
 private function get_default_menu_pages()
 {
     $show_billing = false;
     $pages = array('memberships' => array('title' => __('Memberships', 'membership2'), 'slug' => ''), 'protected-content' => array('title' => __('Protection Rules', 'membership2'), 'slug' => 'protection'), 'members' => array('title' => __('All Members', 'membership2'), 'slug' => 'members'), 'add-member' => array('title' => __('Add Member', 'membership2'), 'slug' => 'add-member'), 'billing' => false, 'addon' => array('title' => __('Add-ons', 'membership2'), 'slug' => 'addon'), 'settings' => array('title' => __('Settings', 'membership2'), 'slug' => 'settings'), 'help' => array('title' => __('Help', 'membership2'), 'slug' => 'help'));
     $show_billing = MS_Model_Membership::have_paid_membership();
     if ($show_billing) {
         $bill_count = MS_Model_Invoice::get_unpaid_invoice_count(null, true);
         if ($bill_count > 0) {
             $msg = '%1$s <span class="awaiting-mod count-%3$s"><span class="pending-count"><i class="hidden">(</i>%2$s<i class="hidden">)</i></span></span>';
         } else {
             $msg = '%1$s';
         }
         $pages['billing'] = array('title' => sprintf($msg, __('Billing', 'membership2'), $bill_count, sanitize_html_class($bill_count, '0')), 'slug' => 'billing');
         /*
          * This condition checks if the site has configured some payment
          * gateways - if not then users cannot sign up for a membership.
          * Show a notice if no payment gateway is configured/activated.
          */
         $gateways = MS_Model_Gateway::get_gateways(true);
         $payment_possible = false;
         foreach ($gateways as $key => $gateway) {
             if ('free' == $key) {
                 continue;
             }
             $payment_possible = true;
             break;
         }
         if (!$payment_possible) {
             lib3()->ui->admin_message(sprintf(__('Oops, looks like you did not activate a payment gateway yet.<br />You need to set up and activate at least one gateway, otherwise your members cannot sign up to a paid membership.<br />%sFix this now &raquo;%s', 'membership2'), '<a href="' . self::get_admin_url('settings', array('tab' => MS_Controller_Settings::TAB_PAYMENT)) . '">', '</a>'), 'err');
         }
     }
     return $pages;
 }
 /**
  * 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;
 }
    /**
     * Displays the edit form for all payment gateways.
     *
     * @since  1.0.0
     */
    protected function gateway_settings()
    {
        $gateways = MS_Model_Gateway::get_gateways();
        $groups = array();
        foreach ($gateways as $gateway) {
            $group = $gateway->group;
            if (empty($group)) {
                continue;
            }
            $groups[$group] = lib2()->array->get($groups[$group]);
            $groups[$group][$gateway->id] = $gateway;
        }
        foreach ($groups as $label => $group) {
            ?>
		<div class="ms-gateway-group">
			<h4><?php 
            echo $label;
            ?>
</h4>

			<?php 
            foreach ($group as $gateway) {
                $this->gateway_item_settings($gateway);
            }
            ?>

		</div>
		<?php 
        }
    }
 /**
  * Show gateway purchase button.
  *
  * Related action hooks:
  * - ms_view_frontend_payment_purchase_button
  * - ms_view_shortcode_invoice_purchase_button
  *
  * @since  1.0.0
  */
 public function purchase_button($subscription, $invoice)
 {
     // Get only active gateways
     $gateways = MS_Model_Gateway::get_gateways(true);
     $data = array();
     $membership = $subscription->get_membership();
     $is_free = false;
     if ($membership->is_free()) {
         $is_free = true;
     } elseif (0 == $invoice->total) {
         $is_free = true;
     } elseif ($invoice->uses_trial) {
         $is_free = true;
     }
     // show gateway purchase button for every active gateway
     foreach ($gateways as $gateway) {
         $view = null;
         // Skip gateways that are not configured.
         if (!$gateway->is_configured()) {
             continue;
         }
         if (!$membership->can_use_gateway($gateway->id)) {
             continue;
         }
         $data['ms_relationship'] = $subscription;
         $data['gateway'] = $gateway;
         $data['step'] = MS_Controller_Frontend::STEP_PROCESS_PURCHASE;
         // Free membership, show only free gateway
         if ($is_free) {
             if (MS_Gateway_Free::ID !== $gateway->id) {
                 continue;
             }
         } elseif (MS_Gateway_Free::ID === $gateway->id) {
             continue;
         }
         $view_class = get_class($gateway) . '_View_Button';
         $view = MS_Factory::create($view_class);
         if (MS_Gateway_Authorize::ID == $gateway->id) {
             /**
              *  set additional step for authorize.net (gateway specific form)
              *  @todo change to use popup, instead of another step (like stripe)
              */
             $data['step'] = 'gateway_form';
         }
         if (!empty($view)) {
             $view = apply_filters('ms_gateway_view_button', $view, $gateway->id);
             $view->data = apply_filters('ms_gateway_view_button_data', $data, $gateway->id);
             $html = apply_filters('ms_controller_gateway_purchase_button_' . $gateway->id, $view->to_html(), $subscription, $this);
             echo $html;
         }
     }
 }
 /**
  * Returns field definitions to render the payment box for the specified
  * membership.
  *
  * @since  1.0.0
  *
  * @return array An array containing all field definitions.
  */
 private function get_fields()
 {
     global $wp_locale;
     $membership = $this->data['membership'];
     $action = MS_Controller_Membership::AJAX_ACTION_UPDATE_MEMBERSHIP;
     $nonce = wp_create_nonce($action);
     $fields = array();
     $fields['price'] = array('id' => 'price', 'title' => __('Payment Amount', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'before' => MS_Plugin::instance()->settings->currency_symbol, 'value' => $membership->price, 'class' => 'ms-text-smallish', 'config' => array('step' => 'any', 'min' => 0), 'placeholder' => '0' . $wp_locale->number_format['decimal_point'] . '00', 'ajax_data' => array(1));
     $fields['payment_type'] = array('id' => 'payment_type', 'title' => __('This Membership requires', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $membership->payment_type, 'field_options' => MS_Model_Membership::get_payment_types(), 'ajax_data' => array(1));
     $fields['period_unit'] = array('id' => 'period_unit', 'title' => __('Grant access for', MS_TEXT_DOMAIN), 'name' => '[period][period_unit]', 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'value' => $membership->period_unit, 'class' => 'ms-text-small', 'config' => array('step' => 1, 'min' => 1), 'placeholder' => '1', 'ajax_data' => array(1));
     $fields['period_type'] = array('id' => 'period_type', 'name' => '[period][period_type]', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $membership->period_type, 'field_options' => MS_Helper_Period::get_period_types('plural'), 'ajax_data' => array(1));
     $fields['pay_cycle_period_unit'] = array('id' => 'pay_cycle_period_unit', 'title' => __('Payment Frequency', MS_TEXT_DOMAIN), 'name' => '[pay_cycle_period][period_unit]', 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'value' => $membership->pay_cycle_period_unit, 'class' => 'ms-text-small', 'config' => array('step' => 1, 'min' => 1), 'placeholder' => '1', 'ajax_data' => array(1));
     $fields['pay_cycle_period_type'] = array('id' => 'pay_cycle_period_type', 'name' => '[pay_cycle_period][period_type]', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $membership->pay_cycle_period_type, 'field_options' => MS_Helper_Period::get_period_types('plural'), 'ajax_data' => array(1));
     $fields['pay_cycle_repetitions'] = array('id' => 'pay_cycle_repetitions', 'title' => __('Total Payments', MS_TEXT_DOMAIN), 'name' => '[pay_cycle_repetitions]', 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'after' => __('payments (0 = unlimited)', MS_TEXT_DOMAIN), 'value' => $membership->pay_cycle_repetitions, 'class' => 'ms-text-small', 'config' => array('step' => '1', 'min' => 0), 'placeholder' => '0', 'ajax_data' => array(1));
     $fields['period_date_start'] = array('id' => 'period_date_start', 'title' => __('Grant access from', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_DATEPICKER, 'value' => $membership->period_date_start, 'placeholder' => __('Start Date...', MS_TEXT_DOMAIN), 'ajax_data' => array(1));
     $fields['period_date_end'] = array('id' => 'period_date_end', 'type' => MS_Helper_Html::INPUT_TYPE_DATEPICKER, 'value' => $membership->period_date_end, 'before' => _x('to', 'date range', MS_TEXT_DOMAIN), 'placeholder' => __('End Date...', MS_TEXT_DOMAIN), 'ajax_data' => array(1));
     $fields['on_end_membership_id'] = array('id' => 'on_end_membership_id', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'title' => __('After this membership ends', MS_TEXT_DOMAIN), 'value' => $membership->on_end_membership_id, 'field_options' => $membership->get_after_ms_ends_options(), 'ajax_data' => array(1));
     $fields['enable_trial_addon'] = array('id' => 'enable_trial_addon', 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON, 'value' => __('Yes, enable Trial Memberships!', MS_TEXT_DOMAIN), 'button_value' => 1, 'ajax_data' => array('action' => MS_Controller_Addon::AJAX_ACTION_TOGGLE_ADDON, '_wpnonce' => wp_create_nonce(MS_Controller_Addon::AJAX_ACTION_TOGGLE_ADDON), 'addon' => MS_Model_Addon::ADDON_TRIAL, 'field' => 'active'));
     $fields['trial_period_enabled'] = array('id' => 'trial_period_enabled', 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER, 'title' => '<strong>' . __('Trial Period', MS_TEXT_DOMAIN) . '</strong>', 'after' => __('Offer Free Trial', MS_TEXT_DOMAIN), 'value' => $membership->trial_period_enabled, 'ajax_data' => array(1));
     $fields['trial_period_unit'] = array('id' => 'trial_period_unit', 'name' => '[trial_period][period_unit]', 'before' => __('The Trial is free and lasts for', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_NUMBER, 'value' => $membership->trial_period_unit, 'class' => 'ms-text-small', 'config' => array('step' => 1, 'min' => 1), 'placeholder' => '1', 'ajax_data' => array(1));
     $fields['trial_period_type'] = array('id' => 'trial_period_type', 'name' => '[trial_period][period_type]', 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $membership->trial_period_type, 'field_options' => MS_Helper_Period::get_period_types('plural'), 'ajax_data' => array(1));
     $fields['membership_id'] = array('id' => 'membership_id', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership->id);
     $fields['action'] = array('id' => 'action', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $action);
     // Get a list of all payment gateways.
     $gateways = MS_Model_Gateway::get_gateways();
     $fields['gateways'] = array();
     foreach ($gateways as $gateway) {
         if ('free' == $gateway->id) {
             continue;
         }
         if (!$gateway->active) {
             continue;
         }
         $payment_types = $gateway->supported_payment_types();
         $wrapper_class = 'ms-payment-type-' . implode(' ms-payment-type-', array_keys($payment_types));
         $fields['gateways'][$gateway->id] = array('id' => 'disabled-gateway-' . $gateway->id, 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER, 'title' => $gateway->name, 'before' => __('Available', MS_TEXT_DOMAIN), 'after' => __('Not available', MS_TEXT_DOMAIN), 'value' => !$membership->can_use_gateway($gateway->id), 'class' => 'reverse', 'wrapper_class' => 'ms-payment-type-wrapper ' . $wrapper_class, 'ajax_data' => array('field' => 'disabled_gateways[' . $gateway->id . ']', '_wpnonce' => $nonce, 'action' => $action, 'membership_id' => $membership->id));
     }
     // Modify some fields for free memberships.
     if ($membership->is_free) {
         $fields['price'] = '';
         $fields['payment_type'] = array('id' => 'payment_type', 'title' => __('Access Structure:', MS_TEXT_DOMAIN), 'type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'value' => $membership->payment_type, 'field_options' => MS_Model_Membership::get_payment_types('free'), 'ajax_data' => array(1));
     }
     // Process the fields and add missing default attributes.
     foreach ($fields as $key => $field) {
         if (!empty($field['ajax_data'])) {
             if (!empty($field['ajax_data']['action'])) {
                 continue;
             }
             if (!isset($fields[$key]['ajax_data']['field'])) {
                 $fields[$key]['ajax_data']['field'] = $fields[$key]['id'];
             }
             $fields[$key]['ajax_data']['_wpnonce'] = $nonce;
             $fields[$key]['ajax_data']['action'] = $action;
             $fields[$key]['ajax_data']['membership_id'] = $membership->id;
         }
     }
     return apply_filters('ms_view_membership_tab_payment_fields', $fields);
 }