-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path404-solution.php
More file actions
1218 lines (1109 loc) · 45.6 KB
/
404-solution.php
File metadata and controls
1218 lines (1109 loc) · 45.6 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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (!defined('ABSPATH')) {
exit;
}
/*
Plugin Name: 404 Solution
Plugin URI: https://www.ajexperience.com/404-solution/
Description: The smartest 404 plugin - uses intelligent matching and spell-checking to find what visitors were actually looking for, not just redirect to homepage
Author: Aaron J
Author URI: https://www.ajexperience.com/404-solution/
Version: 4.1.3
Requires at least: 5.0
Requires PHP: 7.4
License: GPL-3.0-or-later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Domain Path: /languages
Text Domain: 404-solution
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Guard constant definitions so unit tests (and unusual loaders) can define them first.
if (!defined('ABJ404_PP')) {
define('ABJ404_PP', 'abj404_solution');
}
if (!defined('ABJ404_FILE')) {
define('ABJ404_FILE', __FILE__);
}
if (!defined('ABJ404_PATH')) {
define('ABJ404_PATH', plugin_dir_path(ABJ404_FILE));
}
if (!defined('ABJ404_SHORTCODE_NAME')) {
define('ABJ404_SHORTCODE_NAME', 'abj404_solution_page_suggestions');
}
if (!isset($GLOBALS['abj404_display_errors'])) {
$GLOBALS['abj404_display_errors'] = false;
}
// Boot state: tracks whether the plugin loaded successfully.
// If a required file is missing or Loader.php fails, these let us show
// a degraded admin page instead of a fatal error.
$GLOBALS['abj404_boot_ok'] = false;
$GLOBALS['abj404_missing_files'] = array();
$GLOBALS['abj404_boot_error'] = '';
// Used by multiple classes during early admin initialization (e.g. upgrade/migration paths).
// This must be defined before any Loader.php initialization that might touch Logging/SynchronizationUtils.
if (!function_exists('abj404_getUploadsDir')) {
/** @return string */
function abj404_getUploadsDir() {
$uploadsDirArray = wp_upload_dir(null, false);
$uploadsDir = $uploadsDirArray['basedir'];
$uploadsDir .= DIRECTORY_SEPARATOR . 'temp_' . ABJ404_PP . DIRECTORY_SEPARATOR;
return $uploadsDir;
}
}
if (!function_exists('abj404_get_settings_options')) {
/**
* Centralized settings read so call sites don't repeat option-shape checks.
*
* @return array<string, mixed>
*/
function abj404_get_settings_options() {
$options = get_option('abj404_settings');
return is_array($options) ? $options : array();
}
}
// Debug whitelist - only includes localhost/development environments by default
// WARNING: Only add trusted domains to this list. External domains could be a security risk.
// This list is used to enable detailed error logging for debugging purposes.
$GLOBALS['abj404_whitelist'] = array('127.0.0.1', '::1', 'localhost');
// Allow filtering the whitelist for advanced users who need to add custom domains
// Usage: add_filter('abj404_debug_whitelist', function($whitelist) { $whitelist[] = 'yourdomain.com'; return $whitelist; });
if (has_filter('abj404_debug_whitelist')) {
$GLOBALS['abj404_whitelist'] = apply_filters('abj404_debug_whitelist', $GLOBALS['abj404_whitelist']);
}
/**
* @param string $class
* @return void
*/
function abj404_autoloader($class) {
// some people were having issues with possibly parent classes not being loaded before their children.
$childParentMap = [
'ABJ_404_Solution_FunctionsMBString' => 'ABJ_404_Solution_Functions',
'ABJ_404_Solution_FunctionsPreg' => 'ABJ_404_Solution_Functions',
];
// only pay attention if it's for us. don't bother for other things.
if (substr($class, 0, 16) !== 'ABJ_404_Solution') {
return;
}
// Use a deterministic classmap to avoid runtime glob() scans on real sites.
static $abj404_autoLoaderClassMap = null;
if ($abj404_autoLoaderClassMap === null) {
$mapFile = __DIR__ . '/includes/classmap.php';
$abj404_autoLoaderClassMap = file_exists($mapFile) ? require $mapFile : array();
}
if (!array_key_exists($class, $abj404_autoLoaderClassMap)) {
return;
}
// Trait dependency pre-check: classes that use require_once for trait files at file
// scope will cause an uncatchable compile-time fatal if any trait file is missing.
// Verify all trait files exist BEFORE loading the parent class.
static $traitDependencies = null;
if ($traitDependencies === null) {
// Use __DIR__ (not ABJ404_PATH) to match the classmap's path resolution.
$inc = __DIR__ . '/includes/';
$traitDependencies = array(
'ABJ_404_Solution_View' => array(
$inc . 'ViewTrait_Shared.php',
$inc . 'ViewTrait_UI.php',
$inc . 'ViewTrait_Stats.php',
$inc . 'ViewTrait_Settings.php',
$inc . 'ViewTrait_Redirects.php',
$inc . 'ViewTrait_RedirectsTable.php',
$inc . 'ViewTrait_Logs.php',
),
'ABJ_404_Solution_DataAccess' => array(
$inc . 'DataAccessTrait_Maintenance.php',
$inc . 'DataAccessTrait_ViewQueries.php',
$inc . 'DataAccessTrait_Logs.php',
$inc . 'DataAccessTrait_Redirects.php',
$inc . 'DataAccessTrait_Stats.php',
$inc . 'DataAccessTrait_ErrorClassification.php',
),
'ABJ_404_Solution_PluginLogic' => array(
$inc . 'PluginLogicTrait_UrlNormalization.php',
$inc . 'PluginLogicTrait_AdminActions.php',
$inc . 'PluginLogicTrait_ImportExport.php',
$inc . 'PluginLogicTrait_SettingsUpdate.php',
$inc . 'PluginLogicTrait_PageOrdering.php',
),
'ABJ_404_Solution_SpellChecker' => array(
$inc . 'SpellCheckerTrait_PostListeners.php',
$inc . 'SpellCheckerTrait_URLMatching.php',
$inc . 'SpellCheckerTrait_CandidateFiltering.php',
$inc . 'SpellCheckerTrait_LevenshteinEngine.php',
),
'ABJ_404_Solution_DatabaseUpgradesEtc' => array(
$inc . 'DatabaseUpgradesEtcTrait_NGram.php',
$inc . 'DatabaseUpgradesEtcTrait_Maintenance.php',
$inc . 'DatabaseUpgradesEtcTrait_PluginUpdate.php',
),
);
}
if (isset($traitDependencies[$class])) {
foreach ($traitDependencies[$class] as $traitFile) {
if (!file_exists($traitFile)) {
$GLOBALS['abj404_missing_files'][] = $traitFile;
// Don't load the parent class — the compile-time fatal is uncatchable.
return;
}
}
}
// Ensure the parent class is loaded first.
if (array_key_exists($class, $childParentMap)) {
$parentClass = $childParentMap[$class];
if (!class_exists($parentClass, false) && array_key_exists($parentClass, $abj404_autoLoaderClassMap)) {
$parentFile = $abj404_autoLoaderClassMap[$parentClass];
if (!file_exists($parentFile)) {
$GLOBALS['abj404_missing_files'][] = $parentFile;
return;
}
require_once $parentFile;
}
}
$classFile = $abj404_autoLoaderClassMap[$class];
if (!file_exists($classFile)) {
$GLOBALS['abj404_missing_files'][] = $classFile;
return;
}
require_once $classFile;
}
spl_autoload_register('abj404_autoloader');
add_action('doing_it_wrong_run', function($function_name, $message, $version) {
if (strpos($message, '404-solution') !== false &&
$function_name == '_load_textdomain_just_in_time') {
try {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Prepare the plugin path from ABJ404_FILE
$pluginPath = trailingslashit(plugin_dir_path(ABJ404_FILE)); // e.g., /var/www/html/wp-content/plugins/404-solution/
$logMessage = '';
$isOurPlugin = false;
foreach ($backtrace as $index => $frame) {
$file = isset($frame['file']) ? $frame['file'] : '[internal function]';
$line = isset($frame['line']) ? $frame['line'] : '';
$func = $frame['function'];
if (!$isOurPlugin && is_string($file) && strpos($file, $pluginPath) !== false) {
$isOurPlugin = true;
}
$logMessage .= "#$index $func at [$file:$line]\n";
}
if ($isOurPlugin) {
$header = "=== Detected Early Translation ===\n" .
"Function: $function_name\n" .
"Message: $message\n" .
"Version: $version\n";
if (!isset($GLOBALS['abj404_pending_errors'])) {
$GLOBALS['abj404_pending_errors'] = [];
}
$GLOBALS['abj404_pending_errors'][] = $header . $logMessage;
}
} catch (Throwable $e) {
// error_log('Failed to log early translation stack trace: ' . $e->getMessage());
}
}
}, 10, 3);
// shortcode
add_shortcode(ABJ404_SHORTCODE_NAME, 'abj404_shortCodeListener');
if (!function_exists('abj404_shortCodeListener')) {
/**
* @param array<string, mixed>|string $atts
* @return string
*/
function abj404_shortCodeListener($atts) {
if (!$GLOBALS['abj404_boot_ok']) {
return '';
}
abj404_load_textdomain_if_needed();
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
/** @var array<string, mixed> $safeAtts */
$safeAtts = is_array($atts) ? $atts : array();
return ABJ_404_Solution_ShortCode::shortcodePageSuggestions($safeAtts);
}
if (!function_exists('abj404_get_required_runtime_files')) {
/**
* Files required for a healthy runtime/plugin package.
* Covers boot-critical PHP files and essential SQL templates.
*
* @return array<int, string>
*/
function abj404_get_required_runtime_files() {
$inc = ABJ404_PATH . 'includes/';
return array(
// Boot-critical
$inc . 'Loader.php',
$inc . 'bootstrap.php',
$inc . 'classmap.php',
$inc . 'ServiceContainer.php',
$inc . 'ErrorHandler.php',
// Core classes
$inc . 'WordPress_Connector.php',
$inc . 'Functions.php',
$inc . 'Logging.php',
$inc . 'FrontendRequestPipeline.php',
$inc . 'ImportExportService.php',
// View + traits
$inc . 'View.php',
$inc . 'ViewTrait_Shared.php',
$inc . 'ViewTrait_UI.php',
$inc . 'ViewTrait_Stats.php',
$inc . 'ViewTrait_Settings.php',
$inc . 'ViewTrait_Redirects.php',
$inc . 'ViewTrait_RedirectsTable.php',
$inc . 'ViewTrait_Logs.php',
// DataAccess + traits
$inc . 'DataAccess.php',
$inc . 'DataAccessTrait_Maintenance.php',
$inc . 'DataAccessTrait_ViewQueries.php',
$inc . 'DataAccessTrait_Logs.php',
$inc . 'DataAccessTrait_Redirects.php',
$inc . 'DataAccessTrait_Stats.php',
// PluginLogic + traits
$inc . 'PluginLogic.php',
$inc . 'PluginLogicTrait_UrlNormalization.php',
$inc . 'PluginLogicTrait_AdminActions.php',
$inc . 'PluginLogicTrait_ImportExport.php',
$inc . 'PluginLogicTrait_SettingsUpdate.php',
$inc . 'PluginLogicTrait_PageOrdering.php',
// SpellChecker + traits
$inc . 'SpellChecker.php',
$inc . 'SpellCheckerTrait_PostListeners.php',
$inc . 'SpellCheckerTrait_URLMatching.php',
$inc . 'SpellCheckerTrait_CandidateFiltering.php',
$inc . 'SpellCheckerTrait_LevenshteinEngine.php',
// DatabaseUpgradesEtc + traits
$inc . 'DatabaseUpgradesEtc.php',
$inc . 'DatabaseUpgradesEtcTrait_NGram.php',
$inc . 'DatabaseUpgradesEtcTrait_Maintenance.php',
$inc . 'DatabaseUpgradesEtcTrait_PluginUpdate.php',
// SQL templates — all files required for correct operation.
// A test (SqlFileIntegrityListCompletenessTest) verifies this list
// stays in sync with the actual files in includes/sql/.
$inc . 'sql/correctLookupTableIssue.sql',
$inc . 'sql/createEngineProfilesTable.sql',
$inc . 'sql/createLogTable.sql',
$inc . 'sql/createLogsHitsTempTable.sql',
$inc . 'sql/createLookupTable.sql',
$inc . 'sql/createNGramCacheTable.sql',
$inc . 'sql/createPermalinkCacheTable.sql',
$inc . 'sql/createRedirectConditionsTable.sql',
$inc . 'sql/createRedirectsTable.sql',
$inc . 'sql/createSpellingCacheTable.sql',
$inc . 'sql/createViewCacheTable.sql',
$inc . 'sql/deleteOldLogs.sql',
$inc . 'sql/getAdditionalPostData.sql',
$inc . 'sql/getIDsNeededForPermalinkCache.sql',
$inc . 'sql/getLogRecords.sql',
$inc . 'sql/getLogsCount.sql',
$inc . 'sql/getLogsIDandURL.sql',
$inc . 'sql/getLogsIDandURLForAjax.sql',
$inc . 'sql/getMostUnusedRedirects.sql',
$inc . 'sql/getOrphanedAutoRedirects.sql',
$inc . 'sql/getPermalinkFromURL.sql',
$inc . 'sql/getPostsNeedingContentKeywords.sql',
$inc . 'sql/getPublishedCategories.sql',
$inc . 'sql/getPublishedImageIDs.sql',
$inc . 'sql/getPublishedPagesAndPostsIDs.sql',
$inc . 'sql/getPublishedTags.sql',
$inc . 'sql/getRedirectsExport.sql',
$inc . 'sql/getRedirectsForView.sql',
$inc . 'sql/getRedirectsForViewTempTable.sql',
$inc . 'sql/getRedirectsWithLogs.sql',
$inc . 'sql/importDataFromPluginRedirectioner.sql',
$inc . 'sql/insertPermalinkCache.sql',
$inc . 'sql/insertSpellingCache.sql',
$inc . 'sql/logsSetMinLogID.sql',
$inc . 'sql/migrateToNewLogsTable.sql',
$inc . 'sql/selectTableEngines.sql',
$inc . 'sql/updatePermalinkCache.sql',
$inc . 'sql/updatePermalinkCacheParentPages.sql',
);
}
}
if (!function_exists('abj404_verify_runtime_integrity')) {
/**
* Validate that required plugin files are present.
*
* @return array<int, string> Missing file paths.
*/
function abj404_verify_runtime_integrity() {
$missing = array();
foreach (abj404_get_required_runtime_files() as $path) {
if (!file_exists($path)) {
$missing[] = $path;
}
}
return $missing;
}
}
if (!function_exists('abj404_is_benchmark_request')) {
/**
* Benchmark instrumentation is disabled by default and only enabled per-request.
*
* @return bool
*/
function abj404_is_benchmark_request() {
return isset($_GET['abj404_bench']) && (string)$_GET['abj404_bench'] === '1';
}
}
if (!function_exists('abj404_benchmark_bootstrap_start')) {
/** @return void */
function abj404_benchmark_bootstrap_start() {
if (!abj404_is_benchmark_request()) {
return;
}
if (!isset($GLOBALS['abj404_benchmark_state']) || !is_array($GLOBALS['abj404_benchmark_state'])) {
$GLOBALS['abj404_benchmark_state'] = array(
'start' => microtime(true),
'bootstrap_done' => 0.0,
'db_query_count' => 0,
'db_query_ms' => 0.0,
'redirect_lookup_ms' => 0.0,
);
}
}
}
if (!function_exists('abj404_benchmark_mark_bootstrap_done')) {
/** @return void */
function abj404_benchmark_mark_bootstrap_done() {
if (!abj404_is_benchmark_request() || !isset($GLOBALS['abj404_benchmark_state'])) {
return;
}
$GLOBALS['abj404_benchmark_state']['bootstrap_done'] = microtime(true);
}
}
if (!function_exists('abj404_benchmark_record_db_query')) {
/**
* @param float $elapsedMs
* @return void
*/
function abj404_benchmark_record_db_query($elapsedMs) {
if (!abj404_is_benchmark_request() || !isset($GLOBALS['abj404_benchmark_state'])) {
return;
}
$elapsedMs = max(0.0, (float)$elapsedMs);
$GLOBALS['abj404_benchmark_state']['db_query_count']++;
$GLOBALS['abj404_benchmark_state']['db_query_ms'] += $elapsedMs;
}
}
if (!function_exists('abj404_benchmark_record_redirect_lookup')) {
/**
* @param float $elapsedMs
* @return void
*/
function abj404_benchmark_record_redirect_lookup($elapsedMs) {
if (!abj404_is_benchmark_request() || !isset($GLOBALS['abj404_benchmark_state'])) {
return;
}
$GLOBALS['abj404_benchmark_state']['redirect_lookup_ms'] += max(0.0, (float)$elapsedMs);
}
}
if (!function_exists('abj404_benchmark_emit_headers')) {
/** @return void */
function abj404_benchmark_emit_headers() {
if (!abj404_is_benchmark_request() || headers_sent() || !isset($GLOBALS['abj404_benchmark_state'])) {
return;
}
$state = $GLOBALS['abj404_benchmark_state'];
$start = isset($state['start']) ? (float)$state['start'] : 0.0;
$bootstrapDone = isset($state['bootstrap_done']) ? (float)$state['bootstrap_done'] : 0.0;
$now = microtime(true);
$totalMs = ($start > 0.0) ? (($now - $start) * 1000.0) : 0.0;
$bootstrapMs = ($start > 0.0 && $bootstrapDone > 0.0) ? (($bootstrapDone - $start) * 1000.0) : 0.0;
$dbQueryCount = isset($state['db_query_count']) ? (int)$state['db_query_count'] : 0;
$dbQueryMs = isset($state['db_query_ms']) ? (float)$state['db_query_ms'] : 0.0;
$redirectLookupMs = isset($state['redirect_lookup_ms']) ? (float)$state['redirect_lookup_ms'] : 0.0;
header(
'X-ABJ404-Benchmark: ' .
'total_ms=' . round($totalMs, 3) . ';' .
'bootstrap_ms=' . round($bootstrapMs, 3) . ';' .
'db_query_count=' . $dbQueryCount . ';' .
'db_query_ms=' . round($dbQueryMs, 3) . ';' .
'redirect_lookup_ms=' . round($redirectLookupMs, 3)
);
}
}
abj404_benchmark_bootstrap_start();
if (abj404_is_benchmark_request()) {
add_action('send_headers', 'abj404_benchmark_emit_headers', PHP_INT_MAX);
}
}
// Minimal shutdown handler: catches compile/parse fatals in plugin files and
// stores them in a transient so the degraded admin page can display the error
// on the next request. This is important for PHP 7.4 where syntax errors in
// required files produce uncatchable E_COMPILE_ERROR.
if (!function_exists('abj404_boot_shutdown_handler')) {
/** @return void */
function abj404_boot_shutdown_handler() {
if ($GLOBALS['abj404_boot_ok']) {
return;
}
$error = error_get_last();
if ($error === null) {
return;
}
// Only capture fatal/compile errors in our plugin files.
$fatalTypes = E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR;
if (!($error['type'] & $fatalTypes)) {
return;
}
$pluginDir = defined('ABJ404_PATH') ? ABJ404_PATH : __DIR__ . '/';
if (strpos($error['file'], $pluginDir) === false) {
return;
}
$errorInfo = array(
'message' => $error['message'],
'file' => $error['file'],
'line' => $error['line'],
'type' => $error['type'],
'time' => time(),
);
// Use update_option as a fallback — set_transient might not be available
// during a fatal shutdown.
if (function_exists('set_transient')) {
set_transient('abj404_boot_fatal', $errorInfo, 3600);
}
}
}
register_shutdown_function('abj404_boot_shutdown_handler');
// Always load Loader.php to ensure plugin constants (ABJ404_TYPE_404_DISPLAYED,
// ABJ404_STATUS_MANUAL, etc.) are defined in all contexts: admin, REST API, WP-CLI
// eval, and template_redirect. Without this, direct calls to plugin classes via
// wp eval fail with "Undefined constant" errors because Loader.php was previously
// only loaded inside is_admin() — leaving WP-CLI and other non-admin contexts
// without the constants they need.
$__abj404_loader_path = plugin_dir_path( __FILE__ ) . "includes/Loader.php";
if (file_exists($__abj404_loader_path)) {
try {
require_once($__abj404_loader_path);
$GLOBALS['abj404_boot_ok'] = true;
// Clear any stale boot fatal transient from a previous failed load.
if (function_exists('delete_transient')) {
delete_transient('abj404_boot_fatal');
}
} catch (\Throwable $e) {
$GLOBALS['abj404_boot_ok'] = false;
$GLOBALS['abj404_boot_error'] = $e->getMessage();
error_log('404 Solution: boot failed — ' . $e->getMessage());
}
} else {
$GLOBALS['abj404_boot_ok'] = false;
$GLOBALS['abj404_missing_files'][] = $__abj404_loader_path;
$GLOBALS['abj404_boot_error'] = 'Loader.php is missing.';
}
unset($__abj404_loader_path);
if ($GLOBALS['abj404_boot_ok']) {
// admin
if (is_admin()) {
ABJ_404_Solution_WordPress_Connector::init();
ABJ_404_Solution_ViewUpdater::init();
}
// REST API — deferred to rest_api_init so DataAccess/PluginLogic are only loaded on actual REST requests.
add_action('rest_api_init', function() {
$dao = ABJ_404_Solution_DataAccess::getInstance();
$logic = ABJ_404_Solution_PluginLogic::getInstance();
$restController = new ABJ_404_Solution_RestApiController($dao, $logic);
$restController->registerRoutes();
});
// WP-CLI commands.
if (defined('WP_CLI') && WP_CLI) {
add_action('init', function() {
\WP_CLI::add_command('abj404', 'ABJ_404_Solution_WPCLICommands');
}, 1);
}
} elseif (function_exists('is_admin') && is_admin()) {
// Boot failed — register degraded admin page so the admin sees instructions
// instead of a white screen or missing menu item.
add_action('admin_menu', 'abj404_degraded_admin_menu');
add_action('admin_notices', 'abj404_degraded_admin_notice');
}
// --- Degraded-mode functions (always defined, no plugin class dependencies) ---
if (!function_exists('abj404_degraded_admin_menu')) {
/** @return void */
function abj404_degraded_admin_menu() {
$options = function_exists('get_option') ? get_option('abj404_settings') : false;
$options = is_array($options) ? $options : array();
$menuName = '404 Solution';
$badge = " <span class='update-plugins count-1'><span class='plugin-count'>!</span></span>";
if (isset($options['menuLocation']) && $options['menuLocation'] === 'settingsLevel') {
add_menu_page('404 Solution', $menuName . $badge, 'manage_options', 'abj404_solution', 'abj404_degraded_admin_page');
} else {
$ppSlug = defined('ABJ404_PP') ? ABJ404_PP : 'abj404_solution';
add_submenu_page('options-general.php', '404 Solution', $menuName . $badge, 'manage_options', $ppSlug, 'abj404_degraded_admin_page');
}
}
}
if (!function_exists('abj404_degraded_admin_notice')) {
/** @return void */
function abj404_degraded_admin_notice() {
if (!current_user_can('manage_options')) {
return;
}
echo '<div class="notice notice-error"><p><strong>404 Solution:</strong> ';
echo 'Plugin files are missing or corrupt. ';
$ppSlug = defined('ABJ404_PP') ? ABJ404_PP : 'abj404_solution';
echo '<a href="' . esc_url(admin_url('options-general.php?page=' . $ppSlug)) . '">View details</a>';
echo '</p></div>';
}
}
if (!function_exists('abj404_degraded_admin_page')) {
/** @return void */
function abj404_degraded_admin_page() {
if (!current_user_can('manage_options')) {
echo '<div class="wrap">';
echo '<h1>404 Solution</h1>';
echo '<div class="notice notice-error"><p>';
echo '<strong>Permission denied.</strong> ';
echo 'Your user account does not have permission to access this page.';
echo '</p><p>';
echo 'Please verify that your WordPress role has the <code>manage_options</code> capability.';
echo '</p></div></div>';
return;
}
$missingFiles = isset($GLOBALS['abj404_missing_files']) ? $GLOBALS['abj404_missing_files'] : array();
$bootError = isset($GLOBALS['abj404_boot_error']) ? $GLOBALS['abj404_boot_error'] : '';
$pluginDir = defined('ABJ404_PATH') ? ABJ404_PATH : dirname(__FILE__) . '/';
// Check for a stored fatal from a previous request.
$fatalInfo = function_exists('get_transient') ? get_transient('abj404_boot_fatal') : false;
echo '<div class="wrap">';
echo '<h1>404 Solution — Plugin Files Missing</h1>';
echo '<div class="notice notice-error inline"><p>';
echo '<strong>The 404 Solution plugin cannot start</strong> because one or more required files are missing or corrupt. ';
echo 'This usually happens after a failed plugin update or incomplete file upload.';
echo '</p></div>';
if (!empty($missingFiles)) {
echo '<div class="card" style="max-width:800px;">';
echo '<h2>Missing Files</h2>';
echo '<ul style="list-style:disc;padding-left:20px;">';
foreach ($missingFiles as $file) {
// Show relative path for readability.
$relative = str_replace($pluginDir, '', $file);
echo '<li><code>' . esc_html($relative) . '</code></li>';
}
echo '</ul>';
echo '</div>';
}
if ($bootError !== '') {
echo '<div class="card" style="max-width:800px;">';
echo '<h2>Error Details</h2>';
echo '<pre style="white-space:pre-wrap;word-break:break-all;">' . esc_html($bootError) . '</pre>';
echo '</div>';
}
if (is_array($fatalInfo) && !empty($fatalInfo['message'])) {
echo '<div class="card" style="max-width:800px;">';
echo '<h2>Fatal Error (previous request)</h2>';
$fatalFile = isset($fatalInfo['file']) ? str_replace($pluginDir, '', $fatalInfo['file']) : 'unknown';
$fatalLine = isset($fatalInfo['line']) ? $fatalInfo['line'] : '?';
echo '<pre style="white-space:pre-wrap;word-break:break-all;">' . esc_html($fatalInfo['message']) . "\n" . esc_html($fatalFile) . ':' . esc_html((string)$fatalLine) . '</pre>';
echo '</div>';
}
echo '<div class="card" style="max-width:800px;">';
echo '<h2>How to Fix</h2>';
echo '<ol>';
echo '<li>Go to <strong>Plugins → Installed Plugins</strong>, deactivate <strong>404 Solution</strong>, then delete it.</li>';
echo '<li>Reinstall from the WordPress plugin directory: ';
$installUrl = admin_url('plugin-install.php?s=404+solution&tab=search');
echo '<a href="' . esc_url($installUrl) . '" class="button button-primary">Search “404 Solution”</a>';
echo '</li>';
echo '<li>Activate the fresh copy. Your redirects and settings are stored in the database and will not be lost.</li>';
echo '</ol>';
echo '</div>';
echo '</div>'; // .wrap
}
}
if (!function_exists('abj404_admin_page_callback')) {
/**
* Safe wrapper for the admin page callback. Falls back to the degraded
* page if the View class was not loaded during boot.
*
* @return void
*/
function abj404_admin_page_callback() {
// The false parameter avoids triggering the autoloader — if View was not
// loaded during boot, we don't want to attempt loading it again here.
if (class_exists('ABJ_404_Solution_View', false)) {
try {
ABJ_404_Solution_View::handleMainAdminPageActionAndDisplay();
} catch (\Throwable $e) {
echo '<div class="wrap">';
echo '<div class="notice notice-error">';
echo '<p><strong>404 Solution:</strong> An error occurred while rendering this page.</p>';
echo '<details><summary>Show error details</summary>';
echo '<pre style="white-space:pre-wrap;word-break:break-all;max-width:100%;margin:6px 0;">' . esc_html($e->getMessage() . "\n" . $e->getTraceAsString()) . '</pre>';
echo '</details>';
echo '</div>';
echo '</div>';
}
} else {
abj404_degraded_admin_page();
}
}
}
// ----
// get the plugin priority to use before adding the template_redirect action.
$__abj404_options = abj404_get_settings_options();
$__abj404_redirect_priority_raw = isset($__abj404_options['template_redirect_priority']) && is_scalar($__abj404_options['template_redirect_priority']) ? $__abj404_options['template_redirect_priority'] : 9;
$__abj404_template_redirect_priority = absint($__abj404_redirect_priority_raw);
$__abj404_redirect_all = isset($__abj404_options['redirect_all_requests']) && is_scalar($__abj404_options['redirect_all_requests']) ? (string)$__abj404_options['redirect_all_requests'] : '';
$__abj404_update_suggest = isset($__abj404_options['update_suggest_url']) && is_scalar($__abj404_options['update_suggest_url']) ? (string)$__abj404_options['update_suggest_url'] : '';
$GLOBALS['abj404_frontend_runtime_flags'] = array(
'redirect_all_requests' => ($__abj404_redirect_all === '1'),
'update_suggest_url' => ($__abj404_update_suggest === '1'),
);
$__abj404_lang_override = isset($__abj404_options['plugin_language_override']) && is_string($__abj404_options['plugin_language_override']) ? $__abj404_options['plugin_language_override'] : '';
$GLOBALS['abj404_plugin_language_override'] = $__abj404_lang_override;
add_action('template_redirect', 'abj404_404listener', $__abj404_template_redirect_priority);
unset($__abj404_options);
unset($__abj404_template_redirect_priority);
abj404_benchmark_mark_bootstrap_done();
// ---
// 404
if (!function_exists('abj404_404listener')) {
/** @return void */
function abj404_404listener() {
if (!$GLOBALS['abj404_boot_ok']) {
return;
}
$is404 = is_404();
if (!$is404) {
// Performance: do NOT load the whole plugin on every frontend request unless we must.
if (!empty($GLOBALS['abj404_frontend_runtime_flags']['redirect_all_requests'])) {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$connector = ABJ_404_Solution_WordPress_Connector::getInstance();
$connector->processRedirectAllRequests();
return;
}
$updateSuggestEnabled = !empty($GLOBALS['abj404_frontend_runtime_flags']['update_suggest_url']);
$cookieName404 = ABJ404_PP . '_STATUS_404';
$has404StatusCookie = (isset($_COOKIE[$cookieName404]) && $_COOKIE[$cookieName404] == 'true');
// Fast path: if none of the non-404 features are active, bail immediately.
if (!$updateSuggestEnabled && !$has404StatusCookie) {
return;
}
/** If we're currently redirecting to a custom 404 page and we are about to show page
* suggestions then update the URL displayed to the user. */
$cookieName = ABJ404_PP . '_REQUEST_URI_UPDATE_URL';
$queryParamName = ABJ404_PP . '_ref';
$hasUpdateCookie = !empty($_COOKIE[$cookieName]);
$hasUpdateParam = !empty($_GET[$queryParamName]);
// Fast path: nothing pending from prior plugin-driven redirects.
if (!$hasUpdateCookie && !$hasUpdateParam && !$has404StatusCookie) {
return;
}
if ($has404StatusCookie) {
// clear the cookie
setcookie($cookieName404, 'false', time() - 5, "/");
// we're going to a custom 404 page so set the status to 404.
status_header(404);
}
if (!$updateSuggestEnabled) {
return;
}
// Check cookie first, then query param fallback (for 301 redirects where cookies don't survive)
$originalURL = null;
if ($hasUpdateCookie) {
$originalURL = $_COOKIE[$cookieName];
} elseif ($hasUpdateParam) {
$originalURL = urldecode($_GET[$queryParamName]);
}
if ($originalURL !== null) {
// clear the cookie - sanitize before writing to $_REQUEST
$sanitizedOriginal = sanitize_text_field($originalURL);
$_REQUEST[ABJ404_PP . '_REQUEST_URI'] = $sanitizedOriginal;
$_REQUEST[ABJ404_PP . '_REQUEST_URI_UPDATE_URL'] = $sanitizedOriginal;
setcookie($cookieName, '', time() - 5, "/");
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
add_action('wp_head', 'ABJ_404_Solution_ShortCode::updateURLbarIfNecessary');
}
return;
}
// ignore admin screens and login requests on 404 processing path.
// $_SERVER['SCRIPT_NAME'] is not guaranteed (CLI, some test runners, some proxies).
// Use a direct script-name check to avoid invoking wp_login_url() filters.
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
$isLoginScreen = (
($scriptName !== '' && stripos($scriptName, 'wp-login.php') !== false) ||
($requestUri !== '' && stripos($requestUri, 'wp-login.php') !== false)
);
if (is_admin() || $isLoginScreen) {
return;
}
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$connector = ABJ_404_Solution_WordPress_Connector::getInstance();
$connector->process404();
}
}
if (!function_exists('abj404_is_redirect_all_requests_enabled')) {
/**
* Small helper for testability and to keep option-parsing logic consistent.
*
* @param mixed $options Value returned by get_option('abj404_settings')
* @return bool
*/
function abj404_is_redirect_all_requests_enabled($options) {
return is_array($options) &&
array_key_exists('redirect_all_requests', $options) &&
(string)$options['redirect_all_requests'] === '1';
}
}
if (!function_exists('abj404_dailyMaintenanceCronJobListener')) {
/** @return void */
function abj404_dailyMaintenanceCronJobListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$abj404dao = ABJ_404_Solution_DataAccess::getInstance();
$abj404dao->deleteOldRedirectsCron();
$dbUpgrades = ABJ_404_Solution_DatabaseUpgradesEtc::getInstance();
$dbUpgrades->runDatabaseMaintenanceTasks();
} catch (\Throwable $e) {
error_log('404 Solution cron (maintenance): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_updateLogsHitsTableListener')) {
/** @return void */
function abj404_updateLogsHitsTableListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$abj404dao = ABJ_404_Solution_DataAccess::getInstance();
$abj404dao->createRedirectsForViewHitsTable();
} catch (\Throwable $e) {
error_log('404 Solution cron (logs/hits): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_updatePermalinkCacheListener')) {
/**
* @param int $maxExecutionTime
* @param int $executionCount
* @return void
*/
function abj404_updatePermalinkCacheListener($maxExecutionTime, $executionCount = 1) {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$permalinkCache = ABJ_404_Solution_PermalinkCache::getInstance();
$permalinkCache->updatePermalinkCache($maxExecutionTime, $executionCount);
} catch (\Throwable $e) {
error_log('404 Solution cron (permalink cache): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_rebuildNGramCacheListener')) {
/**
* @param int $offset
* @return void
*/
function abj404_rebuildNGramCacheListener($offset = 0) {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$dbUpgrades = ABJ_404_Solution_DatabaseUpgradesEtc::getInstance();
$dbUpgrades->rebuildNGramCacheAsync($offset);
} catch (\Throwable $e) {
error_log('404 Solution cron (ngram cache): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_networkActivationListener')) {
/** @return void */
function abj404_networkActivationListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
ABJ_404_Solution_PluginLogic::networkActivationCronHandler();
} catch (\Throwable $e) {
error_log('404 Solution cron (network activation): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_networkActivationBackgroundListener')) {
/** @return void */
function abj404_networkActivationBackgroundListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$upgradesEtc = ABJ_404_Solution_DatabaseUpgradesEtc::getInstance();
$upgradesEtc->processMultisiteActivationBatch();
} catch (\Throwable $e) {
error_log('404 Solution cron (multisite activation): ' . $e->getMessage());
}
}
}
if (!function_exists('abj404_networkUpgradeBackgroundListener')) {
/** @return void */
function abj404_networkUpgradeBackgroundListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$upgradesEtc = ABJ_404_Solution_DatabaseUpgradesEtc::getInstance();
$upgradesEtc->processMultisiteUpgradeBatch();
} catch (\Throwable $e) {
error_log('404 Solution cron (multisite upgrade): ' . $e->getMessage());
}
}
}
add_action('abj404_cleanupCronAction', 'abj404_dailyMaintenanceCronJobListener');
add_action('abj404_updateLogsHitsTableAction', 'abj404_updateLogsHitsTableListener');
add_action('abj404_updatePermalinkCacheAction', 'abj404_updatePermalinkCacheListener', 10, 2);
add_action('abj404_send_digest', 'abj404_sendDigestCronListener');
if (!function_exists('abj404_sendDigestCronListener')) {
/** @return void */
function abj404_sendDigestCronListener() {
try {
require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
$dao = ABJ_404_Solution_DataAccess::getInstance();
$logger = ABJ_404_Solution_Logging::getInstance();
$emailDigest = new ABJ_404_Solution_EmailDigest($dao, $logger);
$emailDigest->onCronSendDigest();
} catch (\Throwable $e) {
error_log('404 Solution cron (email digest): ' . $e->getMessage());
}
}
}
add_action('abj404_rebuild_ngram_cache_hook', 'abj404_rebuildNGramCacheListener', 10, 1);
add_action('abj404_network_activation_hook', 'abj404_networkActivationListener');
add_action('abj404_network_activation_background', 'abj404_networkActivationBackgroundListener');
add_action('abj404_network_upgrade_background', 'abj404_networkUpgradeBackgroundListener');
/**
* Override the locale for this plugin if user has configured a language override.
* This allows users to use a different language for the 404 Solution plugin
* than their WordPress site language or user language preference.
*
* @param string $locale The current locale.
* @param string $domain The text domain.
* @return string The locale to use for translation loading.
*/
if (!function_exists('abj404_override_plugin_locale')) {
/**
* @param string $locale
* @param string $domain
* @return string
*/
function abj404_override_plugin_locale($locale, $domain) {
// Only override for our plugin's text domain.
// Use the value cached in $GLOBALS at plugin boot to avoid a redundant get_option() call.
if ($domain === '404-solution') {
$override = isset($GLOBALS['abj404_plugin_language_override']) && is_string($GLOBALS['abj404_plugin_language_override']) ? $GLOBALS['abj404_plugin_language_override'] : '';
if ($override !== '') {
return $override;
}
}
return $locale;
}
}
add_filter('plugin_locale', 'abj404_override_plugin_locale', 999, 2);
if (!function_exists('abj404_show_runtime_integrity_notice')) {
/** @return void */
function abj404_show_runtime_integrity_notice() {
if (!is_admin() || !current_user_can('manage_options')) {
return;
}
$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
if ($page !== ABJ404_PP) {
return;
}
$missing = get_transient('abj404_runtime_missing_files');
if (!is_array($missing) || count($missing) === 0) {
return;
}