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