From 0906863d2c96dec97e80b6fb9b7730852cf1007f Mon Sep 17 00:00:00 2001 From: markjaquith Date: Sat, 1 Sep 2007 03:41:50 +0000 Subject: [PATCH] Preserve query string arrays in add_query_arg(). fixes #4878 for trunk git-svn-id: http://svn.automattic.com/wordpress/trunk@5999 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/compat.php | 43 ++++++++++++++++++++++++++++----------- wp-includes/functions.php | 18 ++++++++-------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/wp-includes/compat.php b/wp-includes/compat.php index 4448708df5..38a6652cf0 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -98,18 +98,37 @@ if (!function_exists('array_change_key_case')) { } } -// From php.net -if(!function_exists('http_build_query')) { - function http_build_query( $formdata, $numeric_prefix = null, $key = null ) { - $res = array(); - foreach ((array)$formdata as $k=>$v) { - $tmp_key = urlencode(is_int($k) ? $numeric_prefix.$k : $k); - if ($key) $tmp_key = $key.'['.$tmp_key.']'; - $res[] = ( ( is_array($v) || is_object($v) ) ? http_build_query($v, null, $tmp_key) : $tmp_key."=".urlencode($v) ); - } - $separator = ini_get('arg_separator.output'); - return implode($separator, $res); - } +if (!function_exists('http_build_query')) { + function http_build_query($data, $prefix=null, $sep=null) { + return _http_build_query($data, $prefix, $sep); + } +} + +// from php.net (modified by Mark Jaquith to behave like the native PHP5 function) +function _http_build_query($data, $prefix=null, $sep=null, $key='') { + $ret = array(); + foreach ( (array) $data as $k => $v ) { + $k = urlencode($k); + if ( is_int($k) && $prefix != null ) + $k = $prefix.$k; + if ( !empty($key) ) + $k = $key . '%5B' . $k . '%5D'; + + if ( $v === NULL ) + continue; + elseif ( $v === FALSE ) + $v = '0'; + + if ( is_array($v) || is_object($v) ) + array_push($ret,_http_build_query($v, '', $sep, $k)); + else + array_push($ret, $k.'='.urlencode($v)); + } + + if ( NULL === $sep ) + $sep = ini_get('arg_separator.output'); + + return implode($sep, $ret); } if ( !function_exists('_') ) { diff --git a/wp-includes/functions.php b/wp-includes/functions.php index fdbfefdf19..3a95d0128a 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -644,17 +644,17 @@ function add_query_arg() { $qs[func_get_arg(0)] = func_get_arg(1); } - foreach($qs as $k => $v) { - if ( $v !== FALSE ) { - if ( $ret != '' ) - $ret .= '&'; - if ( empty($v) && !preg_match('|[?&]' . preg_quote($k, '|') . '=|', $query) ) - $ret .= $k; - else - $ret .= "$k=$v"; - } + foreach ( $qs as $k => $v ) { + if ( $v === false ) + unset($qs[$k]); } + + if ( ini_get('arg_separator.output') === '&') + $ret = http_build_query($qs, '', '&'); + else + $ret = _http_build_query($qs, NULL, '&'); $ret = trim($ret, '?'); + $ret = preg_replace('#=(&|$)#', '$1', $ret); $ret = $protocol . $base . $ret . $frag; $ret = rtrim($ret, '?'); return $ret;