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/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.