eth-simple-shortlinks.php 3.52 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1 2 3 4 5 6
<?php
/*
Plugin Name: ETH Simple Shortlinks
Plugin URI: https://ethitter.com/plugins/
Description: Simple non-GET shortlinks using post IDs
Author: Erick Hitter
7
Version: 0.2
Erick Hitter's avatar
Erick Hitter committed
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
Author URI: https://ethitter.com/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

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
*/

class ETH_Simple_Shortlinks {
	/**
	 * PLUGIN SETUP
	 */

	/**
	 * Singleton
	 */
	private static $instance = null;

	/**
	 * Instantiate singleton
	 */
	public static function get_instance() {
		if ( ! is_a( self::$instance, __CLASS__ ) ) {
			self::$instance = new self;
		}

		return self::$instance;
	}

	/**
	 * Dummy magic methods
	 */
	public function __clone() {_doing_it_wrong( __FUNCTION__, 'Cheatin&#8217; huh?', '0.1' ); }
	public function __wakeup() { _doing_it_wrong( __FUNCTION__, 'Cheatin&#8217; huh?', '0.1' ); }
	public function __call( $name = '', $args = array() ) { unset( $name, $args ); return null; }

Erick Hitter's avatar
Erick Hitter committed
53 54 55
	/**
	 * Class properties
	 */
56
	private $slug = 'p';
57
	private $qv   = 'eth-shortlink';
Erick Hitter's avatar
Erick Hitter committed
58

Erick Hitter's avatar
Erick Hitter committed
59 60 61
	/**
	 *
	 */
Erick Hitter's avatar
Erick Hitter committed
62
	private function __construct() {
63
		// Request
Erick Hitter's avatar
Erick Hitter committed
64 65 66
		add_action( 'init', array( $this, 'add_rewrite_rule' ) );
		add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
		add_action( 'parse_request', array( $this, 'action_parse_request' ) );
67 68 69

		// Shortlink
		add_filter( 'get_shortlink', array( $this, 'filter_get_shortlink' ), 10, 2 );
Erick Hitter's avatar
Erick Hitter committed
70 71 72 73 74 75
	}

	/**
	 * Register rewrite rule
	 */
	public function add_rewrite_rule() {
76
		add_rewrite_rule( '^' . $this->slug . '/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' );
Erick Hitter's avatar
Erick Hitter committed
77 78 79 80 81 82 83 84 85 86 87 88 89
	}

	/**
	 * Add custom query var to those permitted, so it can be detected at `parse_request`
	 */
	public function filter_query_vars( $qv ) {
		$qv[] = $this->qv;

		return $qv;
	}

	/**
	 * Catch this plugin's requests and issue redirects, otherwise WP will serve content at duplicate URLs
90 91 92 93
	 *
	 * Let's invalid post IDs fall through to WP's 404 handler, or anything else that might intercede
	 *
	 * URLs aren't validated in case plugins filter permalinks to point to external URLs
Erick Hitter's avatar
Erick Hitter committed
94 95 96
	 */
	public function action_parse_request( $request ) {
		if ( isset( $request->query_vars[ $this->qv ] ) ) {
97
			$dest = get_permalink( $request->query_vars[ 'p' ] );
Erick Hitter's avatar
Erick Hitter committed
98 99

			if ( $dest ) {
100 101
				wp_redirect( $dest, 301 );
				exit;
Erick Hitter's avatar
Erick Hitter committed
102 103 104
			}
		}
	}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	/**
	 * Override shortlinks with this plugin's format
	 */
	public function filter_get_shortlink( $shortlink, $id ) {
		if ( empty( $id ) ) {
			$_p = get_post();

			if ( ! empty( $_p->ID ) ) {
				$id = $_p->ID;
			}
		}

		if ( empty( $id ) ) {
			return $shortlink;
		}

122 123 124 125 126
		if ( ! in_array( get_post_status( $id ), apply_filters( 'eth_simple_shortlinks_allowed_post_statuses', array( 'publish', 'future' ) ) ) ) {
			return $shortlink;
		}

		if ( ! in_array( get_post_type( $id ), apply_filters( 'eth_simple_shortlinks_allowed_post_types', array( 'post', 'page' ) ) ) ) {
127 128 129
			return $shortlink;
		}

130 131
		return user_trailingslashit( home_url( sprintf( '%s/%d', $this->slug, $id ) ) );
	}
Erick Hitter's avatar
Erick Hitter committed
132 133 134
}

ETH_Simple_Shortlinks::get_instance();