Example #1
0
/**
 * The main topic loop. WordPress makes this easy for us
 *
 * @since 2.0.0 bbPress (r2485)
 *
 * @param array $args All the arguments supported by {@link WP_Query}
 * @uses current_user_can() To check if the current user can edit other's topics
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses WP_Query To make query and get the topics
 * @uses is_page() To check if it's a page
 * @uses bbp_is_single_forum() To check if it's a forum
 * @uses bbp_get_forum_id() To get the forum id
 * @uses bbp_get_paged() To get the current page value
 * @uses bbp_get_super_stickies() To get the super stickies
 * @uses bbp_get_stickies() To get the forum stickies
 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
 * @uses get_permalink() To get the permalink
 * @uses add_query_arg() To add custom args to the url
 * @uses apply_filters() Calls 'bbp_topics_pagination' with the pagination args
 * @uses paginate_links() To paginate the links
 * @uses apply_filters() Calls 'bbp_has_topics' with
 *                        bbPres::topic_query::have_posts()
 *                        and bbPres::topic_query
 * @return object Multidimensional array of topic information
 */
function bbp_has_topics($args = array())
{
    /** Defaults **************************************************************/
    // Other defaults
    $default_topic_search = !empty($_REQUEST['ts']) ? $_REQUEST['ts'] : false;
    $default_show_stickies = (bool) (bbp_is_single_forum() || bbp_is_topic_archive()) && false === $default_topic_search;
    $default_post_parent = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';
    // Default argument array
    $default = array('post_type' => bbp_get_topic_post_type(), 'post_parent' => $default_post_parent, 'meta_key' => '_bbp_last_active_time', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'DESC', 'posts_per_page' => bbp_get_topics_per_page(), 'paged' => bbp_get_paged(), 'show_stickies' => $default_show_stickies, 'max_num_pages' => false);
    // Only add 's' arg if searching for topics
    // See https://bbpress.trac.wordpress.org/ticket/2607
    if (!empty($default_topic_search)) {
        $default['s'] = $default_topic_search;
    }
    // What are the default allowed statuses (based on user caps)
    if (bbp_get_view_all()) {
        // Default view=all statuses
        $post_statuses = array(bbp_get_public_status_id(), bbp_get_closed_status_id(), bbp_get_spam_status_id(), bbp_get_trash_status_id(), bbp_get_pending_status_id());
        // Add support for private status
        if (current_user_can('read_private_topics')) {
            $post_statuses[] = bbp_get_private_status_id();
        }
        // Join post statuses together
        $default['post_status'] = implode(',', $post_statuses);
        // Lean on the 'perm' query var value of 'readable' to provide statuses
    } else {
        $default['perm'] = 'readable';
    }
    // Maybe query for topic tags
    if (bbp_is_topic_tag()) {
        $default['term'] = bbp_get_topic_tag_slug();
        $default['taxonomy'] = bbp_get_topic_tag_tax_id();
    }
    /** Setup *****************************************************************/
    // Parse arguments against default values
    $r = bbp_parse_args($args, $default, 'has_topics');
    // Get bbPress
    $bbp = bbpress();
    // Call the query
    $bbp->topic_query = new WP_Query($r);
    // Set post_parent back to 0 if originally set to 'any'
    if ('any' === $r['post_parent']) {
        $r['post_parent'] = 0;
    }
    // Limited the number of pages shown
    if (!empty($r['max_num_pages'])) {
        $bbp->topic_query->max_num_pages = $r['max_num_pages'];
    }
    /** Stickies **************************************************************/
    // Put sticky posts at the top of the posts array
    if (!empty($r['show_stickies']) && $r['paged'] <= 1) {
        // Get super stickies and stickies in this forum
        $stickies = bbp_get_super_stickies();
        // Get stickies for current forum
        if (!empty($r['post_parent'])) {
            $stickies = array_merge($stickies, bbp_get_stickies($r['post_parent']));
        }
        // Remove any duplicate stickies
        $stickies = array_unique($stickies);
        // We have stickies
        if (is_array($stickies) && !empty($stickies)) {
            // Start the offset at -1 so first sticky is at correct 0 offset
            $sticky_offset = -1;
            // Loop over topics and relocate stickies to the front.
            foreach ($stickies as $sticky_index => $sticky_ID) {
                // Get the post offset from the posts array
                $post_offsets = wp_filter_object_list($bbp->topic_query->posts, array('ID' => $sticky_ID), 'OR', 'ID');
                // Continue if no post offsets
                if (empty($post_offsets)) {
                    continue;
                }
                // Loop over posts in current query and splice them into position
                foreach (array_keys($post_offsets) as $post_offset) {
                    $sticky_offset++;
                    $sticky = $bbp->topic_query->posts[$post_offset];
                    // Remove sticky from current position
                    array_splice($bbp->topic_query->posts, $post_offset, 1);
                    // Move to front, after other stickies
                    array_splice($bbp->topic_query->posts, $sticky_offset, 0, array($sticky));
                    // Cleanup
                    unset($stickies[$sticky_index]);
                    unset($sticky);
                }
                // Cleanup
                unset($post_offsets);
            }
            // Cleanup
            unset($sticky_offset);
            // If any posts have been excluded specifically, Ignore those that are sticky.
            if (!empty($stickies) && !empty($r['post__not_in'])) {
                $stickies = array_diff($stickies, $r['post__not_in']);
            }
            // Fetch sticky posts that weren't in the query results
            if (!empty($stickies)) {
                // Query to use in get_posts to get sticky posts
                $sticky_query = array('post_type' => bbp_get_topic_post_type(), 'post_parent' => 'any', 'meta_key' => '_bbp_last_active_time', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'DESC', 'include' => $stickies);
                // Cleanup
                unset($stickies);
                // Conditionally exclude private/hidden forum ID's
                $exclude_forum_ids = bbp_exclude_forum_ids('array');
                if (!empty($exclude_forum_ids)) {
                    $sticky_query['post_parent__not_in'] = $exclude_forum_ids;
                }
                // What are the default allowed statuses (based on user caps)
                if (bbp_get_view_all()) {
                    $sticky_query['post_status'] = $r['post_status'];
                    // Lean on the 'perm' query var value of 'readable' to provide statuses
                } else {
                    $sticky_query['post_status'] = $r['perm'];
                }
                // Get all stickies
                $sticky_posts = get_posts($sticky_query);
                if (!empty($sticky_posts)) {
                    // Get a count of the visible stickies
                    $sticky_count = count($sticky_posts);
                    // Merge the stickies topics with the query topics .
                    $bbp->topic_query->posts = array_merge($sticky_posts, $bbp->topic_query->posts);
                    // Adjust loop and counts for new sticky positions
                    $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count;
                    $bbp->topic_query->post_count = (int) $bbp->topic_query->post_count + (int) $sticky_count;
                    // Cleanup
                    unset($sticky_posts);
                }
            }
        }
    }
    // If no limit to posts per page, set it to the current post_count
    if (-1 === $r['posts_per_page']) {
        $r['posts_per_page'] = $bbp->topic_query->post_count;
    }
    // Add pagination values to query object
    $bbp->topic_query->posts_per_page = $r['posts_per_page'];
    $bbp->topic_query->paged = $r['paged'];
    // Only add pagination if query returned results
    if (((int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts) && (int) $bbp->topic_query->posts_per_page) {
        // Limit the number of topics shown based on maximum allowed pages
        if (!empty($r['max_num_pages']) && $bbp->topic_query->found_posts > $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count) {
            $bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count;
        }
        // If pretty permalinks are enabled, make our pagination pretty
        if (bbp_use_pretty_urls()) {
            // User's topics
            if (bbp_is_single_user_topics()) {
                $base = bbp_get_user_topics_created_url(bbp_get_displayed_user_id());
                // User's favorites
            } elseif (bbp_is_favorites()) {
                $base = bbp_get_favorites_permalink(bbp_get_displayed_user_id());
                // User's subscriptions
            } elseif (bbp_is_subscriptions()) {
                $base = bbp_get_subscriptions_permalink(bbp_get_displayed_user_id());
                // Root profile page
            } elseif (bbp_is_single_user()) {
                $base = bbp_get_user_profile_url(bbp_get_displayed_user_id());
                // View
            } elseif (bbp_is_single_view()) {
                $base = bbp_get_view_url();
                // Topic tag
            } elseif (bbp_is_topic_tag()) {
                $base = bbp_get_topic_tag_link();
                // Page or single post
            } elseif (is_page() || is_single()) {
                $base = get_permalink();
                // Forum archive
            } elseif (bbp_is_forum_archive()) {
                $base = bbp_get_forums_url();
                // Topic archive
            } elseif (bbp_is_topic_archive()) {
                $base = bbp_get_topics_url();
                // Default
            } else {
                $base = get_permalink((int) $r['post_parent']);
            }
            // Use pagination base
            $base = trailingslashit($base) . user_trailingslashit(bbp_get_paged_slug() . '/%#%/');
            // Unpretty pagination
        } else {
            $base = add_query_arg('paged', '%#%');
        }
        // Pagination settings with filter
        $bbp_topic_pagination = apply_filters('bbp_topic_pagination', array('base' => $base, 'format' => '', 'total' => $r['posts_per_page'] === $bbp->topic_query->found_posts ? 1 : ceil((int) $bbp->topic_query->found_posts / (int) $r['posts_per_page']), 'current' => (int) $bbp->topic_query->paged, 'prev_text' => is_rtl() ? '&rarr;' : '&larr;', 'next_text' => is_rtl() ? '&larr;' : '&rarr;', 'mid_size' => 1));
        // Add pagination to query object
        $bbp->topic_query->pagination_links = paginate_links($bbp_topic_pagination);
        // Remove first page from pagination
        $bbp->topic_query->pagination_links = str_replace(bbp_get_paged_slug() . "/1/'", "'", $bbp->topic_query->pagination_links);
    }
    // Return object
    return apply_filters('bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query);
}
Example #2
0
/**
 * Adjusts forum, topic, and reply queries to exclude items that might be
 * contained inside hidden or private forums that the user does not have the
 * capability to view.
 *
 * Doing it with an action allows us to trap all WP_Query's rather than needing
 * to hardcode this logic into each query. It also protects forum content for
 * plugins that might be doing their own queries.
 *
 * @since bbPress (r3291)
 *
 * @param WP_Query $posts_query
 *
 * @uses apply_filters()
 * @uses bbp_exclude_forum_ids()
 * @uses bbp_get_topic_post_type()
 * @uses bbp_get_reply_post_type()
 * @return WP_Query
 */
function bbp_pre_get_posts_normalize_forum_visibility($posts_query = null)
{
    // Bail if all forums are explicitly allowed
    if (true === apply_filters('bbp_include_all_forums', false, $posts_query)) {
        return;
    }
    // Bail if $posts_query is not an object or of incorrect class
    if (!is_object($posts_query) || !is_a($posts_query, 'WP_Query')) {
        return;
    }
    // Get query post types array .
    $post_types = (array) $posts_query->get('post_type');
    // Forums
    if (bbp_get_forum_post_type() === implode('', $post_types)) {
        // Prevent accidental wp-admin post_row override
        if (is_admin() && isset($_REQUEST['post_status'])) {
            return;
        }
        /** Default ***********************************************************/
        // Get any existing post status
        $post_stati = $posts_query->get('post_status');
        // Default to public status
        if (empty($post_stati)) {
            $post_stati[] = bbp_get_public_status_id();
            // Split the status string
        } elseif (is_string($post_stati)) {
            $post_stati = explode(',', $post_stati);
        }
        /** Private ***********************************************************/
        // Remove bbp_get_private_status_id() if user is not capable
        if (!current_user_can('read_private_forums')) {
            $key = array_search(bbp_get_private_status_id(), $post_stati);
            if (!empty($key)) {
                unset($post_stati[$key]);
            }
            // ...or add it if they are
        } else {
            $post_stati[] = bbp_get_private_status_id();
        }
        /** Hidden ************************************************************/
        // Remove bbp_get_hidden_status_id() if user is not capable
        if (!current_user_can('read_hidden_forums')) {
            $key = array_search(bbp_get_hidden_status_id(), $post_stati);
            if (!empty($key)) {
                unset($post_stati[$key]);
            }
            // ...or add it if they are
        } else {
            $post_stati[] = bbp_get_hidden_status_id();
        }
        // Add the statuses
        $posts_query->set('post_status', array_unique(array_filter($post_stati)));
    }
    // Topics Or Replies
    if (array_intersect(array(bbp_get_topic_post_type(), bbp_get_reply_post_type()), $post_types)) {
        // Get forums to exclude
        $forum_ids = bbp_exclude_forum_ids('meta_query');
        // Bail if no forums to exclude
        if (!array_filter($forum_ids)) {
            return;
        }
        // Get any existing meta queries
        $meta_query = $posts_query->get('meta_query');
        // Add our meta query to existing
        $meta_query[] = $forum_ids;
        // Set the meta_query var
        $posts_query->set('meta_query', $meta_query);
    }
}
Example #3
0
/**
 * This function is hooked into the WordPress 'request' action and is
 * responsible for sniffing out the query vars and serving up RSS2 feeds if
 * the stars align and the user has requested a feed of any bbPress type.
 *
 * @since 2.0.0 bbPress (r3171)
 *
 * @param array $query_vars
 * @return array
 */
function bbp_request_feed_trap($query_vars = array())
{
    // Looking at a feed
    if (isset($query_vars['feed'])) {
        // Forum/Topic/Reply Feed
        if (isset($query_vars['post_type'])) {
            // Matched post type
            $post_type = false;
            // Post types to check
            $post_types = array(bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type());
            // Cast query vars as array outside of foreach loop
            $qv_array = (array) $query_vars['post_type'];
            // Check if this query is for a bbPress post type
            foreach ($post_types as $bbp_pt) {
                if (in_array($bbp_pt, $qv_array, true)) {
                    $post_type = $bbp_pt;
                    break;
                }
            }
            // Looking at a bbPress post type
            if (!empty($post_type)) {
                // Supported select query vars
                $select_query_vars = array('p' => false, 'name' => false, $post_type => false);
                // Setup matched variables to select
                foreach ($query_vars as $key => $value) {
                    if (isset($select_query_vars[$key])) {
                        $select_query_vars[$key] = $value;
                    }
                }
                // Remove any empties
                $select_query_vars = array_filter($select_query_vars);
                // What bbPress post type are we looking for feeds on?
                switch ($post_type) {
                    // Forum
                    case bbp_get_forum_post_type():
                        // Define local variable(s)
                        $meta_query = array();
                        // Single forum
                        if (!empty($select_query_vars)) {
                            // Load up our own query
                            query_posts(array_merge(array('post_type' => bbp_get_forum_post_type(), 'feed' => true), $select_query_vars));
                            // Restrict to specific forum ID
                            $meta_query = array(array('key' => '_bbp_forum_id', 'value' => bbp_get_forum_id(), 'type' => 'NUMERIC', 'compare' => '='));
                        }
                        // Only forum replies
                        if (!empty($_GET['type']) && bbp_get_reply_post_type() === $_GET['type']) {
                            // The query
                            $the_query = array('author' => 0, 'feed' => true, 'post_type' => bbp_get_reply_post_type(), 'post_parent' => 'any', 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), 'posts_per_page' => bbp_get_replies_per_rss_page(), 'order' => 'DESC', 'meta_query' => $meta_query);
                            // Output the feed
                            bbp_display_replies_feed_rss2($the_query);
                            // Only forum topics
                        } elseif (!empty($_GET['type']) && bbp_get_topic_post_type() === $_GET['type']) {
                            // The query
                            $the_query = array('author' => 0, 'feed' => true, 'post_type' => bbp_get_topic_post_type(), 'post_parent' => bbp_get_forum_id(), 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), 'posts_per_page' => bbp_get_topics_per_rss_page(), 'order' => 'DESC');
                            // Output the feed
                            bbp_display_topics_feed_rss2($the_query);
                            // All forum topics and replies
                        } else {
                            // Exclude private/hidden forums if not looking at single
                            if (empty($select_query_vars)) {
                                $meta_query = array(bbp_exclude_forum_ids('meta_query'));
                            }
                            // The query
                            $the_query = array('author' => 0, 'feed' => true, 'post_type' => array(bbp_get_reply_post_type(), bbp_get_topic_post_type()), 'post_parent' => 'any', 'post_status' => array(bbp_get_public_status_id(), bbp_get_closed_status_id()), 'posts_per_page' => bbp_get_replies_per_rss_page(), 'order' => 'DESC', 'meta_query' => $meta_query);
                            // Output the feed
                            bbp_display_replies_feed_rss2($the_query);
                        }
                        break;
                        // Topic feed - Show replies
                    // Topic feed - Show replies
                    case bbp_get_topic_post_type():
                        // Single topic
                        if (!empty($select_query_vars)) {
                            // Load up our own query
                            query_posts(array_merge(array('post_type' => bbp_get_topic_post_type(), 'feed' => true), $select_query_vars));
                            // Output the feed
                            bbp_display_replies_feed_rss2(array('feed' => true));
                            // All topics
                        } else {
                            // The query
                            $the_query = array('author' => 0, 'feed' => true, 'post_parent' => 'any', 'posts_per_page' => bbp_get_topics_per_rss_page(), 'show_stickies' => false);
                            // Output the feed
                            bbp_display_topics_feed_rss2($the_query);
                        }
                        break;
                        // Replies
                    // Replies
                    case bbp_get_reply_post_type():
                        // The query
                        $the_query = array('posts_per_page' => bbp_get_replies_per_rss_page(), 'meta_query' => array(array()), 'feed' => true);
                        // All replies
                        if (empty($select_query_vars)) {
                            bbp_display_replies_feed_rss2($the_query);
                        }
                        break;
                }
            }
            // Single Topic Vview
        } elseif (isset($query_vars[bbp_get_view_rewrite_id()])) {
            // Get the view
            $view = $query_vars[bbp_get_view_rewrite_id()];
            // We have a view to display a feed
            if (!empty($view)) {
                // Get the view query
                $the_query = bbp_get_view_query_args($view);
                // Output the feed
                bbp_display_topics_feed_rss2($the_query);
            }
        }
        // @todo User profile feeds
    }
    // No feed so continue on
    return $query_vars;
}
Example #4
0
    /**
     * 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)
    {
        extract($args);
        $title = apply_filters('bbp_replies_widget_title', $instance['title']);
        $max_shown = !empty($instance['max_shown']) ? $instance['max_shown'] : '5';
        $show_date = !empty($instance['show_date']) ? 'on' : false;
        $show_user = !empty($instance['show_user']) ? 'on' : false;
        $post_types = !empty($instance['post_type']) ? array(bbp_get_topic_post_type(), bbp_get_reply_post_type()) : bbp_get_reply_post_type();
        // Note: private and hidden forums will be excluded via the
        // bbp_pre_get_posts_exclude_forums filter and function.
        $widget_query = new WP_Query(array('post_type' => $post_types, 'post_status' => join(',', array(bbp_get_public_status_id(), bbp_get_closed_status_id())), 'posts_per_page' => $max_shown, 'meta_query' => array(bbp_exclude_forum_ids('meta_query'))));
        // Get replies and display them
        if ($widget_query->have_posts()) {
            echo $before_widget;
            echo $before_title . $title . $after_title;
            ?>

			<ul>

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

					<li>

						<?php 
                $reply_id = bbp_get_reply_id($widget_query->post->ID);
                $author_link = bbp_get_reply_author_link(array('post_id' => $reply_id, 'type' => 'both', 'size' => 14));
                $reply_link = '<a class="bbp-reply-topic-title" href="' . esc_url(bbp_get_reply_url($reply_id)) . '" title="' . bbp_get_reply_excerpt($reply_id, 50) . '">' . bbp_get_reply_topic_title($reply_id) . '</a>';
                // Reply author, link, and timestamp
                if ('on' == $show_date && 'on' == $show_user) {
                    // 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 ($show_date == 'on') {
                    // 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 ($show_user == 'on') {
                    // 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 $after_widget;
            // Reset the $post global
            wp_reset_postdata();
        }
    }
Example #5
0
/**
 * Adjusts topic and reply queries to exclude items that might be contained
 * inside hidden or private forums that the user does not have the capability
 * to view.
 *
 * @since bbPress (r3291)
 *
 * @param WP_Query $posts_query
 *
 * @uses apply_filters()
 * @uses bbp_exclude_forum_ids()
 * @uses bbp_get_topic_post_type()
 * @uses bbp_get_reply_post_type()
 * @return WP_Query
 */
