Commit b03129d8 authored by Erick Hitter's avatar Erick Hitter

Add shortlinks to row actions for easy access, introduce utility methods to...

Add shortlinks to row actions for easy access, introduce utility methods to reduce duplication and make it easier to interact with the plugin.
parent 72dce67f
......@@ -4,7 +4,7 @@ Plugin Name: ETH Simple Shortlinks
Plugin URI: https://ethitter.com/plugins/
Description: Simple non-GET shortlinks using post IDs
Author: Erick Hitter
Version: 0.2
Version: 0.3
Author URI: https://ethitter.com/
This program is free software; you can redistribute it and/or modify
......@@ -56,17 +56,23 @@ class ETH_Simple_Shortlinks {
private $slug = 'p';
private $qv = 'eth-shortlink';
private $supported_post_types = array();
private $supported_post_statuses = array();
/**
*
* Register actions and filters
*/
private function __construct() {
// Request
add_action( 'init', array( $this, 'add_rewrite_rule' ) );
add_action( 'wp_loaded', array( $this, 'filter_support' ) );
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
add_action( 'parse_request', array( $this, 'action_parse_request' ) );
// Shortlink
add_filter( 'get_shortlink', array( $this, 'filter_get_shortlink' ), 10, 2 );
add_filter( 'post_row_actions', array( $this, 'filter_row_actions' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'filter_row_actions' ), 10, 2 );
}
/**
......@@ -76,6 +82,14 @@ class ETH_Simple_Shortlinks {
add_rewrite_rule( '^' . $this->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`
*/
......@@ -119,16 +133,48 @@ class ETH_Simple_Shortlinks {
return $shortlink;
}
if ( ! in_array( get_post_status( $id ), apply_filters( 'eth_simple_shortlinks_allowed_post_statuses', array( 'publish', 'future' ) ) ) ) {
if ( ! in_array( get_post_status( $id ), $this->supported_post_statuses ) ) {
return $shortlink;
}
if ( ! in_array( get_post_type( $id ), apply_filters( 'eth_simple_shortlinks_allowed_post_types', array( 'post', 'page' ) ) ) ) {
if ( ! in_array( get_post_type( $id ), $this->supported_post_types ) ) {
return $shortlink;
}
return user_trailingslashit( home_url( sprintf( '%s/%d', $this->slug, $id ) ) );
return $this->get_shortlink( $id );
}
/**
* Provide the shortlink in row actions for easy access
*/
public function filter_row_actions( $actions, $post ) {
if ( ! in_array( get_post_type( $post ), $this->supported_post_types ) ) {
return $actions;
}
$actions['shortlink'] = '<a style="cursor: pointer;" onclick="prompt( \'URL:\', \'' . esc_js( $this->get_shortlink( $post->ID ) ) . '\' );return false;">Shortlink</a>';
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 <code>wp_loaded</code>; this ensures that all post types are registered.', '0.3' );
return false;
}
return ETH_Simple_Shortlinks::get_instance()->get_shortlink( $post_id );
}
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