/**
  * Manages settings actions.
  *
  * Verifies GET and POST requests to manage settings.
  *
  * @since  1.0.0
  */
 public function admin_settings_manager()
 {
     MS_Helper_Settings::print_admin_message();
     $this->get_active_tab();
     $msg = 0;
     $redirect = false;
     if ($this->is_admin_user()) {
         if ($this->verify_nonce() || $this->verify_nonce(null, 'GET')) {
             /**
              * After verifying permissions those filters can be used by Add-ons
              * to process their own settings form.
              *
              * @since  1.0.1.0
              */
             do_action('ms_admin_settings_manager-' . $this->active_tab);
             do_action('ms_admin_settings_manager', $this->active_tab);
             switch ($this->active_tab) {
                 case self::TAB_GENERAL:
                     lib3()->array->equip_request('action', 'network_site');
                     $action = $_REQUEST['action'];
                     $redirect = esc_url_raw(remove_query_arg(array('msg' => $msg)));
                     // See if we change settings for the network-wide mode.
                     if (MS_Plugin::is_network_wide()) {
                         $new_site_id = intval($_REQUEST['network_site']);
                         if ('network_site' == $action && !empty($new_site_id)) {
                             $old_site_id = MS_Model_Pages::get_setting('site_id');
                             if ($old_site_id != $new_site_id) {
                                 MS_Model_Pages::set_setting('site_id', $new_site_id);
                                 $msg = MS_Helper_Settings::SETTINGS_MSG_SITE_UPDATED;
                                 $redirect = esc_url_raw(add_query_arg(array('msg' => $msg)));
                             }
                         }
                     }
                     break;
                 case self::TAB_IMPORT:
                     $tool = MS_Factory::create('MS_Controller_Import');
                     // Output is passed to the view via self::_message()
                     $tool->process();
                     break;
                 case self::TAB_PAYMENT:
                 case self::TAB_MESSAGES:
                     break;
                 default:
                     break;
             }
         }
     }
     if ($redirect) {
         wp_safe_redirect($redirect);
         exit;
     }
 }
 /**
  * Prepare the HTML fields that can be displayed
  *
  * @since  1.0.0
  *
  * @return array
  */
 protected function prepare_fields()
 {
     // Prepare the return value.
     $nav = array();
     $pages = array();
     MS_Model_Pages::create_missing_pages();
     $page_types = MS_Model_Pages::get_page_types();
     $page_types_menu = array('memberships', 'register', 'account');
     $page_types_rest = array_diff($page_types, $page_types_menu);
     // Prepare NAV fields.
     $menu_action = MS_Controller_Pages::AJAX_ACTION_TOGGLE_MENU;
     $menu_nonce = wp_create_nonce($menu_action);
     foreach ($page_types_menu as $type) {
         $nav_exists = MS_Model_Pages::has_menu($type);
         $nav[$type] = array('type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER, 'id' => 'nav_' . $type, 'value' => $nav_exists, 'title' => $page_types[$type], 'ajax_data' => array('action' => $menu_action, 'item' => $type, '_wpnonce' => $menu_nonce));
     }
     $nav['sep'] = array('type' => MS_Helper_Html::TYPE_HTML_SEPARATOR);
     // Prepare PAGES fields.
     $pages_action = MS_Controller_Pages::AJAX_ACTION_UPDATE_PAGES;
     $pages_nonce = wp_create_nonce($pages_action);
     foreach ($page_types as $type => $label) {
         $page_id = MS_Model_Pages::get_setting($type);
         $title = sprintf('<strong>%1$s</strong><span class="lbl-details">: %2$s</span>', $label, MS_Model_Pages::get_description($type));
         $pages[$type] = array('id' => $type, 'type' => MS_Helper_Html::INPUT_TYPE_WP_PAGES, 'title' => $title, 'value' => $page_id, 'field_options' => array('no_item' => __('- Select a page -', MS_TEXT_DOMAIN)), 'ajax_data' => array('field' => $type, 'action' => $pages_action, '_wpnonce' => $pages_nonce));
     }
     $fields = array('nav' => $nav, 'pages' => $pages);
     return apply_filters('ms_view_settings_page_setup_prepare_fields', $fields, $this);
 }
 /**
  * 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;
 }
 /**
  * Exclude the special Membership2 pages from the results as they
  * cannot be protected.
  *
  * @since  1.0.0
  * @param  array $excluded
  * @param  array $args
  * @return array
  */
 public function exclude_items($excluded, $args)
 {
     static $Page_Ids = null;
     if (null === $Page_Ids) {
         $Page_Ids = array();
         $types = MS_Model_Pages::get_page_types();
         foreach ($types as $type => $title) {
             $Page_Ids[] = MS_Model_Pages::get_setting($type);
         }
     }
     return array_merge($excluded, $Page_Ids);
 }