/**
  * Process membership pages requests
  *
  * Verifies GET and POST requests to manage memberships.
  * Redirect to next step after processing.
  *
  * @since  1.0.0
  */
 public function process_admin_page()
 {
     $msg = 0;
     $next_step = null;
     $step = $this->get_step();
     $goto_url = null;
     $membership = $this->load_membership();
     $membership_id = $membership->id;
     $completed = false;
     $is_wizard = MS_Plugin::is_wizard();
     $save_data = array();
     // Check if user came from WPMU Dashboard plugin
     if (!$is_wizard && isset($_SERVER['HTTP_REFERER'])) {
         $referer = $_SERVER['HTTP_REFERER'];
         $params = parse_url($referer, PHP_URL_QUERY);
         $fields = array();
         parse_str($params, $fields);
         if (isset($fields['page']) && 'wpmudev-plugins' == $fields['page']) {
             $url = MS_Controller_Plugin::get_admin_url('settings');
             wp_safe_redirect($url);
             exit;
         }
     }
     // MS_Controller_Rule is executed using this action.
     do_action('ms_controller_membership_admin_page_process_' . $step);
     // Only accessible to admin users
     if (!$this->is_admin_user()) {
         return false;
     }
     if ($this->verify_nonce(null, 'any')) {
         // Take next actions based in current step.
         // Check if we are editing a membership.
         $save_membership = $this->verify_nonce(self::ACTION_SAVE);
         if ($save_membership) {
             // Save the current Setup-Process.
             $save_data = $_POST;
             unset($save_data['_wpnonce']);
             unset($save_data['action']);
             if (isset($_POST['set_private_flag'])) {
                 lib3()->array->equip_post('public');
                 $save_data['public'] = !lib3()->is_true($_POST['public']);
             }
             if (isset($_POST['set_paid_flag'])) {
                 lib3()->array->equip_post('paid');
                 $save_data['is_free'] = !lib3()->is_true($_POST['paid']);
             }
             $msg = $this->save_membership($save_data);
             // Refresh the $membership variable.
             $membership = $this->load_membership();
             $membership_id = $membership->id;
         }
         switch ($step) {
             case self::STEP_MS_LIST:
                 // Display a list of all memberships
                 $fields = array('action', 'membership_id');
                 if (self::validate_required($fields, 'GET')) {
                     $msg = $this->membership_list_do_action($_GET['action'], array(absint($_GET['membership_id'])));
                     $next_step = self::STEP_MS_LIST;
                 }
                 break;
             case self::STEP_ADD_NEW:
                 // Create Membership: Select the Membership-Type
                 $paid = isset($_POST['set_paid_flag']) && isset($_POST['paid']) && lib3()->is_true($_POST['paid']);
                 if ($paid) {
                     $next_step = self::STEP_PAYMENT;
                 } else {
                     $next_step = self::STEP_MS_LIST;
                 }
                 $msg = $this->mark_setup_completed();
                 $completed = true;
                 break;
             case self::STEP_PAYMENT:
                 // Setup payment options
                 $next_step = self::STEP_MS_LIST;
                 break;
             case self::STEP_EDIT:
                 $this->process_edit_page();
                 break;
         }
         if (!empty($next_step)) {
             $args = array('step' => $next_step, 'membership_id' => $membership_id);
             if (!empty($msg)) {
                 $args['msg'] = $msg;
             }
             /*
              * Param 'MENU_SLUG' forces the admin-URL to use the top-level
              * menu slug instead of the current base_slug.
              * During Setup-wizard the base_slug ends in '-setup', but when
              * we reach this point the wizard is completed.
              */
             $slug = 'MENU_SLUG';
             $goto_url = esc_url_raw(add_query_arg($args, MS_Controller_Plugin::get_admin_url($slug)));
             $goto_url = apply_filters('ms_controller_membership_membership_admin_page_process_goto_url', $goto_url, $next_step);
             if ($completed) {
                 MS_Plugin::flush_rewrite_rules($goto_url);
             } else {
                 wp_safe_redirect($goto_url);
             }
             exit;
         }
     } else {
         // No action request found.
     }
 }
 /**
  * Upgrade database.
  *
  * @since  1.0.0
  * @param  bool $force Also execute update logic when version did not change.
  */
 public static function update($force = false)
 {
     static $Done = false;
     if ($Done && !$force) {
         return;
     }
     // Migration handler has its own valid_user check.
     self::check_migration_handler();
     // Updates are only triggered from Admin-Side by an Admin user.
     if (!self::valid_user()) {
         return;
     }
     // Check for correct network-wide protection setup.
     self::check_settings();
     $settings = MS_Factory::load('MS_Model_Settings');
     $old_version = $settings->version;
     // Old: The version in DB.
     $new_version = MS_Plugin::instance()->version;
     // New: Version in file.
     $is_new_setup = empty($old_version);
     // Compare current src version to DB version:
     // We only do UP-grades but no DOWN-grades!
     if ($old_version) {
         $version_changed = version_compare($old_version, $new_version, 'lt');
     } else {
         $version_changed = true;
     }
     if ($force || $version_changed) {
         $Done = true;
         $msg = array();
         /*
          * ----- General update logic, executed on every update ------------
          */
         do_action('ms_model_upgrade_before_update', $settings, $old_version, $new_version, $force);
         // Prepare the Update message.
         if (!$version_changed) {
             $msg[] = sprintf(__('<strong>Membership 2</strong> is set up for version %1$s!', 'membership2'), $new_version);
         } else {
             $msg[] = sprintf(__('<strong>Membership 2</strong> was updated to version %1$s!', 'membership2'), $new_version);
         }
         // Every time the plugin is updated we clear the cache.
         MS_Factory::clear();
         // Create missing Membership pages.
         $new_pages = MS_Model_Pages::create_missing_pages();
         if (!empty($new_pages)) {
             $msg[] = sprintf(__('New Membership pages created: "%1$s".', 'membership2'), implode('", "', $new_pages));
         }
         // Remove an old version of Protected Content
         // TODO: REMOVE THIS BLOCK/FUNCTION END OF 2015
         if ($version_changed) {
             self::remove_old_copy();
         }
         // Note: We do not create menu items on upgrade! Users might have
         // intentionally removed the items from the menu...
         /*
          * ----- Version-Specific update logic -----------------------------
          */
         // Upgrade from a 1.0.0.x version to 1.0.1.0 or higher
         if (version_compare($old_version, '1.0.1.0', 'lt')) {
             self::_upgrade_1_0_1_0();
         }
         // Upgrade from 1.0.1.0 version to 1.0.1.1 or higher
         if (version_compare($old_version, '1.0.1.1', 'lt')) {
             self::_upgrade_1_0_1_1();
         }
         // Upgrade from 1.0.1.x version to 1.0.2.0 or higher
         if (version_compare($old_version, '1.0.2.0', 'lt')) {
             self::_upgrade_1_0_2_0();
         }
         // Upgrade from 1.0.2.x version to 1.0.2.4 or higher
         if (version_compare($old_version, '1.0.2.4', 'lt')) {
             self::_upgrade_1_0_2_4();
         }
         /*
          * ----- General update logic, executed on every update ------------
          */
         $settings->version = $new_version;
         $settings->save();
         // Display a message after the page is reloaded.
         if (!$is_new_setup) {
             lib3()->ui->admin_message(implode('<br>', $msg), '', '', 'ms-update');
         }
         do_action('ms_model_upgrade_after_update', $settings, $old_version, $new_version, $force);
         $addons = MS_Factory::load('MS_Model_Addon');
         $addons->flush_list();
         // This will reload the current page.
         MS_Plugin::flush_rewrite_rules();
     }
 }