PluginProbe
Slash Edit: Admin Shortcuts to Edit Posts and Pages Faster / 1.2.0
Slash Edit: Admin Shortcuts to Edit Posts and Pages Faster v1.2.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.2.0, at slash-edit.php

436 lines 12.3 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: https://mediaron.com
5 * Description: Edit your posts or pages with a simple "/edit" at the end.
6 * Author: Ronald Huereca
7 * Version: 1.2.0
8 * Requires at least: 3.9.1
9 * Author URI: https://mediaron.com
10 * Contributors: ronalfy
11 *
12 * @package slash-edit
13 */
14
15 /**
16 * Slash Edit for WordPress
17 *
18 * @package slash-edit
19 */
20 class Slash_Edit {
21 /**
22 * Singleton instance
23 *
24 * @var Slash_Edit
25 */
26 private static $instance = null;
27
28 /**
29 * Endpoint
30 *
31 * @var string
32 */
33 private $endpoint = 'edit';
34
35 /**
36 * Last rewrite version update
37 *
38 * @var string
39 */
40 private $last_rewrite_version_update = '1.2.1'; // Will increment any time I need to change rewrite rules.
41
42 /**
43 * Get the singleton instance
44 *
45 * @return Slash_Edit
46 */
47 public static function get_instance() {
48 if ( null === self::$instance ) {
49 self::$instance = new self();
50 }
51 return self::$instance;
52 } //end get_instance
53
54 /**
55 * Constructor
56 *
57 * @return void
58 */
59 private function __construct() {
60 add_action( 'init', array( $this, 'init' ), 20 );
61 add_action( 'template_redirect', array( 'Slash_Edit', 'maybe_redirect' ) );
62 add_filter( 'rewrite_rules_array', array( 'Slash_Edit', 'add_rewrite_rules' ) );
63 add_action( 'save_post', array( 'Slash_Edit', 'save_post' ) );
64 $this->endpoint = sanitize_title( apply_filters( 'slash_edit_endpoint', 'edit' ) );
65 add_action( 'admin_init', array( $this, 'maybe_redirect_from_admin' ) );
66 } //end constructor
67
68 /**
69 * Get the endpoint
70 *
71 * @return string
72 */
73 public function get_endpoint() {
74 return $this->endpoint;
75 }
76
77 /**
78 * Activate the plugin
79 *
80 * @return void
81 */
82 public static function activate() {
83 update_option( 'slash_edit_install', 'true' );
84 }
85
86 /**
87 * Add rewrite rules
88 *
89 * @param array $rules Rewrite rules.
90 *
91 * @global WP_Rewrite $wp_rewrite
92 * @return array
93 */
94 public static function add_rewrite_rules( $rules ) {
95 /**
96 * WP_Rewrite $wp_rewrite WordPress rewrite object.
97 *
98 * This is not always set on init.
99 */
100 global $wp_rewrite;
101
102 if ( ! is_object( $wp_rewrite ) ) {
103 return $rules;
104 }
105 // Get taxonomies.
106 $taxonomies = get_taxonomies();
107 $blog_prefix = '';
108 $endpoint = self::get_instance()->get_endpoint();
109 if ( is_multisite() && ! is_subdomain_install() && is_main_site() ) { /* stolen from /wp-admin/options-permalink.php */
110 $blog_prefix = 'blog/';
111 }
112 $exclude = array(
113 'category',
114 'post_tag',
115 'nav_menu',
116 'link_category',
117 'post_format',
118 );
119 foreach ( $taxonomies as $key => $taxonomy ) {
120 if ( in_array( $key, $exclude, true ) ) {
121 continue;
122 }
123 // $key may have a front, so check the rewrite structure for a front.
124 $taxonomy_rewrite = get_taxonomy( $key )->rewrite;
125 $taxonomy_slug = $key;
126 if ( isset( $taxonomy_rewrite['slug'] ) ) {
127 $taxonomy_slug = $taxonomy_rewrite['slug'];
128 }
129 $rule_structure = "{$blog_prefix}{$taxonomy_slug}(?:/([^/]+)/)+{$endpoint}(/(.*))?/?$";
130 $endpoint_structure = 'index.php?' . $key . '=$matches[1]&' . $endpoint . '=$matches[3]';
131 $rules[ $rule_structure ] = $endpoint_structure;
132 }
133
134 // Add post type archive rules. This allows things like /faqs/edit, and will take you to the post type edit screen.
135 $post_types = get_post_types( array( 'has_archive' => true ) );
136 $archive_rules = array();
137 foreach ( $post_types as $post_type ) {
138 $post_type_obj = get_post_type_object( $post_type );
139
140 // Determine the archive slug.
141 $slug = $post_type_obj->name;
142 if ( is_string( $post_type_obj->has_archive ) ) {
143 $slug = $post_type_obj->has_archive;
144 } elseif ( isset( $post_type_obj->rewrite['slug'] ) ) {
145 $slug = $post_type_obj->rewrite['slug'];
146 }
147
148 // Check if we need to add front.
149 $has_front = false;
150 if ( isset( $post_type_obj->rewrite['with_front'] ) && true === $post_type_obj->rewrite['with_front'] && true === $post_type_obj->rewrite['with_front'] ) {
151 $has_front = true;
152 }
153
154 if ( $has_front && ! empty( $wp_rewrite->front ) ) {
155 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
156 }
157
158 // Add the archive edit rule.
159 $rule_structure = "{$blog_prefix}{$slug}/{$endpoint}/?$";
160 $endpoint_structure = 'index.php?post_type=' . $post_type . '&' . $endpoint . '=1';
161 $archive_rules[ $rule_structure ] = $endpoint_structure;
162 }
163 // Merge archive rules at the beginning for precedence.
164 $rules = $archive_rules + $rules;
165 // Add home_url/edit to rewrites.
166 $add_frontpage_edit_rules = false;
167 if ( ! get_page_by_path( $endpoint ) ) {
168 $add_frontpage_edit_rules = true;
169 } else {
170 $page = get_page_by_path( $endpoint );
171 if ( is_a( $page, 'WP_Post' ) && 'publish' === $page->post_status ) {
172 $add_frontpage_edit_rules = true;
173 }
174 }
175 if ( $add_frontpage_edit_rules ) {
176 $edit_array_rule = array( "{$endpoint}/?$" => 'index.php?' . $endpoint . '=frontpage' );
177 $rules = $edit_array_rule + $rules;
178 }
179 return $rules;
180 }
181
182 /**
183 * Deactivate the plugin
184 *
185 * @return void
186 */
187 public static function deactivate() {
188 }
189
190 /**
191 * Initialize the plugin
192 *
193 * @return void
194 */
195 public function init() {
196 global $wp_rewrite;
197 $endpoint = self::get_instance()->get_endpoint();
198
199 // Determine if we need to flush rules for a new version of the plugin.
200 $version = get_option( 'slash_edit_version', '1.0.0' );
201 if ( version_compare( $this->last_rewrite_version_update, $version, 'gt' ) ) {
202 update_option( 'slash_edit_version', $this->last_rewrite_version_update );
203 flush_rewrite_rules( true );
204 }
205
206 // Delete rewrite rules if plugin is deactivated.
207 if ( isset( $_GET['action'] ) && 'deactive' === sanitize_text_field( wp_unslash( $_GET['action'] ) ) ) {
208 $plugin_basename = plugin_basename( __FILE__ );
209
210 // Let's see if we're being deactivated.
211 if ( isset( $_GET['plugin'] ) && sanitize_text_field( wp_unslash( $_GET['plugin'] ) ) === $plugin_basename ) {
212 $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '';
213 if ( wp_verify_nonce( $nonce, 'deactivate-plugin_' . $plugin_basename ) ) {
214 add_rewrite_endpoint( $endpoint, EP_NONE );
215 flush_rewrite_rules( false );
216 }
217 }
218 }
219
220 // Refresh rewrite rules if plugin is activated.
221 add_rewrite_endpoint( $endpoint, EP_PERMALINK | EP_PAGES | EP_CATEGORIES | EP_TAGS | EP_AUTHORS | EP_ALL_ARCHIVES ); // todo - adding EP_ATTACHMENT messes up EP_PERMALINK and EP_PAGES.
222 if ( get_option( 'slash_edit_install', 'false' ) === 'true' ) {
223 flush_rewrite_rules( false );
224 delete_option( 'slash_edit_install' );
225 }
226 } //end init
227
228 /**
229 * Maybe redirect from admin.
230 *
231 * @return void
232 */
233 public static function maybe_redirect_from_admin() {
234 if ( ! is_admin() ) {
235 return;
236 }
237 $action_name = 'slash_edit_redirect';
238 $maybe_action = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'action', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
239 if ( $action_name !== $maybe_action ) {
240 return;
241 }
242
243 $token = sanitize_key( wp_unslash( filter_input( INPUT_GET, 'token', FILTER_SANITIZE_SPECIAL_CHARS ) ) );
244 if ( ! $token ) {
245 return;
246 }
247 $edit_url = get_transient( 'slash_edit_token_' . $token );
248 if ( ! $edit_url ) {
249 wp_safe_redirect( esc_url_raw( site_url() ) );
250 exit;
251 }
252 delete_transient( 'slash_edit_token_' . $token );
253
254 /**
255 * Filter the capability check.
256 *
257 * @param string $capability_check The capability check.
258 * @param string $edit_url The edit URL.
259 * @return string
260 */
261 $capability_check = apply_filters( 'slash_edit_capability_check', 'edit_others_posts', $edit_url );
262
263 /**
264 * Filter Can Edit override. If true, the user can edit the item regardless of the capability check.
265 *
266 * @param bool $can_edit Whether the user can override the capability check and edit the item.
267 * @param string $edit_url The edit URL.
268 * @return bool
269 */
270 $can_edit = apply_filters( 'slash_edit_can_edit', false, $edit_url );
271
272 if ( current_user_can( $capability_check ) || $can_edit ) {
273 wp_safe_redirect( esc_url_raw( $edit_url ) );
274 } else {
275 wp_die( __( 'You are not authorized to edit this item.', 'slash-edit' ) );
276 }
277 exit;
278 }
279
280 /**
281 * Maybe redirect.
282 *
283 * @return void
284 */
285 public static function maybe_redirect() {
286 global $wp_query;
287 $endpoint = self::get_instance()->get_endpoint();
288 if ( ! isset( $wp_query->query_vars[ $endpoint ] ) ) {
289 return;
290 }
291
292 $edit_url = false;
293 /**
294 * Post, page, attachment, or CPTs.
295 */
296 if ( is_attachment() || is_single() || is_page() || is_singular() ) {
297 // Get the post, page, or cpt id.
298 $post = get_queried_object();
299 $post_id = isset( $post->ID ) ? $post->ID : false;
300 if ( false === $post_id ) {
301 return;
302 }
303
304 // Build the url.
305 $edit_url = add_query_arg(
306 array(
307 'post' => absint( $post_id ),
308 'action' => 'edit',
309 ),
310 admin_url( 'post.php' )
311 );
312 } elseif ( is_author() ) { /* Author Page */
313 $user_data = get_queried_object();
314 if ( is_a( $user_data, 'WP_User' ) ) {
315 $user_id = $user_data->ID;
316 // Build the url.
317 $edit_url = add_query_arg(
318 array(
319 'user_id' => absint( $user_id ),
320 'action' => 'edit',
321 ),
322 admin_url( 'user-edit.php' )
323 );
324 }
325 } elseif ( is_category() || is_tag() || is_tax() ) {
326 $tax_data = get_queried_object();
327 if ( is_object( $tax_data ) && isset( $tax_data->term_id ) ) {
328 $term_id = $tax_data->term_id;
329 $taxonomy = $tax_data->taxonomy;
330
331 // Get taxonomy post types.
332 $post_types = get_post_types( array( 'taxonomies' => array( $taxonomy ) ) );
333 $post_type = current( array_keys( $post_types ) );
334 if ( count( $post_types ) > 1 ) {
335 $post_type = '';
336 }
337
338 // Build the url.
339 $edit_url = add_query_arg(
340 array(
341 'tag_ID' => absint( $term_id ),
342 'taxonomy' => $taxonomy,
343 'action' => 'edit',
344 'post_type' => $post_type,
345 ),
346 admin_url( 'edit-tags.php' )
347 );
348 }
349 } elseif ( is_post_type_archive() ) {
350 $post_type = sanitize_key( get_query_var( 'post_type' ) );
351 $post_type_obj = get_post_type_object( $post_type );
352 if ( null !== $post_type_obj ) {
353 $edit_url = add_query_arg(
354 array(
355 'post_type' => $post_type,
356 ),
357 admin_url( 'edit.php' )
358 );
359 }
360 }
361 // Fail safe for home_url/edit/.
362 if ( false === $edit_url && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && 'frontpage' === get_query_var( 'edit' ) ) {
363 // Build the url.
364 $edit_url = add_query_arg(
365 array(
366 'post' => get_option( 'page_on_front' ),
367 'action' => 'edit',
368 ),
369 admin_url( 'post.php' )
370 );
371 } elseif ( 'frontpage' === get_query_var( 'edit' ) ) {
372 // No front page set - so redirect back to homepage.
373 $edit_url = home_url();
374 } elseif ( is_home() ) {
375 $edit_url = admin_url( 'options-reading.php' );
376 }
377
378 // Filter to rule them all.
379 $edit_url = apply_filters( 'slash_edit_url', $edit_url ); // Return false for no redirect.
380
381 // Return if nothing to redirect to.
382 if ( false === $edit_url ) {
383 return;
384 }
385
386 // Create token. Lasts 5 minutes.
387 $token = sanitize_key( wp_generate_password( 20, false ) );
388 set_transient( 'slash_edit_token_' . $token, esc_url_raw( $edit_url ), 5 * MINUTE_IN_SECONDS );
389
390 $redirect_url = add_query_arg(
391 array(
392 'token' => $token,
393 'action' => 'slash_edit_redirect',
394 ),
395 admin_url()
396 );
397
398 // Redirect to admin with token.
399 wp_safe_redirect( esc_url_raw( $redirect_url ) );
400 exit;
401 }
402
403 /**
404 * 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.
405 *
406 * @param int $post_id The post ID.
407 * @return void
408 */
409 public static function save_post( $post_id = 0 ) {
410 $endpoint = self::get_instance()->get_endpoint();
411 if ( wp_is_post_revision( $post_id ) ) {
412 return;
413 }
414 global $post;
415 if ( ! is_object( $post ) ) {
416 $maybe_post = get_post( $post_id );
417 }
418 if ( 0 === $maybe_post->post_parent && $endpoint === $maybe_post->post_name && 'page' === $post->post_type ) {
419 flush_rewrite_rules( false );
420 }
421 }
422 } //end class Slash_Edit
423
424 add_action( 'plugins_loaded', 'slash_edit_instantiate' );
425 /**
426 * Instantiate the Slash_Edit class
427 *
428 * @return void
429 */
430 function slash_edit_instantiate() { // phpcs:ignore
431 Slash_Edit::get_instance();
432 }
433
434 register_activation_hook( __FILE__, array( 'Slash_Edit', 'activate' ) );
435 register_deactivation_hook( __FILE__, array( 'Slash_Edit', 'deactivate' ) );
436