Skip to content

Instantly share code, notes, and snippets.

@jsilence
Forked from singpolyma/base60.php
Created April 17, 2011 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsilence/923886 to your computer and use it in GitHub Desktop.
Save jsilence/923886 to your computer and use it in GitHub Desktop.
<?php
/* Code from http://tantek.pbworks.com/NewBase60
License: http://creativecommons.org/licenses/by-sa/3.0/
Author: Tantek Çelik <http://tantek.com>
*/
function strcat($a, $b) {
return $a.$b;
}
function numtosxg($n) {
$s = "";
$m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
if ($n===undefined || $n===0) { return 0; }
while ($n>0) {
$d = $n % 60;
$s = strcat($m[$d],$s);
$n = ($n-$d)/60;
}
return $s;
}
function numtosxgf($n, $f) {
$s = numtosxg($n);
if ($f===undefined) {
$f=1;
}
$f -= strlen($s);
while ($f > 0) {
$s = strcat("0",$s);
--$f;
}
return $s;
}
function sxgtonum($s) {
$n = 0;
$j = strlen($s);
for ($i=0;$i<$j;$i++) { // iterate from first to last char of $s
$c = ord($s[$i]); // put current ASCII of char into $c
if ($c>=48 && $c<=57) { $c=$c-48; }
else if ($c>=65 && $c<=72) { $c-=55; }
else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
else if ($c>=74 && $c<=78) { $c-=56; }
else if ($c==79) { $c=0; } // error correct typo capital O to 0
else if ($c>=80 && $c<=90) { $c-=57; }
else if ($c==95) { $c=34; } // underscore
else if ($c>=97 && $c<=107) { $c-=62; }
else if ($c>=109 && $c<=122) { $c-=63; }
else { $c = 0; } // treat all other noise as 0
$n = 60*$n + $c;
}
return $n;
}
?>
<?php
/*
Plugin Name: Shorten
Plugin URI: http://singpolyma.net/plugins/shorten
Description: Provides a URL shortener in Wordpress.
Version: 0.1
Author: Stephen Paul Weber
Author URI: http://singpolyma.net
License: MIT license (http://www.opensource.org/licenses/mit-license.php)
*/
@include dirname(__FILE__).'/base60.php';
if(!$_SERVER['SCRIPT_URI'] && $_SERVER['REQUEST_URI'])
$_SERVER['SCRIPT_URI'] = $_SERVER['REQUEST_URI'];
if(!$_SERVER['SCRIPT_URI'] && $_SERVER['SCRIPT_NAME'])
$_SERVER['SCRIPT_URI'] = $_SERVER['SCRIPT_NAME'];
$path = preg_replace('#^'.preg_replace('/https?/','https?',get_bloginfo('home')).'#','',$_SERVER['SCRIPT_URI']);
preg_match('/^\/?\/p\/(\d+)\/?$/', $path, $matches);
if($matches[1]) {
header('Location: '.get_bloginfo('home').'?shortened&p='.$matches[1], true, 301);
exit;
} else if(function_exists('sxgtonum')) {
preg_match('/^\/?\/s\/([0-9a-zA-Z]+)\/?$/', $path, $matches);
if($matches[1]) {
header('Location: '.get_bloginfo('home').'?shortened&p='.sxgtonum($matches[1]), true, 301);
exit;
}
}
function shortened_head() {
global $post;
$shorten_options = get_option('shorten_options');
$shorten_base = $shorten_options['shorten_base'];
if(is_page() || is_single()) {
if(function_exists('numtosxg')) {
echo '<link rev="canonical" rel="self alternate shorter short_url" href="'.$shorten_base.'/s/'.numtosxg($post->ID).'" />';
echo '<link rel="self alternate shorter short_url" href="'.$shorten_base.'/p/'.$post->ID.'" />';
} else {
echo '<link rev="canonical" rel="self alternate shorter short_url" href="'.$shorten_base.'/p/'.$post->ID.'" />';
}
}
}
add_action('wp_head', 'shortened_head');
// Manage plugin options
//
add_action( 'admin_init', 'shorten_register_settings' );
function shorten_register_settings() {
register_setting( 'shorten_options', 'shorten_options' );
}
// Plugin Options Menu
function shorten_plugin_menu() {
add_options_page('Shorten Options', 'Shorten', 'manage_options', 'shorten-options', 'shorten_plugin_options_page');
}
function shorten_plugin_options_page() {
?>
<div class="wrap">
<h2>Shorten Options</h2>
<form method="post" action="options.php">
<?php settings_fields('shorten_options'); ?>
<?php $shorten_options = get_option('shorten_options'); ?>
<table class="form-table">
<tr valign="top"><th scope="row">Shortlink Domain</th>
<td><input type="text" name="shorten_options[shorten_base]" value="<?php echo $shorten_options['shorten_base']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
add_action('admin_menu', 'shorten_plugin_menu');
?>
@singpolyma
Copy link

This looks pretty interesting, but the patch is very messy because of all the unnecessary whitespace change in what seems to be mostly unmodified code at the top and in the shortened_head function. Could you maybe clean this up a bit?

@singpolyma
Copy link

There's also a whitespace error (whitespace on end of line) on line 79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment