Make WordPress Core

Changeset 28671


Ignore:
Timestamp:
06/05/2014 02:09:12 AM (10 years ago)
Author:
SergeyBiryukov
Message:

Avoid an empty href attribute in paginate_links(). Add unit tests.

props obenland, Nessworthy.
fixes #24606.

Location:
trunk
Files:
2 edited

Legend:

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

    r28669 r28671  
    24772477    $page_links = array();
    24782478    $dots = false;
     2479    $base = str_replace( '%_%', $args['format'], $args['base'] );
    24792480
    24802481    if ( $args['prev_next'] && $current && 1 < $current ) :
    2481         $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
    2482         $link = str_replace( '%#%', $current - 1, $link );
     2482        $link = str_replace( '%#%', $current - 1, $base );
    24832483        if ( $add_args )
    24842484            $link = add_query_arg( $add_args, $link );
     
    25002500        else :
    25012501            if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
    2502                 $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
    2503                 $link = str_replace( '%#%', $n, $link );
     2502                $link = str_replace( '%#%', $n, $base );
    25042503                if ( $add_args )
    25052504                    $link = add_query_arg( $add_args, $link );
     
    25162515    endfor;
    25172516    if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) :
    2518         $link = str_replace( '%_%', $args['format'], $args['base'] );
    2519         $link = str_replace( '%#%', $current + 1, $link );
     2517        $link = str_replace( '%#%', $current + 1, $base );
    25202518        if ( $add_args )
    25212519            $link = add_query_arg( $add_args, $link );
  • trunk/tests/phpunit/tests/general/paginateLinks.php

    r28397 r28671  
    8080        remove_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
    8181    }
     82
     83    /**
     84     * @ticket 24606
     85     */
     86    function test_paginate_links_base_value() {
     87
     88        // Current page: 2
     89        $links = paginate_links( array(
     90            'current'  => 2,
     91            'total'    => 5,
     92            'end_size' => 1,
     93            'mid_size' => 1,
     94            'type'     => 'array',
     95        ) );
     96
     97        // It's supposed to link to page 1:
     98        $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=1' ) ), $links[0] );
     99        $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=1' ) ), $links[1] );
     100
     101        // It's not supposed to have an empty href.
     102        $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'prev page-numbers', 'href' => '' ) ), $links[0] );
     103        $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'page-numbers',      'href' => '' ) ), $links[1] );
     104
     105        // Current page: 1
     106        $links = paginate_links( array(
     107            'current'  => 1,
     108            'total'    => 5,
     109            'end_size' => 1,
     110            'mid_size' => 1,
     111            'type'     => 'array',
     112        ) );
     113
     114        $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] );
     115        $this->assertTag( array( 'tag' => 'a',    'attributes' => array( 'href' => '?page=2'  ) ), $links[1] );
     116    }
     117
    82118}
Note: See TracChangeset for help on using the changeset viewer.