Skip to content
Snippets Groups Projects

Row actions and utility functions

Merged Erick Hitter requested to merge develop into master
+ 75
5
Compare changes
  • Side-by-side
  • Inline
+ 75
5
@@ -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,24 @@ 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_action( 'admin_head-edit.php', array( $this, 'add_admin_header_assets' ) );
add_filter( 'post_row_actions', array( $this, 'filter_row_actions' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'filter_row_actions' ), 10, 2 );
}
/**
@@ -77,6 +84,14 @@ class ETH_Simple_Shortlinks {
}
/**
* 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 ) {
@@ -119,16 +134,71 @@ 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 );
}
/**
* Header assets for shortlink in row actions
*/
public function add_admin_header_assets() {
global $typenow;
if ( ! in_array( $typenow, $this->supported_post_types ) ) {
return;
}
?>
<script type="text/javascript">
jQuery( document ) .ready( function( $ ) {
$( '.row-actions .shortlink a' ).click( function( e ) {
e.preventDefault();
prompt( 'URL:', $( this ).attr('href') );
} );
} );
</script>
<?php
}
/**
* 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 href="' . esc_js( $this->get_shortlink( $post->ID ) ) . '">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 );
}
Loading