/**
  * Initialize the rule object.
  *
  * @since  4.6
  */
 protected function init()
 {
     global $wpdb;
     $this->filename = basename(__FILE__);
     // 'membership_lvl' rule.
     $this->add_rule('membership_lvl', __('By Membership Level', PO_LANG), __('Shows the PopUp if the user has a certain Membership Level.', PO_LANG), '', 25);
     // 'membership_sub' rule.
     $this->add_rule('membership_sub', __('By Membership Subscription', PO_LANG), __('Shows the PopUp if the user does not have a certain Membership Level.', PO_LANG), 'membership', 25);
     // -- Initialize rule.
     $this->is_active = function_exists('M_get_membership_active') && M_get_membership_active();
     if (!$this->is_active) {
         return;
     }
     $prefix = $wpdb->get_blog_prefix();
     $table_lvl = defined('MEMBERSHIP_TABLE_LEVELS') ? MEMBERSHIP_TABLE_LEVELS : $prefix . 'm_membership_levels';
     $sql = "\r\n\t\t\tSELECT *\r\n\t\t\tFROM {$table_lvl}\r\n\t\t";
     $this->levels = $wpdb->get_results($sql);
     $table_sub = defined('MEMBERSHIP_TABLE_SUBSCRIPTIONS') ? MEMBERSHIP_TABLE_SUBSCRIPTIONS : $prefix . 'm_subscriptions';
     $sql = "\r\n\t\t\tSELECT *\r\n\t\t\tFROM {$table_sub}\r\n\t\t";
     $this->subscriptions = $wpdb->get_results($sql);
 }
 function dashboard_members()
 {
     global $page, $action;
     $plugin = get_plugin_data(membership_dir('membershippremium.php'));
     $membershipactive = M_get_membership_active();
     echo __('Membership protection ', 'membership');
     echo __(' is ', 'membership');
     // Membership active toggle
     if ($membershipactive == 'no') {
         echo '<strong>' . __('disabled', 'membership') . '</strong> <a id="enablemembership" href="' . wp_nonce_url("?page=" . $page . "&amp;action=activate", 'toggle-plugin') . '" title="' . __('Click here to enable the plugin', 'membership') . '">' . __('[Enable it]', 'membership') . '</a>';
     } else {
         echo '<strong>' . __('enabled', 'membership') . '</strong> <a id="enablemembership" href="' . wp_nonce_url("?page=" . $page . "&amp;action=deactivate", 'toggle-plugin') . '" title="' . __('Click here to enable the plugin', 'membership') . '">' . __('[Disable it]', 'membership') . '</a>';
     }
     echo '<br/><br/>';
     echo "<strong>" . __('Member breakdown', 'membership') . "</strong><br/>";
     $detail = $this->get_subscriptions_and_levels(array('sub_status' => 'active'));
     $subs = $this->get_subscriptions(array('sub_status' => 'active'));
     $levels = $this->get_membership_levels(array('level_id' => 'active'));
     echo "<table style='width: 100%;'>";
     echo "<tbody>";
     echo "<tr>";
     echo "<td style='width: 48%' valign='top'>";
     if ($levels) {
         $levelcount = 0;
         echo "<table style='width: 100%;'>";
         echo "<tbody>";
         echo "<tr>";
         echo "<td colspan='2'><strong>" . __('Levels', 'membership') . "</strong></td>";
         echo "</tr>";
         foreach ($levels as $key => $level) {
             echo "<tr>";
             echo "<td><a href='" . admin_url('admin.php?page=membershiplevels&action=edit&level_id=') . $level->id . "'>" . esc_html($level->level_title) . "</a></td>";
             // find out how many people are in this level
             $thiscount = $this->count_on_level($level->id);
             echo "<td style='text-align: right;'>" . (int) $thiscount . "</td>";
             $levelcount += (int) $thiscount;
             echo "</tr>";
         }
         echo "</tbody>";
         echo "</table>";
     }
     echo "</td>";
     echo "<td style='width: 48%' valign='top'>";
     if ($subs) {
         $subcount = 0;
         echo "<table style='width: 100%;'>";
         echo "<tbody>";
         echo "<tr>";
         echo "<td colspan='2'><strong>" . __('Subscriptions', 'membership') . "</strong></td>";
         echo "</tr>";
         foreach ($subs as $key => $sub) {
             echo "<tr>";
             echo "<td><a href='" . admin_url('admin.php?page=membershipsubs&action=edit&sub_id=') . $sub->id . "'>" . $sub->sub_name . "</a></td>";
             // find out how many people are in this sub
             $thiscount = $this->count_on_sub($sub->id);
             echo "<td style='text-align: right;'>" . (int) $thiscount . "</td>";
             $subcount += (int) $thiscount;
             echo "</tr>";
         }
         echo "</tbody>";
         echo "</table>";
     }
     echo "</td>";
     echo "</tr>";
     echo "</tbody>";
     echo "</table>";
     echo "<br/><strong>" . __('Member counts', 'membership') . "</strong><br/>";
     echo "<table style='width: 100%;'>";
     echo "<tbody>";
     echo "<tr>";
     echo "<td style='width: 48%' valign='top'>";
     echo "<table style='width: 100%;'>";
     echo "<tbody>";
     $usercount = $this->db->get_var("SELECT count(*) FROM {$this->db->users} INNER JOIN {$this->db->usermeta} ON {$this->db->users}.ID = {$this->db->usermeta}.user_id WHERE {$this->db->usermeta}.meta_key = '{$this->db->prefix}capabilities'");
     echo "<tr>";
     echo "<td>" . __('Total Members', 'membership') . "</td>";
     echo "<td style='text-align: right;'>" . $usercount . "</td>";
     echo "</tr>";
     $deactivecount = $this->db->get_var($this->db->prepare("SELECT count(*) FROM {$this->db->usermeta} WHERE meta_key = %s AND meta_value = %s", $this->db->prefix . 'membership_active', 'no'));
     echo "<tr>";
     echo "<td>" . __('Deactivated Members', 'membership') . "</td>";
     echo "<td style='text-align: right;'>" . $deactivecount . "</td>";
     echo "</tr>";
     echo "</tbody>";
     echo "</table>";
     echo "</td>";
     echo "<td style='width: 48%' valign='top'></td>";
     echo "</tr>";
     echo "</tbody>";
     echo "</table>";
 }
 function initialise_plugin()
 {
     global $user, $member, $M_options, $M_Rules, $wp_query, $wp_rewrite, $M_active, $bp;
     if (defined('MEMBERSHIP_GLOBAL_TABLES') && MEMBERSHIP_GLOBAL_TABLES === true) {
         if (function_exists('get_blog_option')) {
             $M_options = get_blog_option(MEMBERSHIP_GLOBAL_MAINSITE, 'membership_options', array());
         } else {
             $M_options = get_option('membership_options', array());
         }
     } else {
         $M_options = get_option('membership_options', array());
     }
     // Check if the membership plugin is active
     $M_active = M_get_membership_active();
     // Create our subscription page shortcode
     add_shortcode('subscriptionform', array(&$this, 'do_subscription_shortcode'));
     add_shortcode('accountform', array(&$this, 'do_account_shortcode'));
     add_shortcode('upgradeform', array(&$this, 'do_upgrade_shortcode'));
     add_shortcode('renewform', array(&$this, 'do_renew_shortcode'));
     // Moved extra shortcodes over to the main plugin for new registration forms
     add_shortcode('subscriptiontitle', array(&$this, 'do_subscriptiontitle_shortcode'));
     add_shortcode('subscriptiondetails', array(&$this, 'do_subscriptiondetails_shortcode'));
     add_shortcode('subscriptionprice', array(&$this, 'do_subscriptionprice_shortcode'));
     add_shortcode('subscriptionbutton', array(&$this, 'do_subscriptionbutton_shortcode'));
     do_action('membership_register_shortcodes');
     // Check if we are on a membership specific page
     add_filter('the_posts', array(&$this, 'check_for_membership_pages'), 1);
     // Check for subscription shortcodes - and if needed queue styles
     add_filter('the_posts', array(&$this, 'add_subscription_styles'));
     $user = wp_get_current_user();
     if (!method_exists($user, 'has_cap') || $user->has_cap('membershipadmin') || $M_active == 'no') {
         // Admins can see everything
         return;
     }
     if ($M_active == 'no') {
         // The plugin isn't active so just return
         return;
     }
     if (!method_exists($user, 'has_cap') || $user->has_cap('membershipadmin')) {
         // Admins can see everything - unless we have a cookie set to limit viewing
         if (empty($_COOKIE['membershipuselevel']) || $_COOKIE['membershipuselevel'] == '0') {
             return;
         }
     }
     // More tags
     if (isset($M_options['moretagdefault']) && $M_options['moretagdefault'] == 'no') {
         // More tag content is not visible by default - works for both web and rss content - unfortunately
         add_filter('the_content_more_link', array(&$this, 'show_moretag_protection'), 99, 2);
         add_filter('the_content', array(&$this, 'replace_moretag_content'), 1);
         add_filter('the_content_feed', array(&$this, 'replace_moretag_content'), 1);
     }
     // Shortcodes setup
     if (!empty($M_options['membershipshortcodes'])) {
         foreach ($M_options['membershipshortcodes'] as $key => $value) {
             if (!empty($value)) {
                 add_shortcode(stripslashes(trim($value)), array(&$this, 'do_membership_shortcode'));
             }
         }
         // Shortcodes now default to protected for those entered by the user (which will be none for new users / installs)
         $this->override_shortcodes();
     }
     // Downloads protection
     if (!empty($M_options['membershipdownloadgroups'])) {
         add_filter('the_content', array(&$this, 'protect_download_content'));
     }
     // Makes sure that despite other rules, the pages set in the options panel are available to the user
     add_action('pre_get_posts', array(&$this, 'ensure_option_pages_visible'), 999);
     // check for a no-access page and always filter it if needed
     if (!empty($M_options['nocontent_page']) && $M_options['nocontent_page'] != $M_options['registration_page']) {
         add_filter('get_pages', array(&$this, 'hide_nocontent_page_from_menu'), 99);
     }
     // New registration form settings
     if (isset($M_options['formtype']) && $M_options['formtype'] == 'new') {
         add_action('wp_ajax_nopriv_buynow', array(&$this, 'popover_signup_form'));
         //login and register are no-priv only because, well they aren't logged in or registered
         add_action('wp_ajax_nopriv_register_user', array(&$this, 'popover_register_process'));
         add_action('wp_ajax_nopriv_login_user', array(&$this, 'popover_login_process'));
         // if logged in:
         add_action('wp_ajax_buynow', array(&$this, 'popover_sendpayment_form'));
         add_action('wp_ajax_register_user', array(&$this, 'popover_register_process'));
         add_action('wp_ajax_login_user', array(&$this, 'popover_login_process'));
     }
 }
 function add_admin_bar_items()
 {
     global $M_options, $user;
     if (empty($user) || !method_exists($user, 'has_cap')) {
         $user = wp_get_current_user();
     }
     if (!method_exists($user, 'has_cap') || $user->has_cap('membershipadmin')) {
         $active = M_get_membership_active();
         if ($active == 'yes') {
             add_action('admin_bar_menu', array(&$this, 'add_admin_bar_view_site_as'), 8);
         } else {
             add_action('admin_bar_menu', array(&$this, 'add_admin_bar_enabled_item'), 8);
         }
     }
 }
    /**
     * Integration with the WPMU Dev Membership plugin:
     * If the plugin is installed and active this function returns a list of
     * all membership levels.
     *
     * If the plugin is not active the return value is boolean false.
     *
     * @since  2.0
     * @return bool|array
     */
    public function get_membership_levels()
    {
        $Result = null;
        if (null === $Result) {
            if (function_exists('M_get_membership_active') && 'no' != M_get_membership_active() && defined('MEMBERSHIP_TABLE_LEVELS')) {
                global $wpdb;
                $Result = $wpdb->get_results(sprintf('SELECT
							id, level_title, level_active
						FROM %s
						ORDER BY id', MEMBERSHIP_TABLE_LEVELS), ARRAY_A);
            } else {
                $Result = false;
            }
        }
        return $Result;
    }