From d1235e323ee290d4cc14d5d4eb66ce367f902b27 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 1 Apr 2015 19:14:46 +0000 Subject: [PATCH] Respect numerical keys in `add_query_arg()`, use `array_replace()` instead of `array_merge()`. Adds unit test. Props tyxla. Fixes #31306. git-svn-id: https://develop.svn.wordpress.org/trunk@31966 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 2 +- tests/phpunit/tests/functions.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index fbe1219866..42f6873b64 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -782,7 +782,7 @@ function add_query_arg() { $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string if ( is_array( $args[0] ) ) { $kayvees = $args[0]; - $qs = array_merge( $qs, $kayvees ); + $qs = array_replace( $qs, $kayvees ); } else { $qs[ $args[0] ] = $args[1]; } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 527780ae27..4d3a4bbdd5 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -258,6 +258,20 @@ class Tests_Functions extends WP_UnitTestCase { $_SERVER['REQUEST_URI'] = $old_req_uri; } + /** + * @ticket 31306 + */ + function test_add_query_arg_numeric_keys() { + $url = add_query_arg( array( 'foo' => 'bar' ), '1=1' ); + $this->assertEquals('1=1&foo=bar', $url); + + $url = add_query_arg( array( 'foo' => 'bar', '1' => '2' ), '1=1' ); + $this->assertEquals('1=2&foo=bar', $url); + + $url = add_query_arg( array( '1' => '2' ), 'foo=bar' ); + $this->assertEquals('foo=bar&1=2', $url); + } + /** * @ticket 21594 */