Skip to:
Content

bbPress.org


Ignore:
Timestamp:
09/07/2017 05:40:59 AM (9 years ago)
Author:
johnjamesjacoby
Message:

Pagination: abstract and normalize common functionality.

This change introduces a few new helper functions, and audits the links generated where loops of forums, topics, and replies are made visible. It addresses a number of edge-cases in the pagination code, including:

  • view=all state not carrying over
  • Total-page boundary maybe using the wrong value to calculate the total number of available pages
  • Inconsistent output of values across post types and shortcodes
  • Inability to filter pagination arguments in certain use cases
  • Reduces code repetition and increases general happiness

Trunk, for 2.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/search/template.php

    r6672 r6680  
    7777
    7878        // Add pagination values to query object
    79         $bbp->search_query->posts_per_page = $r['posts_per_page'];
    80         $bbp->search_query->paged          = $r['paged'];
     79        $bbp->search_query->posts_per_page = (int) $r['posts_per_page'];
     80        $bbp->search_query->paged          = (int) $r['paged'];
    8181
    8282        // Never home, regardless of what parse_query says
    … …  
    8686        if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) {
    8787
    88                 // Array of arguments to add after pagination links
    89                 $add_args = array();
    90 
    91                 // If pretty permalinks are enabled, make our pagination pretty
    92                 if ( bbp_use_pretty_urls() ) {
    93 
    94                         // Shortcode territory
    95                         if ( is_page() || is_single() ) {
    96                                 $base = trailingslashit( get_permalink() );
    97 
    98                         // Default search location
    99                         } else {
    100                                 $base = trailingslashit( bbp_get_search_results_url() );
    101                         }
    102 
    103                         // Add pagination base
    104                         $base = $base . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    105 
    106                 // Unpretty permalinks
    107                 } else {
    108                         $base = add_query_arg( 'paged', '%#%' );
    109                 }
    110 
    111                 // Add args
    112                 if ( bbp_get_view_all() ) {
    113                         $add_args['view'] = 'all';
    114                 }
     88                // Total for pagination boundaries
     89                $total_pages = ( $bbp->search_query->posts_per_page === $bbp->search_query->found_posts )
     90                        ? 1
     91                        : ceil( $bbp->search_query->found_posts / $bbp->search_query->posts_per_page );
     92
     93                // Pagination settings with filter
     94                $bbp_search_pagination = apply_filters( 'bbp_search_results_pagination', array(
     95                        'base'    => bbp_get_search_pagination_base(),
     96                        'total'   => $total_pages,
     97                        'current' => $bbp->search_query->paged
     98                ) );
    11599
    116100                // Add pagination to query object
    117                 $bbp->search_query->pagination_links = paginate_links(
    118                         apply_filters( 'bbp_search_results_pagination', array(
    119                                 'base'      => $base,
    120                                 'format'    => '',
    121                                 'total'     => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ),
    122                                 'current'   => (int) $bbp->search_query->paged,
    123                                 'prev_text' => is_rtl() ? '→' : '←',
    124                                 'next_text' => is_rtl() ? '←' : '→',
    125                                 'mid_size'  => 1,
    126                                 'add_args'  => $add_args,
    127                         ) )
    128                 );
    129 
    130                 // Remove first page from pagination
    131                 if ( bbp_use_pretty_urls() ) {
    132                         $bbp->search_query->pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $bbp->search_query->pagination_links );
    133                 } else {
    134                         $bbp->search_query->pagination_links = preg_replace( '/&paged=1(?=[^0-9])/m', '', $bbp->search_query->pagination_links );
    135                 }
     101                $bbp->search_query->pagination_links = bbp_paginate_links( $bbp_search_pagination );
    136102        }
    137103
    … …  
    343309                return apply_filters( 'bbp_get_search_terms', $search_terms, $passed_terms );
    344310        }
     311
     312/** Pagination ****************************************************************/
     313
     314/**
     315 * Return the base URL used inside of pagination links
     316 *
     317 * @since 2.6.0 bbPress (r6679)
     318 *
     319 * @return string
     320 */
     321function bbp_get_search_pagination_base() {
     322
     323        // If pretty permalinks are enabled, make our pagination pretty
     324        if ( bbp_use_pretty_urls() ) {
     325
     326                // Shortcode territory
     327                if ( is_page() || is_single() ) {
     328                        $base = trailingslashit( get_permalink() );
     329
     330                // Default search location
     331                } else {
     332                        $base = trailingslashit( bbp_get_search_results_url() );
     333                }
     334
     335                // Add pagination base
     336                $base = $base . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
     337
     338        // Unpretty permalinks
     339        } else {
     340                $base = add_query_arg( 'paged', '%#%' );
     341        }
     342
     343        // Filter & return
     344        return apply_filters( 'bbp_get_search_pagination_base', $base );
     345}
    345346
    346347/**
Note: See TracChangeset for help on using the changeset viewer.