Skip to content

Commit

Permalink
Preserve query string arrays in add_query_arg(). fixes #4878 for trunk
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.automattic.com/wordpress/trunk@5999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
markjaquith committed Sep 1, 2007
1 parent 93840b1 commit 0906863
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
43 changes: 31 additions & 12 deletions wp-includes/compat.php
Expand Up @@ -98,18 +98,37 @@ function array_change_key_case($input, $case = CASE_LOWER)
}
}

// 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('_') ) {
Expand Down
18 changes: 9 additions & 9 deletions wp-includes/functions.php
Expand Up @@ -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;
Expand Down

0 comments on commit 0906863

Please sign in to comment.