/**
 * register widget : multiple instance
 * frontend instance
 *
 * @param array $args
 * @param array $widget_args
 * @return void
 * @since 1.0
 */
function mgm_sidebar_widget_registration($args, $widget_args = 1)
{
    global $wpdb, $user_ID, $current_user, $mgm_sidebar_widget;
    extract($args, EXTR_SKIP);
    if (is_numeric($widget_args)) {
        $widget_args = array('number' => $widget_args);
    }
    $widget_args = wp_parse_args($widget_args, array('number' => -1));
    extract($widget_args, EXTR_SKIP);
    // options init
    $options = $mgm_sidebar_widget->register_widget;
    // mgm_pr($options);
    // check
    if (!isset($options[$number])) {
        return;
    }
    //skip widget if BUDDYPRESS is loaded
    if (defined('BP_VERSION')) {
        return;
    }
    //skip registation page:
    if (in_array(trailingslashit(mgm_current_url()), array(trailingslashit(mgm_get_custom_url('register'))), trailingslashit(mgm_get_custom_url('register', true)))) {
        return;
    }
    // skip if on transactions page:
    foreach (mgm_get_payment_page_query_vars() as $query_var) {
        // set if
        if ($isset_query_var = mgm_get_query_var($query_var)) {
            return;
        }
    }
    // check
    if (isset($_GET['method']) && preg_match('/payment_/', $_GET['method'])) {
        return;
    }
    // set
    $title = isset($options[$number]['title']) ? $options[$number]['title'] : __('Magic Members - Register', 'mgm');
    $intro = isset($options[$number]['intro']) ? $options[$number]['intro'] : '';
    $use_custom_fields = isset($options[$number]['use_custom_fields']) ? $options[$number]['use_custom_fields'] : true;
    //Issue #777
    $default_subscription_pack = isset($options[$number]['default_subscription_pack']) ? $options[$number]['default_subscription_pack'] : false;
    // user looged in
    if (!$user_ID) {
        // if hide on custom register page
        $post_id = get_the_ID();
        // post custom register
        if ($post_id > 0) {
            // if match
            if (get_permalink($post_id) == mgm_get_custom_url('register')) {
                return "";
            }
        }
        // start actual widget
        echo $before_widget;
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        // echo $intro;
        echo mgm_sidebar_user_register_form($use_custom_fields, $default_subscription_pack);
        echo $after_widget;
    }
}
function mgm_posts_for_membership($membership_type = '')
{
    $posts = mgm_get_posts_for_level($membership_type, false);
    $membership_type = is_array($membership_type) ? $membership_type : array(0 => $membership_type);
    $levels = '';
    if (!empty($membership_type)) {
        $i = 0;
        $cnt = count($membership_type);
        foreach ($membership_type as $key => $lvl) {
            $sep = '';
            if ($i > 0 && $i == $cnt - 1) {
                $sep = ' and ';
            } elseif ($i > 0) {
                $sep = ', ';
            }
            $membership = mgm_get_class('membership_types');
            $levels .= $sep . '"' . mgm_stripslashes_deep($membership->get_type_name($lvl)) . '"';
            $i++;
        }
    }
    $html = '';
    $css_group = mgm_get_css_group();
    //issue #867
    if ($css_group != 'none') {
        //expand this if needed
        $css_link_format = '<link rel="stylesheet" href="%s" type="text/css" media="all" />';
        $css_file = MGM_ASSETS_URL . 'css/' . $css_group . '/mgm.pages.css';
        $html .= sprintf($css_link_format, $css_file);
    }
    $html .= '<div class="postbox mgm_margin10px0px">' . '<h3>' . sprintf(__('Accessible Contents For %s', 'mgm'), $levels) . '</h3>' . '<div class="inside">';
    $html .= '<div class="table width100">' . '<div class="row br_bottom">' . '<div class="cell th_div width25"> ' . __('Post Title', 'mgm') . '</b></div>' . '<div class="cell th_div width45"><b>' . __('Post Content', 'mgm') . '</b></div>' . '<div class="cell th_div width15"><b>' . __('Published', 'mgm') . '</b></div>' . '<div class="cell th_div width15"><b>' . __('Membership Type', 'mgm') . '</b></div>' . '</div>';
    if (isset($posts['total']) && $posts['total'] > 0) {
        $pattern = get_shortcode_regex();
        $snippet_length = 200;
        foreach ($posts['posts'] as $id => $obj) {
            // check purchaseble
            $published = date('jS F Y', strtotime($obj->post_date));
            $title = $obj->post_title;
            $content = $obj->post_content;
            if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
                $title = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($title);
                $content = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($content);
            }
            $content = preg_replace('/' . $pattern . '/s', '', $content);
            $content = substr(strip_tags($content), 0, $snippet_length);
            $content .= strlen($content) > $snippet_length ? '...' : '';
            $html .= '<div class="row br_bottom ' . ($alt = $alt == '' ? 'alternate' : '') . '">' . '<div class="cell width25"><a href="' . get_permalink($obj->ID) . '">' . $title . '</a></div>' . '<div class="cell width45">' . $content . '</div>' . '<div class="cell width15">' . $published . '</div>' . '<div class="cell width15">' . mgm_stripslashes_deep($obj->access_membership_type) . '</div>' . '</div>';
        }
    } else {
        $html .= '<div class="row br_bottom ' . ($alt = $alt == '' ? 'alternate' : '') . '">' . '<div class="cell">' . __('No posts available', 'mgm') . '</div>' . '</div>';
    }
    $html .= '</div>';
    if (isset($posts['total']) && $posts['total'] > 0) {
        $html .= '<div class="mgm_margin10px">';
        if (isset($_GET['show']) && $_GET['show'] == 'all') {
            $html .= '<div class="mgm_content_back_link_div">' . '<a href="' . add_query_arg(array('show' => 'paged'), mgm_current_url()) . '" class="button">' . __('Back', 'mgm') . '</a>' . '</div>';
        }
        $html .= '<div class="mgm_content_total_post_div">' . sprintf(__('Total Accessible Contents: %d', 'mgm'), $posts['total']) . '</div>';
        $html .= '<div class="mgm_content_total_publish_div">';
        if ((!isset($_GET['show']) || isset($_GET['show']) && $_GET['show'] == 'paged') && $posts['total'] > count($posts['posts'])) {
            $html .= '<a href="' . add_query_arg(array('show' => 'all'), mgm_current_url()) . '" class="button">' . __('See All', 'mgm') . '</a>';
        }
        $html .= '</div>';
        $html .= '<br/><div class="clearfix"></div>';
        $html .= '</div>';
    }
    $html .= '</div>' . '</div>';
    $html = apply_filters('mgm_posts_for_membership_html', $html);
    return $html;
}
/**
 * hide protected post/pages from apearing in list/feeds etc
 *
 * @param object $query
 * @return object $query
 * @since 1.0
 */
