WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/12/2015 02:19:44 AM (6 years ago)
Author:
dd32
Message:

In paginate_links(), don't override custom format arguments when setting up default 'add_args'.

Since 4.1 [29780], the default value of the 'add_args' argument in
paginate_links() has been determined by parsing the current URL. This change
had the side effect of overriding custom values of 'format' that changed the
pagination query var, with the result that plugins using paginate_links()
with a custom format generated the incorrect links unless explicitly
declaring 'add_args=false' to prevent the default values from overriding. We
fix this behavior by parsing URL query vars into the 'add_args' array only
after the explicit function params have been parsed, and by skipping the
current page's pagination query var when doing this parsing (to avoid the
override).

Props obenland.
Merges [31203], [31432] to the 4.1 branch.
Fixes #30831.

Location:
branches/4.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/tests/phpunit/tests/general/paginateLinks.php

    r30133 r31433  
    230230        }
    231231    }
     232
     233    /**
     234     * @ticket 30831
     235     */
     236    function test_paginate_links_with_custom_query_args() {
     237        add_filter( 'get_pagenum_link', array( $this, 'add_query_arg' ) );
     238        $links = paginate_links( array(
     239            'current'  => 2,
     240            'total'    => 5,
     241            'end_size' => 1,
     242            'mid_size' => 1,
     243            'type'     => 'array',
     244            'add_args' => array(
     245                'baz' => 'qux',
     246            ),
     247        ) );
     248        remove_filter( 'get_pagenum_link', array( $this, 'add_query_arg' ) );
     249
     250        $document = new DOMDocument();
     251        $document->preserveWhiteSpace = false;
     252
     253        $data = array(
     254            0 => home_url( '/?baz=qux&foo=bar&s=search+term' ),
     255            1 => home_url( '/?baz=qux&foo=bar&s=search+term' ),
     256            3 => home_url( '/?paged=3&baz=qux&foo=bar&s=search+term' ),
     257            5 => home_url( '/?paged=5&baz=qux&foo=bar&s=search+term' ),
     258            6 => home_url( '/?paged=3&baz=qux&foo=bar&s=search+term' ),
     259        );
     260
     261        foreach ( $data as $index => $expected_href ) {
     262            $document->loadHTML( $links[ $index ] );
     263            $tag = $document->getElementsByTagName( 'a' )->item( 0 );
     264            $this->assertNotNull( $tag );
     265
     266            $href = $tag->attributes->getNamedItem( 'href' )->value;
     267            $this->assertEquals( $expected_href, $href );
     268        }
     269    }
     270
     271    /**
     272     * @ticket 30831
     273     */
     274    public function test_paginate_links_should_allow_non_default_format_without_add_args() {
     275        // Fake the query params.
     276        $request_uri = $_SERVER['REQUEST_URI'];
     277        $_SERVER['REQUEST_URI'] = add_query_arg( 'foo', 3, home_url() );
     278
     279        $links = paginate_links( array(
     280            'base'    => add_query_arg( 'foo', '%#%' ),
     281            'format'  => '',
     282            'total'   => 5,
     283            'current' => 3,
     284            'type'    => 'array',
     285        ) );
     286
     287        $this->assertContains( '?foo=1', $links[1] );
     288        $this->assertContains( '?foo=2', $links[2] );
     289        $this->assertContains( '?foo=4', $links[4] );
     290        $this->assertContains( '?foo=5', $links[5] );
     291
     292        $_SERVER['REQUEST_URI'] = $request_uri;
     293    }
     294
     295    /**
     296     * @ticket 30831
     297     */
     298    public function test_paginate_links_should_allow_add_args_to_be_bool_false() {
     299        // Fake the query params.
     300        $request_uri = $_SERVER['REQUEST_URI'];
     301        $_SERVER['REQUEST_URI'] = add_query_arg( 'foo', 3, home_url() );
     302
     303        $links = paginate_links( array(
     304            'add_args' => false,
     305            'base'    => add_query_arg( 'foo', '%#%' ),
     306            'format'  => '',
     307            'total'   => 5,
     308            'current' => 3,
     309            'type'    => 'array',
     310        ) );
     311
     312        $this->assertContains( "<span class='page-numbers current'>3</span>", $links );
     313    }
    232314}
Note: See TracChangeset for help on using the changeset viewer.