Esempio n. 1
0
/**
 * Returns pagination links of a topic within the topic loop
 *
 * @since 2.0.0 bbPress (r2966)
 *
 * @param array $args This function supports these arguments:
 *  - topic_id: Topic id
 *  - before: Before the links
 *  - after: After the links
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
 * @uses user_trailingslashit() To add a trailing slash
 * @uses trailingslashit() To add a trailing slash
 * @uses get_permalink() To get the permalink of the topic
 * @uses add_query_arg() To add query args
 * @uses bbp_get_topic_reply_count() To get topic reply count
 * @uses bbp_show_topic_lead() Are we showing the topic as a lead?
 * @uses get_option() To get replies per page option
 * @uses paginate_links() To paginate the links
 * @uses apply_filters() Calls 'bbp_get_topic_pagination' with the links
 *                        and arguments
 * @return string Pagination links
 */
function bbp_get_topic_pagination($args = array())
{
    // Bail if threading replies
    if (bbp_thread_replies()) {
        return;
    }
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('topic_id' => bbp_get_topic_id(), 'before' => '<span class="bbp-topic-pagination">', 'after' => '</span>'), 'get_topic_pagination');
    // If pretty permalinks are enabled, make our pagination pretty
    if (bbp_use_pretty_urls()) {
        $base = trailingslashit(get_permalink($r['topic_id'])) . user_trailingslashit(bbp_get_paged_slug() . '/%#%/');
    } else {
        $base = add_query_arg('paged', '%#%', get_permalink($r['topic_id']));
    }
    // Get total and add 1 if topic is included in the reply loop
    $total = bbp_get_topic_reply_count($r['topic_id'], true);
    // Bump if topic is in loop
    if (!bbp_show_lead_topic()) {
        $total++;
    }
    // Add pagination to query object
    $pagination_links = paginate_links(array('base' => $base, 'format' => '', 'total' => ceil((int) $total / (int) bbp_get_replies_per_page()), 'current' => 0, 'prev_next' => false, 'mid_size' => 2, 'end_size' => 3, 'add_args' => bbp_get_view_all() ? array('view' => 'all') : false));
    if (!empty($pagination_links)) {
        // Remove first page from pagination
        if (bbp_use_pretty_urls()) {
            $pagination_links = str_replace(bbp_get_paged_slug() . '/1/', '', $pagination_links);
        } else {
            $pagination_links = str_replace('&#038;paged=1', '', $pagination_links);
        }
        // Add before and after to pagination links
        $pagination_links = $r['before'] . $pagination_links . $r['after'];
    }
    return apply_filters('bbp_get_topic_pagination', $pagination_links, $args);
}
Esempio n. 2
0
/**
 * Return the paginated url to the reply in the reply loop
 *
 * @since 2.0.0 bbPress (r2679)
 *
 * @param int $reply_id Optional. Reply id
 * @param string $redirect_to Optional. Pass a redirect value for use with
 *                              shortcodes and other fun things.
 * @uses bbp_get_reply_id() To get the reply id
 * @uses bbp_get_reply_topic_id() To get the reply topic id
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses bbp_get_reply_position() To get the reply position
 * @uses get_option() To get the replies per page option
 * @uses bbp_use_pretty_urls() To check if the site uses pretty URLs
 * @uses add_query_arg() To add custom args to the url
 * @uses apply_filters() Calls 'bbp_get_reply_url' with the reply url,
 *                        reply id and bool count hidden
 * @return string Link to reply relative to paginated topic
 */
