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/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
Note: See TracChangeset for help on using the changeset viewer.