all repos — wp-shortslug @ 9b980e55a36c1b73d8dd26e732c8cd7ee181d266

wp-shortslug.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
<?php
/*
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.2
Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/
License: GPLv3
Required minimum PHP version: 5.3
*/

/*  Copyright 2015 Peter Molnar ( hello@petermolnar.eu )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 3, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

if (!class_exists('WP_SHORTSLUG')):

class WP_SHORTSLUG {
	const base = '0123456789abcdefghijklmnopqrstuvwxyz';
	const base_camel = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

	public function __construct () {
		register_activation_hook( __FILE__ , array( &$this, 'plugin_activate' ) );

		// init all the things!
		add_action( 'init', array( &$this, 'init'));

		// 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"), 2);
				add_action("{$status}_to_publish", array(&$this, "maybe_generate_slug"), 1);
			}
		}

	}

	public function init() {
		// shortlink replacement
		add_filter( 'get_shortlink', array(&$this, 'shorturl'), 1, 4 );
	}

	/**
	 * activate hook
	 */
	public static function plugin_activate() {
		if ( version_compare( phpversion(), 5.3, '<' ) ) {
			die( 'The minimum PHP version required for this plugin is 5.3' );
		}
	}

	/**
	 * 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
	 *
	 */
	public static function shorturl ( $shortlink = '', $id = '', $context = '', $allow_slugs = '' ) {
		global $post;
		$post = static::fix_post($post);

		if ($post === false )
			return $shortlink;

		$url = static::shortslug($post);

		$base = rtrim( get_bloginfo('url'), '/' ) . '/';
		return $base.$url;
	}

	/**
	 * print meta shortlink
	 *
	 */
	public function shortlink () {
		if (function_exists('is_singular') && is_singular()) {
			$url = static::shorturl();
			if ( !empty($url) ) {
				/* this is a good hook location to also check for the existence of
				 * the _wp_old_slug entry: before printing a non-existent url, make
				 * sure it exists in the DB
				 *
				 * this is mostly for retro-fitting non-existant entries for posts
				 * that were published earlier than the plugin was intalled/activated
				 *
				 */
				static::check_shorturl();

				printf ('<link rel="shortlink" href="%s" />%s', static::shorturl() , "\n");
			}
		}
	}

	/**
	 *
	 */
	public static function shortslug ( &$post ) {
		$post = static::fix_post($post);

		if ($post === false)
			return false;

		$epoch = get_the_time('U', $post->ID);
		$url36 = static::epoch2url($epoch);

		return $url36;
	}

	/**
	 * 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) {
		$post = static::fix_post($post);

		if ($post === false)
			return false;

		$url36 = static::shortslug($post);

		// if the generated url is the same as the current slug, walk away
		if ( $url36 == $post->post_name )
			return true;

		$meta = get_post_meta( $post->ID, '_wp_old_slug', false);

		/*
		 * there should be only our shorturl living here, so in case
		 * WP did something nasty, clean up the too many shortslugs
		 *
		 * 3 is a by-the-guts number; posts with more then 3 _wp_old_slug,
		 * matching the criteria of lowecase-with-nums, 5 at max 6 char length
		 * shouldn't really exist
		 *
		 */
		if ( count($meta) > 6 ) {
			foreach ($meta as $key => $slug ) {
				// base36 matches
				if (preg_match('/^[0-9a-z]{5,6}$/', $slug)) {
					static::debug('deleting slug ' . $slug . ' from ' . $post->ID );
					delete_post_meta($post->ID, '_wp_old_slug', $slug);
					unset($meta[$key]);
				}
			}
		}

		if ( !in_array($url36,$meta)) {
			static::debug('adding slug ' . $url36 . ' to ' . $post->ID );
			add_post_meta($post->ID, '_wp_old_slug', $url36);
		}

		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_error = false;
		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, $b = 36 ) {

		if ($b <= 36 )
			$base = static::base;
		else
			$base = static::base_camel;

		$limit = strlen($str);
		$res=strpos($base,$str[0]);
		for($i=1;$i<$limit;$i++) {
			$res = $b * $res + strpos($base,$str[$i]);
		}

		return $res;
	}

	/**
	 * 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, $b = 36 ) {

		if ($b <= 36 )
			$base = static::base;
		else
			$base = static::base_camel;

		$r = $num  % $b ;
		$res = $base[$r];
		$q = floor($num/$b);
		while ($q) {
			$r = $q % $b;
			$q =floor($q/$b);
			$res = $base[$r].$res;
		}

		return $res;
	}

	/**
	 * do everything to get the Post object
	 */
	public static function fix_post ( &$post = null ) {
		if ($post === null || !static::is_post($post))
			global $post;

		if (static::is_post($post))
			return $post;

		return false;
	}

	/**
	 * test if an object is actually a post
	 */
	public static function is_post ( &$post ) {
		if ( !empty($post) && is_object($post) && isset($post->ID) && !empty($post->ID) )
			return true;

		return false;
	}

	/**
	 *
	 * debug messages; will only work if WP_DEBUG is on
	 * or if the level is LOG_ERR, but that will kill the process
	 *
	 * @param string $message
	 * @param int $level
	 */
	public static function debug( $message, $level = LOG_NOTICE ) {
		if ( @is_array( $message ) || @is_object ( $message ) )
			$message = json_encode($message);


		switch ( $level ) {
			case LOG_ERR :
				wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
				exit;
			default:
				if ( !defined( 'WP_DEBUG' ) || WP_DEBUG != true )
					return;
				break;
		}

		error_log(  __CLASS__ . ": " . $message );
	}

}

$WP_SHORTSLUG = new WP_SHORTSLUG();

endif;