View difference between Paste ID: wHttk8UT and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | function add_query_arg() { |
2 | $ret = ''; | |
3 | if ( is_array( func_get_arg(0) ) ) { | |
4 | if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) ) | |
5 | if ( get_bloginfo('url') != '' ) | |
6 | $uri = get_bloginfo('url') . $_SERVER['REQUEST_URI']; | |
7 | else | |
8 | $uri = $_SERVER['REQUEST_URI']; | |
9 | else | |
10 | $uri = @func_get_arg( 1 ); | |
11 | } else { | |
12 | if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) ) | |
13 | if ( get_bloginfo('url') != '' ) | |
14 | $uri = get_bloginfo('url') . $_SERVER['REQUEST_URI']; | |
15 | else | |
16 | $uri = $_SERVER['REQUEST_URI']; | |
17 | else | |
18 | $uri = @func_get_arg( 2 ); | |
19 | } | |
20 | ||
21 | if ( $frag = strstr( $uri, '#' ) ) | |
22 | $uri = substr( $uri, 0, -strlen( $frag ) ); | |
23 | else | |
24 | $frag = ''; | |
25 | ||
26 | if ( preg_match( '|^https?://|i', $uri, $matches ) ) { | |
27 | $protocol = $matches[0]; | |
28 | $uri = substr( $uri, strlen( $protocol ) ); | |
29 | } else { | |
30 | $protocol = ''; | |
31 | } | |
32 | ||
33 | if ( strpos( $uri, '?' ) !== false ) { | |
34 | $parts = explode( '?', $uri, 2 ); | |
35 | if ( 1 == count( $parts ) ) { | |
36 | $base = '?'; | |
37 | $query = $parts[0]; | |
38 | } else { | |
39 | $base = $parts[0] . '?'; | |
40 | $query = $parts[1]; | |
41 | } | |
42 | } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) { | |
43 | $base = $uri . '?'; | |
44 | $query = ''; | |
45 | } else { | |
46 | $base = ''; | |
47 | $query = $uri; | |
48 | } | |
49 | ||
50 | wp_parse_str( $query, $qs ); | |
51 | $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string | |
52 | if ( is_array( func_get_arg( 0 ) ) ) { | |
53 | $kayvees = func_get_arg( 0 ); | |
54 | $qs = array_merge( $qs, $kayvees ); | |
55 | } else { | |
56 | $qs[func_get_arg( 0 )] = func_get_arg( 1 ); | |
57 | } | |
58 | ||
59 | foreach ( (array) $qs as $k => $v ) { | |
60 | if ( $v === false ) | |
61 | unset( $qs[$k] ); | |
62 | } | |
63 | ||
64 | $homeurl = str_replace(( is_ssl() ? 'https://' : 'http://' ), '', get_bloginfo('url')); | |
65 | ||
66 | $ret = build_query( $qs ); | |
67 | $ret = trim( $ret, '?' ); | |
68 | $ret = preg_replace( '#=(&|$)#', '$1', $ret ); | |
69 | $ret = $protocol . $base . $ret . $frag; | |
70 | $ret = rtrim( $ret, '?' ); | |
71 | $ret = str_replace($_SERVER['HTTP_HOST'], $homeurl, $ret); | |
72 | return $ret; | |
73 | } |