-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathredirection-admin.php
More file actions
901 lines (764 loc) · 25.2 KB
/
redirection-admin.php
File metadata and controls
901 lines (764 loc) · 25.2 KB
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
<?php
require_once __DIR__ . '/models/monitor.php';
require_once __DIR__ . '/models/file-io.php';
require_once __DIR__ . '/database/database.php';
require_once __DIR__ . '/redirection-capabilities.php';
class Redirection_Admin {
/**
* @var Redirection_Admin|null
*/
private static $instance = null;
/**
* @var Red_Monitor|null
* @phpstan-ignore property.onlyWritten
*/
private $monitor;
/**
* @var WP_Error|false
*/
private $fixit_failed = false;
/**
* @return Redirection_Admin|null
*/
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Redirection_Admin();
}
return self::$instance;
}
public function __construct() {
// TODO: remove this once we have a stable installed version
if ( ! class_exists( 'Red_Options' ) ) {
if ( function_exists( 'opcache_reset' ) ) {
opcache_reset();
}
add_action( 'admin_notices', [ $this, 'show_incomplete_installation_notice' ] );
return;
}
add_action( 'admin_menu', [ $this, 'admin_menu' ] );
add_action( 'admin_notices', [ $this, 'update_nag' ] );
add_filter( 'plugin_action_links_' . basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), [ $this, 'plugin_settings' ] );
add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 4 );
add_filter( 'redirection_save_options', [ $this, 'flush_schedule' ] );
add_filter( 'set-screen-option', [ $this, 'set_per_page' ], 10, 3 );
add_filter(
'set_screen_option_redirection_log_per_page',
function ( $ignore, $option, $value ) {
return $value;
},
10,
3
);
add_action( 'redirection_redirect_updated', [ $this, 'set_default_group' ], 10, 2 );
add_action( 'redirection_redirect_updated', [ $this, 'clear_cache' ] );
if ( defined( 'REDIRECTION_FLYING_SOLO' ) && REDIRECTION_FLYING_SOLO ) {
add_filter( 'script_loader_src', [ $this, 'flying_solo' ], 10, 2 );
}
register_deactivation_hook( REDIRECTION_FILE, [ 'Redirection_Admin', 'plugin_deactivated' ] );
register_uninstall_hook( REDIRECTION_FILE, [ 'Redirection_Admin', 'plugin_uninstall' ] );
$this->monitor = new Red_Monitor( Red_Options::get() );
$this->run_hacks();
}
/**
* These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites
* @return void
*/
public static function plugin_activated() {
Red_Database::apply_to_sites(
function () {
Red_Flusher::clear();
red_set_options();
}
);
}
/**
* These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites
* @return void
*/
public static function plugin_deactivated() {
Red_Database::apply_to_sites(
function () {
Red_Flusher::clear();
}
);
}
/**
* These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites
* @return void
*/
public static function plugin_uninstall() {
$database = Red_Database::get_latest_database();
Red_Database::apply_to_sites(
function () use ( $database ) {
$database->remove();
}
);
}
/**
* Show the database upgrade nag
*
* @return void
*/
public function update_nag() {
$options = Red_Options::get();
// Is the site configured to upgrade automatically?
if ( $options['plugin_update'] === 'admin' ) {
$this->automatic_upgrade();
return;
}
// Can the user perform a manual database upgrade?
if ( ! Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_OPTION_MANAGE ) ) {
return;
}
// Default manual update, with nag
$status = new Red_Database_Status();
$message = false;
if ( $status->needs_installing() ) {
/* translators: 1: URL to plugin page */
$message = sprintf( __( 'Please complete your <a href="%s">Redirection setup</a> to activate the plugin.', 'redirection' ), esc_url( $this->get_plugin_url() ) );
} elseif ( $status->needs_updating() ) {
/* translators: 1: URL to plugin page, 2: current version, 3: target version */
$message = sprintf( __( 'Redirection\'s database needs to be updated - <a href="%1$1s">click to update</a>.', 'redirection' ), esc_url( $this->get_plugin_url() ) );
}
if ( $message === false || strpos( Redirection_Request::get_request_url(), 'page=redirection.php' ) !== false ) {
return;
}
// Known HTML and so isn't escaped
// phpcs:ignore
echo '<div class="update-nag notice notice-warning" style="width: 95%">' . $message . '</div>';
}
/**
* Show incomplete installation error
*
* @return void
*/
public function show_incomplete_installation_notice() {
?>
<div class="notice notice-error">
<p>
<strong><?php esc_html_e( 'Redirection Error: Incomplete Installation Detected', 'redirection' ); ?></strong>
</p>
<p>
<?php esc_html_e( 'Redirection has detected that required files are missing or were not properly loaded. This likely means your cache needs clearing.', 'redirection' ); ?>
</p>
<p>
<?php esc_html_e( 'Please try clearing your cache and reloading the page.', 'redirection' ); ?>
</p>
</div>
<?php
}
/**
* Perform an automatic DB upgrade
*
* @return void
*/
private function automatic_upgrade() {
$loop = 0;
$status = new Red_Database_Status();
$database = new Red_Database();
// Loop until the DB is upgraded, or until a max is exceeded (just in case)
while ( $loop < 20 ) {
if ( ! $status->needs_updating() ) {
break;
}
$database->apply_upgrade( $status );
if ( $status->is_error() ) {
// If an error occurs then switch to 'prompt' mode and let the user deal with it.
red_set_options( [ 'plugin_update' => 'prompt' ] );
return;
}
$loop++;
}
}
/**
* So it finally came to this... some plugins include their JS in all pages, whether they are needed or not. If there is an error
* then this can prevent Redirection running and it's a little sensitive about that. We use the nuclear option here to disable
* all other JS while viewing Redirection
* @param string $src
* @param string $handle
* @return string|false
*/
public function flying_solo( $src, $handle ) {
$request = Redirection_Request::get_request_url();
if ( strpos( $request, 'page=redirection.php' ) !== false ) {
if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) {
if ( $this->ignore_this_plugin( $src ) ) {
return false;
}
}
}
return $src;
}
/**
* @param string $src
* @return bool
*/
private function ignore_this_plugin( $src ) {
$ignore = array(
'mootools',
'wp-seo-',
'authenticate',
'wordpress-seo',
'yikes',
);
foreach ( $ignore as $text ) {
if ( strpos( $src, $text ) !== false ) {
return true;
}
}
return false;
}
/**
* @param mixed $options
* @return mixed
*/
public function flush_schedule( $options ) {
Red_Flusher::schedule();
return $options;
}
/**
* @param mixed $status
* @param mixed $option
* @param mixed $value
* @return mixed
*/
public function set_per_page( $status, $option, $value ) {
if ( $option === 'redirection_log_per_page' ) {
$value = max( 1, min( intval( $value, 10 ), Red_Log::MAX_PER_PAGE ) );
return $value;
}
return $status;
}
/**
* @param array<int|string, string> $links
* @return array<int|string, string>
*/
public function plugin_settings( array $links ): array {
$status = new Red_Database_Status();
if ( $status->needs_updating() ) {
array_unshift( $links, '<a style="color: red" href="' . esc_url( $this->get_plugin_url() ) . '&sub=support">' . __( 'Upgrade Database', 'redirection' ) . '</a>' );
}
array_unshift( $links, '<a href="' . esc_url( $this->get_plugin_url() ) . '&sub=options">' . __( 'Settings', 'redirection' ) . '</a>' );
return $links;
}
/**
* @param mixed $plugin_meta
* @param mixed $plugin_file
* @param mixed $plugin_data
* @param mixed $status
* @return mixed
*/
public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
if ( $plugin_file === basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ) ) {
$plugin_data['Description'] .= '<p>' . __( 'Please upgrade your database', 'redirection' ) . '</p>';
}
return $plugin_meta;
}
/**
* @return string
*/
private function get_plugin_url() {
return admin_url( 'tools.php?page=' . basename( REDIRECTION_FILE ) );
}
/**
* @return string
*/
private function get_first_available_page_url() {
$pages = Redirection_Capabilities::get_available_pages();
if ( count( $pages ) > 0 ) {
return $this->get_plugin_url() . ( $pages[0] === 'redirect' ? '' : '&sub=' . rawurlencode( $pages[0] ) );
}
return admin_url();
}
/**
* @param mixed $name
* @return string|null
*/
private function get_query( $name ) {
if ( isset( $_GET[ $name ] ) ) {
return sanitize_text_field( $_GET[ $name ] );
}
return null;
}
/**
* @return void
*/
public function redirection_head() {
global $wp_version;
// Does user have access to this page?
if ( $this->get_current_page() === false ) {
// Redirect to root plugin page
wp_safe_redirect( $this->get_first_available_page_url() );
die();
}
if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && is_string( $_REQUEST['action'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) !== false ) {
$action = sanitize_text_field( $_REQUEST['action'] );
if ( $action === 'fixit' ) {
$this->run_fixit();
} elseif ( $action === 'rest_api' && isset( $_REQUEST['rest_api'] ) && is_string( $_REQUEST['rest_api'] ) ) {
$this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) );
}
}
$build = REDIRECTION_VERSION . '-' . REDIRECTION_BUILD;
$preload = $this->get_preload_data();
$options = Red_Options::get();
$versions = array(
'Plugin: ' . REDIRECTION_VERSION . ' ' . REDIRECTION_DB_VERSION,
'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')',
'PHP: ' . phpversion(),
'Browser: ' . Redirection_Request::get_user_agent(),
'JavaScript: ' . plugin_dir_url( REDIRECTION_FILE ) . 'build/redirection.js?ver=' . $build,
'REST API: ' . red_get_rest_api(),
);
$this->inject();
// Add contextual help to some pages
if ( in_array( $this->get_current_page(), [ 'redirect', 'log', '404s', 'groups' ], true ) ) {
add_screen_option(
'per_page',
array(
/* translators: maximum number of log entries */
'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), Red_Log::MAX_PER_PAGE ),
'default' => Red_Log::DEFAULT_PER_PAGE,
'option' => 'redirection_log_per_page',
)
);
}
$assets = include plugin_dir_path( REDIRECTION_FILE ) . 'build/redirection.asset.php';
$dependencies = $assets['dependencies'];
$version = $assets['version'];
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'build/redirection.js', $dependencies, $version, true );
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'build/redirection.css', [], $version );
$is_new = false;
$major_version = implode( '.', array_slice( explode( '.', REDIRECTION_VERSION ), 0, 2 ) );
if ( $this->get_query( 'page' ) === 'redirection.php' && strpos( REDIRECTION_VERSION, '-beta' ) === false ) {
$is_new = version_compare( (string) $options['update_notice'], $major_version ) < 0;
}
$status = new Red_Database_Status();
$status->check_tables_exist();
// Fix some sites having a version set to +OK - not sure why
if ( $options['database'] === '+OK' ) {
red_set_options( [ 'database' => REDIRECTION_DB_VERSION ] );
$status->stop_update();
}
wp_localize_script(
'redirection',
'Redirectioni10n',
array(
'api' => [
'WP_API_root' => esc_url_raw( red_get_rest_api() ),
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
'site_health' => admin_url( 'site-health.php' ),
'current' => $options['rest_api'],
'routes' => [
Red_Options::API_JSON => red_get_rest_api( Red_Options::API_JSON ),
Red_Options::API_JSON_INDEX => red_get_rest_api( Red_Options::API_JSON_INDEX ),
Red_Options::API_JSON_RELATIVE => red_get_rest_api( Red_Options::API_JSON_RELATIVE ),
],
],
'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
'pluginRoot' => $this->get_plugin_url(),
'per_page' => $this->get_per_page(),
'locale' => implode( '-', array_slice( explode( '-', str_replace( '_', '-', get_locale() ) ), 0, 2 ) ),
'settings' => $options,
'preload' => $preload,
'versions' => implode( "\n", $versions ),
'version' => REDIRECTION_VERSION,
'database' => $status->get_json(),
'caps' => [
'pages' => Redirection_Capabilities::get_available_pages(),
'capabilities' => Redirection_Capabilities::get_all_capabilities(),
],
'update_notice' => $is_new ? $major_version : false,
)
);
wp_set_script_translations( 'redirection', 'redirection' );
$this->add_help_tab();
}
/**
* Some plugins misbehave, so this attempts to 'fix' them so Redirection can get on with it's work
* @return void
*/
private function run_hacks() {
add_filter( 'ip-geo-block-admin', array( $this, 'ip_geo_block' ) );
}
/**
* This works around the IP Geo Block plugin being very aggressive and breaking Redirection
*
* @param array<string, mixed> $validate
* @return array<string, mixed>
*/
public function ip_geo_block( array $validate ): array {
$url = Redirection_Request::get_request_url();
$override = array(
'tools.php?page=redirection.php',
'action=red_proxy&rest_path=redirection',
);
foreach ( $override as $path ) {
if ( strpos( $url, $path ) !== false ) {
return array(
'result' => 'passed',
'auth' => false,
'asn' => false,
'code' => false,
'ip' => false,
);
}
}
return $validate;
}
/**
* @return void
*/
private function run_fixit() {
if ( Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SUPPORT_MANAGE ) ) {
require_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$fixer = new Red_Fixer();
$result = $fixer->fix( $fixer->get_status() );
if ( is_wp_error( $result ) ) {
$this->fixit_failed = $result;
}
}
}
/**
* @param mixed $api
* @return void
*/
private function set_rest_api( $api ) {
if ( $api >= 0 && $api <= Red_Options::API_JSON_RELATIVE ) {
red_set_options( array( 'rest_api' => intval( $api, 10 ) ) );
}
}
/**
* @return array{importers: list<array{id: string, name: string, total: int}>}|array{pluginStatus: array<string, mixed>}|array{}
*/
private function get_preload_data(): array {
$status = new Red_Database_Status();
if ( $status->needs_installing() ) {
include_once __DIR__ . '/models/importer.php';
return [
'importers' => Red_Plugin_Importer::get_plugins(),
];
}
if ( $this->get_current_page() === 'support' ) {
require_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$fixer = new Red_Fixer();
return array(
'pluginStatus' => $fixer->get_json(),
);
}
return [];
}
/**
* @return void
*/
private function add_help_tab() {
/* translators: URL */
$content = sprintf( __( 'You can find full documentation about using Redirection on the <a href="%s" target="_blank">redirection.me</a> support site.', 'redirection' ), 'https://redirection.me/support/?utm_source=redirection&utm_medium=plugin&utm_campaign=context-help' );
$title = __( 'Redirection Support', 'redirection' );
$current_screen = get_current_screen();
if ( $current_screen === null ) {
return;
}
$current_screen->add_help_tab(
array(
'id' => 'redirection',
'title' => 'Redirection',
'content' => "<h2>$title</h2><p>$content</p>",
)
);
}
/**
* @return int
*/
private function get_per_page() {
$per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 );
return $per_page > 0 ? max( 5, min( $per_page, Red_Log::MAX_PER_PAGE ) ) : Red_Log::DEFAULT_PER_PAGE;
}
/**
* @return void
*/
public function admin_menu() {
$hook = add_management_page( 'Redirection', 'Redirection', Redirection_Capabilities::get_plugin_access(), basename( REDIRECTION_FILE ), [ $this, 'admin_screen' ] );
add_action( 'load-' . $hook, [ $this, 'redirection_head' ] );
}
/**
* @return bool
*/
private function check_minimum_wp() {
$wp_version = get_bloginfo( 'version' );
if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) {
return false;
}
return true;
}
/**
* Update the cache key when updating or creating a redirect
*
* @return void
*/
public function clear_cache() {
$settings = Red_Options::get();
if ( $settings['cache_key'] > 0 ) {
red_set_options( [ 'cache_key' => time() ] );
}
}
/**
* @param mixed $id
* @param mixed $redirect
* @return void
*/
public function set_default_group( $id, $redirect ) {
red_set_options( array( 'last_group_id' => $redirect->get_group_id() ) );
}
/**
* @return void
*/
public function admin_screen() {
if ( count( Redirection_Capabilities::get_all_capabilities() ) === 0 ) {
die( 'You do not have sufficient permissions to access this page.' );
}
if ( $this->check_minimum_wp() === false ) {
$this->show_minimum_wordpress();
return;
}
if ( $this->fixit_failed instanceof WP_Error ) {
$this->show_fixit_failed();
}
Red_Flusher::schedule();
$this->show_main();
}
/**
* @return void
*/
private function show_fixit_failed() {
if ( $this->fixit_failed === false ) {
return;
}
?>
<div class="notice notice-error">
<h1><?php echo esc_html( $this->fixit_failed->get_error_message() ); ?></h1>
<p><?php echo esc_html( $this->fixit_failed->get_error_data() ); ?></p>
</div>
<?php
}
/**
* @return void
*/
private function show_minimum_wordpress() {
global $wp_version;
/* translators: 1: Expected WordPress version, 2: Actual WordPress version */
$wp_requirement = sprintf( __( 'Redirection requires WordPress v%1$1s, you are using v%2$2s - please update your WordPress', 'redirection' ), REDIRECTION_MIN_WP, $wp_version );
?>
<div class="react-error">
<h1><?php esc_html_e( 'Unable to load Redirection', 'redirection' ); ?></h1>
<p><?php echo esc_html( $wp_requirement ); ?></p>
</div>
<?php
}
/**
* @return void
*/
private function show_load_fail() {
?>
<div class="react-error" style="display: none">
<h1><?php esc_html_e( 'Unable to load Redirection ☹️', 'redirection' ); ?> v<?php echo esc_html( REDIRECTION_VERSION ); ?></h1>
<p><?php esc_html_e( "This may be caused by another plugin - look at your browser's error console for more details.", 'redirection' ); ?></p>
<p><?php esc_html_e( 'If you are using a page caching plugin or service (CloudFlare, OVH, etc) then you can also try clearing that cache.', 'redirection' ); ?></p>
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: redirection script filename */
__( 'Also check if your browser is able to load %s:', 'redirection' ),
'<code>' . esc_html( 'redirection.js' ) . '</code>'
)
);
?>
</p>
<p><code><?php echo esc_html( plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js?ver=' . rawurlencode( REDIRECTION_VERSION ) . '-' . rawurlencode( REDIRECTION_BUILD ) ); ?></code></p>
<p><?php esc_html_e( 'Please note that Redirection requires the WordPress REST API to be enabled. If you have disabled this then you won\'t be able to use Redirection', 'redirection' ); ?></p>
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: URL to common problems documentation */
__( 'Please see the <a href="%s">list of common problems</a>.', 'redirection' ),
esc_url( 'https://redirection.me/support/problems/' )
)
);
?>
</p>
<p><?php esc_html_e( 'If you think Redirection is at fault then create an issue.', 'redirection' ); ?></p>
<p class="versions">
<?php
echo wp_kses_post(
__( '<code>Redirectioni10n</code> is not defined. This usually means another plugin is blocking Redirection from loading. Please disable all plugins and try again.', 'redirection' )
);
?>
</p>
<p>
<a class="button-primary" target="_blank" href="https://github.com/johngodley/redirection/issues/new?title=Problem%20starting%20Redirection%20<?php echo esc_attr( REDIRECTION_VERSION ); ?>">
<?php esc_html_e( 'Create Issue', 'redirection' ); ?>
</a>
</p>
</div>
<?php
}
/**
* @return void
*/
private function show_main() {
?>
<div id="react-modal"></div>
<div id="react-ui">
<div class="react-loading">
<h1><?php esc_html_e( 'Loading, please wait...', 'redirection' ); ?></h1>
<span class="react-loading-spinner"></span>
</div>
<noscript><?php esc_html_e( 'Please enable JavaScript', 'redirection' ); ?></noscript>
<?php $this->show_load_fail(); ?>
</div>
<script>
var prevError = window.onerror;
var errors = [];
var timeout = 0;
var timer = setInterval( function() {
if ( isRedirectionLoaded() ) {
resetAll();
} else if ( errors.length > 0 || timeout++ === 5 ) {
showError();
}
}, 5000 );
function isRedirectionLoaded() {
return typeof redirection !== 'undefined';
}
function showError() {
var errorText = "";
if ( errors.length > 0 ) {
errorText = "```\n" + errors.join( ',' ) + "\n```\n\n";
}
resetAll();
if ( document.querySelector( '.react-loading' ) ) {
document.querySelector( '.react-loading' ).style.display = 'none';
document.querySelector( '.react-error' ).style.display = 'block';
if ( typeof Redirectioni10n !== 'undefined' && Redirectioni10n ) {
document.querySelector( '.versions' ).innerHTML = Redirectioni10n.versions.replace( /\n/g, '<br />' );
document.querySelector( '.react-error .button-primary' ).href += '&body=' + encodeURIComponent( errorText ) + encodeURIComponent( Redirectioni10n.versions );
}
} else {
document.querySelector( '#react-ui' ).innerHTML = '<p>Sorry something went very wrong.</p>';
}
}
function resetAll() {
clearInterval( timer );
window.onerror = prevError;
}
window.onerror = function( error, url, line ) {
console.error( error );
errors.push( error + ' ' + url + ' ' + line );
};
</script>
<?php
}
/**
* Get the current plugin page.
* Uses $_GET['sub'] to determine the current page unless a page is supplied.
*
* @param string|false $page Current page.
* @return string|boolean Current page, or false.
*/
private function get_current_page( $page = false ) {
if ( $page === false ) {
$page = 'redirect';
if ( $this->get_query( 'sub' ) !== null ) {
$page = $this->get_query( 'sub' );
}
}
// Are we allowed to access this page?
if ( in_array( $page, Redirection_Capabilities::get_available_pages(), true ) ) {
return $page;
}
return false;
}
/**
* @return void
*/
private function inject() {
$page = $this->get_query( 'page' );
$current_page = $this->get_current_page();
if ( $page !== null && $current_page !== 'redirect' && $page === 'redirection.php' ) {
$this->try_export_logs();
$this->try_export_redirects();
$this->try_export_rss();
}
}
/**
* @return void
*/
public function try_export_rss() {
$token = $this->get_query( 'token' );
$sub = $this->get_query( 'sub' );
if ( $token !== null && $sub === 'rss' && Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_MANAGE ) ) {
$options = Red_Options::get();
if ( $token === $options['token'] && $options['token'] !== '' ) {
$module = $this->get_query( 'module' );
$items = Red_Item::get_all_for_module( intval( $module, 10 ) );
$exporter = Red_FileIO::create( 'rss' );
if ( $exporter !== false ) {
$exporter->force_download();
echo $exporter->get_data( $items, array() );
die();
}
}
}
}
/**
* @return void
*/
private function try_export_logs() {
if ( Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE ) && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) !== false ) {
if ( $this->get_current_page() === 'log' ) {
Red_Redirect_Log::export_to_csv();
} elseif ( $this->get_current_page() === '404s' ) {
Red_404_Log::export_to_csv();
}
die();
}
}
/**
* @return void
*/
private function try_export_redirects() {
$sub = $this->get_query( 'sub' );
if ( $sub !== 'io' ) {
return;
}
$export = $this->get_query( 'export' );
$exporter = $this->get_query( 'exporter' );
if ( Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE ) && $export !== null && $exporter !== null && check_admin_referer( 'wp_rest' ) !== false ) {
$export = Red_FileIO::export( $export, $exporter );
if ( $export !== false ) {
$export['exporter']->force_download();
// This data is not displayed and will be downloaded to a file
echo str_replace( '&', '&', wp_kses( $export['data'], 'strip' ) );
die();
}
}
}
}
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
// @phpstan-ignore return.void
add_action( 'init', array( 'Redirection_Admin', 'init' ) );
// This is causing a lot of problems with the REST API - disable qTranslate
add_filter(
'qtranslate_language_detect_redirect',
function ( $lang, $url ) {
$url = Redirection_Request::get_request_url();
if ( strpos( $url, '/wp-json/' ) !== false || strpos( $url, '?rest_route' ) !== false ) {
return false;
}
return $lang;
},
10,
2
);