version 2.0; see changelog
Peter Molnar hello@petermolnar.eu
Thu, 03 Dec 2015 12:07:52 +0000
2 files changed,
87 insertions(+),
15 deletions(-)
M
readme.txt
→
readme.txt
@@ -3,16 +3,16 @@ Contributors: cadeyrn
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=AS8Y2GSMDTJZC Tags: shortlink, shorturl, slug Requires at least: 3.0 -Tested up to: 4.3.1 -Stable tag: 0.1 +Tested up to: 4.4 +Stable tag: 0.2 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html -Automatical, decodable short slugs for a post to replace shorturl +Automatic, decodable short slugs for a post to replace shorturl == Description == -The plugin automatically adds ( for future and old posts as well ) a short slug entry, fitting the existing WordPress way of adding old slugs. (It populates the _wp_old_slug hidden post meta.) By this, these can be used as short permalinks to the post while they still look nice. +The plugin automatically adds ( for future and old posts as well ) a short slug entry, fitting the existing WordPress way of adding old slugs. (It populates the _wp_old_slug hidden post meta.) This is then replaces the shortlink. The entry is generated from the publish date epoch, by a base 36 conversion, therefore contains only numbers and lowercase letters; by this method the entry is reversible and re-generatable in case of need.@@ -30,6 +30,14 @@
* every A. indicates BIG changes. * every .B version indicates new features. * every ..C indicates bugfixes for A.B version. + += 0.2 = +*2015-12-03* + +* Tested up till WordPress 4.4 +* added the option to have base larger than 36; in this case [a-zA-Z0-9] are allowed instead of [a-z0-9] +* auto-trigger slug generation & cleanup if needed +* auto-trigger old slug redirection in case the current query hits a 404 = 0.1 = *2015-11-11*
M
wp-shortslug.php
→
wp-shortslug.php
@@ -3,7 +3,7 @@ /*
Plugin Name: wp-shortslug Plugin URI: https://github.com/petermolnar/wp-shortslug Description: reversible automatic short slug based on post pubdate epoch for WordPress -Version: 0.1 +Version: 0.2 Author: Peter Molnar <hello@petermolnar.eu> Author URI: http://petermolnar.eu/ License: GPLv3@@ -29,7 +29,7 @@ if (!class_exists('WP_SHORTSLUG')):
class WP_SHORTSLUG { const base = '0123456789abcdefghijklmnopqrstuvwxyz'; - const b = 36; + const base_camel = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; public function __construct () { // init all the things!@@ -38,10 +38,14 @@
// replace shortlink add_action( 'wp_head', array(&$this, 'shortlink')); + // trigger fallback redirection by _wp_old_slug + add_action( 'wp_head', array(&$this, 'try_redirect')); + if (function_exists('is_admin') && is_admin() && !defined('DOING_AJAX')) { $statuses = array ('new', 'draft', 'auto-draft', 'pending', 'private', 'future' ); foreach ($statuses as $status) { - add_action("{$status}_to_publish", array(&$this, "check_shorturl")); + add_action("{$status}_to_publish", array(&$this, "check_shorturl"), 2); + add_action("{$status}_to_publish", array(&$this, "maybe_generate_slug"), 1); } }@@ -53,6 +57,17 @@ add_filter( 'get_shortlink', array(&$this, 'shorturl'), 1, 4 );
} /** + * try to redirect by old slug in case the current result is 404 + * + */ + public static function try_redirect () { + global $wp_query; + if ($wp_query->is_404 == true) { + wp_old_slug_redirect(); + } + } + + /** * absolute short url * */@@ -111,7 +126,7 @@ /**
* since WordPress has it's built-in rewrite engine, it's eaiser to use * that for adding the short urls */ - public static function check_shorturl(&$post = null) { + public static function check_shorturl($post = null) { $post = static::fix_post($post); if ($post === false)@@ -134,7 +149,7 @@ * matching the criteria of lowecase-with-nums, 5 at max 6 char length
* shouldn't really exist * */ - if ( count($meta) > 3 ) { + if ( count($meta) > 6 ) { foreach ($meta as $key => $slug ) { // base36 matches if (preg_match('/^[0-9a-z]{5,6}$/', $slug)) {@@ -154,13 +169,60 @@ return true;
} /** + * since WordPress has it's built-in rewrite engine, it's eaiser to use + * that for adding the short urls + */ + public static function maybe_generate_slug($post = null) { + $post = static::fix_post($post); + + if ($post === false) + return false; + + // auto-generated slug for empty title; this we should replace + $pattern = '/^' . $post->ID . '-[0-9]$/'; + + if ( !preg_match( $pattern, $post->post_name ) && !empty($post->post_title) ) { + static::debug( 'post '. $post->ID .' name is ' . $post->post_name . ' which does not match pattern ' . $pattern .' and the post_title is not empty, so not replacing slug with shortslug.' ); + return false; + } + + $url36 = static::shortslug($post); + + static::debug( 'replacing slug of '. $post->ID .' with shortslug: ' . $url36 ); + + $_post = array( + 'ID' => $post->ID, + 'post_name' => $url36, + ); + + wp_update_post( $_post, $wp_error ); + + if (is_wp_error($wp_error)) { + $errors = json_encode($post_id->get_error_messages()); + static::debug( $errors ); + } + + + $meta = get_post_meta( $post->ID, '_wp_old_slug', false); + if (in_array($url36,$meta)) { + static::debug('removing slug ' . $url36 . ' from ' . $post->ID ); + delete_post_meta($post->ID, '_wp_old_slug', $url36); + } + + return true; + } + + + /** * decode short string and covert it back to UNIX EPOCH * */ - public static function url2epoch( $str ) { + public static function url2epoch( $str, $b = 36 ) { - $b = static::b; - $base = static::base; + if ($b <= 36 ) + $base = static::base; + else + $base = static::base_camel; $limit = strlen($str); $res=strpos($base,$str[0]);@@ -176,10 +238,12 @@ * convert UNIX EPOCH to short string
* * thanks to https://stackoverflow.com/questions/4964197/converting-a-number-base-10-to-base-62-a-za-z0-9 */ - public static function epoch2url($num) { + public static function epoch2url($num, $b = 36 ) { - $b = static::b; - $base = static::base; + if ($b <= 36 ) + $base = static::base; + else + $base = static::base_camel; $r = $num % $b ; $res = $base[$r];