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

Merge branch 'develop' into 'master'

Row actions and utility functions

Adds row actions for supported post types, and introduces various utility methods and functions to simplify plugin and reduce repetition.

See merge request !1
parents 72dce67f ae511d2e
No related branches found
No related tags found
1 merge request!1Row actions and utility functions
...@@ -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.2 Version: 0.3
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
...@@ -56,17 +56,24 @@ class ETH_Simple_Shortlinks { ...@@ -56,17 +56,24 @@ class ETH_Simple_Shortlinks {
private $slug = 'p'; private $slug = 'p';
private $qv = 'eth-shortlink'; private $qv = 'eth-shortlink';
private $supported_post_types = array();
private $supported_post_statuses = array();
/** /**
* * Register actions and filters
*/ */
private function __construct() { private function __construct() {
// Request // Request
add_action( 'init', array( $this, 'add_rewrite_rule' ) ); 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_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
add_action( 'parse_request', array( $this, 'action_parse_request' ) ); add_action( 'parse_request', array( $this, 'action_parse_request' ) );
// Shortlink // Shortlink
add_filter( 'get_shortlink', array( $this, 'filter_get_shortlink' ), 10, 2 ); 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 );
} }
/** /**
...@@ -76,6 +83,14 @@ class ETH_Simple_Shortlinks { ...@@ -76,6 +83,14 @@ class ETH_Simple_Shortlinks {
add_rewrite_rule( '^' . $this->slug . '/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' ); 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` * Add custom query var to those permitted, so it can be detected at `parse_request`
*/ */
...@@ -119,16 +134,71 @@ class ETH_Simple_Shortlinks { ...@@ -119,16 +134,71 @@ class ETH_Simple_Shortlinks {
return $shortlink; 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; 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 $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(); 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 );
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment