WordPress.org

Make WordPress Core

Changeset 28397


Ignore:
Timestamp:
05/14/2014 10:28:08 PM (8 years ago)
Author:
wonderboymusic
Message:

Eliminate use of extract() in paginate_links(). Adds unit tests. Moves tests/general/template.php (which only had one method) to tests/general/paginateLinks.php.

See #22400.

Location:
trunk
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r28379 r28397  
    24482448
    24492449    $args = wp_parse_args( $args, $defaults );
    2450     extract($args, EXTR_SKIP);
    24512450
    24522451    // Who knows what else people pass in $args
    2453     $total = (int) $total;
    2454     if ( $total < 2 )
     2452    $total = (int) $args['total'];
     2453    if ( $total < 2 ) {
    24552454        return;
    2456     $current  = (int) $current;
    2457     $end_size = 0  < (int) $end_size ? (int) $end_size : 1; // Out of bounds?  Make it the default.
    2458     $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
    2459     $add_args = is_array($add_args) ? $add_args : false;
     2455    }
     2456    $current  = (int) $args['current'];
     2457    $end_size = (int) $args['end_size']; // Out of bounds?  Make it the default.
     2458    if ( $end_size < 1 ) {
     2459        $end_size = 1;
     2460    }
     2461    $mid_size = (int) $args['mid_size'];
     2462    if ( $mid_size < 0 ) {
     2463        $mid_size = 2;
     2464    }
     2465    $add_args = is_array( $args['add_args'] ) ? $args['add_args'] : false;
    24602466    $r = '';
    24612467    $page_links = array();
    24622468    $dots = false;
    24632469
    2464     if ( $prev_next && $current && 1 < $current ) :
    2465         $link = str_replace('%_%', 2 == $current ? '' : $format, $base);
    2466         $link = str_replace('%#%', $current - 1, $link);
     2470    if ( $args['prev_next'] && $current && 1 < $current ) :
     2471        $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
     2472        $link = str_replace( '%#%', $current - 1, $link );
    24672473        if ( $add_args )
    24682474            $link = add_query_arg( $add_args, $link );
    2469         $link .= $add_fragment;
     2475        $link .= $args['add_fragment'];
    24702476
    24712477        /**
     
    24762482         * @param string $link The paginated link URL.
    24772483         */
    2478         $page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $prev_text . '</a>';
     2484        $page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['prev_text'] . '</a>';
    24792485    endif;
    24802486    for ( $n = 1; $n <= $total; $n++ ) :
    24812487        if ( $n == $current ) :
    2482             $page_links[] = "<span class='page-numbers current'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</span>";
     2488            $page_links[] = "<span class='page-numbers current'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . "</span>";
    24832489            $dots = true;
    24842490        else :
    2485             if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
    2486                 $link = str_replace('%_%', 1 == $n ? '' : $format, $base);
    2487                 $link = str_replace('%#%', $n, $link);
     2491            if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
     2492                $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
     2493                $link = str_replace( '%#%', $n, $link );
    24882494                if ( $add_args )
    24892495                    $link = add_query_arg( $add_args, $link );
    2490                 $link .= $add_fragment;
     2496                $link .= $args['add_fragment'];
    24912497
    24922498                /** This filter is documented in wp-includes/general-template.php */
    2493                 $page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</a>";
     2499                $page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . "</a>";
    24942500                $dots = true;
    2495             elseif ( $dots && !$show_all ) :
     2501            elseif ( $dots && ! $args['show_all'] ) :
    24962502                $page_links[] = '<span class="page-numbers dots">' . __( '&hellip;' ) . '</span>';
    24972503                $dots = false;
     
    24992505        endif;
    25002506    endfor;
    2501     if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
    2502         $link = str_replace('%_%', $format, $base);
    2503         $link = str_replace('%#%', $current + 1, $link);
     2507    if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) :
     2508        $link = str_replace( '%_%', $args['format'], $args['base'] );
     2509        $link = str_replace( '%#%', $current + 1, $link );
    25042510        if ( $add_args )
    25052511            $link = add_query_arg( $add_args, $link );
    2506         $link .= $add_fragment;
     2512        $link .= $args['add_fragment'];
    25072513
    25082514        /** This filter is documented in wp-includes/general-template.php */
    2509         $page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
     2515        $page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['next_text'] . '</a>';
    25102516    endif;
    2511     switch ( $type ) {
     2517    switch ( $args['type'] ) {
    25122518        case 'array' :
    25132519            return $page_links;
  • trunk/tests/phpunit/tests/general/paginateLinks.php

    r28396 r28397  
    11<?php
    22
    3 class Tests_General_Template extends WP_UnitTestCase {
     3class Tests_Paginate_Links extends WP_UnitTestCase {
    44
    55    private $i18n_count = 0;
     6
     7    function test_defaults() {
     8        $expected =<<<EXPECTED
     9<a class='page-numbers' href=''>1</a>
     10<span class="page-numbers dots">&hellip;</span>
     11<a class='page-numbers' href='?page=50'>50</a>
     12EXPECTED;
     13
     14        $links = paginate_links( array( 'total' => 50 ) );
     15        $this->assertEquals( $expected, $links );
     16    }
     17
     18    function test_format() {
     19        $expected =<<<EXPECTED
     20<a class='page-numbers' href=''>1</a>
     21<span class="page-numbers dots">&hellip;</span>
     22<a class='page-numbers' href='/page/50/'>50</a>
     23EXPECTED;
     24
     25        $links = paginate_links( array( 'total' => 50, 'format' => '/page/%#%/' ) );
     26        $this->assertEquals( $expected, $links );
     27    }
     28
     29    function test_prev_next_false() {
     30        $expected =<<<EXPECTED
     31<a class='page-numbers' href=''>1</a>
     32<span class='page-numbers current'>2</span>
     33<a class='page-numbers' href='?page=3'>3</a>
     34<a class='page-numbers' href='?page=4'>4</a>
     35<span class="page-numbers dots">&hellip;</span>
     36<a class='page-numbers' href='?page=50'>50</a>
     37EXPECTED;
     38
     39        $links = paginate_links( array( 'total' => 50, 'prev_next' => false, 'current' => 2 ) );
     40        $this->assertEquals( $expected, $links );
     41    }
     42
     43    function test_prev_next_true() {
     44        $expected =<<<EXPECTED
     45<a class="prev page-numbers" href="">&laquo; Previous</a>
     46<a class='page-numbers' href=''>1</a>
     47<span class='page-numbers current'>2</span>
     48<a class='page-numbers' href='?page=3'>3</a>
     49<a class='page-numbers' href='?page=4'>4</a>
     50<span class="page-numbers dots">&hellip;</span>
     51<a class='page-numbers' href='?page=50'>50</a>
     52<a class="next page-numbers" href="?page=3">Next &raquo;</a>
     53EXPECTED;
     54
     55        $links = paginate_links( array( 'total' => 50, 'prev_next' => true, 'current' => 2 ) );
     56        $this->assertEquals( $expected, $links );
     57    }
    658
    759    function increment_i18n_count() {
     
    2880        remove_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
    2981    }
    30 
    3182}
Note: See TracChangeset for help on using the changeset viewer.