/**
 * This function filters the list of forums based on the users rank as set by the Mebmers plugin
 */
function tehnik_bpp_filter_forums_by_permissions($args = '')
{
    $bbp = bbpress();
    // Setup possible post__not_in array
    $post_stati[] = bbp_get_public_status_id();
    // Check if user can read private forums
    if (current_user_can('read_private_forums')) {
        $post_stati[] = bbp_get_private_status_id();
    }
    // Check if user can read hidden forums
    if (current_user_can('read_hidden_forums')) {
        $post_stati[] = bbp_get_hidden_status_id();
    }
    // The default forum query for most circumstances
    $meta_query = array('post_type' => bbp_get_forum_post_type(), 'post_parent' => bbp_is_forum_archive() ? 0 : bbp_get_forum_id(), 'post_status' => implode(',', $post_stati), 'posts_per_page' => get_option('_bbp_forums_per_page', 50), 'orderby' => 'menu_order', 'order' => 'ASC');
    //Get an array of IDs which the current user has permissions to view
    $allowed_forums = tehnik_bpp_get_permitted_post_ids(new WP_Query($meta_query));
    // The default forum query with allowed forum ids array added
    $meta_query['post__in'] = $allowed_forums;
    $bbp_f = bbp_parse_args($args, $meta_query, 'has_forums');
    // Run the query
    $bbp->forum_query = new WP_Query($bbp_f);
    return apply_filters('bpp_filter_forums_by_permissions', $bbp->forum_query->have_posts(), $bbp->forum_query);
}
    /**
     * Displays the output, the replies list
     *
     * @since bbPress (r2653)
     *
     * @param mixed $args
     * @param array $instance
     * @uses apply_filters() Calls 'bbp_reply_widget_title' with the title
     * @uses bbp_get_reply_author_link() To get the reply author link
     * @uses bbp_get_reply_author() To get the reply author name
     * @uses bbp_get_reply_id() To get the reply id
     * @uses bbp_get_reply_url() To get the reply url
     * @uses bbp_get_reply_excerpt() To get the reply excerpt
     * @uses bbp_get_reply_topic_title() To get the reply topic title
     * @uses get_the_date() To get the date of the reply
     * @uses get_the_time() To get the time of the reply
     */
    public function widget($args, $instance)
    {
        // Get widget settings
        $settings = $this->parse_settings($instance);
        // Typical WordPress filter
        $settings['title'] = apply_filters('widget_title', $settings['title'], $instance, $this->id_base);
        // bbPress filter
        $settings['title'] = apply_filters('bbp_replies_widget_title', $settings['title'], $instance, $this->id_base);
        // Note: private and hidden forums will be excluded via the
        // bbp_pre_get_posts_exclude_forums filter and function.
        $query_data = array('post_type' => bbp_get_reply_post_type(), 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), 'posts_per_page' => (int) $settings['max_shown']);
        //Get an array of IDs which the current user has permissions to view
        $allowed_posts = tehnik_bpp_get_permitted_post_ids(new WP_Query($query_data));
        // The default forum query with allowed forum ids array added
        $query_data['post__in'] = $allowed_posts;
        $widget_query = new WP_Query($query_data);
        // Bail if no replies
        if (!$widget_query->have_posts()) {
            return;
        }
        echo $args['before_widget'];
        if (!empty($settings['title'])) {
            echo $args['before_title'] . $settings['title'] . $args['after_title'];
        }
        ?>

        <ul>

            <?php 
        while ($widget_query->have_posts()) {
            $widget_query->the_post();
            ?>

                <li>

                    <?php 
            // Verify the reply ID
            $reply_id = bbp_get_reply_id($widget_query->post->ID);
            $reply_link = '<a class="bbp-reply-topic-title" href="' . esc_url(bbp_get_reply_url($reply_id)) . '" title="' . esc_attr(bbp_get_reply_excerpt($reply_id, 50)) . '">' . bbp_get_reply_topic_title($reply_id) . '</a>';
            // Only query user if showing them
            if ('on' == $settings['show_user']) {
                $author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14));
            } else {
                $author_link = false;
            }
            // Reply author, link, and timestamp
            if ('on' == $settings['show_date'] && !empty($author_link)) {
                // translators: 1: reply author, 2: reply link, 3: reply timestamp
                printf(_x('%1$s on %2$s %3$s', 'widgets', 'bbpress'), $author_link, $reply_link, '<div>' . bbp_get_time_since(get_the_time('U')) . '</div>');
                // Reply link and timestamp
            } elseif ('on' == $settings['show_date']) {
                // translators: 1: reply link, 2: reply timestamp
                printf(_x('%1$s %2$s', 'widgets', 'bbpress'), $reply_link, '<div>' . bbp_get_time_since(get_the_time('U')) . '</div>');
                // Reply author and title
            } elseif (!empty($author_link)) {
                // translators: 1: reply author, 2: reply link
                printf(_x('%1$s on %2$s', 'widgets', 'bbpress'), $author_link, $reply_link);
                // Only the reply title
            } else {
                // translators: 1: reply link
                printf(_x('%1$s', 'widgets', 'bbpress'), $reply_link);
            }
            ?>

                </li>

            <?php 
        }
        ?>

        </ul>

        <?php 
        echo $args['after_widget'];
        // Reset the $post global
        wp_reset_postdata();
    }