/**
  * Prepare list items.
  *
  * @since  1.0.0
  */
 public function prepare_items()
 {
     $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns());
     $per_page = apply_filters('ms_helper_listtable_member_items_per_page', self::DEFAULT_PAGE_SIZE);
     $current_page = $this->get_pagenum();
     $args = array('number' => $per_page, 'offset' => ($current_page - 1) * $per_page);
     // Prepare the filter details.
     $args = $this->prepare_query_args($args);
     $total_items = MS_Model_Member::get_members_count($args);
     $this->items = MS_Model_Member::get_members($args);
     $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page));
     do_action('ms_helper_listtable_member_prepare_items', $args, $this);
 }
 /**
  * Main entry point: Handles the export action.
  *
  * This task will exit the current request as the result will be a download
  * and no HTML page that is displayed.
  *
  * @since  1.0.0
  */
 public function process()
 {
     $data = (object) array();
     $data->source_key = self::KEY;
     $data->source = 'Membership2';
     $data->plugin_version = MS_PLUGIN_VERSION;
     $data->export_time = date('Y-m-d H:i');
     $data->notes = array(__('Exported data:', MS_TEXT_DOMAIN), __('- Memberships (without protection rules)', MS_TEXT_DOMAIN), __('- Members (including Stripe/Authorize payment settings)', MS_TEXT_DOMAIN), __('- Subscriptions (link between Members and Memberships)', MS_TEXT_DOMAIN), __('- Invoices', MS_TEXT_DOMAIN));
     $data->memberships = array();
     // Export the base membership (i.e. the Membership2 settings)
     $membership = MS_Model_Membership::get_base();
     $data->memberships[] = $this->export_membership($membership->id);
     // Export all memberships.
     $memberships = MS_Model_Membership::get_memberships(array('post_parent' => 0));
     foreach ($memberships as $membership) {
         $data->memberships[] = $this->export_membership($membership->id);
     }
     // Export the members.
     $members = MS_Model_Member::get_members();
     $data->members = array();
     foreach ($members as $member) {
         if (!$member->is_member) {
             continue;
         }
         $data->members[] = $this->export_member($member->id);
     }
     // Export plugin settings.
     $obj = array();
     $data->settings = $this->export_settings();
     // Export Coupons.
     $data->coupons = array();
     lib2()->net->file_download(json_encode($data), 'membership2-export.json');
 }
 /**
  * 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;
 }