/** * 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('&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); }
/** * Get the replies that a user created * * @since bbPress (r4225) * * @param int $user_id Optional. User id * @uses bbp_get_user_id() To get the topic id * @uses bbp_has_replies() To get the topics created by the user * @return array|bool Results if the user has created topics, otherwise false */ function bbp_get_user_replies_created($user_id = 0) { // Validate user $user_id = bbp_get_user_id($user_id); if (empty($user_id)) { return false; } // Try to get the topics $query = bbp_has_replies(array('post_type' => array(bbp_get_topic_post_type(), bbp_get_reply_post_type()), 'post_parent' => 'any', 'posts_per_page' => bbp_get_replies_per_page(), 'paged' => bbp_get_paged(), 'orderby' => 'date', 'order' => 'DESC', 'author' => $user_id, 'show_stickies' => false)); return apply_filters('bbp_get_user_replies_created', $query, $user_id); }
/** * Return the paginated url to the reply in the reply loop * * @since 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 WP_Rewrite::using_permalinks() To check if the blog uses * permalinks * @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 { global $wp_rewrite; // Pretty permalinks if ($wp_rewrite->using_permalinks()) { $url = trailingslashit($topic_url) . trailingslashit($wp_rewrite->pagination_base) . 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); }
/** * The main search loop. WordPress does the heavy lifting. * * @since bbPress (r4579) * * @param mixed $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 WP_Rewrite::using_permalinks() To check if the blog is using permalinks * @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 = '') { global $wp_rewrite; /** Defaults **************************************************************/ $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, 's' => bbp_get_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()); // 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(); // Call the query 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 ($wp_rewrite->using_permalinks()) { // 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($wp_rewrite->pagination_base . '/%#%/'); // 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() ? '→' : '←', 'next_text' => is_rtl() ? '←' : '→', 'mid_size' => 1, 'add_args' => $add_args))); // Remove first page from pagination if ($wp_rewrite->using_permalinks()) { $bbp->search_query->pagination_links = str_replace($wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links); } else { $bbp->search_query->pagination_links = str_replace('&paged=1', '', $bbp->search_query->pagination_links); } } // Return object return apply_filters('bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query); }
function x_bbpress_show_search_pagination() { $total_search_items = bbpress()->search_query->found_posts; if (bbp_get_replies_per_page() <= $total_search_items) { return true; } else { return false; } }
/** * Redirect to the group forum screen * * @since bbPress (r3653) */ public function new_reply_redirect_to($redirect_url = '', $redirect_to = '', $reply_id = 0) { global $wp_rewrite; if (bp_is_group()) { $topic_id = bbp_get_reply_topic_id($reply_id); $topic = bbp_get_topic($topic_id); $reply_position = bbp_get_reply_position($reply_id, $topic_id); $reply_page = ceil((int) $reply_position / (int) bbp_get_replies_per_page()); $reply_hash = '#post-' . $reply_id; $topic_url = trailingslashit(bp_get_group_permalink(groups_get_current_group())) . trailingslashit($this->slug) . trailingslashit($this->topic_slug) . trailingslashit($topic->post_name); // Don't include pagination if on first page if (1 >= $reply_page) { $redirect_url = trailingslashit($topic_url) . $reply_hash; // Include pagination } else { $redirect_url = trailingslashit($topic_url) . trailingslashit($wp_rewrite->pagination_base) . trailingslashit($reply_page) . $reply_hash; } // Add topic view query arg back to end if it is set if (bbp_get_view_all()) { $redirect_url = bbp_add_view_all($redirect_url); } } return $redirect_url; }
/** * @covers ::bbp_reply_url * @covers ::bbp_get_reply_url * * @ticket BBP2845 */ public function test_bbp_get_reply_url() { if (is_multisite()) { $this->markTestSkipped('Skipping URL tests in multisite for now.'); } $f = $this->factory->forum->create(); $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f))); $r = $this->factory->reply->create_many(7, array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t))); // Store the original reply pagination option value. $default_reply_page = bbp_get_replies_per_page(); // Lower the reply pagination value to test without so many replies. update_option('_bbp_replies_per_page', 3); // Reply menu position is unaltered when bbp_show_lead_topic() true. add_filter('bbp_show_lead_topic', '__return_true'); // 1st reply is on the first page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '/#post-' . bbp_get_reply_id($r[0]); $this->assertSame($reply_url, bbp_get_reply_url($r[0])); // 2nd reply is on the first page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '/#post-' . bbp_get_reply_id($r[1]); $this->assertSame($reply_url, bbp_get_reply_url($r[1])); // 3rd reply is on the first page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '/#post-' . bbp_get_reply_id($r[2]); $this->assertSame($reply_url, bbp_get_reply_url($r[2])); // 4th reply is on the second page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[3]); $this->assertSame($reply_url, bbp_get_reply_url($r[3])); // 5th reply is on the second page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[4]); $this->assertSame($reply_url, bbp_get_reply_url($r[4])); // 6th reply is on the second page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[5]); $this->assertSame($reply_url, bbp_get_reply_url($r[5])); // 7th reply is on the third page, 3 replies and 1 topic per page. $reply_url = bbp_get_topic_permalink($t) . '&paged=3#post-' . bbp_get_reply_id($r[6]); $this->assertSame($reply_url, bbp_get_reply_url($r[6])); // Remove the filter for WordPress < 4.0 compatibility. remove_filter('bbp_show_lead_topic', '__return_true'); // Reply menu position is bumped by 1 when bbp_show_lead_topic() false. add_filter('bbp_show_lead_topic', '__return_false'); // 1st reply is on the first page, 2 replies and 1 topic per first page. $reply_url = bbp_get_topic_permalink($t) . '/#post-' . bbp_get_reply_id($r[0]); $this->assertSame($reply_url, bbp_get_reply_url($r[0])); // 2nd reply is on the first page, 2 replies and 1 topic per first page. $reply_url = bbp_get_topic_permalink($t) . '/#post-' . bbp_get_reply_id($r[1]); $this->assertSame($reply_url, bbp_get_reply_url($r[1])); // 3rd reply is on the second page, 2 replies and 1 topic per first page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[2]); $this->assertSame($reply_url, bbp_get_reply_url($r[2])); // 4th reply is on the second page, 3 replies per subsequent page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[3]); $this->assertSame($reply_url, bbp_get_reply_url($r[3])); // 5th reply is on the second page, 3 replies per subsequent page. $reply_url = bbp_get_topic_permalink($t) . '&paged=2#post-' . bbp_get_reply_id($r[4]); $this->assertSame($reply_url, bbp_get_reply_url($r[4])); // 6th reply is on the third page, 3 replies per subsequent page. $reply_url = bbp_get_topic_permalink($t) . '&paged=3#post-' . bbp_get_reply_id($r[5]); $this->assertSame($reply_url, bbp_get_reply_url($r[5])); // 7th reply is on the third page, 3 replies per subsequent page. $reply_url = bbp_get_topic_permalink($t) . '&paged=3#post-' . bbp_get_reply_id($r[6]); $this->assertSame($reply_url, bbp_get_reply_url($r[6])); // Remove the filter for WordPress < 4.0 compatibility. remove_filter('bbp_show_lead_topic', '__return_false'); // Restore the original reply pagination option value. update_option('_bbp_replies_per_page', $default_reply_page); }
/** * Returns pagination links of a topic within the topic loop * * @since bbPress (r2966) * * @param mixed $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 WP_Rewrite::using_permalinks() To check if the blog is using * permalinks * @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 = '') { global $wp_rewrite; $defaults = array('topic_id' => bbp_get_topic_id(), 'before' => '<span class="bbp-topic-pagination">', 'after' => '</span>'); $r = bbp_parse_args($args, $defaults, 'get_topic_pagination'); extract($r); // If pretty permalinks are enabled, make our pagination pretty if ($wp_rewrite->using_permalinks()) { $base = trailingslashit(get_permalink($topic_id)) . user_trailingslashit($wp_rewrite->pagination_base . '/%#%/'); } else { $base = add_query_arg('paged', '%#%', get_permalink($topic_id)); } // Get total and add 1 if topic is included in the reply loop $total = bbp_get_topic_reply_count($topic_id); // Bump if topic is in loop if (!bbp_show_lead_topic()) { $total++; } // Pagination settings $pagination = 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); // Add pagination to query object $pagination_links = paginate_links($pagination); if (!empty($pagination_links)) { // Remove first page from pagination if ($wp_rewrite->using_permalinks()) { $pagination_links = str_replace($wp_rewrite->pagination_base . '/1/', '', $pagination_links); } else { $pagination_links = str_replace('&paged=1', '', $pagination_links); } // Add before and after to pagination links $pagination_links = $before . $pagination_links . $after; } return apply_filters('bbp_get_topic_pagination', $pagination_links, $args); }