function bbp_get_reply_url($reply_id = 0, $redirect_to = '')
{
    // Set needed variables
    $reply_id = bbp_get_reply_id($reply_id);
    $topic_id = bbp_get_reply_topic_id($reply_id);
    // Hierarchical reply page
    if (bbp_thread_replies()) {
        $reply_page = 1;
        // Standard reply page
    } else {
        $reply_page = ceil((int) bbp_get_reply_position($reply_id, $topic_id) / (int) bbp_get_replies_per_page());
    }
    $reply_hash = '#post-' . $reply_id;
    $topic_link = bbp_get_topic_permalink($topic_id, $redirect_to);
    $topic_url = remove_query_arg('view', $topic_link);
    // Don't include pagination if on first page
    if (1 >= $reply_page) {
        $url = trailingslashit($topic_url) . $reply_hash;
        // Include pagination
    } else {
        // Pretty permalinks
        if (bbp_use_pretty_urls()) {
            $url = trailingslashit($topic_url) . trailingslashit(bbp_get_paged_slug()) . trailingslashit($reply_page) . $reply_hash;
            // Yucky links
        } else {
            $url = add_query_arg('paged', $reply_page, $topic_url) . $reply_hash;
        }
    }
    // Add topic view query arg back to end if it is set
    if (bbp_get_view_all()) {
        $url = bbp_add_view_all($url);
    }
    return apply_filters('bbp_get_reply_url', $url, $reply_id, $redirect_to);
}
Esempio n. 3
0
 /**
  * Add bbPress-specific rewrite rules for uri's that are not
  * setup for us by way of custom post types or taxonomies. This includes:
  * - Front-end editing
  * - Topic views
  * - User profiles
  *
  * @since bbPress (r2688)
  * @todo Extract into an API
  */
 public static function add_rewrite_rules()
 {
     /** Setup *************************************************************/
     // Add rules to top or bottom?
     $priority = 'top';
     // Single Slugs
     $forum_slug = bbp_get_forum_slug();
     $topic_slug = bbp_get_topic_slug();
     $reply_slug = bbp_get_reply_slug();
     $ttag_slug = bbp_get_topic_tag_tax_slug();
     // Archive Slugs
     $user_slug = bbp_get_user_slug();
     $view_slug = bbp_get_view_slug();
     $search_slug = bbp_get_search_slug();
     $topic_archive_slug = bbp_get_topic_archive_slug();
     $reply_archive_slug = bbp_get_reply_archive_slug();
     // Tertiary Slugs
     $feed_slug = 'feed';
     $edit_slug = 'edit';
     $paged_slug = bbp_get_paged_slug();
     $user_favs_slug = bbp_get_user_favorites_slug();
     $user_subs_slug = bbp_get_user_subscriptions_slug();
     // Unique rewrite ID's
     $feed_id = 'feed';
     $edit_id = bbp_get_edit_rewrite_id();
     $view_id = bbp_get_view_rewrite_id();
     $paged_id = bbp_get_paged_rewrite_id();
     $search_id = bbp_get_search_rewrite_id();
     $user_id = bbp_get_user_rewrite_id();
     $user_favs_id = bbp_get_user_favorites_rewrite_id();
     $user_subs_id = bbp_get_user_subscriptions_rewrite_id();
     $user_tops_id = bbp_get_user_topics_rewrite_id();
     $user_reps_id = bbp_get_user_replies_rewrite_id();
     // Rewrite rule matches used repeatedly below
     $root_rule = '/([^/]+)/?$';
     $feed_rule = '/([^/]+)/' . $feed_slug . '/?$';
     $edit_rule = '/([^/]+)/' . $edit_slug . '/?$';
     $paged_rule = '/([^/]+)/' . $paged_slug . '/?([0-9]{1,})/?$';
     // Search rules (without slug check)
     $search_root_rule = '/?$';
     $search_paged_rule = '/' . $paged_slug . '/?([0-9]{1,})/?$';
     /** Add ***************************************************************/
     // User profile rules
     $tops_rule = '/([^/]+)/' . $topic_archive_slug . '/?$';
     $reps_rule = '/([^/]+)/' . $reply_archive_slug . '/?$';
     $favs_rule = '/([^/]+)/' . $user_favs_slug . '/?$';
     $subs_rule = '/([^/]+)/' . $user_subs_slug . '/?$';
     $tops_paged_rule = '/([^/]+)/' . $topic_archive_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
     $reps_paged_rule = '/([^/]+)/' . $reply_archive_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
     $favs_paged_rule = '/([^/]+)/' . $user_favs_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
     $subs_paged_rule = '/([^/]+)/' . $user_subs_slug . '/' . $paged_slug . '/?([0-9]{1,})/?$';
     // Edit Forum|Topic|Reply|Topic-tag
     add_rewrite_rule($forum_slug . $edit_rule, 'index.php?' . bbp_get_forum_post_type() . '=$matches[1]&' . $edit_id . '=1', $priority);
     add_rewrite_rule($topic_slug . $edit_rule, 'index.php?' . bbp_get_topic_post_type() . '=$matches[1]&' . $edit_id . '=1', $priority);
     add_rewrite_rule($reply_slug . $edit_rule, 'index.php?' . bbp_get_reply_post_type() . '=$matches[1]&' . $edit_id . '=1', $priority);
     add_rewrite_rule($ttag_slug . $edit_rule, 'index.php?' . bbp_get_topic_tag_tax_id() . '=$matches[1]&' . $edit_id . '=1', $priority);
     // User Pagination|Edit|View
     add_rewrite_rule($user_slug . $tops_paged_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_tops_id . '=1&' . $paged_id . '=$matches[2]', $priority);
     add_rewrite_rule($user_slug . $reps_paged_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_reps_id . '=1&' . $paged_id . '=$matches[2]', $priority);
     add_rewrite_rule($user_slug . $favs_paged_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_favs_id . '=1&' . $paged_id . '=$matches[2]', $priority);
     add_rewrite_rule($user_slug . $subs_paged_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_subs_id . '=1&' . $paged_id . '=$matches[2]', $priority);
     add_rewrite_rule($user_slug . $tops_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_tops_id . '=1', $priority);
     add_rewrite_rule($user_slug . $reps_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_reps_id . '=1', $priority);
     add_rewrite_rule($user_slug . $favs_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_favs_id . '=1', $priority);
     add_rewrite_rule($user_slug . $subs_rule, 'index.php?' . $user_id . '=$matches[1]&' . $user_subs_id . '=1', $priority);
     add_rewrite_rule($user_slug . $edit_rule, 'index.php?' . $user_id . '=$matches[1]&' . $edit_id . '=1', $priority);
     add_rewrite_rule($user_slug . $root_rule, 'index.php?' . $user_id . '=$matches[1]', $priority);
     // Topic-View Pagination|Feed|View
     add_rewrite_rule($view_slug . $paged_rule, 'index.php?' . $view_id . '=$matches[1]&' . $paged_id . '=$matches[2]', $priority);
     add_rewrite_rule($view_slug . $feed_rule, 'index.php?' . $view_id . '=$matches[1]&' . $feed_id . '=$matches[2]', $priority);
     add_rewrite_rule($view_slug . $root_rule, 'index.php?' . $view_id . '=$matches[1]', $priority);
     // Search All
     add_rewrite_rule($search_slug . $search_paged_rule, 'index.php?' . $paged_id . '=$matches[1]', $priority);
     add_rewrite_rule($search_slug . $search_root_rule, 'index.php?' . $search_id, $priority);
 }