function bbp_pre_get_posts_exclude_forums($posts_query)
{
    // Bail if all forums are explicitly allowed
    if (true === apply_filters('bbp_include_all_forums', $posts_query)) {
        return;
    }
    // Bail if $posts_query is not an object or of incorrect class
    if (!is_object($posts_query) || !is_a($posts_query, 'WP_Query')) {
        return;
    }
    // Bail if filters are suppressed on this query
    if (true == $posts_query->get('suppress_filters')) {
        return;
    }
    // Only exclude forums on bbPress queries
    switch ($posts_query->get('post_type')) {
        // Forums
        case bbp_get_forum_post_type():
            // Prevent accidental wp-admin post_row override
            if (is_admin() && isset($_REQUEST['post_status'])) {
                break;
            }
            // Define local variable
            $status = array();
            // All users can see published forums
            $status[] = bbp_get_public_status_id();
            // Add bbp_get_private_status_id() if user is capable
            if (current_user_can('read_private_forums')) {
                $status[] = bbp_get_private_status_id();
            }
            // Add bbp_get_hidden_status_id() if user is capable
            if (current_user_can('read_hidden_forums')) {
                $status[] = bbp_get_hidden_status_id();
            }
            // Implode and add the statuses
            $posts_query->set('post_status', implode(',', $status));
            break;
            // Topics
        // Topics
        case bbp_get_topic_post_type():
            // Replies
        // Replies
        case bbp_get_reply_post_type():
            // Get forums to exclude
            $forum_ids = bbp_exclude_forum_ids('meta_query');
            // Bail if no forums to exclude
            if (empty($forum_ids)) {
                return;
            }
            // Get any existing meta queries
            $meta_query = $posts_query->get('meta_query');
            // Add our meta query to existing
            $meta_query[] = $forum_ids;
            // Set the meta_query var
            $posts_query->set('meta_query', $meta_query);
            break;
    }
}
Example #6
0
/**
 * This function is hooked into the WordPress 'request' action and is
 * responsible for sniffing out the query vars and serving up RSS2 feeds if
 * the stars align and the user has requested a feed of any bbPress type.
 *
 * @since bbPress (r3171)
 *
 * @param array $query_vars
 * @return array
 */
