Skip to content

Instantly share code, notes, and snippets.

@hakre
Created March 23, 2011 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/882734 to your computer and use it in GitHub Desktop.
Save hakre/882734 to your computer and use it in GitHub Desktop.
behavior test add_query_arg_old
<?php
/**
* test-query-arg.php
*
* USAGE:
*
* Place into wordpress install folder, then
* request it via the browser.
*
* @see https://core.trac.wordpress.org/ticket/16943
*/
error_reporting(-1); ini_set('display_errors', 1);
require dirname(__FILE__) . '/wp-load.php';
$key = 'test';
$val = '123';
$pair = array( $key => $val );
$tests = array(
'Adding Empty String' => array('new', '', '/example.com/?new=test&mix=test'),
'Adding Empty Zero' => array('new', 0, '/example.com/?new=test&mix=test'),
'Verplant' => array('mix', false, 'mix=?mix=test'),
'Verplant 2' => array('mix', '?äöü', 'mix=?mix=test'),
'Remove One Param' => array('mix'),
'Remove Two Param' => array('mix', false),
'Remove Three Param' => array('mix', false, 'mix=das&so=toll'),
'Simple URL' => array($pair, 'http://example.com/more/'),
'Request URI' => array($pair),
'Empty URL 1' => array($pair, ''),
'Empty URL 2' => array($key, $val, ''),
array($key, $val),
array($pair),
array(),
);
// skip first 3 tests as they work
// array_splice($tests, 0, -4);
$funcs = array(
'add_query_arg',
'add_query_arg_old',
);
$label = array('ok', 'failed');
$bgcolor = array('green', 'red');
$color = array('white', 'white');
foreach($tests as $name => $test) {
$params = $test;
echo '<table border="1">';
echo "<tr><th colspan=\"2\">Test $name</th></tr>\n";
foreach($funcs as $index => $func) {
$result[$index] = call_user_func_array($func, $params);
echo "<tr><td>$func:</td><td>{$result[$index]}</td></tr>\n";
}
$check = count(array_unique($result))-1;
echo "<tr bgcolor=\"{$bgcolor[$check]}\" style=\"color:{$color[$check]}\"><td>Result:</td><td><b>{$label[$check]}</b></td></tr>\n";
echo '</table>';
}
return;
// original implementation:
function add_query_arg_old() {
$ret = '';
if ( is_array( func_get_arg(0) ) ) {
if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
$uri = $_SERVER['REQUEST_URI'];
else
$uri = @func_get_arg( 1 );
} else {
if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
$uri = $_SERVER['REQUEST_URI'];
else
$uri = @func_get_arg( 2 );
}
if ( $frag = strstr( $uri, '#' ) )
$uri = substr( $uri, 0, -strlen( $frag ) );
else
$frag = '';
if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
$protocol = $matches[0];
$uri = substr( $uri, strlen( $protocol ) );
} else {
$protocol = '';
}
if ( strpos( $uri, '?' ) !== false ) {
$parts = explode( '?', $uri, 2 );
if ( 1 == count( $parts ) ) {
$base = '?';
$query = $parts[0];
} else {
$base = $parts[0] . '?';
$query = $parts[1];
}
} elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
$base = $uri . '?';
$query = '';
} else {
$base = '';
$query = $uri;
}
wp_parse_str( $query, $qs );
$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
if ( is_array( func_get_arg( 0 ) ) ) {
$kayvees = func_get_arg( 0 );
$qs = array_merge( $qs, $kayvees );
} else {
$qs[func_get_arg( 0 )] = func_get_arg( 1 );
}
foreach ( (array) $qs as $k => $v ) {
if ( $v === false )
unset( $qs[$k] );
}
$ret = build_query( $qs );
$ret = trim( $ret, '?' );
$ret = preg_replace( '#=(&|$)#', '$1', $ret );
$ret = $protocol . $base . $ret . $frag;
$ret = rtrim( $ret, '?' );
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment