Created
January 3, 2019 20:53
-
-
Save rinogo/345dcfb149f1020f501b262ff45035d2 to your computer and use it in GitHub Desktop.
YOURLS plugin: keep query string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Keep Query String | |
Description: Adds short URL query string, if any, to the long URL | |
Version: 0.1 | |
Author: Ozh | |
*/ | |
yourls_add_filter('redirect_location', 'ozh_kqs'); | |
function ozh_kqs($url){ | |
if (yourls_is_GO()) { | |
//$url = yourls_add_query_arg($_GET, $url); | |
$url = ozh_kqs_yourls_add_query_arg($_GET, $url); | |
} | |
return $url; | |
} | |
// well I think that's it | |
/** | |
* Add a query var to a URL and return URL. Completely stolen from WP. | |
* | |
* Works with one of these parameter patterns: | |
* array( 'var' => 'value' ) | |
* array( 'var' => 'value' ), $url | |
* 'var', 'value' | |
* 'var', 'value', $url | |
* If $url omitted, uses $_SERVER['REQUEST_URI'] | |
* | |
* The result of this function call is a URL : it should be escaped before being printed as HTML | |
* | |
* @since 1.5 | |
* @param string|array $param1 Either newkey or an associative_array. | |
* @param string $param2 Either newvalue or oldquery or URI. | |
* @param string $param3 Optional. Old query or URI. | |
* @return string New URL query string. | |
*/ | |
function ozh_kqs_yourls_add_query_arg() { | |
$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 ); | |
} | |
$uri = str_replace( '&', '&', $uri ); | |
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; | |
} | |
parse_str( $query, $qs ); | |
//THE FOLLOWING LINE IS COMMENTED OUT DUE TO THE ISSUE DESCRIBED AT https://gist.github.com/ozh/1d078e9cbd5f9757be3bb32011c48461#gistcomment-2172150 | |
//$qs = yourls_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 = http_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
Original: https://gist.github.com/ozh/1d078e9cbd5f9757be3bb32011c48461#gistcomment-2172150
There's probably a better way to do this, but this simple fix seems to work for now...