function bbp_request_feed_trap($query_vars = array())
{
    // Looking at a feed
    if (isset($query_vars['feed'])) {
        // Forum/Topic/Reply Feed
        if (isset($query_vars['post_type'])) {
            // What bbPress post type are we looking for feeds on?
            switch ($query_vars['post_type']) {
                // Forum
                case bbp_get_forum_post_type():
                    // Define local variable(s)
                    $meta_query = array();
                    // Single forum
                    if (isset($query_vars[bbp_get_forum_post_type()])) {
                        // Get the forum by the path
                        $forum = get_page_by_path($query_vars[bbp_get_forum_post_type()], OBJECT, bbp_get_forum_post_type());
                        $forum_id = $forum->ID;
                        // Load up our own query
                        query_posts(array('post_type' => bbp_get_forum_post_type(), 'ID' => $forum_id, 'feed' => true));
                        // Restrict to specific forum ID
                        $meta_query = array(array('key' => '_bbp_forum_id', 'value' => $forum_id, 'type' => 'numeric', 'compare' => '='));
                    }
                    // Only forum replies
                    if (!empty($_GET['type']) && bbp_get_reply_post_type() == $_GET['type']) {
                        // The query
                        $the_query = array('author' => 0, 'feed' => true, 'post_type' => bbp_get_reply_post_type(), 'post_parent' => 'any', 'post_status' => join(',', array(bbp_get_public_status_id(), bbp_get_closed_status_id())), 'posts_per_page' => bbp_get_replies_per_rss_page(), 'order' => 'DESC', 'meta_query' => $meta_query);
                        // Output the feed
                        bbp_display_replies_feed_rss2($the_query);
                        // Only forum topics
                    } elseif (!empty($_GET['type']) && bbp_get_topic_post_type() == $_GET['type']) {
                        // The query
                        $the_query = array('author' => 0, 'feed' => true, 'post_type' => bbp_get_topic_post_type(), 'post_parent' => $forum_id, 'post_status' => join(',', array(bbp_get_public_status_id(), bbp_get_closed_status_id())), 'posts_per_page' => bbp_get_topics_per_rss_page(), 'order' => 'DESC');
                        // Output the feed
                        bbp_display_topics_feed_rss2($the_query);
                        // All forum topics and replies
                    } else {
                        // Exclude private/hidden forums if not looking at single
                        if (empty($query_vars['forum'])) {
                            $meta_query = array(bbp_exclude_forum_ids('meta_query'));
                        }
                        // The query
                        $the_query = array('author' => 0, 'feed' => true, 'post_type' => array(bbp_get_reply_post_type(), bbp_get_topic_post_type()), 'post_parent' => 'any', 'post_status' => join(',', array(bbp_get_public_status_id(), bbp_get_closed_status_id())), 'posts_per_page' => bbp_get_replies_per_rss_page(), 'order' => 'DESC', 'meta_query' => $meta_query);
                        // Output the feed
                        bbp_display_replies_feed_rss2($the_query);
                    }
                    break;
                    // Topic feed - Show replies
                // Topic feed - Show replies
                case bbp_get_topic_post_type():
                    // Single topic
                    if (isset($query_vars[bbp_get_topic_post_type()])) {
                        // Load up our own query
                        query_posts(array('post_type' => bbp_get_topic_post_type(), 'name' => $query_vars[bbp_get_topic_post_type()], 'feed' => true));
                        // Output the feed
                        bbp_display_replies_feed_rss2(array('feed' => true));
                        // All topics
                    } else {
                        // The query
                        $the_query = array('author' => 0, 'feed' => true, 'post_parent' => 'any', 'posts_per_page' => bbp_get_topics_per_rss_page(), 'show_stickies' => false);
                        // Output the feed
                        bbp_display_topics_feed_rss2($the_query);
                    }
                    break;
                    // Replies
                // Replies
                case bbp_get_reply_post_type():
                    // The query
                    $the_query = array('posts_per_page' => bbp_get_replies_per_rss_page(), 'meta_query' => array(array()), 'feed' => true);
                    // All replies
                    if (!isset($query_vars[bbp_get_reply_post_type()])) {
                        bbp_display_replies_feed_rss2($the_query);
                    }
                    break;
            }
            // Single Topic Vview
        } elseif (isset($query_vars['bbp_view'])) {
            // Get the view
            $view = $query_vars['bbp_view'];
            // We have a view to display a feed
            if (!empty($view)) {
                // Get the view query
                $the_query = bbp_get_view_query_args($view);
                // Output the feed
                bbp_display_topics_feed_rss2($the_query);
            }
        }
        // @todo User profile feeds
    }
    // No feed so continue on
    return $query_vars;
}