generated from christian-krieg/yourls-plugin-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented base URL rewrite via query parameter
* When called over the WebAPI, the user can specify the base URL of the shortlink with a query parameter * The name of this query parameter can be freely chosen and configured in the Plugin settings * This way, multiple domains can be easily handeled by one YOURLS instance
- Loading branch information
1 parent
203183e
commit e0ff25a
Showing
1 changed file
with
25 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
Plugin URI: https://github.com/christian-krieg/yourls-plugin-rewrite-yourls-site | ||
Description: Rewrite short URLs such that they point to alternative (base) URLs | ||
(e.g., https://acme.com/r), different from the base domain of the | ||
Yourls installation. This can be helpful if you want to server | ||
Yourls installation. This can be helpful if you want to serve | ||
Yourls from a sub-domain (e.g., https://link.acme.com), | ||
Version: 1.0.0 | ||
Version: 1.0.2 | ||
Author: Christian Krieg <[email protected]> | ||
*/ | ||
|
||
|
@@ -42,7 +42,14 @@ | |
// | ||
yourls_add_filter( 'yourls_link', 'ck_base_url_rewrite' ); | ||
function ck_base_url_rewrite ($link) { | ||
$base_url = yourls_get_option( 'ck_base_url', ''); | ||
$default_base_url = yourls_get_option( 'ck_default_base_url', ''); | ||
$base_url_query_parameter = yourls_get_option( 'ck_base_url_query_parameter', ''); | ||
|
||
$base_url = ( | ||
isset($_REQUEST[$base_url_query_parameter]) | ||
? $_REQUEST[$base_url_query_parameter] | ||
: $default_base_url | ||
); | ||
|
||
if ( empty($base_url) ) | ||
return $link; | ||
|
@@ -70,14 +77,17 @@ function ck_base_url_rewrite_init() { | |
// The function that will draw the admin page | ||
function ck_base_url_rewrite_display_page () { | ||
// Check if form was submitted | ||
if( isset( $_POST['base_url'] ) ) { | ||
if( isset( $_POST['default_base_url'] ) or | ||
isset( $_POST['base_url_query_parameter'] ) | ||
) { | ||
// If so, verify nonce | ||
yourls_verify_nonce( 'base_url_settings' ); | ||
// and process submission if nonce is valid | ||
ck_base_url_rewrite_settings_update(); | ||
} | ||
|
||
$base_url = yourls_get_option('ck_base_url', ''); | ||
$default_base_url = yourls_get_option('ck_default_base_url', ''); | ||
$base_url_query_parameter = yourls_get_option('ck_base_url_query_parameter', ''); | ||
$nonce = yourls_create_nonce('base_url_settings'); | ||
|
||
echo <<<HTML | ||
|
@@ -86,8 +96,12 @@ function ck_base_url_rewrite_display_page () { | |
<form method="post"> | ||
<input type="hidden" name="nonce" value="$nonce" /> | ||
<p> | ||
<label>Shortlink Base URL:</label> | ||
<input type="text" name="base_url" value="$base_url" /> | ||
<label>Shortlink Default Base URL:</label> | ||
<input type="text" name="default_base_url" value="$default_base_url" /> | ||
</p> | ||
<p> | ||
<label>Query parameter (Leave blank to disable):</label> | ||
<input type="text" name="base_url_query_parameter" value="$base_url_query_parameter" /> | ||
</p> | ||
<p><input type="submit" value="Save" class="button" /></p> | ||
</form> | ||
|
@@ -98,7 +112,9 @@ function ck_base_url_rewrite_display_page () { | |
|
||
// The function that updates the configuration option | ||
function ck_base_url_rewrite_settings_update() { | ||
$base_url = $_POST['base_url']; | ||
yourls_update_option( 'ck_base_url', $base_url ); | ||
$default_base_url = $_POST['default_base_url']; | ||
$base_url_query_parameter = $_POST['base_url_query_parameter']; | ||
yourls_update_option( 'ck_default_base_url', $default_base_url ); | ||
yourls_update_option( 'ck_base_url_query_parameter', $base_url_query_parameter); | ||
} | ||
|