Esempio n. 4
0
/**
 * The main search loop. WordPress does the heavy lifting.
 *
 * @since 2.3.0 bbPress (r4579)
 *
 * @param array $args All the arguments supported by {@link WP_Query}
 * @uses bbp_get_view_all() Are we showing all results?
 * @uses bbp_get_public_status_id() To get the public status id
 * @uses bbp_get_closed_status_id() To get the closed status id
 * @uses bbp_get_spam_status_id() To get the spam status id
 * @uses bbp_get_trash_status_id() To get the trash status id
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses bbp_get_replies_per_page() To get the replies per page option
 * @uses bbp_get_paged() To get the current page value
 * @uses bbp_get_search_terms() To get the search terms
 * @uses WP_Query To make query and get the search results
 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
 * @uses bbp_get_search_url() To get the forum search url
 * @uses paginate_links() To paginate search results
 * @uses apply_filters() Calls 'bbp_has_search_results' with
 *                        bbPress::search_query::have_posts()
 *                        and bbPress::reply_query
 * @return object Multidimensional array of search information
 */
function bbp_has_search_results($args = array())
{
    /** Defaults **************************************************************/
    $default_search_terms = bbp_get_search_terms();
    $default_post_type = array(bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type());
    // Default query args
    $default = array('post_type' => $default_post_type, 'posts_per_page' => bbp_get_replies_per_page(), 'paged' => bbp_get_paged(), 'orderby' => 'date', 'order' => 'DESC', 'ignore_sticky_posts' => true);
    // Only set 's' if search terms exist
    // https://bbpress.trac.wordpress.org/ticket/2607
    if (false !== $default_search_terms) {
        $default['s'] = $default_search_terms;
    }
    // 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';
    }
    /** Setup *****************************************************************/
    // Parse arguments against default values
    $r = bbp_parse_args($args, $default, 'has_search_results');
    // Get bbPress
    $bbp = bbpress();
    // Only call the search query if 's' is not empty
    if (!empty($r['s'])) {
        $bbp->search_query = new WP_Query($r);
    }
    // Add pagination values to query object
    $bbp->search_query->posts_per_page = $r['posts_per_page'];
    $bbp->search_query->paged = $r['paged'];
    // Never home, regardless of what parse_query says
    $bbp->search_query->is_home = false;
    // Only add pagination is query returned results
    if (!empty($bbp->search_query->found_posts) && !empty($bbp->search_query->posts_per_page)) {
        // Array of arguments to add after pagination links
        $add_args = array();
        // If pretty permalinks are enabled, make our pagination pretty
        if (bbp_use_pretty_urls()) {
            // Shortcode territory
            if (is_page() || is_single()) {
                $base = trailingslashit(get_permalink());
                // Default search location
            } else {
                $base = trailingslashit(bbp_get_search_results_url());
            }
            // Add pagination base
            $base = $base . user_trailingslashit(bbp_get_paged_slug() . '/%#%/');
            // Unpretty permalinks
        } else {
            $base = add_query_arg('paged', '%#%');
        }
        // Add args
        if (bbp_get_view_all()) {
            $add_args['view'] = 'all';
        }
        // Add pagination to query object
        $bbp->search_query->pagination_links = paginate_links(apply_filters('bbp_search_results_pagination', array('base' => $base, 'format' => '', 'total' => ceil((int) $bbp->search_query->found_posts / (int) $r['posts_per_page']), 'current' => (int) $bbp->search_query->paged, 'prev_text' => is_rtl() ? '&rarr;' : '&larr;', 'next_text' => is_rtl() ? '&larr;' : '&rarr;', 'mid_size' => 1, 'add_args' => $add_args)));
        // Remove first page from pagination
        if (bbp_use_pretty_urls()) {
            $bbp->search_query->pagination_links = str_replace(bbp_get_paged_slug() . '/1/', '', $bbp->search_query->pagination_links);
        } else {
            $bbp->search_query->pagination_links = str_replace('&#038;paged=1', '', $bbp->search_query->pagination_links);
        }
    }
    // Return object
    return apply_filters('bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query);
}
Esempio n. 5
0
 /**
  * Fix pagination of replies on topic view
  *
  * @since 2.2.0 bbPress (r4266)
  *
  * @param array $args
  * @uses bbp_get_topic_id()
  * @uses maybe_map_permalink_to_group
  * @return array
  */
 public function replies_pagination($args)
 {
     $new = $this->maybe_map_permalink_to_group(bbp_get_topic_id());
     if (empty($new)) {
         return $args;
     }
     $args['base'] = trailingslashit($new) . bbp_get_paged_slug() . '/%#%/';
     return $args;
 }