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

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