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
|
<?php
/**
* Plugin Name: RedirectTracer
* Description: Adds debugging output to the HTTP header response to find out which file/function triggered a redirect.
* TURN ON WP_DEBUG and Inspect the HTTP response in your browsers developer console to see the debug information. OR check the debug log
* ----------------------------------------------------------------------------
*/
class RedirectTracer
{
/**
* Returns the singleton instance.
* @return RedirectTracer
*/
public static function instance()
{
static $instance = null;
if (null === $instance) {
$instance = new RedirectTracer();
}
return $instance;
}
/**
* Constructor.
*/
protected function __construct()
{
if (WP_DEBUG) {
add_filter(
'wp_redirect',
array( $this, 'redirect_headers' ),
9999
);
}
}
/**
* Adds some redirect headers with debugging information to the response.
*/
public function redirect_headers($location)
{
if ($location) {
$trace = $this->get_trace();
error_log('REDIRECT Backtrace:');
error_log(print_r($trace, true));
if (! headers_sent()) {
foreach ($trace as $ind => $line) {
header("WPDev-Redirect-Trace-$ind: $line", false);
}
} else {
echo "\n";
foreach ($trace as $ind => $line) {
echo "<!-- wdpu-redirect-trace-$ind: $line -->\n";
}
}
}
return $location;
}
/**
* Generates an array of stack-trace information. Each array item is a
* simple string that can be directly output.
* @return array Trace information
*/
public function get_trace()
{
$result = array();
$trace = debug_backtrace();
$trace_count = count($trace);
$_num = 0;
$start_at = 0;
// Skip the first 2 trace lines (filter call inside wp_redirect)
if ($trace_count > 2) {
$start_at = 2;
}
for ($i = $start_at; $i < $trace_count; $i += 1) {
$trace_info = $trace[$i];
$line_info = $trace_info;
$j = $i;
while (empty($line_info['line']) && $j < $trace_count) {
$line_info = $trace[$j];
$j += 1;
}
$_file = empty($line_info['file']) ? '' : $line_info['file'];
$_line = empty($line_info['line']) ? '' : $line_info['line'];
$_args = empty($trace_info['args']) ? array() : $trace_info['args'];
$_class = empty($trace_info['class']) ? '' : $trace_info['class'];
$_type = empty($trace_info['type']) ? '' : $trace_info['type'];
$_function = empty($trace_info['function']) ? '' : $trace_info['function'];
$_num += 1;
$_arg_string = '';
$_args_arr = array();
if ($i > 0 && is_array($_args) && count($_args)) {
foreach ($_args as $arg) {
if (is_scalar($arg)) {
if (is_bool($arg)) {
$_args_arr[] = ( $arg ? 'true' : 'false' );
} elseif (is_string($arg)) {
$_args_arr[] = '"' . $arg . '"';
} else {
$_args_arr[] = $arg;
}
} elseif (is_array($arg)) {
$_args_arr[] = '[Array]';
} elseif (is_object($arg)) {
$_args_arr[] = '[' . get_class($arg) . ']';
} elseif (is_null($arg)) {
$_args_arr[] = 'NULL';
} else {
$_args_arr[] = '[?]';
}
}
$_arg_string = implode(',', $_args_arr);
}
if (strlen($_file) > 80) {
$_file = '...' . substr($_file, -77);
} else {
$_file = str_pad($_file, 80, ' ', STR_PAD_RIGHT);
}
$result_item = sprintf(
'%s:%s %s(%s)',
$_file,
str_pad($_line, 5, ' ', STR_PAD_LEFT),
$_class . $_type . $_function,
$_arg_string
);
$_num_str = str_pad($_num, 2, '0', STR_PAD_LEFT);
$result[$_num_str] = $result_item;
}
return $result;
}
}
RedirectTracer::instance();
|