Skip to content
Snippets Groups Projects
Commit 72dce67f authored by Erick Hitter's avatar Erick Hitter
Browse files

Improve when redirects are issued and which post types/statuses are supported.

parent f4818f69
Branches
Tags
No related merge requests found
...@@ -4,7 +4,7 @@ Plugin Name: ETH Simple Shortlinks ...@@ -4,7 +4,7 @@ Plugin Name: ETH Simple Shortlinks
Plugin URI: https://ethitter.com/plugins/ Plugin URI: https://ethitter.com/plugins/
Description: Simple non-GET shortlinks using post IDs Description: Simple non-GET shortlinks using post IDs
Author: Erick Hitter Author: Erick Hitter
Version: 0.1 Version: 0.2
Author URI: https://ethitter.com/ Author URI: https://ethitter.com/
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
...@@ -54,7 +54,7 @@ class ETH_Simple_Shortlinks { ...@@ -54,7 +54,7 @@ class ETH_Simple_Shortlinks {
* Class properties * Class properties
*/ */
private $slug = 'p'; private $slug = 'p';
private $qv = 'eth-shortlink'; private $qv = 'eth-shortlink';
/** /**
* *
...@@ -87,23 +87,19 @@ class ETH_Simple_Shortlinks { ...@@ -87,23 +87,19 @@ class ETH_Simple_Shortlinks {
/** /**
* Catch this plugin's requests and issue redirects, otherwise WP will serve content at duplicate URLs * Catch this plugin's requests and issue redirects, otherwise WP will serve content at duplicate URLs
*
* 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
*/ */
public function action_parse_request( $request ) { public function action_parse_request( $request ) {
if ( isset( $request->query_vars[ $this->qv ] ) ) { if ( isset( $request->query_vars[ $this->qv ] ) ) {
$home_url = user_trailingslashit( home_url() ); $dest = get_permalink( $request->query_vars[ 'p' ] );
$dest = get_permalink( $request->query_vars['p'] );
if ( $dest ) { if ( $dest ) {
$dest = wp_validate_redirect( $dest, $home_url ); wp_redirect( $dest, 301 );
$status = 301; exit;
} else {
$dest = $home_url;
$status = 302;
} }
wp_redirect( $dest, $status );
exit;
} }
} }
...@@ -123,7 +119,11 @@ class ETH_Simple_Shortlinks { ...@@ -123,7 +119,11 @@ class ETH_Simple_Shortlinks {
return $shortlink; return $shortlink;
} }
if ( ! in_array( get_post_status( $id ), array( 'publish', 'future' ) ) ) { 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' ) ) ) ) {
return $shortlink; return $shortlink;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment