/**
  * 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();
 }
 /**
  * Process purchase using gateway.
  *
  * Related Action Hooks:
  * - ms_controller_frontend_signup_process_purchase
  *
  * @since  1.0.0
  */
 public function process_purchase()
 {
     $fields = array('gateway', 'ms_relationship_id');
     lib2()->array->equip_request('gateway', 'ms_relationship_id');
     $valid = true;
     $nonce_name = $_REQUEST['gateway'] . '_' . $_REQUEST['ms_relationship_id'];
     if (!self::validate_required($fields, 'any')) {
         $valid = false;
         $err = 'GAT-01 (invalid fields)';
     } elseif (!MS_Model_Gateway::is_valid_gateway($_REQUEST['gateway'])) {
         $valid = false;
         $err = 'GAT-02 (invalid gateway)';
     } elseif (!$this->verify_nonce($nonce_name, 'any')) {
         $valid = false;
         $err = 'GAT-03 (invalid nonce)';
     }
     if ($valid) {
         $subscription = MS_Factory::load('MS_Model_Relationship', $_REQUEST['ms_relationship_id']);
         $gateway_id = $_REQUEST['gateway'];
         $gateway = MS_Model_Gateway::factory($gateway_id);
         try {
             $invoice = $gateway->process_purchase($subscription);
             // If invoice is successfully paid, redirect to welcome page.
             if ($invoice->is_paid() || $invoice->uses_trial && MS_Model_Invoice::STATUS_BILLED == $invoice->status) {
                 // Make sure to respect the single-membership rule
                 $this->validate_membership_states($subscription);
                 // Redirect user to the Payment-Completed page.
                 if (!defined('IS_UNIT_TEST')) {
                     MS_Model_Pages::redirect_to(MS_Model_Pages::MS_PAGE_REG_COMPLETE, array('ms_relationship_id' => $subscription->id));
                 }
             } elseif (MS_Gateway_Manual::ID == $gateway_id) {
                 // For manual gateway payments.
                 $this->add_action('the_content', 'purchase_info_content');
             } else {
                 // Something went wrong, the payment was not successful.
                 $this->add_action('the_content', 'purchase_error_content');
             }
         } catch (Exception $e) {
             MS_Helper_Debug::log($e->getMessage());
             switch ($gateway_id) {
                 case MS_Gateway_Authorize::ID:
                     $_POST['auth_error'] = $e->getMessage();
                     // call action to step back
                     do_action('ms_controller_frontend_signup_gateway_form');
                     break;
                 case MS_Gateway_Stripe::ID:
                     $_POST['error'] = sprintf(__('Error: %s', MS_TEXT_DOMAIN), $e->getMessage());
                     // Hack to send the error message back to the payment_table.
                     MS_Plugin::instance()->controller->controllers['frontend']->add_action('the_content', 'payment_table', 1);
                     break;
                 default:
                     do_action('ms_controller_gateway_form_error', $e);
                     $this->add_action('the_content', 'purchase_error_content');
                     break;
             }
         }
     } else {
         MS_Helper_Debug::log('Error Code ' . $err);
         $this->add_action('the_content', 'purchase_error_content');
     }
     // Hack to show signup page in case of errors
     $ms_page = MS_Model_Pages::get_page(MS_Model_Pages::MS_PAGE_REGISTER);
     if ($ms_page) {
         // During unit-testing the $ms_page object might be empty.
         global $wp_query;
         $wp_query->query_vars['page_id'] = $ms_page->ID;
         $wp_query->query_vars['post_type'] = 'page';
     }
     do_action('ms_controller_gateway_process_purchase_after', $this);
 }