slug . '/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' );
}
/**
* Allow filtering of supported statuses and types
*/
public function filter_support() {
$this->supported_post_statuses = apply_filters( 'eth_simple_shortlinks_allowed_post_statuses', array( 'publish', 'future' ) );
$this->supported_post_types = apply_filters( 'eth_simple_shortlinks_allowed_post_types', array( 'post', 'page' ) );
}
/**
* 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
*
* 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 ) {
if ( isset( $request->query_vars[ $this->qv ] ) ) {
$dest = get_permalink( $request->query_vars[ 'p' ] );
if ( $dest ) {
wp_redirect( $dest, 301 );
exit;
}
}
}
/**
* 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;
}
if ( ! in_array( get_post_status( $id ), $this->supported_post_statuses ) ) {
return $shortlink;
}
if ( ! in_array( get_post_type( $id ), $this->supported_post_types ) ) {
return $shortlink;
}
return $this->get_shortlink( $id );
}
/**
* Header assets for shortlink in row actions
*/
public function add_admin_header_assets() {
global $typenow;
if ( ! in_array( $typenow, $this->supported_post_types ) ) {
return;
}
?>
supported_post_types ) ) {
return $actions;
}
$actions['shortlink'] = 'Shortlink';
return $actions;
}
/**
* Utility method for building permlink
*/
public function get_shortlink( $post_id ) {
return user_trailingslashit( home_url( sprintf( '%s/%d', $this->slug, $post_id ) ) );
}
}
ETH_Simple_Shortlinks::get_instance();
/**
* Shortcut for using the shortlink outside of this plugin's considerations
*/
function eth_simple_shortlinks_get( $post_id ) {
if ( ! did_action( 'wp_loaded' ) ) {
_doing_it_wrong( __FUNCTION__, 'Shortlinks cannot be generated until after wp_loaded
; this ensures that all post types are registered.', '0.3' );
return false;
}
return ETH_Simple_Shortlinks::get_instance()->get_shortlink( $post_id );
}