/**
  * Check pages for the presence of Membership special pages.
  *
  * Related Action Hooks:
  * - template_redirect
  *
  * @since  1.0.0
  */
 public function check_for_membership_pages()
 {
     global $post, $wp_query;
     // For invoice page purchase process
     $fields = array('gateway', 'ms_relationship_id', 'step');
     if (!empty($post) && isset($post->post_type) && $post->post_type == MS_Model_Invoice::get_post_type() && self::validate_required($fields) && self::STEP_PROCESS_PURCHASE == $_POST['step']) {
         do_action('ms_controller_frontend_signup_process_purchase', $this);
     }
     $the_page = MS_Model_Pages::current_page();
     if ($the_page) {
         // Fix the main query flags for best theme support:
         // Our Membership-Pages are always single pages...
         $wp_query->is_single = false;
         $wp_query->is_page = true;
         $wp_query->is_singular = true;
         $wp_query->is_home = false;
         $wp_query->is_frontpage = false;
         $wp_query->tax_query = null;
         $the_type = MS_Model_Pages::get_page_type($the_page);
         switch ($the_type) {
             case MS_Model_Pages::MS_PAGE_MEMBERSHIPS:
                 if (!MS_Model_Member::is_logged_in()) {
                     wp_safe_redirect(MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER));
                     exit;
                 }
                 if (MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL == $this->get_action()) {
                     $this->membership_cancel();
                 } else {
                     $this->signup_process();
                 }
                 break;
             case MS_Model_Pages::MS_PAGE_REGISTER:
                 if (MS_Model_Member::is_logged_in()) {
                     wp_safe_redirect(MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_MEMBERSHIPS));
                     exit;
                 }
                 if (MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL == $this->get_action()) {
                     $this->membership_cancel();
                 } else {
                     $this->signup_process();
                 }
                 break;
             case MS_Model_Pages::MS_PAGE_ACCOUNT:
                 $this->user_account_manager();
                 break;
             case MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT:
                 // Set up the protection shortcode.
                 $scode = MS_Plugin::instance()->controller->controllers['membership_shortcode'];
                 $scode->page_is_protected();
                 break;
             case MS_Model_Pages::MS_PAGE_REG_COMPLETE:
                 // Do nothing...
                 break;
             default:
                 // Do nothing...
                 break;
         }
     }
 }
 /**
  * Returns an array with access-information on the current page/user
  *
  * @since  1.0.0
  *
  * @return array {
  *     Access information
  *
  *     @type bool $has_access If the current user can view the current page.
  *     @type array $memberships List of active membership-IDs the user has
  *         registered to.
  * }
  */
 public function get_access_info()
 {
     static $Info = null;
     if (null === $Info) {
         $Info = array('has_access' => null, 'is_admin' => false, 'memberships' => array(), 'url' => MS_Helper_Utility::get_current_url());
         // The ID of the main system membership.
         $base_id = MS_Model_Membership::get_base()->id;
         $simulation = $this->member->is_simulated_user() || isset($_GET['explain']) && 'access' == $_GET['explain'];
         if ($simulation) {
             $Info['reason'] = array();
         }
         if ($this->member->is_normal_admin()) {
             // Admins have access to ALL memberships.
             $Info['is_admin'] = true;
             $Info['has_access'] = true;
             if ($simulation) {
                 $Info['reason'][] = __('Allow: Admin-User always has access', MS_TEXT_DOMAIN);
             }
             $memberships = MS_Model_Membership::get_memberships();
             foreach ($memberships as $membership) {
                 $Info['memberships'][] = $membership->id;
             }
         } else {
             /*
              * A non-admin visitor is only guaranteed access to special
              * Membership2 pages:
              * Registration, Login, etc.
              */
             $ms_page = MS_Model_Pages::current_page();
             if ($ms_page) {
                 $Info['has_access'] = true;
                 if ($simulation) {
                     $Info['reason'][] = __('Allow: This is a Membership Page', MS_TEXT_DOMAIN);
                 }
             }
             // Build a list of memberships the user belongs to and check permission.
             foreach ($this->member->subscriptions as $subscription) {
                 // Verify status of the membership.
                 // Only active, trial or canceled (until it expires) status memberships.
                 if (!$this->member->has_membership($subscription->membership_id)) {
                     if ($simulation) {
                         $Info['reason'][] = sprintf(__('Skipped: Not a member of "%s"', MS_TEXT_DOMAIN), $subscription->get_membership()->name);
                     }
                     continue;
                 }
                 if ($base_id !== $subscription->membership_id) {
                     $Info['memberships'][] = $subscription->membership_id;
                 }
                 // If permission is not clear yet then check current membership...
                 if (true !== $Info['has_access']) {
                     $membership = $subscription->get_membership();
                     $access = $membership->has_access_to_current_page();
                     if (null === $access) {
                         if ($simulation) {
                             $Info['reason'][] = sprintf(__('Ignored: Membership "%s"', MS_TEXT_DOMAIN), $membership->name);
                             $Info['reason'][] = $membership->_access_reason;
                         }
                         continue;
                     }
                     if ($simulation) {
                         $Info['reason'][] = sprintf(__('%s: Membership "%s"', MS_TEXT_DOMAIN), $access ? __('Allow', MS_TEXT_DOMAIN) : __('Deny', MS_TEXT_DOMAIN), $membership->name);
                         $Info['deciding_membership'] = $membership->id;
                         if ($access) {
                             $Info['deciding_rule'] = $membership->_allow_rule;
                         } else {
                             $Info['deciding_rule'] = $membership->_deny_rule;
                         }
                         $Info['reason'][] = $membership->_access_reason;
                     }
                     $Info['has_access'] = $access;
                 }
             }
             if (null === $Info['has_access']) {
                 $Info['has_access'] = true;
                 if ($simulation) {
                     $Info['reason'][] = __('Allow: Page is not protected', MS_TEXT_DOMAIN);
                 }
             }
             // "membership-id: 0" means: User does not belong to any membership.
             if (!count($Info['memberships'])) {
                 $Info['memberships'][] = 0;
             }
         }
         $Info = apply_filters('ms_model_plugin_get_access_info', $Info);
         if ($simulation) {
             $access = lib2()->session->get_clear('ms-access');
             lib2()->session->add('ms-access', $Info);
             for ($i = 0; $i < 9; $i += 1) {
                 if (isset($access[$i])) {
                     lib2()->session->add('ms-access', $access[$i]);
                 }
             }
             if (WP_DEBUG && isset($_GET['explain']) && 'access' == $_GET['explain']) {
                 echo '<style>code{background:#EEE;background:rgba(0,0,0,0.1);padding:1px 4px;}</style>';
                 echo '<h3>Note</h3>';
                 echo '<p>To disable the URL param <code>?explain=access</code> you have to set <code>WP_DEBUG</code> to false.</p>';
                 echo '<hr><h3>Recent Access checks</h3>';
                 lib2()->debug->stacktrace_off();
                 foreach ($access as $item) {
                     printf('<a href="%1$s">%1$s</a>: <strong>%2$s</strong>', $item['url'], $item['has_access'] ? __('Allow', MS_TEXT_DOMAIN) : __('Deny', MS_TEXT_DOMAIN));
                     // Intended debug output, leave it here.
                     lib2()->debug->dump($item);
                 }
                 wp_die('');
             }
         }
     }
     return $Info;
 }