function mgm_hide_protected($query)
{
    global $post;
    // do not run when admin section loaded #1459
    if (is_admin()) {
        return $query;
    }
    // if loading from feed
    if (is_feed() && isset($_GET['token']) && mgm_use_rss_token()) {
        // get user by rss token, only for feed
        $user = mgm_get_user_by_token(strip_tags($_GET['token']));
    } else {
        // current user
        if (function_exists('wp_get_current_user')) {
            $user = wp_get_current_user();
        } else {
            global $user_ID;
            // pick
            $user = get_userdata($user_ID);
        }
    }
    // get system
    $system_obj = mgm_get_class('system');
    // flag
    $run_cat_notin = $run_term_notin = false;
    // user is not a spider
    if (!mgm_is_a_bot()) {
        // hide post
        $hide_posts = mgm_content_exclude_by_user($user->ID, 'post');
        // hide post
        // set filter
        if (is_array($hide_posts) && !empty($hide_posts)) {
            $query->set('post__not_in', array_unique($hide_posts));
            // set negation
        }
        // hide cats
        $hide_cats = mgm_content_exclude_by_user($user->ID, 'category');
        //hide cats
        // set filter
        if (is_array($hide_cats) && !empty($hide_cats)) {
            // flag
            $run_cat_notin = true;
            //category not found redirection
            //skip admin and home
            //consider only posts:
            if (!is_super_admin() && !is_home() && is_single()) {
                // url
                $category_access_redirect_url = $system_obj->get_setting('category_access_redirect_url');
                //skip if same url:
                if (!empty($category_access_redirect_url) && trailingslashit(mgm_current_url()) != trailingslashit($category_access_redirect_url)) {
                    //check returned category ids belongs to the loaded post:
                    if (isset($post->ID) && is_numeric($post->ID)) {
                        //get post categories
                        $post_cats = wp_get_post_categories($post->ID);
                        // loop
                        foreach ($post_cats as $cat) {
                            //redirect if post category exists in blocked categories
                            if (in_array($cat, $hide_cats)) {
                                //redirect:
                                mgm_redirect($category_access_redirect_url);
                                exit;
                            }
                        }
                    }
                }
            }
            //issue#: 510
            if ($run_cat_notin) {
                // set
                $query->set('category__not_in', array_unique($hide_cats));
                // set negation
                // issue#: 510
                if (substr(get_bloginfo('version'), 0, 3) > 3.0 && !is_page()) {
                    //Note: selectively attach the filter to not apply in other scenarios
                    //issue #1600
                    $post_name = $query->query_vars['name'];
                    if (empty($post_name)) {
                        $current_uri = trim($_SERVER['REQUEST_URI']);
                        $uri = explode('?', $current_uri);
                        $uriArr = explode('/', $uri[0]);
                        if (!empty($uriArr)) {
                            $post_name = $uriArr[1];
                        }
                    }
                    $member = mgm_get_member($user->ID);
                    $membership_type = $member->membership_type;
                    $membership_type = empty($membership_type) ? 'guest' : $membership_type;
                    $arr_memberships = mgm_get_subscribed_membershiptypes($user->ID, $member);
                    if (!in_array($membership_type, $arr_memberships)) {
                        $arr_memberships[] = $membership_type;
                    }
                    $accessible = false;
                    $post_data = mgm_get_post_data_by_name($post_name);
                    // check found
                    if (isset($post_data->ID)) {
                        $post_obj = mgm_get_post($post_data->ID);
                        if (count(array_intersect($post_obj->access_membership_types, $arr_memberships)) > 0) {
                            $accessible = true;
                        }
                    }
                    //not accessible add filter
                    if (!$accessible) {
                        //to filter posts as per category__not_in values
                        add_filter('posts_search', 'mgm_attach_category_not_in');
                    }
                }
            }
            // if on category archive listing page: check cateory is accessible, if not redirect to category_access_redirect_url setting url
            if (!is_super_admin() && !is_home() && is_category()) {
                // url
                $the_url = mgm_current_url();
                //get archived category details
                $loaded_cat = get_category_by_path($the_url, false);
                //getting subcategory by path - issue #1578
                if (empty($loaded_cat)) {
                    $flag = false;
                    $url_segments = preg_split('#/#', $the_url);
                    foreach ($url_segments as $key => $url_segment) {
                        if (strtolower($url_segment) == 'category') {
                            $flag = $key;
                        }
                    }
                    if ($flag) {
                        if (!empty($url_segments[$flag + 2])) {
                            $slug = $url_segments[$flag + 2];
                            $loaded_cat = get_category_by_slug($slug);
                        }
                    }
                }
                // url
                if (!isset($category_access_redirect_url)) {
                    $category_access_redirect_url = $system_obj->get_setting('category_access_redirect_url');
                }
                // issue #: 657
                // if the loaded category cannot be accessed by the user, and if category_access_redirect_url is set, redirect
                if (isset($loaded_cat->cat_ID) && in_array($loaded_cat->cat_ID, $hide_cats) && !empty($category_access_redirect_url) && trailingslashit($the_url) != trailingslashit($category_access_redirect_url)) {
                    // redirect:
                    mgm_redirect($category_access_redirect_url);
                    exit;
                }
            }
        }
    }
    // endif
    // hide terms
    $hide_terms = mgm_content_exclude_by_user($user->ID, 'taxonomy');
    //hide terms
    // set filter
    if (is_array($hide_terms) && !empty($hide_terms)) {
        // flag
        $run_term_notin = true;
        // set filter
        $query->set('tag__not_in', array_unique($hide_terms));
        // set negation
        // set in search
        if (substr(get_bloginfo('version'), 0, 3) > 3.0 && !is_page()) {
            //note: selectively attach the filter to not apply in other scenarios
            add_filter('posts_search', 'mgm_attach_tag_not_in');
            //to filter posts as per tag__not_in values
        }
    }
    // term check
    if ($run_cat_notin || $run_term_notin) {
        add_filter('list_terms_exclusions', 'mgm_exclude_terms');
        // terms
    }
    // return
    return $query;
}
/**
 * Check transaction page content is to be loaded if buddypress and mgm shares registration page.
 */
function mgm_is_bp_registration()
{
    // If Buddypress is enabled
    if (mgm_is_plugin_active('buddypress/bp-loader.php')) {
        // If setting is enabled - is registration urls same for BP and MGM
        if (bool_from_yn(mgm_get_setting('share_registration_url_with_bp'))) {
            // if current url is registration url and transaction page is to be loaded
            if (false !== strpos(mgm_current_url(), untrailingslashit(mgm_get_setting('register_url'))) && isset($_GET['method']) && preg_match('/^payment/', $_GET['method'])) {
                return true;
            }
        }
    }
    return false;
}