Make WordPress Core

Changeset 52187


Ignore:
Timestamp:
11/16/2021 06:48:47 PM (5 years ago)
Author:
hellofromTonya
Message:

HTTP API: Remove empty ? when only anchor remains in add_query_arg().

If after processing through add_query_arg() a ?# remains, this commit removes the unnecessary and unused ? character as there are no query args in the URL.

Includes tests.

Follow-up to [1823], [5193], [5999], [6005].

Props benjaminanakenam, sabernhardt, costdev, hellofromTonya.
Fixes #44499.

Location:
trunk
Files:
2 edited

Legend:

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

    r52088 r52187  
    11701170        $ret = $protocol . $base . $ret . $frag;
    11711171        $ret = rtrim( $ret, '?' );
     1172        $ret = str_replace( '?#', '#', $ret );
    11721173        return $ret;
    11731174}
  • trunk/tests/phpunit/tests/functions.php

    r52010 r52187  
    664664                $url = add_query_arg( array( '1' => '2' ), 'foo=bar' );
    665665                $this->assertSame( 'foo=bar&1=2', $url );
     666        }
     667
     668        /**
     669         * Tests that add_query_arg removes the question mark when
     670         * a parameter is set to false.
     671         *
     672         * @dataProvider data_add_query_arg_removes_question_mark
     673         *
     674         * @ticket 44499
     675         * @group  add_query_arg
     676         *
     677         * @covers ::add_query_arg
     678         *
     679         * @param string $url      Url to test.
     680         * @param string $expected Expected URL.
     681         */
     682        public function test_add_query_arg_removes_question_mark( $url, $expected, $key = 'param', $value = false ) {
     683                $this->assertSame( $expected, add_query_arg( $key, $value, $url ) );
     684        }
     685
     686        /**
     687         * Data provider.
     688         *
     689         * @return array
     690         */
     691        public function data_add_query_arg_removes_question_mark() {
     692                return array(
     693                        'anchor'                                     => array(
     694                                'url'      => 'http://example.org?#anchor',
     695                                'expected' => 'http://example.org#anchor',
     696                        ),
     697                        '/ then anchor'                              => array(
     698                                'url'      => 'http://example.org/?#anchor',
     699                                'expected' => 'http://example.org/#anchor',
     700                        ),
     701                        'invalid query param and anchor'             => array(
     702                                'url'      => 'http://example.org?param=value#anchor',
     703                                'expected' => 'http://example.org#anchor',
     704                        ),
     705                        '/ then invalid query param and anchor'      => array(
     706                                'url'      => 'http://example.org/?param=value#anchor',
     707                                'expected' => 'http://example.org/#anchor',
     708                        ),
     709                        '?#anchor when adding valid key/value args'  => array(
     710                                'url'      => 'http://example.org?#anchor',
     711                                'expected' => 'http://example.org?foo=bar#anchor',
     712                                'key'      => 'foo',
     713                                'value'    => 'bar',
     714                        ),
     715                        '/?#anchor when adding valid key/value args' => array(
     716                                'url'      => 'http://example.org/?#anchor',
     717                                'expected' => 'http://example.org/?foo=bar#anchor',
     718                                'key'      => 'foo',
     719                                'value'    => 'bar',
     720                        ),
     721                );
    666722        }
    667723
Note: See TracChangeset for help on using the changeset viewer.