Commit b0856e8d authored by Erick Hitter's avatar Erick Hitter

Permalink and redirect

parent 677588e3
......@@ -50,10 +50,57 @@ class ETH_Simple_Shortlinks {
public function __wakeup() { _doing_it_wrong( __FUNCTION__, 'Cheatin’ huh?', '0.1' ); }
public function __call( $name = '', $args = array() ) { unset( $name, $args ); return null; }
/**
* Class properties
*/
private $qv = 'eth-shortlink';
/**
*
*/
private function __construct() {}
private function __construct() {
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' ) );
}
/**
* Register rewrite rule
*/
public function add_rewrite_rule() {
add_rewrite_rule( '^p/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' );
}
/**
* 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;
}
}
}
ETH_Simple_Shortlinks::get_instance();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment