public
Created

behavior test add_query_arg_old

  • Download Gist
test-query-arg.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
<?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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.