From 5d5c9bf6a78a52a48981abb651c321da10109fde Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 16 Nov 2021 18:48:47 +0000 Subject: [PATCH] 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 --- src/wp-includes/functions.php | 1 + tests/phpunit/tests/functions.php | 56 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index b71a83761c..2417bcdf76 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -1169,6 +1169,7 @@ function add_query_arg( ...$args ) { $ret = preg_replace( '#=(&|$)#', '$1', $ret ); $ret = $protocol . $base . $ret . $frag; $ret = rtrim( $ret, '?' ); + $ret = str_replace( '?#', '#', $ret ); return $ret; } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index bcf8525216..96dee380ec 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -665,6 +665,62 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertSame( 'foo=bar&1=2', $url ); } + /** + * Tests that add_query_arg removes the question mark when + * a parameter is set to false. + * + * @dataProvider data_add_query_arg_removes_question_mark + * + * @ticket 44499 + * @group add_query_arg + * + * @covers ::add_query_arg + * + * @param string $url Url to test. + * @param string $expected Expected URL. + */ + public function test_add_query_arg_removes_question_mark( $url, $expected, $key = 'param', $value = false ) { + $this->assertSame( $expected, add_query_arg( $key, $value, $url ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_add_query_arg_removes_question_mark() { + return array( + 'anchor' => array( + 'url' => 'http://example.org?#anchor', + 'expected' => 'http://example.org#anchor', + ), + '/ then anchor' => array( + 'url' => 'http://example.org/?#anchor', + 'expected' => 'http://example.org/#anchor', + ), + 'invalid query param and anchor' => array( + 'url' => 'http://example.org?param=value#anchor', + 'expected' => 'http://example.org#anchor', + ), + '/ then invalid query param and anchor' => array( + 'url' => 'http://example.org/?param=value#anchor', + 'expected' => 'http://example.org/#anchor', + ), + '?#anchor when adding valid key/value args' => array( + 'url' => 'http://example.org?#anchor', + 'expected' => 'http://example.org?foo=bar#anchor', + 'key' => 'foo', + 'value' => 'bar', + ), + '/?#anchor when adding valid key/value args' => array( + 'url' => 'http://example.org/?#anchor', + 'expected' => 'http://example.org/?foo=bar#anchor', + 'key' => 'foo', + 'value' => 'bar', + ), + ); + } + /** * @ticket 21594 */