Skip to content

Commit 5d5c9bf

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@52187 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3c4d1d8 commit 5d5c9bf

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/wp-includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,7 @@ function add_query_arg( ...$args ) {
11691169
$ret = preg_replace( '#=(&|$)#', '$1', $ret );
11701170
$ret = $protocol . $base . $ret . $frag;
11711171
$ret = rtrim( $ret, '?' );
1172+
$ret = str_replace( '?#', '#', $ret );
11721173
return $ret;
11731174
}
11741175

tests/phpunit/tests/functions.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,62 @@ public function test_add_query_arg_numeric_keys() {
665665
$this->assertSame( 'foo=bar&1=2', $url );
666666
}
667667

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+
);
722+
}
723+
668724
/**
669725
* @ticket 21594
670726
*/

0 commit comments

Comments
 (0)