Skip to:
Content

bbPress.org

Changeset 6680


Ignore:
Timestamp:
09/07/2017 05:40:59 AM (4 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.

Location:
trunk/src/includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/abstraction.php

    r6601 r6680  
    190190
    191191/**
     192 * Remove the first-page from a pagination links result set, ensuring that it
     193 * points to the canonical first page URL.
     194 *
     195 * This is a bit of an SEO hack, to guarantee that the first page in a loop will
     196 * never have pagination appended to the end of it, regardless of what the other
     197 * functions have decided for us.
     198 *
     199 * @since 2.6.0 bbPress (r6678)
     200 *
     201 * @param string $pagination_links The HTML links used for pagination
     202 *
     203 * @return string
     204 */
     205function bbp_make_first_page_canonical( $pagination_links = '' ) {
     206
     207    // Default value
     208    $retval = $pagination_links;
     209
     210    // Remove first page from pagination
     211    if ( ! empty( $pagination_links ) ) {
     212        $retval = bbp_use_pretty_urls()
     213            ? str_replace( bbp_get_paged_slug() . '/1/', '', $pagination_links )
     214            : preg_replace( '/&paged=1(?=[^0-9])/m', '', $pagination_links );
     215    }
     216
     217    // Filter & return
     218    return apply_filters( 'bbp_make_first_page_canonical', $retval, $pagination_links );
     219}
     220
     221/**
     222 * A convenient wrapper for common calls to paginate_links(), complete with
     223 * support for parameters that aren't used internally by bbPress.
     224 *
     225 * @since 2.6.0 bbPress (r6679)
     226 *
     227 * @param array $args
     228 *
     229 * @return string
     230 */
     231function bbp_paginate_links( $args = array() ) {
     232
     233    // Maybe add view-all args
     234    $add_args = empty( $args['add_args'] ) && bbp_get_view_all()
     235        ? array( 'view' => 'all' )
     236        : false;
     237
     238    // Pagination settings with filter
     239    $r = bbp_parse_args( $args, array(
     240
     241        // Used by callers
     242        'base'      => '',
     243        'total'     => 1,
     244        'current'   => bbp_get_paged(),
     245        'prev_next' => true,
     246        'prev_text' => is_rtl() ? '→' : '←',
     247        'next_text' => is_rtl() ? '←' : '→',
     248        'mid_size'  => 1,
     249        'end_size'  => 3,
     250        'add_args'  => $add_args,
     251
     252        // Unused by callers
     253        'show_all'           => false,
     254        'type'               => 'plain',
     255        'format'             => '',
     256        'add_fragment'       => '',
     257        'before_page_number' => '',
     258        'after_page_number'  => ''
     259    ), 'paginate_links' );
     260
     261    // Return paginated links
     262    return bbp_make_first_page_canonical( paginate_links( $r ) );
     263}
     264
     265/**
    192266 * Is the environment using pretty URLs?
    193267 *
  • trunk/src/includes/replies/template.php

    r6627 r6680  
    172172
    173173    // Set posts_per_page value if replies are threaded
    174     $replies_per_page = $r['posts_per_page'];
     174    $replies_per_page = (int) $r['posts_per_page'];
    175175    if ( true === $r['hierarchical'] ) {
    176176        $r['posts_per_page'] = -1;
     
    184184
    185185    // Add pagination values to query object
    186     $bbp->reply_query->posts_per_page = $replies_per_page;
    187     $bbp->reply_query->paged          = $r['paged'];
     186    $bbp->reply_query->posts_per_page = (int) $replies_per_page;
     187    $bbp->reply_query->paged          = (int) $r['paged'];
    188188
    189189    // Never home, regardless of what parse_query says
     
    196196
    197197    // Only add reply to if query returned results
    198     if ( (int) $bbp->reply_query->found_posts ) {
     198    if ( ! empty( $bbp->reply_query->found_posts ) ) {
    199199
    200200        // Get reply to for each reply
     
    217217
    218218    // Only add pagination if query returned results
    219     if ( (int) $bbp->reply_query->found_posts && (int) $bbp->reply_query->posts_per_page ) {
    220 
    221         // If pretty permalinks are enabled, make our pagination pretty
    222         if ( bbp_use_pretty_urls() ) {
    223 
    224             // User's replies
    225             if ( bbp_is_single_user_replies() ) {
    226                 $base = bbp_get_user_replies_created_url( bbp_get_displayed_user_id() );
    227 
    228             // Root profile page
    229             } elseif ( bbp_is_single_user() ) {
    230                 $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
    231 
    232             // Page or single post
    233             } elseif ( is_page() || is_single() ) {
    234                 $base = get_permalink();
    235 
    236             // Single topic
    237             } else {
    238                 $base = get_permalink( bbp_get_topic_id() );
    239             }
    240 
    241             $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    242 
    243         // Unpretty permalinks
    244         } else {
    245             $base = add_query_arg( 'paged', '%#%' );
    246         }
     219    if ( ! empty( $bbp->reply_query->found_posts ) && ! empty( $bbp->reply_query->posts_per_page ) ) {
    247220
    248221        // Figure out total pages
    249222        if ( true === $r['hierarchical'] ) {
    250223            $walker      = new BBP_Walker_Reply;
    251             $total_pages = ceil( (int) $walker->get_number_of_root_elements( $bbp->reply_query->posts ) / (int) $replies_per_page );
     224            $total_pages = ceil( $walker->get_number_of_root_elements( $bbp->reply_query->posts ) / $bbp->reply_query->posts_per_page );
    252225        } else {
    253             $total_pages = ceil( (int) $bbp->reply_query->found_posts / (int) $replies_per_page );
     226
     227            // Total for pagination boundaries
     228            $total_pages = ( $bbp->reply_query->posts_per_page === $bbp->reply_query->found_posts )
     229                ? 1
     230                : ceil( $bbp->reply_query->found_posts / $bbp->reply_query->posts_per_page );
     231
     232            // Pagination settings with filter
     233            $bbp_replies_pagination = apply_filters( 'bbp_replies_pagination', array(
     234                'base'    => bbp_get_replies_pagination_base( bbp_get_topic_id() ),
     235                'total'   => $total_pages,
     236                'current' => $bbp->reply_query->paged
     237            ) );
    254238
    255239            // Add pagination to query object
    256             $bbp->reply_query->pagination_links = paginate_links( apply_filters( 'bbp_replies_pagination', array(
    257                 'base'      => $base,
    258                 'format'    => '',
    259                 'total'     => $total_pages,
    260                 'current'   => (int) $bbp->reply_query->paged,
    261                 'prev_text' => is_rtl() ? '→' : '←',
    262                 'next_text' => is_rtl() ? '←' : '→',
    263                 'mid_size'  => 1,
    264                 'add_args'  => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
    265             ) ) );
    266 
    267             // Remove first page from pagination
    268             if ( bbp_use_pretty_urls() ) {
    269                 $bbp->reply_query->pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $bbp->reply_query->pagination_links );
    270             } else {
    271                 $bbp->reply_query->pagination_links = preg_replace( '/&paged=1(?=[^0-9])/m', '', $bbp->reply_query->pagination_links );
    272             }
     240            $bbp->reply_query->pagination_links = bbp_paginate_links( $bbp_replies_pagination );
    273241        }
    274242    }
     
    22242192    }
    22252193
     2194/** Pagination ****************************************************************/
     2195
     2196/**
     2197 * Return the base URL used inside of pagination links
     2198 *
     2199 * @since 2.6.0 bbPress (r6679)
     2200 *
     2201 * @param int $topic_id
     2202 * @return string
     2203 */
     2204function bbp_get_replies_pagination_base( $topic_id = 0 ) {
     2205
     2206    // If pretty permalinks are enabled, make our pagination pretty
     2207    if ( bbp_use_pretty_urls() ) {
     2208
     2209        // User's replies
     2210        if ( bbp_is_single_user_replies() ) {
     2211            $base = bbp_get_user_replies_created_url( bbp_get_displayed_user_id() );
     2212
     2213        // Root profile page
     2214        } elseif ( bbp_is_single_user() ) {
     2215            $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
     2216
     2217        // Page or single post
     2218        } elseif ( is_page() || is_single() ) {
     2219            $base = get_permalink();
     2220
     2221        // Single topic
     2222        } else {
     2223            $base = get_permalink( $topic_id );
     2224        }
     2225
     2226        $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
     2227
     2228    // Unpretty permalinks
     2229    } else {
     2230        $base = add_query_arg( 'paged', '%#%' );
     2231    }
     2232
     2233    // Filter & return
     2234    return apply_filters( 'bbp_get_replies_pagination_base', $base, $topic_id );
     2235}
     2236
    22262237/**
    22272238 * Output the topic pagination count
  • 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/**
  • trunk/src/includes/topics/template.php

    r6644 r6680  
    203203    // Limited the number of pages shown
    204204    if ( ! empty( $r['max_num_pages'] ) ) {
    205         $bbp->topic_query->max_num_pages = $r['max_num_pages'];
     205        $bbp->topic_query->max_num_pages = (int) $r['max_num_pages'];
    206206    }
    207207
     
    223223
    224224    // Only add pagination if query returned results
    225     if ( ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $bbp->topic_query->posts_per_page ) {
     225    if ( ( ! empty( $bbp->topic_query->post_count ) || ! empty( $bbp->topic_query->found_posts ) ) && ! empty( $bbp->topic_query->posts_per_page ) ) {
    226226
    227227        // Limit the number of topics shown based on maximum allowed pages
     
    231231
    232232        // Total topics for pagination boundaries
    233         $total = ( $r['posts_per_page'] === $bbp->topic_query->found_posts )
     233        $total_pages = ( $bbp->topic_query->posts_per_page === $bbp->topic_query->found_posts )
    234234            ? 1
    235             : ceil( (int) $bbp->topic_query->found_posts / (int) $r['posts_per_page'] );
     235            : ceil( $bbp->topic_query->found_posts / $bbp->topic_query->posts_per_page );
     236
     237        // Maybe add view-all args
     238        $add_args = bbp_get_view_all()
     239            ? array( 'view' => 'all' )
     240            : false;
    236241
    237242        // Pagination settings with filter
     
    239244            'base'      => bbp_get_topics_pagination_base( $r['post_parent'] ),
    240245            'format'    => '',
    241             'total'     => (int) $total,
    242             'current'   => (int) $bbp->topic_query->paged,
     246            'total'     => $total_pages,
     247            'current'   => $bbp->topic_query->paged,
    243248            'prev_text' => is_rtl() ? '→' : '←',
    244249            'next_text' => is_rtl() ? '←' : '→',
    245             'mid_size'  => 1
     250            'mid_size'  => 1,
     251            'add_args'  => $add_args,
    246252        ) );
    247253
    248254        // Add pagination to query object
    249         $bbp->topic_query->pagination_links = paginate_links( $bbp_topic_pagination );
    250 
    251         // Remove first page from pagination
    252         $bbp->topic_query->pagination_links = str_replace( bbp_get_paged_slug() . "/1/'", "'", $bbp->topic_query->pagination_links );
     255        $bbp->topic_query->pagination_links = bbp_paginate_links( $bbp_topic_pagination );
    253256    }
    254257
     
    781784
    782785        // If pretty permalinks are enabled, make our pagination pretty
    783         if ( bbp_use_pretty_urls() ) {
    784             $base = trailingslashit( get_permalink( $r['topic_id'] ) ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    785         } else {
    786             $base = add_query_arg( 'paged', '%#%', get_permalink( $r['topic_id'] ) );
    787         }
     786        $base = bbp_use_pretty_urls()
     787            ? trailingslashit( get_permalink( $r['topic_id'] ) ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' )
     788            : add_query_arg( 'paged', '%#%', get_permalink( $r['topic_id'] ) );
    788789
    789790        // Get total and add 1 if topic is included in the reply loop
     
    794795            $total++;
    795796        }
     797
     798        // Total for pagination boundaries
     799        $total_pages = ceil( $total / bbp_get_replies_per_page() );
    796800
    797801        // Maybe add view-all args
     
    800804            : false;
    801805
    802         // Add pagination to query object
    803         $pagination_links = paginate_links( array(
     806        // Pagination settings with filter
     807        $bbp_topic_pagination = apply_filters( 'bbp_get_topic_pagination', array(
    804808            'base'      => $base,
    805             'format'    => '',
    806             'total'     => ceil( (int) $total / (int) bbp_get_replies_per_page() ),
     809            'total'     => $total_pages,
    807810            'current'   => 0,
    808811            'prev_next' => false,
    809812            'mid_size'  => 2,
    810             'end_size'  => 3,
     813            'end_size'  => 2,
    811814            'add_args'  => $add_args
    812815        ) );
    813816
     817        // Add pagination to query object
     818        $pagination_links = bbp_paginate_links( $bbp_topic_pagination );
     819
     820        // Maybe add before and after to pagination links
    814821        if ( ! empty( $pagination_links ) ) {
    815 
    816             // Remove first page from pagination
    817             if ( bbp_use_pretty_urls() ) {
    818                 $pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $pagination_links );
    819             } else {
    820                 $pagination_links = preg_replace( '/&paged=1(?=[^0-9])/m', '', $pagination_links );
    821             }
    822 
    823             // Add before and after to pagination links
    824822            $pagination_links = $r['before'] . $pagination_links . $r['after'];
    825823        }
     
    29132911
    29142912    // Filter & return
    2915     return apply_filters( 'bbp_get_topics_pagination_base', $base );
     2913    return apply_filters( 'bbp_get_topics_pagination_base', $base, $forum_id );
    29162914}
    29172915
Note: See TracChangeset for help on using the changeset viewer.