PluginProbe
Slash Edit: Admin Shortcuts to Edit Posts and Pages Faster / 1.1.0
Slash Edit: Admin Shortcuts to Edit Posts and Pages Faster v1.1.0
trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.3.0 1.3.2
slash-edit / slash-edit.php

slash-edit.php in Slash Edit: Admin Shortcuts to Edit Posts and Pages Faster 1.1.0, at slash-edit.php

183 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.1.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 private $last_rewrite_version_update = '1.1.0'; //Will increment any time I need to change rewrite rules
16
17 //Singleton
18 public static function get_instance() {
19 if ( null == self::$instance ) {
20 self::$instance = new self;
21 }
22 return self::$instance;
23 } //end get_instance
24
25 private function __construct() {
26 add_action( 'init', array( $this, 'init' ), 20 );
27 add_action( 'template_redirect', array( 'Slash_Edit', 'maybe_redirect' ) );
28 add_filter( 'rewrite_rules_array', array( 'Slash_Edit', 'add_rewrite_rules' ) );
29 add_action( 'save_post', array( 'Slash_Edit', 'save_post' ) );
30 $this->endpoint = sanitize_title( apply_filters( 'slash_edit_endpoint', 'edit' ) );
31
32 } //end constructor
33
34 public function get_endpoint() {
35 return $this->endpoint;
36 }
37
38 public static function activate() {
39 update_option( 'slash_edit_install', 'true' );
40 }
41
42 public static function add_rewrite_rules( $rules ) {
43 //Get taxonomies
44 $taxonomies = get_taxonomies();
45 $blog_prefix = '';
46 $endpoint = Slash_Edit::get_instance()->get_endpoint();
47 if ( is_multisite() && !is_subdomain_install() && is_main_site() ) { /* stolen from /wp-admin/options-permalink.php */
48 $blog_prefix = 'blog/';
49 }
50 $exclude = array(
51 'category',
52 'post_tag',
53 'nav_menu',
54 'link_category',
55 'post_format'
56 );
57 foreach( $taxonomies as $key => $taxonomy ) {
58 if ( in_array( $key, $exclude ) ) continue;
59 $rules[ "{$blog_prefix}{$key}/([^/]+)/{$endpoint}(/(.*))?/?$" ] = 'index.php?' . $key . '=$matches[1]&' . $endpoint . '=$matches[3]';
60 }
61 //Add home_url/edit to rewrites
62 $add_frontpage_edit_rules = false;
63 if ( !get_page_by_path( $endpoint ) ) {
64 $add_frontpage_edit_rules = true;
65 } else {
66 $page = get_page_by_path( $endpoint );
67 if ( is_a( $page, 'WP_Post' ) && $page->post_status != 'publish' ) {
68 $add_frontpage_edit_rules = true;
69 }
70 }
71 if ( $add_frontpage_edit_rules ) {
72 $edit_array_rule = array( "{$endpoint}/?$" => 'index.php?' . $endpoint . '=frontpage' );
73 $rules = $edit_array_rule + $rules;
74 }
75 return $rules;
76 }
77 public static function deactivate() {
78 }
79
80 public function init() {
81 global $wp_rewrite;
82 $endpoint = Slash_Edit::get_instance()->get_endpoint();
83
84 //Determine if we need to flush rules for a new version of the plugin
85 $version = get_option( 'slash_edit_version', '1.0.0' );
86 if ( version_compare( $this->last_rewrite_version_update, $version, 'gt' ) ) {
87 update_option( 'slash_edit_version', $this->last_rewrite_version_update );
88 flush_rewrite_rules( false );
89 }
90
91 //Delete rewrite rules if plugin is deactivated
92 if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'deactivate' ) {
93 $plugin_basename = plugin_basename( __FILE__ );
94
95 //Let's see if we're being deactivated
96 if ( $_GET[ 'plugin' ] === $plugin_basename ) {
97 if ( wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'deactivate-plugin_' . $plugin_basename ) ) {
98 add_rewrite_endpoint( $endpoint, EP_NONE );
99 flush_rewrite_rules( false );
100 }
101 }
102 }
103
104 //Refresh rewrite rules if plugin is activated
105 add_rewrite_endpoint( $endpoint, EP_PERMALINK | EP_PAGES | EP_CATEGORIES | EP_TAGS | EP_AUTHORS ); //todo - adding EP_ATTACHMENT messes up EP_PERMALINK and EP_PAGES
106 if ( get_option( 'slash_edit_install', 'false' ) == 'true' ) {
107 flush_rewrite_rules( false );
108 delete_option( 'slash_edit_install' );
109 }
110 } //end init
111
112 public static function maybe_redirect() {
113 global $wp_query;
114 $endpoint = Slash_Edit::get_instance()->get_endpoint();
115 if ( !isset( $wp_query->query_vars[ $endpoint ] ) ) return;
116
117 $edit_url = false;
118 if ( is_attachment() || is_single() || is_page() ) { /* Post, page, attachment, or CPTs */
119 //Get the post, page, or cpt id
120 $post = get_queried_object();
121 $post_id = isset( $post->ID ) ? $post->ID : false;
122 if ( $post_id === false ) return;
123
124 //Build the url
125 $edit_url = add_query_arg( array( 'post' => absint( $post_id ), 'action' => 'edit' ), admin_url( 'post.php' ) );
126 } elseif ( is_author() ) { /* Author Page */
127 $user_data = get_queried_object();
128 if ( is_a( $user_data, 'WP_User' ) ) {
129 $user_id = $user_data->ID;
130 //Build the url
131 $edit_url = add_query_arg( array( 'user_id' => absint( $user_id ), 'action' => 'edit' ), admin_url( 'user-edit.php' ) );
132 }
133 } elseif ( is_category() || is_tag() || is_tax() ) {
134 $tax_data = get_queried_object();
135 if ( is_object( $tax_data ) && isset( $tax_data->term_id ) ) {
136 $term_id = $tax_data->term_id;
137 $taxonomy = $tax_data->taxonomy;
138 //Build the url
139 $edit_url = add_query_arg( array( 'tag_ID' => absint( $term_id ), 'taxonomy' => $taxonomy, 'action' => 'edit', 'post_type' => get_post_type() ), admin_url( 'edit-tags.php' ) );
140 }
141 }
142 //Fail safe for home_url/edit/
143 if ( $edit_url === false && 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && 'frontpage' == get_query_var( 'edit' ) ) {
144 //Build the url
145 $edit_url = add_query_arg( array( 'post' => get_option( 'page_on_front' ), 'action' => 'edit' ), admin_url( 'post.php' ) );
146 } elseif ( 'frontpage' == get_query_var( 'edit' ) ) {
147 //No front page set - so redirect back to homepage
148 $edit_url = home_url();
149 }
150
151 //Filter to rule them all
152 $edit_url = apply_filters( 'slash_edit_url', $edit_url ); //Return false for no redirect
153
154 //Return if nothing to redirect to
155 if ( $edit_url === false ) return;
156
157
158 //Redirect yo
159 wp_safe_redirect( esc_url_raw( $edit_url ) );
160 exit;
161 }
162
163 //Update rewrite rules if a parent page with slug 'edit' is edited and/or update - This way if there is a page with path www.domain.com/edit/, the page has priority
164 public static function save_post( $post_id = 0 ) {
165 if ( wp_is_post_revision( $post_id ) )
166 return;
167 global $post;
168 if ( !is_object( $post ) )
169 $post = get_post( $post_id );
170 if( $post->post_parent == 0 && $post->post_name == 'edit' && $post->post_type == 'page' ) {
171 flush_rewrite_rules( false );
172 }
173 }
174
175 } //end class Slash_Edit
176
177 add_action( 'plugins_loaded', 'slash_edit_instantiate' );
178 function slash_edit_instantiate() {
179 Slash_Edit::get_instance();
180 } //end slash_edit_instantiate
181
182 register_activation_hook( __FILE__, array( 'Slash_Edit', 'activate' ) );
183 register_activation_hook( __FILE__, array( 'Slash_Edit', 'deactivate' ) );