eth-simple-shortlinks.php 3.19 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
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
<?php
/*
Plugin Name: ETH Simple Shortlinks
Plugin URI: https://ethitter.com/plugins/
Description: Simple non-GET shortlinks using post IDs
Author: Erick Hitter
Version: 0.1
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';
Erick Hitter's avatar
Erick Hitter committed
57
58
	private $qv = 'eth-shortlink';

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
	}

	/**
	 * 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
	 */
	public function action_parse_request( $request ) {
		if ( isset( $request->query_vars[ $this->qv ] ) ) {
			$home_url = user_trailingslashit( home_url() );

			$dest = get_permalink( $request->query_vars['p'] );

			if ( $dest ) {
				$dest   = wp_validate_redirect( $dest, $home_url );
				$status = 301;
			} else {
				$dest   = $home_url;
				$status = 302;
			}

			wp_redirect( $dest, $status );
			exit;
		}
	}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

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

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

ETH_Simple_Shortlinks::get_instance();