Skip to content

Instantly share code, notes, and snippets.

@ma4a
Last active October 15, 2016 19:33
Show Gist options
  • Select an option

  • Save ma4a/89e0fb4968faddeca1bfb9f80dd425e6 to your computer and use it in GitHub Desktop.

Select an option

Save ma4a/89e0fb4968faddeca1bfb9f80dd425e6 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Preview URL
Plugin URI: http://yourls.org/
Description: Preview URLs before you're redirected there
Version: 1.0
Author: Ozh
Author URI: http://ozh.org/
Version: 1.1
Author: Matze
Author URI: https://gist.github.com/ma4a
*/
// EDIT THIS
// Character to add to a short URL to trigger the preview interruption
define( 'OZH_PREVIEW_CHAR', '~' );
define( 'SHOW_SYSTEM_INFO', false ); //true or false
// DO NO EDIT FURTHER
// Handle failed loader request and check if there's a ~
yourls_add_action( 'loader_failed', 'ozh_preview_loader_failed' );
function ozh_preview_loader_failed( $args ) {
$request = $args[0];
$pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
if( preg_match( "@^([$pattern]+)".OZH_PREVIEW_CHAR."$@", $request, $matches ) ) {
$keyword = isset( $matches[1] ) ? $matches[1] : '';
$keyword = yourls_sanitize_keyword( $keyword );
ozh_preview_show( $keyword );
die();
}
}
// Show the preview screen for a short URL
function ozh_preview_show( $keyword ) {
require_once( YOURLS_INC.'/functions-html.php' );
yourls_html_head( 'preview', 'Short URL preview' );
if(SHOW_SYSTEM_INFO) yourls_html_logo();
$title = yourls_get_keyword_title( $keyword );
$url = yourls_get_keyword_longurl( $keyword );
$base = YOURLS_SITE;
$char = OZH_PREVIEW_CHAR;
echo <<<HTML
<h2>Link Preview</h2>
<p>You requested the short URL <strong><a href="$base/$keyword">$base/$keyword</a></strong></p>
<p>This short URL points to:</p>
<ul>
<li>Long URL: <strong><a href="$base/$keyword">$url</a></strong></li>
<li>Page title: <strong>$title</strong></li>
</ul>
<p>If you still want to visit this link, please <strong><a href="$base/$keyword">click here</a></str$
<p>Thank you for using our shortening service.</p>
HTML;
if(SHOW_SYSTEM_INFO) yourls_html_footer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment