| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Slash Edit |
| 4 |
Plugin URI: http://wordpress.org/extend/plugins/slash-edit/ |
| 5 |
Description: Edit your posts or pages with a simple "/edit" at the end |
| 6 |
Author: ronalfy |
| 7 |
Version: 1.0.0 |
| 8 |
Requires at least: 3.9.1 |
| 9 |
Author URI: http://www.ronalfy.com |
| 10 |
Contributors: ronalfy |
| 11 |
*/ |
| 12 |
class Slash_Edit { |
| 13 |
private static $instance = null; |
| 14 |
private $endpoint = 'edit'; |
| 15 |
|
| 16 |
//Singleton |
| 17 |
public static function get_instance() { |
| 18 |
if ( null == self::$instance ) { |
| 19 |
self::$instance = new self; |
| 20 |
} |
| 21 |
return self::$instance; |
| 22 |
} //end get_instance |
| 23 |
|
| 24 |
private function __construct() { |
| 25 |
add_action( 'init', array( $this, 'init' ), 20 ); |
| 26 |
add_action( 'template_redirect', array( $this, 'maybe_redirect' ) ); |
| 27 |
add_filter( 'rewrite_rules_array', array( $this, 'add_rewrite_rules' ) ); |
| 28 |
$this->endpoint = sanitize_title( apply_filters( 'slash_edit_endpoint', 'edit' ) ); |
| 29 |
} //end constructor |
| 30 |
|
| 31 |
public static function activate() { |
| 32 |
update_option( 'slash_edit_install', 'true' ); |
| 33 |
} |
| 34 |
|
| 35 |
public function add_rewrite_rules( $rules ) { |
| 36 |
//Get taxonomies |
| 37 |
$taxonomies = get_taxonomies(); |
| 38 |
$blog_prefix = ''; |
| 39 |
if ( is_multisite() && !is_subdomain_install() && is_main_site() ) { /* stolen from /wp-admin/options-permalink.php */ |
| 40 |
$blog_prefix = 'blog/'; |
| 41 |
} |
| 42 |
$exclude = array( |
| 43 |
'category', |
| 44 |
'post_tag', |
| 45 |
'nav_menu', |
| 46 |
'link_category', |
| 47 |
'post_format' |
| 48 |
); |
| 49 |
foreach( $taxonomies as $key => $taxonomy ) { |
| 50 |
if ( in_array( $key, $exclude ) ) continue; |
| 51 |
$rules[ "{$blog_prefix}{$key}/([^/]+)/{$this->endpoint}(/(.*))?/?$" ] = 'index.php?' . $key . '=$matches[1]&' . $this->endpoint . '=$matches[3]'; |
| 52 |
} |
| 53 |
return $rules; |
| 54 |
} |
| 55 |
public static function deactivate() { |
| 56 |
} |
| 57 |
|
| 58 |
public function init() { |
| 59 |
global $wp_rewrite; |
| 60 |
|
| 61 |
//Delete rewrite rules if plugin is deactivated |
| 62 |
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deactivate' ) { |
| 63 |
$plugin_basename = plugin_basename( __FILE__ ); |
| 64 |
|
| 65 |
//Let's see if we're being deactivated |
| 66 |
if ( $_GET[ 'plugin' ] === $plugin_basename ) { |
| 67 |
if ( wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'deactivate-plugin_' . $plugin_basename ) ) { |
| 68 |
add_rewrite_endpoint( $this->endpoint, EP_NONE ); |
| 69 |
flush_rewrite_rules( false ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
//Refresh rewrite rules if plugin is activated |
| 75 |
add_rewrite_endpoint( $this->endpoint, EP_PERMALINK | EP_PAGES | EP_CATEGORIES | EP_TAGS | EP_AUTHORS ); //todo - adding EP_ATTACHMENT messes up EP_PERMALINK and EP_PAGES |
| 76 |
if ( get_option( 'slash_edit_install', 'false' ) == 'true' ) { |
| 77 |
flush_rewrite_rules( false ); |
| 78 |
delete_option( 'slash_edit_install' ); |
| 79 |
} |
| 80 |
} //end init |
| 81 |
|
| 82 |
public function maybe_redirect() { |
| 83 |
global $wp_query; |
| 84 |
|
| 85 |
if ( !isset( $wp_query->query_vars[ $this->endpoint ] ) ) return; |
| 86 |
|
| 87 |
$edit_url = false; |
| 88 |
if ( is_attachment() || is_single() || is_page() ) { /* Post, page, attachment, or CPTs */ |
| 89 |
//Get the post, page, or cpt id |
| 90 |
$post = get_queried_object(); |
| 91 |
$post_id = isset( $post->ID ) ? $post->ID : false; |
| 92 |
if ( $post_id === false ) return; |
| 93 |
|
| 94 |
//Build the url |
| 95 |
$edit_url = esc_url_raw( add_query_arg( array( 'post' => absint( $post_id ), 'action' => 'edit' ), admin_url( 'post.php' ) ) ); |
| 96 |
} elseif ( is_author() ) { /* Author Page */ |
| 97 |
$user_data = get_queried_object(); |
| 98 |
if ( is_a( $user_data, 'WP_User' ) ) { |
| 99 |
$user_id = $user_data->ID; |
| 100 |
//Build the url |
| 101 |
$edit_url = esc_url_raw( add_query_arg( array( 'user_id' => absint( $user_id ), 'action' => 'edit' ), admin_url( 'user-edit.php' ) ) ); |
| 102 |
} |
| 103 |
} elseif ( is_category() || is_tag() || is_tax() ) { |
| 104 |
$tax_data = get_queried_object(); |
| 105 |
if ( is_object( $tax_data ) && isset( $tax_data->term_id ) ) { |
| 106 |
$term_id = $tax_data->term_id; |
| 107 |
$taxonomy = $tax_data->taxonomy; |
| 108 |
//Build the url |
| 109 |
$edit_url = esc_url_raw( add_query_arg( array( 'tag_ID' => absint( $term_id ), 'taxonomy' => $taxonomy, 'action' => 'edit', 'post_type' => get_post_type() ), admin_url( 'edit-tags.php' ) ) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
//Return if nothing to redirect to |
| 114 |
if ( $edit_url === false ) return; |
| 115 |
|
| 116 |
|
| 117 |
//Redirect yo |
| 118 |
wp_safe_redirect( $edit_url ); |
| 119 |
exit; |
| 120 |
} |
| 121 |
|
| 122 |
} //end class Slash_Edit |
| 123 |
|
| 124 |
add_action( 'plugins_loaded', 'slash_edit_instantiate' ); |
| 125 |
function slash_edit_instantiate() { |
| 126 |
Slash_Edit::get_instance(); |
| 127 |
} //end slash_edit_instantiate |
| 128 |
|
| 129 |
register_activation_hook( __FILE__, array( 'Slash_Edit', 'activate' ) ); |
| 130 |
register_activation_hook( __FILE__, array( 'Slash_Edit', 'deactivate' ) ); |