From 0bebcf246fed49f16e58834e2a95c182736f63c3 Mon Sep 17 00:00:00 2001 From: rboren Date: Wed, 20 Oct 2004 21:28:37 +0000 Subject: [PATCH] Introducing add_query_arg() and remove_query_arg(). Props: Owen Winkler. git-svn-id: http://svn.automattic.com/wordpress/trunk@1823 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/options.php | 6 ++-- wp-includes/functions.php | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/wp-admin/options.php b/wp-admin/options.php index 5174f2fe9..a1cb8b0c8 100644 --- a/wp-admin/options.php +++ b/wp-admin/options.php @@ -65,8 +65,10 @@ case 'update': $message = sprintf(__('%d setting(s) saved... '), $any_changed); } - $referred = str_replace('?updated=true' , '', $_SERVER['HTTP_REFERER']); - $goback = str_replace('?updated=true', '', $_SERVER['HTTP_REFERER']) . '?updated=true'; + //$referred = str_replace('?updated=true' , '', $_SERVER['HTTP_REFERER']); + $referred = remove_query_arg('updated' , $_SERVER['HTTP_REFERER']); + //$goback = str_replace('?updated=true', '', $_SERVER['HTTP_REFERER']) . '?updated=true'; + $goback = add_query_arg('updated', 'true', $_SERVER['HTTP_REFERER']); $goback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $goback); header('Location: ' . $goback); break; diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 8a3eafc2e..ab6f198ea 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2245,4 +2245,65 @@ function is_plugin_page() { return false; } +/* +add_query_arg: Returns a modified querystring by adding +a single key & value or an associative array. +Setting a key value to emptystring removes the key. +Omitting oldquery_or_uri uses the $_SERVER value. + +Parameters: +add_query_arg(newkey, newvalue, oldquery_or_uri) or +add_query_arg(associative_array, oldquery_or_uri) +*/ +function add_query_arg() { + $ret = ''; + if(is_array(func_get_arg(0))) { + $uri = @func_get_arg(1); + } + else { + $uri = @func_get_arg(2); + } + if ('' == $uri) { + $uri = $_SERVER['REQUEST_URI']; + } + if (strstr($uri, '?')) { + $parts = explode('?', $uri, 2); + if (1 == count($parts)) { + $base = '?'; + $query = $parts[0]; + } + else { + $base = $parts[0] . '?'; + $query = $parts[1]; + } + } + else { + $base = $uri . '?'; + $query = ''; + } + parse_str($query, $qs); + 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($qs as $k => $v) + { + if($v != '') + { + if($ret != '') $ret .= '&'; + $ret .= "$k=$v"; + } + } + $ret = $base . $ret; + return trim($ret, '?'); +} + +function remove_query_arg($key, $query) { + add_query_arg($key, '', $query); +} ?>