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.
* It is now possible to define the name of a query parameter in the plugin settings, which can be used in the YOURLS REST API * If used, the query parameter's value is used as base URL for generated shortlinks
- Loading branch information
1 parent
e0ff25a
commit 728024f
Showing
3 changed files
with
96 additions
and
14 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -6,14 +6,13 @@ | |
(e.g., https://acme.com/r), different from the base domain of the | ||
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.2 | ||
Version: 1.1.0 | ||
Author: Christian Krieg <[email protected]> | ||
*/ | ||
|
||
// No direct call | ||
if( !defined( 'YOURLS_ABSPATH' ) ) die(); | ||
|
||
|
||
// | ||
// This filter function actually rewrites short URLs by replacing the original | ||
// base URL (as specified by ``YOURLS_SITE`` in `config.php`) with the base URL | ||
|
@@ -26,29 +25,39 @@ | |
// | ||
// YOURLS_SITE: "https://link.acme.com" (a constant defined in `config.php`) | ||
// ck_base_url: "https://acme.com/r" (a configuration option) | ||
// ck_base_url_query_parameter: "base_url" (a configuration option) | ||
// | ||
// Then, YOURLS will create the (example) short link | ||
// Then, YOURLS will create the (example) short link (when accessed from the | ||
// YOURLS admin interface, or from the REST API without specifying the | ||
// `base_url` query parameter) | ||
// | ||
// https://acme.com/r/Ac3fG | ||
// | ||
// instead of | ||
// | ||
// https://link.acme.com/Ac3fG | ||
// | ||
// If invoking YOURLS via its REST API while setting the ``base_url`` query | ||
// parameter to ``https://foo.bar``, YOURLS returns the following shortlink | ||
// (let's say the full API call is specified like this: | ||
// `https://lnk.acme.com/yourls-api.php?signature=XXXXXXXXXX&action=shorturl&format=txt&base_url=https://foo.bar&url=https://short.this/url/for/me`): | ||
// | ||
// https://foo.bar/Ac3fG | ||
// | ||
// It is assumed that the system behind ``https://acme.com/r`` performs (e.g., | ||
// wildcard) redirects from ``https://acme.com/r/(.*)`` to | ||
// ``https://link.acme.com/$1`` which are then further redirected to the target | ||
// URL. | ||
// | ||
yourls_add_filter( 'yourls_link', 'ck_base_url_rewrite' ); | ||
function ck_base_url_rewrite ($link) { | ||
$default_base_url = yourls_get_option( 'ck_default_base_url', ''); | ||
$base_url_default = yourls_get_option( 'ck_base_url_default', ''); | ||
$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 | ||
: $base_url_default | ||
); | ||
|
||
if ( empty($base_url) ) | ||
|
@@ -77,7 +86,7 @@ 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['default_base_url'] ) or | ||
if( isset( $_POST['base_url_default'] ) or | ||
isset( $_POST['base_url_query_parameter'] ) | ||
) { | ||
// If so, verify nonce | ||
|
@@ -86,7 +95,7 @@ function ck_base_url_rewrite_display_page () { | |
ck_base_url_rewrite_settings_update(); | ||
} | ||
|
||
$default_base_url = yourls_get_option('ck_default_base_url', ''); | ||
$base_url_default = yourls_get_option('ck_base_url_default', ''); | ||
$base_url_query_parameter = yourls_get_option('ck_base_url_query_parameter', ''); | ||
$nonce = yourls_create_nonce('base_url_settings'); | ||
|
||
|
@@ -97,7 +106,7 @@ function ck_base_url_rewrite_display_page () { | |
<input type="hidden" name="nonce" value="$nonce" /> | ||
<p> | ||
<label>Shortlink Default Base URL:</label> | ||
<input type="text" name="default_base_url" value="$default_base_url" /> | ||
<input type="text" name="base_url_default" value="$base_url_default" /> | ||
</p> | ||
<p> | ||
<label>Query parameter (Leave blank to disable):</label> | ||
|
@@ -112,9 +121,9 @@ function ck_base_url_rewrite_display_page () { | |
|
||
// The function that updates the configuration option | ||
function ck_base_url_rewrite_settings_update() { | ||
$default_base_url = $_POST['default_base_url']; | ||
$base_url_default = $_POST['base_url_default']; | ||
$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_default', $base_url_default ); | ||
yourls_update_option( 'ck_base_url_query_parameter', $base_url_query_parameter); | ||
} | ||
|