-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrewrite-rules.php
620 lines (496 loc) · 16.4 KB
/
rewrite-rules.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
/**
* Debug Bar Rewrite Rules. Actuall plugin.
*
* @package WordPress\Plugins\Debug Bar Rewrite Rules
* @author Oleg Butuzov
* @link https://github.com/butuzov/Debug-Bar-Rewrite-Rules
* @version 0.6.5
* @license http://creativecommons.org/licenses/GPL/2.0/ GNU General Public License, version 2 or higher
*
* @wordpress-plugin
* Plugin Name: Debug Bar Rewrite Rules
* Plugin URI: https://github.com/butuzov/Debug-Bar-Rewrite-Rules
* Description: Debug Bar Rewrite Rules helps to debug WP Rewrite Rules with and without of the help of the Debug Bar.
* Version: 0.6.5
* Author: Oleg Butuzov
* Author URI: https://github.com/butuzov
* Depends: Debug Bar
* Text Domain: debug-bar-rewrite-rules
* Domain Path: /languages/
*/
// Avoid direct calls to this file.
if ( ! function_exists( 'add_action' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
// Using DBRR_FILE instead __FILE__ is only once answer if you
// developing plugin as symlink.
if ( ! defined( 'DBRR_FILE' ) && isset( $plugin ) ) {
define( 'DBRR_FILE', $plugin );
} else {
define( 'DBRR_FILE', __FILE__ );
}
if ( ! function_exists( 'umdbrr_activate' ) ) {
/**
* Activate function.
*
* @return void
*/
function umdbrr_activate() {
if ( '' !== get_option( 'permalink_structure' ) ) {
new UA_Made_Rewrite_Rules();
flush_rewrite_rules();
}
update_option( 'debug_bar_rewrite_rules_installed', 1 );
}
// Registring Activation hook.
register_activation_hook( DBRR_FILE, 'umdbrr_activate' );
}
if ( ! function_exists( 'umdbrr_deactivate' ) ) {
/**
* Uninstall/Deactivate function.
*
* On Uninstall/Deactivate action - removing settings/options.
*
* @return void
*/
function umdbrr_deactivate() {
delete_option( 'debug_bar_rewrite_rules_filters_list' );
delete_option( 'debug_bar_rewrite_rules_installed' );
}
// Deactivation or Uninstall.
foreach ( array( 'deactivation', 'uninstall' ) as $hook ) {
call_user_func( sprintf( 'register_%s_hook', $hook ), DBRR_FILE, 'umdbrr_deactivate' );
}
}
/**
* Basic DBRR class.
*/
class UA_Made_Rewrite_Rules {
const NAME = 'debug-bar-rewrite-rules';
/**
* Plugin Version
*
* @var $instance Class Instance.
*/
public $version = '0.6.5';
/**
* Single instance
*
* @var $instance Class Instance.
*/
private static $instance;
/**
* Plugin Title.
*
* @var $title Plugin Title.
*/
public $title;
/**
* Get Instance.
*/
public static function i() {
if ( is_null( self::$instance ) ) {
self::$instance = new UA_Made_Rewrite_Rules();
}
return self::$instance;
}
/**
* Class Constructor.
*/
public function __construct() {
global $wpdb;
// Running code as Singleton Pattern.
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
$_is_security_class = ! empty( $trace[1]['class'] ) && __CLASS__ === $trace[1]['class'];
$_is_instance_method = ! empty( $trace[1]['function'] ) && 'i' === $trace[1]['function'];
if ( ! $_is_security_class || ! $_is_instance_method ) {
return self::i();
}
// Setting up a tracker.
add_action( 'generate_rewrite_rules', array( $this, 'track_generate_rewrite_rules' ) );
// Adding panel to Debug Bar.
add_filter( 'debug_bar_panels', array( $this, 'debug_bar_panel' ) );
// Adding init method.
add_action( 'init', array( $this, 'init' ) );
}
/**
* Init action.
*
* @return void
*/
public function init() {
$this->initialize();
// In case if debug bar not available,
// we will have own admin page in 'tools' section.
if ( ! class_exists( 'Debug_Bar' ) ) {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
}
// Add Domate Link (temporary)
add_filter('plugin_row_meta', array( $this, 'plugin_row_links' ), 10, 2);
// Flush rules ajax action.
add_action( 'wp_ajax_debug_bar_rewrite_rules', array( $this, 'ajax' ) );
}
/**
* Adds Help Ukraine and Help Plugin Author link
*
* (temporary)
*/
function plugin_row_links( $plugin_meta, $plugin_file ) {
if ( "debug-bar-rewrite-rules/rewrite-rules.php" == $plugin_file ) {
$plugin_meta[] = sprintf('<a href="%s">%s</a>', 'https://github.com/butuzov/Debug-Bar-Rewrite-Rules/issues', __('Report Issue') );
$plugin_meta[] = sprintf('<a href="%s">%s</a>', 'https://github.com/butuzov/butuzov/blob/main/README.md#user-content-main', __('Plugin author asks for help') );
}
return $plugin_meta;
}
/**
* Add some initial data and load localization.
*
* @return void
*/
private function initialize() {
if ( empty( $this->initialized ) ) {
$this->initialized = true;
// Localization loading.
// That's sad but `load_plugin_textdomain` doesn't looks like it working, so we use other way.
$file_path = plugin_dir_path( __FILE__ ) . 'languages/' .
$this::NAME . '-' . get_locale() . '.mo';
load_textdomain( $this::NAME, $file_path );
// Translations for title/panel title and "page title".
$this->title = __( 'Rewrite Rules', 'debug-bar-rewrite-rules' );
$this->pagetitle = __( 'WordPress Rewrite Rules Inspector', 'debug-bar-rewrite-rules' );
}
}
/**
* Admin Menu Action Hook.
*
* Debug Bar Rewrite Rules as WordPress Admin Page
* (in case if debug bar not available).
*
* @return void
*/
public function admin_menu() {
$this->hook = add_management_page( $this->pagetitle, $this->title, 'manage_options', 'rewrite-rules', array( $this, 'admin_page' ) );
}
/**
* Standalone admin page implementation for view callback.
*
* @return void
*/
public function admin_page() {
$this->view();
}
/**
* View for Admin Page.
*
* A WordPress admin tool page 'view', in case if debug bar not available,
* and we forced to use standard wp-admin page.
*
* @return void
*/
private function view() {
if ( get_option( 'rewrite_rules' ) === "" ) {
echo // WPCS: XSS OK.
'<div class="wrap debug-bar-rewrites-urls">',
sprintf( '<h2>%s</h2>', $this->pagetitle ),
$this->notice_no_rewrite_rules(),
'</div>';
return;
}
echo // WPCS: XSS OK.
'<div class="wrap debug-bar-rewrites-urls">',
sprintf( '<h2>%s</h2>', $this->pagetitle ),
$this->notice_debug_bar(),
$this->stats(),
$this->rules(),
$this->filters(),
'</div>';
}
/**
* Displays error message about missing rewrite_rules.
*
* @return void
*/
public function notice_no_rewrite_rules() {
$message = __( 'Would you like to enable <a href="%s">%s</a> ?', 'debug-bar-rewrite-rules' );
$message = sprintf( $message, admin_url( 'options-permalink.php' ), __( 'Permalinks' ) );
printf( '<div class="%s"><p>%s</p></div>', 'error', $message );
}
/**
* Debug Bar notice message
*
* @return void
*/
public function notice_debug_bar() {
$message = __( 'Consider installing <a href="%s">%s</a> option of the alternative interface.', 'debug-bar-rewrite-rules' );
$message = sprintf( $message, "https://wordpress.org/plugins/debug-bar/", 'Debug Bar' );
printf( '<div class="%s"><p>%s</p></div>', 'updated', $message );
}
/**
* Adds panel, as defined in the included class, to Debug Bar.
*
* @param array $panels Array of Debug_Bar_Panel objects.
*
* @return array
*/
public function debug_bar_panel( $panels ) {
// Initializing Settings.
$this->initialize();
// Initializing Panel.
if ( ! class_exists( 'Debug_Bar_Rewrite_Rules_Panel' ) ) {
require_once 'debug-bar.php';
add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) );
$panels[] = new Debug_Bar_Rewrite_Rules_Panel( $this );
}
return $panels;
}
/**
* Assets
*
* Load styles and scripts for back- and front-end.
*
* will work for both debug bar and standalone mode.
*
* @param string $hook_suffix The dynamic portion of the hook, `$hook_suffix` -
* refers to the hook suffix for the admin page.
* @return void
*/
public function assets( $hook_suffix = '' ) {
$is_debug_bar = class_exists( 'Debug_Bar' ) && is_admin_bar_showing();
$is_admin_page = ! empty( $this->hook ) && $hook_suffix === $this->hook;
if ( ! ( $is_debug_bar || $is_admin_page ) ) {
return;
}
$suffix = ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.dev' : '' );
// Style Enqueue.
$style_url = plugins_url( 'assets/' . $this::NAME . $suffix . '.css', DBRR_FILE );
wp_enqueue_style( $this::NAME, $style_url, false, $this->version, 'all' );
// Script will be registread and enqueued to run with additional data.
$script_url = plugins_url( 'assets/' . $this::NAME . $suffix . '.js', DBRR_FILE );
$dependencies = array( 'jquery', 'underscore' );
wp_register_script( $this::NAME, $script_url, $dependencies, $this->version, true );
wp_localize_script( $this::NAME,
'debugBarRewriteRules',
array(
'nonce' => wp_create_nonce( 'debug-bar-rewrite-rules-nonce' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'validator' => plugins_url( 'validator.php', DBRR_FILE ),
'home' => trailingslashit( get_home_url() ),
'matches' => __( 'Matches', 'debug-bar-rewrite-rules' ),
));
wp_enqueue_script( $this::NAME );
}
/**
* Simple Template imlementation.
*
* This is a simple implementation of template
* functionality for WordPress.
*
* @param string $template Template location in file system.
* @param array $data Array of additional parameters used in tempalte.
* @return string HTML contents.
*/
private function template( $template, $data = array() ) {
$content = ''; // Initializing empty variable.
if ( ! file_exists( __DIR__ . '/' . $template ) && WP_DEBUG === true ) {
$content = sprintf( '<h2>Template <em class="debug">%s</em> isn\'t found</h2>', $template );
} elseif ( file_exists( __DIR__ . '/' . $template ) ) {
ob_start();
include __DIR__ . '/' . $template;
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
/**
* Info panel for Rewrite Rules.
*
* @return string
*/
public function rules() {
$domain = trailingslashit( get_home_url() );
return $this->template( '/templates/info-rules.php',
array(
'rewrite_rules' => get_option( 'rewrite_rules' ),
'i' => 0,
'domain' => $domain,
'width' => ( strlen( $domain ) * 8 ) . 'px',
));
}
/**
* Info panel for Available Hooks and Filters that affects rewrite rules.
*
* @return string
*/
public function filters() {
$stats = get_option( 'debug_bar_rewrite_rules_filters_list' );
$data = array( 'filters' => array() );
$stats['list'] = is_array( $stats['list'] ) && ! empty( $stats['list'] )
? $stats['list'] : array();
// Sorting the list of filters.
foreach ( $stats['list'] as $filter ) {
$data['filters'][ $filter ] = array(
'rowcount' => 0,
'filters' => array(),
);
if ( empty( $stats['details'][ $filter ] ) ) {
$data['filters'][ $filter ]['rowcount']++;
} else {
foreach ( $stats['details'][ $filter ] as $priority => $callbacks ) {
foreach ( $callbacks as $callback ) {
$data['filters'][ $filter ]['rowcount']++;
$data['filters'][ $filter ]['filters'][ $priority ][] = $callback;
}
}
}
}
$data['l10n'] = array(
// Functions.
'function' => __( 'Function', 'debug-bar-rewrite-rules' ),
'anonymus' => __( 'Anonymous lambda function', 'debug-bar-rewrite-rules' ),
'closure' => __( 'Closure anonymous function', 'debug-bar-rewrite-rules' ),
// Classes.
'invoked' => __( 'Callable Object', 'debug-bar-rewrite-rules' ),
'dynamic' => __( 'Dynamic method', 'debug-bar-rewrite-rules' ),
'static' => __( 'Static method', 'debug-bar-rewrite-rules' ),
);
return $this->template( '/templates/info-filters.php', $data ); // WPCS: XSS OK.
}
/**
* Will print Debug-Bar styled stats for rewrite rules.
*
* @return string
*/
public function stats() {
$stats = get_option( 'debug_bar_rewrite_rules_filters_list' );
$rules = get_option( 'rewrite_rules' );
$data = array(
'count_rules' => is_array( $rules ) ? count( $rules ) : 0,
'count_filters' => count( $stats['list'] ),
'count_filters_hooked' => empty( $stats['details'] ) ? 0 : $stats['count'],
);
return $this->template( '/templates/info-stats.php', $data ); // WPCS: XSS OK.
}
/**
* Ajax action - Reseting data.
*
* @return void
*/
public function ajax() {
$return = array();
$nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING );
$has_valid_nonce = wp_verify_nonce( $nonce, 'debug-bar-rewrite-rules-nonce' );
if ( ! current_user_can( 'manage_options' ) || ! $has_valid_nonce ) {
wp_die( wp_json_encode( $return ) );
}
flush_rewrite_rules();
$stats = get_option( 'debug_bar_rewrite_rules_filters_list' );
$results = array(
'filters' => $this->filters(),
'rules' => $this->rules(),
'count_rules' => count( get_option( 'rewrite_rules' ) ),
'count_filters' => count( $stats['list'] ),
'count_filters_hooked' => empty( $stats['details'] ) ? 0 : $stats['count'],
);
header( 'Content-type: application/json; charset=utf-8' );
wp_die( wp_json_encode( $results ) );
}
/**
* Tracking all alterations of the rewrites rules by tracking all filters.
*
* Unfortunatelly we can't track `add_rewrite_rule` function call due a
* luck of filters/actions in it.
*
* @param WP_Rewrite $wp_rewrite WP_Rewrite Object.
* @return void
*/
public function track_generate_rewrite_rules( $wp_rewrite ) {
// Current wp filters used atm.
global $wp_filter;
// List of native filters.
$native = array(
'post' => 'post',
'date' => 'date',
'root' => 'root',
'comments' => 'comments',
'search' => 'search',
'author' => 'author',
'page' => 'page',
'tag' => 'post_tag',
);
foreach ( array_keys( $wp_rewrite->extra_permastructs ) as $name ) {
if ( ! in_array( $name, $native, true ) ) {
$rules[] = sprintf( '%s_rewrite_rules', $name );
}
}
// Additional loop for native permanent structure.
foreach ( array_keys( $native ) as $name ) {
$rules[] = sprintf( '%s_rewrite_rules', $name );
}
// Final rule.
$rules[] = 'rewrite_rules_array';
$stat['rules'] = $rules;
// FIlters array initiation.
$stat = array(
'list' => $stat['rules'], // Filters available.
'count' => 0, // Total filters actions.
'details' => array(), // Actions / Filters that changing rewrite rules.
);
foreach ( $rules as $filter ) {
if ( empty( $wp_filter[ $filter ] ) ) {
continue;
}
foreach ( $wp_filter[ $filter ] as $priority => $callbacks ) {
// Incrementing counter.
$stat['count'] += count( $callbacks );
// Going thought callbacks.
foreach ( $callbacks as $callback ) {
$is_string = is_string( $callback['function'] );
$is_array = is_array( $callback['function'] );
$is_object = is_object( $callback['function'] );
$is_lambda = $is_string && strpos( $callback['function'], 'lambda_' ) !== false;
$is_callable = is_callable( $callback['function'] );
$function_exists = $is_string && function_exists( $callback['function'] );
if ( $is_array ) {
if ( is_object( $callback['function'][0] ) ) {
// Method of class object called.
$types = array( 'object', '->' );
$class = '$' . get_class( $callback['function'][0] );
} else {
// Static method of class called.
$types = array( 'static', '::' );
$class = $callback['function'][0];
}
$method = $callback['function'][1] . '()';
$type = 'static' === array_shift( $types ) ? 'static' : 'dynamic';
$view = sprintf( '%s%s%s', $class, array_shift( $types ), $method );
} elseif ( $is_string && $is_callable && $function_exists ) {
if ( $is_lambda ) {
// Lambda function call.
$type = 'anonymus';
$view = '';
} else {
// Real Function Call.
$type = 'function';
$view = $callback['function'];
}
} elseif ( $is_object && 'Closure' === get_class( $callback['function'] ) ) {
// Anonymos function call.
$type = 'closure';
$view = '';
} elseif ( $is_object && $is_callable ) {
// Callable Object (Instance of Class with __invoke method).
$type = 'invoked';
$view = get_class( $callback['function'] );
}
$stat['details'][ $filter ][ $priority ][] = array( $type, $view );
}
}
}
update_option( 'debug_bar_rewrite_rules_filters_list', $stat );
}
}
new UA_Made_Rewrite_Rules();